#!/usr/bin/python
+import os
import sys
import copy
from pprint import pformat
pprinter = PrettyPrinter(indent=4)
+def optparse_listvalue_callback(option, opt, value, parser):
+ setattr(parser.values, option.dest, value.split(','))
+
def args(*args, **kwargs):
def _decorator(func):
func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
return _decorator
class Commands(object):
-
def _get_commands(self):
available_methods = []
for attrib in dir(self):
sfa_record = Record(dict=record)
sfa_record.dump(format)
if outfile:
- save_records_to_file(outfile, records)
+ save_records_to_file(outfile, records)
- def register(self, record):
- pass
- def update(self, record):
- pass
+ def _record_dict(self, xrn=None, type=None, url=None, key=None, \
+ description=None, slices='', researchers=''):
+ record_dict = {}
+ if xrn:
+ if type:
+ xrn = Xrn(xrn, type)
+ else:
+ xrn = Xrn(xrn)
+ record_dict['urn'] = xrn.get_urn()
+ record_dict['hrn'] = xrn.get_hrn()
+ record_dict['type'] = xrn.get_type()
+ if url:
+ record_dict['url'] = url
+ if key:
+ try:
+ pubkey = open(key, 'r').read()
+ except IOError:
+ pubkey = key
+ record_dict['keys'] = [pubkey]
+ if slices:
+ record_dict['slices'] = slices
+ if researchers:
+ record_dict['researchers'] = researchers
+ if description:
+ record_dict['description'] = description
+ return record_dict
+
+ @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
+ @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
+ @args('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
+ @args('-d', '--description', dest='description', metavar='<description>',
+ help='Description', default=None)
+ @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
+ default=None)
+ @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ @args('-p', '--pis', dest='pis', metavar='<PIs>',
+ help='Principal Investigators/Project Managers ',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ def register(self, xrn, type=None, url=None, description=None, key=None, slices='',
+ pis='', researchers=''):
+ record_dict = self._record_dict(xrn=xrn, type=type, url=url, key=key,
+ slices=slices, researchers=researchers)
+ self.api.manager.Register(self.api, record_dict)
+
+
+ @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
+ @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
+ @args('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
+ @args('-d', '--description', dest='description', metavar='<description>',
+ help='Description', default=None)
+ @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
+ default=None)
+ @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ @args('-p', '--pis', dest='pis', metavar='<PIs>',
+ help='Principal Investigators/Project Managers ',
+ default='', type="str", action='callback', callback=optparse_listvalue_callback)
+ def update(self, xrn, type=None, url=None, description=None, key=None, slices='',
+ pis='', researchers=''):
+ record_dict = self._record_dict(xrn=xrn, type=type, url=url, description=description,
+ key=key, slices=slices, researchers=researchers)
+ self.api.manager.Update(self.api, record_dict)
@args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
@args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
def import_registry(self):
- pass
-
+ from sfa.importer import Importer
+ importer = Importer()
+ importer.run()
@args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False,
help='Remove all registry records and all files in %s area' % Hierarchy().basedir)
@args('-c', '--certs', dest='certs', metavar='<certs>', action='store_true', default=False,
help='Remove all cached certs/gids found in %s' % Hierarchy().basedir )
@args('-0', '--no-reinit', dest='reinit', metavar='<reinit>', action='store_false', default=True,
- help='By default a new DB schema is installed after the cleanup; this option prevents that')
+ help='Prevents new DB schema from being installed after cleanup')
def nuke(self, all=False, certs=False, reinit=True):
from sfa.storage.dbschema import DBSchema
from sfa.util.sfalogging import _SfaLogger
# for convenience we re-create the schema here, so there's no need for an explicit
# service sfa restart
# however in some (upgrade) scenarios this might be wrong
- if options.reinit:
+ if reinit:
logger.info("re-creating empty schema")
dbschema.init_or_upgrade()
# remove the server certificate and all gids found in /var/lib/sfa/authorities
- if options.clean_certs:
+ if certs:
logger.info("Purging cached certificates")
for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
for file in files:
os.unlink(path)
# just remove all files that do not match 'server.key' or 'server.cert'
- if options.all:
+ if all:
logger.info("Purging registry filesystem cache")
preserved_files = [ 'server.key', 'server.cert']
for (dir,_,files) in os.walk(Hierarchy().basedir):
@args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
def export(self, xrn, type=None, outfile=None):
from sfa.storage.alchemy import dbsession
+ from sfa.storage.model import RegRecord
+ hrn = Xrn(xrn).get_hrn()
request=dbsession.query(RegRecord).filter_by(hrn=hrn)
if type: request = request.filter_by(type=type)
record=request.first()
if xrn:
options['geni_slice_urn'] = xrn
resources = self.api.manager.ListResources(self.api, [], options)
- pprinter.pprint(resources)
+ print resources
@args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
@args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file')
@args('-u', '--user', dest='user', metavar='<user>', help='hrn/urn of slice user')
@args('-k', '--key', dest='key', metavar='<key>', help="path to user's public key file")
def create(self, xrn, rspec, user, key):
- xrn = Xrn(xrn)
+ xrn = Xrn(xrn, 'slice')
slice_urn=xrn.get_urn()
- slice_hrn=xrn.get_hrn()
rspec_string = open(rspec).read()
user_xrn = Xrn(user, 'user')
user_urn = user_xrn.get_urn()
user_key_string = open(key).read()
users = [{'urn': user_urn, 'keys': [user_key_string]}]
options={}
- self.api.manager.CreateSliver(self, slice_urn, slice_hrn, [], rspec_string, users, options)
+ self.api.manager.CreateSliver(self, slice_urn, [], rspec_string, users, options)
@args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
def delete(self, xrn):
command_instance = command_class()
actions = command_instance._get_commands()
if len(argv) < 1:
- if hasattr(command_instance, '__call__'):
- action = ''
- command = command_instance.__call__
- else:
- print script_name + " category action [<args>]"
- print "Available actions for %s category:" % category
- for k in actions:
- print "\t%s" % k
- sys.exit(2)
+ action = '__call__'
else:
action = argv.pop(0)
+
+ if hasattr(command_instance, action):
command = getattr(command_instance, action)
+ else:
+ print script_name + " category action [<args>]"
+ print "Available actions for %s category:" % category
+ for k in actions:
+ print "\t%s" % k
+ sys.exit(2)
# ensure options are valid
options = getattr(command, 'options', [])