Changeset 266e866 for fedd


Ignore:
Timestamp:
Dec 3, 2008 2:24:15 PM (16 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
Children:
dceeef9
Parents:
b312431b
Message:

Add user databas manipulation commands. Collapse some code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/fixed_resource.py

    rb312431b r266e866  
    44import MySQLdb
    55from federation.fixed_resource import read_key_db, write_key_db, \
    6         read_project_db, write project_db
     6        read_project_db, write_project_db, read_user_db, write_user_db
    77from optparse import OptionParser
    88
     
    2727                "[opts] (--help for details)", version="0.1")
    2828        self.add_option('-t','--type', dest='type', type='choice',
    29                 choices=['keys','projects'], help="database type")
     29                choices=['keys','projects', 'users'], help="database type")
    3030        self.add_option('-u','--user', dest='user', default=None,
    3131                action='store', help='user to add/delete')
     
    4141                help='database file (synonym for --database)')
    4242
     43def 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
    4378
    4479parser = key_opts()
    4580action = sys.argv[1]
     81init_actions = set(['init', 'initall'])
    4682
    4783# Check the action
     
    4985else: sys.exit("Bad action, must be one of %s" % ", ".join(actions))
    5086
    51 # Parse and check the the options for consistency
    5287(opts, args) = parser.parse_args()
    5388if not opts.file:
     
    5691    sys.exit("Must specify database type (--type)")
    5792elif opts.type == 'keys':
    58     if action != "init" and action != "initall" and action != "addall":
     93    if action not in init_actions:
    5994        if opts.user and (opts.key or opts.keyfile):
    6095            user = opts.user
     
    68103        else:
    69104            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
    70111elif opts.type == 'projects':
    71     if action != "init" and action != "initall" and action != "addall":
     112    if action not in init_actions:
    72113        if opts.project: project = opts.project
    73114        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")
     118elif 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")
    74125else:
    75126    sys.exit("Invalid --type field (how'd you do that?)")
    76127
    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 
    136128sys.exit(0)
Note: See TracChangeset for help on using the changeset viewer.