Changeset f0dc2ca for fedd


Ignore:
Timestamp:
Nov 29, 2008 11:43: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:
ec4fb42
Parents:
73102d1
Message:

split script out

Location:
fedd
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd/fixed_resource.py

    • Property mode changed from 100755 to 100644
    r73102d1 rf0dc2ca  
    4848        print >>f, "%s" % p
    4949    f.close()
    50 
    51 
    52 # This is the command line utility
    53 if __name__ =='__main__':
    54     import sys
    55     import MySQLdb
    56     from optparse import OptionParser
    57 
    58     actions = set(("initall", "init", "add", "delete"))
    59 
    60     def read_key_file(file):
    61         """
    62         Read a single line from a keyfile and trim the whitespace
    63         """
    64         f = open(file, 'r')
    65         key = f.readline().rstrip()
    66         f.close()
    67         return key
    68 
    69     class key_opts(OptionParser):
    70         """
    71         Options to the command line, pretty self describing
    72         """
    73         def __init__(self):
    74             OptionParser.__init__(self, usage="%prog " + \
    75                     "(init|add|delete|initall) " + \
    76                     "[opts] (--help for details)", version="0.1")
    77             self.add_option('-t','--type', dest='type', type='choice',
    78                     choices=['keys','projects'], help="database type")
    79             self.add_option('-u','--user', dest='user', default=None,
    80                     action='store', help='user to add/delete')
    81             self.add_option('-p','--project', dest='project', default=None,
    82                     action='store', help='project to add/delete')
    83             self.add_option('-k','--key', dest='key', default=None,
    84                     action='store', help='key to add/delete (string)')
    85             self.add_option('-K','--keyfile', dest='keyfile', default=None,
    86                     action='store', help='key to add/delete (file)')
    87             self.add_option('-d', '--database', dest='file', default=None,
    88                     action='store', help='database file')
    89             self.add_option('-f', '--file', dest='file', action='store',
    90                     help='database file (synonym for --database)')
    91 
    92 
    93     parser = key_opts()
    94     action = sys.argv[1]
    95 
    96     # Check the action
    97     if action in actions: del sys.argv[1]
    98     else: sys.exit("Bad action, must be one of %s" % ", ".join(actions))
    99 
    100     # Parse and check the the options for consistency
    101     (opts, args) = parser.parse_args()
    102     if not opts.file:
    103         sys.exit("Must specify DB file")
    104     if not opts.type:
    105         sys.exit("Must specify database type (--type)")
    106     elif opts.type == 'keys':
    107         if action != "init" and action != "initall" and action != "addall":
    108             if opts.user and (opts.key or opts.keyfile):
    109                 user = opts.user
    110                 if opts.key:
    111                     key = opts.key
    112                 elif opts.keyfile:
    113                     try:
    114                         key = read_key_file(opts.keyfile)
    115                     except IOError, e:
    116                         sys.exit("Error reading keyfile: %s" % e)
    117             else:
    118                 sys.exit("Must specify user and key")
    119     elif opts.type == 'projects':
    120         if action != "init" and action != "initall" and action != "addall":
    121             if opts.project: project = opts.project
    122             else: sys.exit("Must specify project")
    123     else:
    124         sys.exit("Invalid --type field (how'd you do that?)")
    125 
    126     if opts.type == 'keys':
    127         if action != 'init' and action != 'initall':
    128             keys = read_key_db(opts.file)
    129         else:
    130             keys = set()
    131        
    132         if action == 'initall':
    133             # Add all users from the Emulab DB
    134             try:
    135                 db = MySQLdb.connect(db="tbdb")
    136             except:
    137                 sys.exit("Cannot access the Emulab database")
    138 
    139             c = db.cursor()
    140             c.execute("SELECT uid, pubkey FROM user_pubkeys")
    141             for u, k in c.fetchall():
    142                 keys.add((u, k))
    143             c.close()
    144             db.close()
    145         elif action == 'add':
    146             keys.add((user, key))
    147         elif action == 'delete':
    148             if (user, key) in keys:
    149                 keys.remove((user, key))
    150             else:
    151                 print >>sys.stderr, "Cannot delete (%s, %s): not in db" % \
    152                         (user, key)
    153         # init action falls through to write an empty DB
    154         write_key_db(opts.file, keys)
    155     else:
    156         if action != 'init' and action != 'initall':
    157             projects = read_project_db(opts.file)
    158         else:
    159             projects = set()
    160        
    161         if action == 'initall':
    162             # Add all projects from the Emulab DB
    163             try:
    164                 db = MySQLdb.connect(db="tbdb")
    165             except:
    166                 sys.exit("Cannot access the Emulab database")
    167 
    168             c = db.cursor()
    169             c.execute("SELECT pid FROM projects")
    170             for p in c.fetchall():
    171                 projects.add(p)
    172             c.close()
    173             db.close()
    174         elif action == 'add':
    175             projects.add(project)
    176         elif action == 'delete':
    177             if project in projects:
    178                 projects.remove(project)
    179             else:
    180                 print >>sys.stderr, "Cannot delete %s: not in db" % project
    181 
    182         # init action falls through to write an empty DB
    183         write_project_db(opts.file, projects)
    184 
    185     sys.exit(0)
Note: See TracChangeset for help on using the changeset viewer.