source: TI12-security/trunk/python/ndg.security.server/ndg/security/server/initCredReposDb.py @ 4840

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.server/ndg/security/server/initCredReposDb.py@5042
Revision 4840, 2.1 KB checked in by pjkersha, 11 years ago (diff)

Fix problem with search and replace licence not adding a new line.

Line 
1#!/usr/bin/env python
2"""NDG Security - initialisation script for MySQL Credential Repository
3database
4
5Use with care!  It initialise all the tables in the database.
6
7NERC Data Grid Project
8"""
9__author__ = "P J Kershaw"
10__date__ = "25/04/06"
11__copyright__ = "(C) 2009 Science and Technology Facilities Council"
12__license__ = "BSD - see LICENSE file in top-level directory"
13__contact__ = "Philip.Kershaw@stfc.ac.uk"
14__author__ = "P J Kershaw"
15__revision__ = "$Id$"
16
17# Command line processing
18import sys
19import os
20import optparse
21import getpass
22
23from ndg.security.server.Session import *
24
25
26def main():
27    parser = optparse.OptionParser()
28    parser.add_option("-u",
29                      "--username",
30                      dest="username",
31                      help="Database username")
32
33    parser.add_option("-n",
34                      "--hostname",
35                      dest="hostname",
36                      help="database hostname - default is \"localhost\"")
37
38    parser.add_option("-D",
39                      "--database-name",
40                      dest="dbName",
41                      help="database name - default is \"ndgCredentialRepository\"")
42
43    (opt, args) = parser.parse_args()
44
45    if not opt.username:
46        sys.stderr.write("Error, No username set.\n\n")
47        parser.print_help()
48        sys.exit(1)
49
50    if not opt.hostname:
51        opt.hostname = "localhost"
52
53    if not opt.dbName:
54        opt.dbName = "ndgCredentialRepository"
55
56
57    # Obtain from prompt
58    try:
59        password = getpass.getpass(prompt="Database password: ")
60    except KeyboardInterrupt:
61        sys.exit(1)
62
63
64    try:
65        dbURI = "mysql://%s:%s@%s/%s" % \
66                            (opt.username, password, opt.hostname, opt.dbName)
67        credentialRepository = SessionManagerCredentialRepository(dbURI=dbURI)
68    except Exception, e:
69        sys.stderr.write("%s\n" % str(e))
70        sys.exit(1)
71
72    # This method prompts the user for confirmation of table initialisation
73    try:
74        credentialRepository._initTables()
75    except Exception, e:
76        sys.stderr.write("Error creating tables: %s\n" % str(e))
77
78if __name__ == "__main__":
79    main()
Note: See TracBrowser for help on using the repository browser.