Changeset 159a447


Ignore:
Timestamp:
Nov 18, 2008 10:55:47 AM (15 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:
fd729b9
Parents:
7583a62
Message:

pre rename commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/fixed_key.py

    r7583a62 r159a447  
    44# includes a command line utility to manipulate the DB.
    55
    6 def read_db(file):
     6def read_key_db(file):
    77    """
    88    Read the set of fixed keys fom the file
     
    1717    return keys
    1818
    19 def write_db(file, keys):
     19def write_key_db(file, keys):
    2020    """
    2121    Write the set of keys to the given file
     
    2727    f.close()
    2828
     29def read_project_db(file):
     30    """
     31    Read the set of fixed keys fom the file
     32    """
     33    projects = set()
     34
     35    f = open(file, "r")
     36    for line in f:
     37        projects.add(line.rstrip())
     38    f.close()
     39    return projects
     40
     41def write_project_db(file, projects):
     42    """
     43    Write the set of keys to the given file
     44    """
     45
     46    f = open(file, 'w')
     47    for p in projects:
     48        print >>f, "%s" % p
     49    f.close()
     50
     51
    2952# This is the command line utility
    3053if __name__ =='__main__':
     
    3356    from optparse import OptionParser
    3457
    35     actions = set(("init", "add", "delete"))
     58    actions = set(("initall", "init", "add", "delete"))
    3659
    3760    def read_key_file(file):
     
    4669    class key_opts(OptionParser):
    4770        def __init__(self):
    48             OptionParser.__init__(self, usage="%prog (init|add|delete) " + \
     71            OptionParser.__init__(self, usage="%prog " + \
     72                    "(init|add|delete|initall) " + \
    4973                    "[opts] (--help for details)", version="0.1")
    50             self.add_option('-A', '--all-users', dest='all', default=False,
    51                     action='store_true', help='add all users to the database')
     74            self.add_option('-t','--type', dest='type', type='choice',
     75                    choices=['keys','projects'], help="database type")
    5276            self.add_option('-u','--user', dest='user', default=None,
    5377                    action='store', help='user to add/delete')
     78            self.add_option('-p','--project', dest='project', default=None,
     79                    action='store', help='project to add/delete')
    5480            self.add_option('-k','--key', dest='key', default=None,
    5581                    action='store', help='key to add/delete (string)')
     
    74100    if not opts.file:
    75101        sys.exit("Must specify DB file")
     102    if not opts.type:
     103        sys.exit("Must specify database type (--type)")
     104    elif opts.type == 'keys':
     105        if action != "init" and action != "initall" and action != "addall":
     106            if opts.user and (opts.key or opts.keyfile):
     107                user = opts.user
     108                if opts.key:
     109                    key = opts.key
     110                elif opts.keyfile:
     111                    try:
     112                        key = read_key_file(opts.keyfile)
     113                    except IOError, e:
     114                        sys.exit("Error reading keyfile: %s" % e)
     115            else:
     116                sys.exit("Must specify user and key")
     117    elif opts.type == 'projects':
     118        if action != "init" and action != "initall" and action != "addall":
     119            if opts.project: project = opts.project
     120            else: sys.exit("Must specify project")
     121    else:
     122        sys.exit("Invalid --type field (how'd you do that?)")
    76123
    77     # If we're initializing, clear the DB unless we're adding all from the
    78     # Emulab DB
    79     if action == 'init':
    80         if not opts.all:
    81             write_db(opts.file, keys)
    82             sys.exit(0)
    83     else:
    84         keys = read_db(opts.file)
     124    if opts.type == 'keys':
     125        if action != 'init' and action != 'initall':
     126            keys = read_key_db(opts.file)
     127        else:
     128            keys = set()
     129       
     130        if action == 'initall':
     131            # Add all users from the Emulab DB
     132            try:
     133                db = MySQLdb.connect(db="tbdb")
     134            except:
     135                sys.exit("Cannot access the Emulab database")
    85136
    86     if opts.all:
    87         if action == 'delete':
    88             sys.exit("delete and --all don't mix")
    89         # Add all users from the Emulab DB
    90         try:
    91             db = MySQLdb.connect(db="tbdb")
    92         except:
    93             sys.exit("Cannot access the Emulab database")
    94 
    95         c = db.cursor()
    96         c.execute("SELECT uid, pubkey from user_pubkeys")
    97         for u, k in c.fetchall():
    98             keys.add((u, k))
    99         c.close()
    100         db.close()
    101         write_db(opts.file, keys)
    102     else:
    103         # Add/delete a single user
    104         if opts.user: user = opts.user
    105         else: sys.exit("Must provide user (--user)")
    106 
    107         if opts.key:
    108             key = opts.key
    109         elif opts.keyfile:
    110             try:
    111                 key = read_key_file(opts.keyfile)
    112             except IOError, e:
    113                 sys.exit("Error reading keyfile: %s" % e)
    114         else:
    115             sys.exit("Must provide key (--key or --keyfile)")
    116        
    117         if action == 'add':
     137            c = db.cursor()
     138            c.execute("SELECT uid, pubkey FROM user_pubkeys")
     139            for u, k in c.fetchall():
     140                keys.add((u, k))
     141            c.close()
     142            db.close()
     143        elif action == 'add':
    118144            keys.add((user, key))
    119             write_db(opts.file, keys)
    120         elif action == 'delete':
     145        elif action == 'delete':
    121146            if (user, key) in keys:
    122147                keys.remove((user, key))
    123                 write_db(opts.file, keys)
    124148            else:
    125                 print >>sys.stderr, "Cannot delete (%s, %s) not in db" % \
     149                print >>sys.stderr, "Cannot delete (%s, %s): not in db" % \
    126150                        (user, key)
     151        write_key_db(opts.file, keys)
     152    else:
     153        if action != 'init' and action != 'initall':
     154            projects = read_project_db(opts.file)
    127155        else:
    128             sys.exit("How did we get here??")
     156            projects = set()
     157       
     158        if action == 'initall':
     159            # Add all projects from the Emulab DB
     160            try:
     161                db = MySQLdb.connect(db="tbdb")
     162            except:
     163                sys.exit("Cannot access the Emulab database")
     164
     165            c = db.cursor()
     166            c.execute("SELECT pid FROM projects")
     167            for p in c.fetchall():
     168                projects.add(p)
     169            c.close()
     170            db.close()
     171        elif action == 'add':
     172            projects.add(project)
     173        elif action == 'delete':
     174            if project in projects:
     175                projects.remove(project)
     176            else:
     177                print >>sys.stderr, "Cannot delete %s: not in db" % project
     178        write_project_db(opts.file, projects)
     179
    129180    sys.exit(0)
Note: See TracChangeset for help on using the changeset viewer.