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

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

Updated contact e-mail address

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) 2007 STFC & NERC"
12__license__ = \
13"""This software may be distributed under the terms of the Q Public
14License, version 1.0 or later."""
15__contact__ = "Philip.Kershaw@stfc.ac.uk"
16__author__ = "P J Kershaw"
17__revision__ = "$Id$"
18
19# Command line processing
20import sys
21import os
22import optparse
23import getpass
24
25from ndg.security.server.Session import *
26
27
28def main():
29    parser = optparse.OptionParser()
30    parser.add_option("-u",
31                      "--username",
32                      dest="username",
33                      help="Database username")
34
35    parser.add_option("-n",
36                      "--hostname",
37                      dest="hostname",
38                      help="database hostname - default is \"localhost\"")
39
40    parser.add_option("-D",
41                      "--database-name",
42                      dest="dbName",
43                      help="database name - default is \"ndgCredentialRepository\"")
44
45    (opt, args) = parser.parse_args()
46
47    if not opt.username:
48        sys.stderr.write("Error, No username set.\n\n")
49        parser.print_help()
50        sys.exit(1)
51
52    if not opt.hostname:
53        opt.hostname = "localhost"
54
55    if not opt.dbName:
56        opt.dbName = "ndgCredentialRepository"
57
58
59    # Obtain from prompt
60    try:
61        password = getpass.getpass(prompt="Database password: ")
62    except KeyboardInterrupt:
63        sys.exit(1)
64
65
66    try:
67        dbURI = "mysql://%s:%s@%s/%s" % \
68                            (opt.username, password, opt.hostname, opt.dbName)
69        credentialRepository = SessionManagerCredentialRepository(dbURI=dbURI)
70    except Exception, e:
71        sys.stderr.write("%s\n" % str(e))
72        sys.exit(1)
73
74    # This method prompts the user for confirmation of table initialisation
75    try:
76        credentialRepository._initTables()
77    except Exception, e:
78        sys.stderr.write("Error creating tables: %s\n" % str(e))
79
80if __name__ == "__main__":
81    main()
Note: See TracBrowser for help on using the repository browser.