1 | #!/usr/bin/env python |
---|
2 | """NDG Security - initialisation script for MySQL Credential Repository |
---|
3 | database |
---|
4 | |
---|
5 | Use with care! It initialise all the tables in the database. |
---|
6 | |
---|
7 | NERC 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 |
---|
18 | import sys |
---|
19 | import os |
---|
20 | import optparse |
---|
21 | import getpass |
---|
22 | |
---|
23 | from ndg.security.server.Session import * |
---|
24 | |
---|
25 | |
---|
26 | def 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 | |
---|
78 | if __name__ == "__main__": |
---|
79 | main() |
---|