Changeset 266e866
- Timestamp:
- Dec 3, 2008 2:24:15 PM (16 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
- Children:
- dceeef9
- Parents:
- b312431b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/fixed_resource.py
rb312431b r266e866 4 4 import MySQLdb 5 5 from federation.fixed_resource import read_key_db, write_key_db, \ 6 read_project_db, write project_db6 read_project_db, write_project_db, read_user_db, write_user_db 7 7 from optparse import OptionParser 8 8 … … 27 27 "[opts] (--help for details)", version="0.1") 28 28 self.add_option('-t','--type', dest='type', type='choice', 29 choices=['keys','projects' ], help="database type")29 choices=['keys','projects', 'users'], help="database type") 30 30 self.add_option('-u','--user', dest='user', default=None, 31 31 action='store', help='user to add/delete') … … 41 41 help='database file (synonym for --database)') 42 42 43 def process_db(action, file, key, read_db, write_db, query): 44 """ 45 The logic of insertion/deletion/initialization is the same regardless of 46 the DB. This function encapsulates that control flow. 47 """ 48 49 if action != 'init' and action != 'initall': 50 keys = read_db(file) 51 else: 52 keys = set() 53 54 # init action falls through to write an empty DB 55 if action == 'initall': 56 # Add all keys returned by the query from the Emulab DB 57 try: 58 db = MySQLdb.connect(db="tbdb") 59 except: 60 sys.exit("Cannot access the Emulab database") 61 62 c = db.cursor() 63 c.execute(query) 64 for k in c.fetchall(): 65 keys.add(k) 66 c.close() 67 db.close() 68 elif action == 'add': 69 keys.add(key) 70 elif action == 'delete': 71 if key in keys: 72 keys.remove(key) 73 else: 74 print >>sys.stderr, "Cannot delete %s: not in db" % (key,) 75 76 write_db(file, keys) 77 43 78 44 79 parser = key_opts() 45 80 action = sys.argv[1] 81 init_actions = set(['init', 'initall']) 46 82 47 83 # Check the action … … 49 85 else: sys.exit("Bad action, must be one of %s" % ", ".join(actions)) 50 86 51 # Parse and check the the options for consistency52 87 (opts, args) = parser.parse_args() 53 88 if not opts.file: … … 56 91 sys.exit("Must specify database type (--type)") 57 92 elif opts.type == 'keys': 58 if action != "init" and action != "initall" and action != "addall":93 if action not in init_actions: 59 94 if opts.user and (opts.key or opts.keyfile): 60 95 user = opts.user … … 68 103 else: 69 104 sys.exit("Must specify user and key") 105 else: 106 key = None 107 user = None 108 process_db(action, opts.file, (user, key), read_key_db, write_key_db, 109 "SELECT uid, pubkey FROM user_pubkeys") 110 70 111 elif opts.type == 'projects': 71 if action != "init" and action != "initall" and action != "addall":112 if action not in init_actions: 72 113 if opts.project: project = opts.project 73 114 else: sys.exit("Must specify project") 115 else: project = None 116 process_db(action, opts.file, project, read_project_db, write_project_db, 117 "SELECT pid FROM projects") 118 elif opts.type == 'users': 119 if action not in init_actions: 120 if opts.user: user = opts.user 121 else: sys.exit("Must specify user") 122 else: user = None 123 process_db(action, opts.file, user, read_user_db, write_user_db, 124 "SELECT uid FROM users") 74 125 else: 75 126 sys.exit("Invalid --type field (how'd you do that?)") 76 127 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 DB85 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 DB105 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 DB114 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" % project132 133 # init action falls through to write an empty DB134 write_project_db(opts.file, projects)135 136 128 sys.exit(0)
Note: See TracChangeset
for help on using the changeset viewer.