[f0dc2ca] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import MySQLdb |
---|
| 5 | from fedd.fixed_resource import read_key_db, write_key_db, \ |
---|
| 6 | read_project_db, write project_db |
---|
| 7 | from optparse import OptionParser |
---|
| 8 | |
---|
| 9 | actions = set(("initall", "init", "add", "delete")) |
---|
| 10 | |
---|
| 11 | def read_key_file(file): |
---|
| 12 | """ |
---|
| 13 | Read a single line from a keyfile and trim the whitespace |
---|
| 14 | """ |
---|
| 15 | f = open(file, 'r') |
---|
| 16 | key = f.readline().rstrip() |
---|
| 17 | f.close() |
---|
| 18 | return key |
---|
| 19 | |
---|
| 20 | class key_opts(OptionParser): |
---|
| 21 | """ |
---|
| 22 | Options to the command line, pretty self describing |
---|
| 23 | """ |
---|
| 24 | def __init__(self): |
---|
| 25 | OptionParser.__init__(self, usage="%prog " + \ |
---|
| 26 | "(init|add|delete|initall) " + \ |
---|
| 27 | "[opts] (--help for details)", version="0.1") |
---|
| 28 | self.add_option('-t','--type', dest='type', type='choice', |
---|
| 29 | choices=['keys','projects'], help="database type") |
---|
| 30 | self.add_option('-u','--user', dest='user', default=None, |
---|
| 31 | action='store', help='user to add/delete') |
---|
| 32 | self.add_option('-p','--project', dest='project', default=None, |
---|
| 33 | action='store', help='project to add/delete') |
---|
| 34 | self.add_option('-k','--key', dest='key', default=None, |
---|
| 35 | action='store', help='key to add/delete (string)') |
---|
| 36 | self.add_option('-K','--keyfile', dest='keyfile', default=None, |
---|
| 37 | action='store', help='key to add/delete (file)') |
---|
| 38 | self.add_option('-d', '--database', dest='file', default=None, |
---|
| 39 | action='store', help='database file') |
---|
| 40 | self.add_option('-f', '--file', dest='file', action='store', |
---|
| 41 | help='database file (synonym for --database)') |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | parser = key_opts() |
---|
| 45 | action = sys.argv[1] |
---|
| 46 | |
---|
| 47 | # Check the action |
---|
| 48 | if action in actions: del sys.argv[1] |
---|
| 49 | else: sys.exit("Bad action, must be one of %s" % ", ".join(actions)) |
---|
| 50 | |
---|
| 51 | # Parse and check the the options for consistency |
---|
| 52 | (opts, args) = parser.parse_args() |
---|
| 53 | if not opts.file: |
---|
| 54 | sys.exit("Must specify DB file") |
---|
| 55 | if not opts.type: |
---|
| 56 | sys.exit("Must specify database type (--type)") |
---|
| 57 | elif opts.type == 'keys': |
---|
| 58 | if action != "init" and action != "initall" and action != "addall": |
---|
| 59 | if opts.user and (opts.key or opts.keyfile): |
---|
| 60 | user = opts.user |
---|
| 61 | if opts.key: |
---|
| 62 | key = opts.key |
---|
| 63 | elif opts.keyfile: |
---|
| 64 | try: |
---|
| 65 | key = read_key_file(opts.keyfile) |
---|
| 66 | except IOError, e: |
---|
| 67 | sys.exit("Error reading keyfile: %s" % e) |
---|
| 68 | else: |
---|
| 69 | sys.exit("Must specify user and key") |
---|
| 70 | elif opts.type == 'projects': |
---|
| 71 | if action != "init" and action != "initall" and action != "addall": |
---|
| 72 | if opts.project: project = opts.project |
---|
| 73 | else: sys.exit("Must specify project") |
---|
| 74 | else: |
---|
| 75 | sys.exit("Invalid --type field (how'd you do that?)") |
---|
| 76 | |
---|
| 77 | if opts.type == 'keys': |
---|
| 78 | if action != 'init' and action != 'initall': |
---|
| 79 | keys = read_key_db(opts.file) |
---|
| 80 | else: |
---|
| 81 | keys = set() |
---|
| 82 | |
---|
| 83 | if action == 'initall': |
---|
| 84 | # Add all users from the Emulab DB |
---|
| 85 | try: |
---|
| 86 | db = MySQLdb.connect(db="tbdb") |
---|
| 87 | except: |
---|
| 88 | sys.exit("Cannot access the Emulab database") |
---|
| 89 | |
---|
| 90 | c = db.cursor() |
---|
| 91 | c.execute("SELECT uid, pubkey FROM user_pubkeys") |
---|
| 92 | for u, k in c.fetchall(): |
---|
| 93 | keys.add((u, k)) |
---|
| 94 | c.close() |
---|
| 95 | db.close() |
---|
| 96 | elif action == 'add': |
---|
| 97 | keys.add((user, key)) |
---|
| 98 | elif action == 'delete': |
---|
| 99 | if (user, key) in keys: |
---|
| 100 | keys.remove((user, key)) |
---|
| 101 | else: |
---|
| 102 | print >>sys.stderr, "Cannot delete (%s, %s): not in db" % \ |
---|
| 103 | (user, key) |
---|
| 104 | # init action falls through to write an empty DB |
---|
| 105 | write_key_db(opts.file, keys) |
---|
| 106 | else: |
---|
| 107 | if action != 'init' and action != 'initall': |
---|
| 108 | projects = read_project_db(opts.file) |
---|
| 109 | else: |
---|
| 110 | projects = set() |
---|
| 111 | |
---|
| 112 | if action == 'initall': |
---|
| 113 | # Add all projects from the Emulab DB |
---|
| 114 | try: |
---|
| 115 | db = MySQLdb.connect(db="tbdb") |
---|
| 116 | except: |
---|
| 117 | sys.exit("Cannot access the Emulab database") |
---|
| 118 | |
---|
| 119 | c = db.cursor() |
---|
| 120 | c.execute("SELECT pid FROM projects") |
---|
| 121 | for p in c.fetchall(): |
---|
| 122 | projects.add(p) |
---|
| 123 | c.close() |
---|
| 124 | db.close() |
---|
| 125 | elif action == 'add': |
---|
| 126 | projects.add(project) |
---|
| 127 | elif action == 'delete': |
---|
| 128 | if project in projects: |
---|
| 129 | projects.remove(project) |
---|
| 130 | else: |
---|
| 131 | print >>sys.stderr, "Cannot delete %s: not in db" % project |
---|
| 132 | |
---|
| 133 | # init action falls through to write an empty DB |
---|
| 134 | write_project_db(opts.file, projects) |
---|
| 135 | |
---|
| 136 | sys.exit(0) |
---|