5 from pprint import pformat, PrettyPrinter
6 from optparse import OptionParser
8 from sfa.generic import Generic
9 from sfa.util.xrn import Xrn
10 from sfa.storage.record import Record
11 from sfa.client.sfi import save_records_to_file
12 from sfa.trust.hierarchy import Hierarchy
13 from sfa.trust.gid import GID
15 pprinter = PrettyPrinter(indent=4)
18 help_basedir=Hierarchy().basedir
20 help_basedir='*unable to locate Hierarchy().basedir'
22 def optparse_listvalue_callback(option, opt, value, parser):
23 setattr(parser.values, option.dest, value.split(','))
25 def args(*args, **kwargs):
27 func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
31 ### utility to match command-line args to names
33 def __init__ (self, names):
35 # is an input string acceptable for one of the known names?
37 def fits (input, name):
38 return name.find(input)==0
39 # returns one of the names if the input name has a unique match
41 def only_match (self, input):
42 matches=[ name for name in self.names if Candidates.fits(input,name) ]
43 if len(matches)==1: return matches[0]
46 class Commands(object):
47 def _get_commands(self):
48 available_methods = []
49 for attrib in dir(self):
50 if callable(getattr(self, attrib)) and not attrib.startswith('_'):
51 available_methods.append(attrib)
52 return available_methods
55 class RegistryCommands(Commands):
56 def __init__(self, *args, **kwds):
57 self.api= Generic.the_flavour().make_api(interface='registry')
60 """Display the Registry version"""
61 version = self.api.manager.GetVersion(self.api, {})
62 pprinter.pprint(version)
64 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='authority to list (hrn/urn - mandatory)')
65 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
66 @args('-r', '--recursive', dest='recursive', metavar='<recursive>', help='list all child records',
67 action='store_true', default=False)
68 def list(self, xrn, type=None, recursive=False):
69 """List names registered at a given authority - possibly filtered by type"""
71 options = {'recursive': recursive}
72 records = self.api.manager.List(self.api, xrn.get_hrn(), options=options)
73 for record in records:
74 if not type or record['type'] == type:
75 print "%s (%s)" % (record['hrn'], record['type'])
78 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
79 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
80 @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file')
81 @args('-f', '--format', dest='format', metavar='<display>', type='choice',
82 choices=('text', 'xml', 'simple'), help='display record in different formats')
83 def show(self, xrn, type=None, format=None, outfile=None):
84 """Display details for a registered object"""
85 records = self.api.manager.Resolve(self.api, xrn, type, True)
86 for record in records:
87 sfa_record = Record(dict=record)
88 sfa_record.dump(format)
90 save_records_to_file(outfile, records)
93 def _record_dict(self, xrn=None, type=None,
94 url=None, description=None, email='',
96 slices=[], researchers=[], pis=[]):
103 record_dict['urn'] = xrn.get_urn()
104 record_dict['hrn'] = xrn.get_hrn()
105 record_dict['type'] = xrn.get_type()
107 record_dict['url'] = url
109 record_dict['description'] = description
112 pubkey = open(key, 'r').read()
115 record_dict['keys'] = [pubkey]
117 record_dict['slices'] = slices
119 record_dict['researcher'] = researchers
121 record_dict['email'] = email
123 record_dict['pi'] = pis
126 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
127 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
128 @args('-e', '--email', dest='email', default="",
129 help="email (mandatory for users)")
130 @args('-u', '--url', dest='url', metavar='<url>', default=None,
131 help="URL, useful for slices")
132 @args('-d', '--description', dest='description', metavar='<description>',
133 help='Description, useful for slices', default=None)
134 @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
136 @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns',
137 default='', type="str", action='callback', callback=optparse_listvalue_callback)
138 @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers',
139 default='', type="str", action='callback', callback=optparse_listvalue_callback)
140 @args('-p', '--pis', dest='pis', metavar='<PIs>',
141 help='Principal Investigators/Project Managers ',
142 default='', type="str", action='callback', callback=optparse_listvalue_callback)
143 def register(self, xrn, type=None, url=None, description=None, key=None, slices='',
144 pis='', researchers='',email=''):
145 """Create a new Registry record"""
146 record_dict = self._record_dict(xrn=xrn, type=type, url=url, key=key,
147 slices=slices, researchers=researchers, email=email, pis=pis)
148 self.api.manager.Register(self.api, record_dict)
151 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
152 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
153 @args('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
154 @args('-d', '--description', dest='description', metavar='<description>',
155 help='Description', default=None)
156 @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
158 @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns',
159 default='', type="str", action='callback', callback=optparse_listvalue_callback)
160 @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers',
161 default='', type="str", action='callback', callback=optparse_listvalue_callback)
162 @args('-p', '--pis', dest='pis', metavar='<PIs>',
163 help='Principal Investigators/Project Managers ',
164 default='', type="str", action='callback', callback=optparse_listvalue_callback)
165 def update(self, xrn, type=None, url=None, description=None, key=None, slices='',
166 pis='', researchers=''):
167 """Update an existing Registry record"""
168 record_dict = self._record_dict(xrn=xrn, type=type, url=url, description=description,
169 key=key, slices=slices, researchers=researchers, pis=pis)
170 self.api.manager.Update(self.api, record_dict)
172 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
173 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
174 def remove(self, xrn, type=None):
175 """Remove given object from the registry"""
177 self.api.manager.Remove(self.api, xrn)
180 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
181 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
182 def credential(self, xrn, type=None):
183 """Invoke GetCredential"""
184 cred = self.api.manager.GetCredential(self.api, xrn, type, self.api.hrn)
188 def import_registry(self):
189 """Run the importer"""
190 from sfa.importer import Importer
191 importer = Importer()
194 @args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False,
195 help='Remove all registry records and all files in %s area' % help_basedir)
196 @args('-c', '--certs', dest='certs', metavar='<certs>', action='store_true', default=False,
197 help='Remove all cached certs/gids found in %s' % help_basedir )
198 @args('-0', '--no-reinit', dest='reinit', metavar='<reinit>', action='store_false', default=True,
199 help='Prevents new DB schema from being installed after cleanup')
200 def nuke(self, all=False, certs=False, reinit=True):
201 """Cleanup local registry DB, plus various additional filesystem cleanups optionally"""
202 from sfa.storage.dbschema import DBSchema
203 from sfa.util.sfalogging import _SfaLogger
204 logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
205 logger.setLevelFromOptVerbose(self.api.config.SFA_API_LOGLEVEL)
206 logger.info("Purging SFA records from database")
210 # for convenience we re-create the schema here, so there's no need for an explicit
211 # service sfa restart
212 # however in some (upgrade) scenarios this might be wrong
214 logger.info("re-creating empty schema")
215 dbschema.init_or_upgrade()
217 # remove the server certificate and all gids found in /var/lib/sfa/authorities
219 logger.info("Purging cached certificates")
220 for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
222 if file.endswith('.gid') or file == 'server.cert':
226 # just remove all files that do not match 'server.key' or 'server.cert'
228 logger.info("Purging registry filesystem cache")
229 preserved_files = [ 'server.key', 'server.cert']
230 for (dir,_,files) in os.walk(Hierarchy().basedir):
232 if file in preserved_files: continue
237 class CertCommands(Commands):
239 def import_gid(self, xrn):
242 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
243 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
244 @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
245 def export(self, xrn, type=None, outfile=None):
246 """Fetch an object's GID from the Registry"""
247 from sfa.storage.alchemy import dbsession
248 from sfa.storage.model import RegRecord
249 hrn = Xrn(xrn).get_hrn()
250 request=dbsession.query(RegRecord).filter_by(hrn=hrn)
251 if type: request = request.filter_by(type=type)
252 record=request.first()
254 gid = GID(string=record.gid)
256 # check the authorities hierarchy
257 hierarchy = Hierarchy()
259 auth_info = hierarchy.get_auth_info(hrn)
260 gid = auth_info.gid_object
262 print "Record: %s not found" % hrn
266 outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
267 gid.save_to_file(outfile, save_parents=True)
269 @args('-g', '--gidfile', dest='gid', metavar='<gid>', help='path of gid file to display (mandatory)')
270 def display(self, gidfile):
271 """Print contents of a GID file"""
272 gid_path = os.path.abspath(gidfile)
273 if not gid_path or not os.path.isfile(gid_path):
274 print "No such gid file: %s" % gidfile
276 gid = GID(filename=gid_path)
277 gid.dump(dump_parents=True)
280 class AggregateCommands(Commands):
282 def __init__(self, *args, **kwds):
283 self.api= Generic.the_flavour().make_api(interface='aggregate')
286 """Display the Aggregate version"""
287 version = self.api.manager.GetVersion(self.api, {})
288 pprinter.pprint(version)
291 """List the running slices at this Aggregate"""
292 print self.api.manager.ListSlices(self.api, [], {})
294 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
295 def status(self, xrn):
296 """Display the status of a slice or slivers"""
297 urn = Xrn(xrn, 'slice').get_urn()
298 status = self.api.manager.SliverStatus(self.api, urn, [], {})
299 pprinter.pprint(status)
301 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
302 @args('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>',
303 default='GENI', help='version/format of the resulting rspec response')
304 def resources(self, xrn=None, rspec_version='GENI'):
305 """Display the available resources at an aggregate
306 or the resources allocated by a slice"""
307 options = {'geni_rspec_version': rspec_version}
309 options['geni_slice_urn'] = Xrn(xrn, 'slice').get_urn()
311 resources = self.api.manager.ListResources(self.api, [], options)
314 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
315 @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file (mandatory)')
316 @args('-u', '--user', dest='user', metavar='<user>', help='hrn/urn of slice user (mandatory)')
317 @args('-k', '--key', dest='key', metavar='<key>', help="path to user's public key file (mandatory)")
318 def create(self, xrn, rspec, user, key):
319 """Allocate slivers"""
320 xrn = Xrn(xrn, 'slice')
321 slice_urn=xrn.get_urn()
322 rspec_string = open(rspec).read()
323 user_xrn = Xrn(user, 'user')
324 user_urn = user_xrn.get_urn()
325 user_key_string = open(key).read()
326 users = [{'urn': user_urn, 'keys': [user_key_string]}]
328 self.api.manager.CreateSliver(self, slice_urn, [], rspec_string, users, options)
330 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
331 def delete(self, xrn):
333 self.api.manager.DeleteSliver(self.api, xrn, [], {})
335 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
336 def start(self, xrn):
338 self.api.manager.start_slice(self.api, xrn, [])
340 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
343 self.api.manager.stop_slice(self.api, xrn, [])
345 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
346 def reset(self, xrn):
348 self.api.manager.reset_slice(self.api, xrn)
351 # @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
352 # @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='request rspec', default=None)
353 # def ticket(self, xrn, rspec):
358 class SliceManagerCommands(AggregateCommands):
360 def __init__(self, *args, **kwds):
361 self.api= Generic.the_flavour().make_api(interface='slicemgr')
366 CATEGORIES = {'certificate': CertCommands,
367 'registry': RegistryCommands,
368 'aggregate': AggregateCommands,
369 'slicemgr': SliceManagerCommands}
371 def find_category (self, string):
372 for (k,v) in SfaAdmin.CATEGORIES.items():
373 if k.startswith(string): return k
374 for (k,v) in SfaAdmin.CATEGORIES.items():
375 if k.find(string) >=1: return k
378 def summary_usage (self, category=None):
379 print "Usage:", self.script_name + " category action [<options>]"
380 if category and category in SfaAdmin.CATEGORIES:
381 categories=[category]
383 categories=SfaAdmin.CATEGORIES
385 cls=SfaAdmin.CATEGORIES[c]
386 print "==================== category=%s"%c
387 names=cls.__dict__.keys()
390 method=cls.__dict__[name]
391 if name.startswith('_'): continue
393 format="%%-%ds"%margin
395 doc=getattr(method,'__doc__',None)
397 print "<missing __doc__>"
399 lines=[line.strip() for line in doc.split("\n")]
402 for extra_line in lines: print margin*" ",extra_line
406 argv = copy.deepcopy(sys.argv)
407 self.script_name = argv.pop(0)
408 # ensure category is specified
412 # ensure category is valid
413 category_str = argv.pop(0)
414 category=self.find_category (category_str)
418 usage = "%%prog %s action [options]" % (category)
419 parser = OptionParser(usage=usage)
420 command_class = SfaAdmin.CATEGORIES.get(category, None)
421 if not command_class:
422 self.summary_usage(category)
424 # ensure command is valid
425 command_instance = command_class()
426 actions = command_instance._get_commands()
432 if hasattr(command_instance, action):
433 command = getattr(command_instance, action)
435 self.summary_usage(category)
437 # ensure options are valid
438 options = getattr(command, 'options', [])
439 usage = "%%prog %s %s [options]" % (category, action)
440 parser = OptionParser(usage=usage)
441 for arg, kwd in options:
442 parser.add_option(*arg, **kwd)
443 (opts, cmd_args) = parser.parse_args(argv)
444 cmd_kwds = vars(opts)
446 # dont overrride meth
447 for k, v in cmd_kwds.items():
453 #print "invoking %s *=%s **=%s"%(command.__name__,cmd_args, cmd_kwds)
454 command(*cmd_args, **cmd_kwds)
457 print "Possible wrong number of arguments supplied"
459 #traceback.print_exc()
460 print command.__doc__
464 print "Command failed, please check log for more info"
468 ('create delete reset resources slices start status stop version',
475 ('reso','resources'),
484 def test_candidates ():
485 for (names, tuples) in candidates_specs:
487 for (input,expected) in tuples:
488 got=Candidates(names).only_match(input)
489 if got==expected: print '.',
490 else: print 'X FAIL','names[',names,'] input',input,'expected',expected,'got',got
492 if __name__ == '__main__':