Merge branch 'master' into senslab2
[sfa.git] / sfa / client / sfaadmin.py
1 #!/usr/bin/python
2 import os
3 import sys
4 import copy
5 from pprint import pformat, PrettyPrinter
6 from optparse import OptionParser
7
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
14 from sfa.trust.certificate import convert_public_key
15
16 from sfa.client.candidates import Candidates
17
18 pprinter = PrettyPrinter(indent=4)
19
20 try:
21     help_basedir=Hierarchy().basedir
22 except:
23     help_basedir='*unable to locate Hierarchy().basedir'
24
25 def optparse_listvalue_callback(option, opt, value, parser):
26     setattr(parser.values, option.dest, value.split(','))
27
28 def args(*args, **kwargs):
29     def _decorator(func):
30         func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
31         return func
32     return _decorator
33
34 class Commands(object):
35     def _get_commands(self):
36         command_names = []
37         for attrib in dir(self):
38             if callable(getattr(self, attrib)) and not attrib.startswith('_'):
39                 command_names.append(attrib)
40         return command_names
41
42
43 class RegistryCommands(Commands):
44     def __init__(self, *args, **kwds):
45         self.api= Generic.the_flavour().make_api(interface='registry')
46  
47     def version(self):
48         """Display the Registry version""" 
49         version = self.api.manager.GetVersion(self.api, {})
50         pprinter.pprint(version)
51
52     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='authority to list (hrn/urn - mandatory)') 
53     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
54     @args('-r', '--recursive', dest='recursive', metavar='<recursive>', help='list all child records', 
55           action='store_true', default=False) 
56     def list(self, xrn, type=None, recursive=False):
57         """List names registered at a given authority - possibly filtered by type"""
58         xrn = Xrn(xrn, type) 
59         options = {'recursive': recursive}    
60         records = self.api.manager.List(self.api, xrn.get_hrn(), options=options)
61         for record in records:
62             if not type or record['type'] == type:
63                 print "%s (%s)" % (record['hrn'], record['type'])
64
65
66     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
67     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
68     @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file') 
69     @args('-f', '--format', dest='format', metavar='<display>', type='choice', 
70           choices=('text', 'xml', 'simple'), help='display record in different formats') 
71     def show(self, xrn, type=None, format=None, outfile=None):
72         """Display details for a registered object"""
73         records = self.api.manager.Resolve(self.api, xrn, type, details=True)
74         for record in records:
75             sfa_record = Record(dict=record)
76             sfa_record.dump(format) 
77         if outfile:
78             save_records_to_file(outfile, records)  
79
80
81     def _record_dict(self, xrn=None, type=None, 
82                      url=None, description=None, email='', 
83                      key=None, 
84                      slices=[], researchers=[], pis=[]):
85         record_dict = {}
86         if xrn:
87             if type:
88                 xrn = Xrn(xrn, type)
89             else:
90                 xrn = Xrn(xrn)
91             record_dict['urn'] = xrn.get_urn()
92             record_dict['hrn'] = xrn.get_hrn()
93             record_dict['type'] = xrn.get_type()
94         if url:
95             record_dict['url'] = url
96         if description:
97             record_dict['description'] = description
98         if key:
99             try:
100                 pubkey = open(key, 'r').read()
101             except IOError:
102                 pubkey = key
103             record_dict['keys'] = [pubkey]
104         if slices:
105             record_dict['slices'] = slices
106         if researchers:
107             record_dict['researcher'] = researchers
108         if email:
109             record_dict['email'] = email
110         if pis:
111             record_dict['pi'] = pis
112         return record_dict
113
114
115     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
116     @args('-t', '--type', dest='type', metavar='<type>', help='object type (mandatory)',)
117     @args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False, help='check all users GID')
118     def check_gid(self, xrn=None, type=None, all=None):
119         """Check the correspondance between the GID and the PubKey"""
120
121         # db records
122         from sfa.storage.alchemy import dbsession
123         from sfa.storage.model import RegRecord
124         db_query = dbsession.query(RegRecord).filter_by(type=type)
125         if xrn and not all:
126             hrn = Xrn(xrn).get_hrn()
127             db_query = db_query.filter_by(hrn=hrn)
128         elif all and xrn:
129             print "Use either -a or -x <xrn>, not both !!!"
130             sys.exit(1)
131         elif not all and not xrn:
132             print "Use either -a or -x <xrn>, one of them is mandatory !!!"
133             sys.exit(1)
134
135         records = db_query.all()
136         if not records:
137             print "No Record found"
138             sys.exit(1)
139
140         OK = []
141         NOK = []
142         for record in records:
143              # get the pubkey stored in SFA DB
144              db_pubkey_str = record.reg_keys[0].key
145              db_pubkey_obj = convert_public_key(db_pubkey_str)
146
147              # get the pubkey from the gid
148              gid_str = record.gid
149              gid_obj = GID(string = gid_str)
150              gid_pubkey_obj = gid_obj.get_pubkey()
151
152              # Check if gid_pubkey_obj and db_pubkey_obj are the same
153              check = gid_pubkey_obj.is_same(db_pubkey_obj)
154              if check :
155                  OK.append(record.hrn)
156              else:
157                  NOK.append(record.hrn)
158
159         print "GID/PubKey correpondence is OK for: %s\nGID/PubKey correpondence is NOT OK for: %s" %(OK,NOK)
160
161
162
163     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
164     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
165     @args('-e', '--email', dest='email', default="",
166           help="email (mandatory for users)")
167     @args('-u', '--url', dest='url', metavar='<url>', default=None,
168           help="URL, useful for slices")
169     @args('-d', '--description', dest='description', metavar='<description>', 
170           help='Description, useful for slices', default=None)
171     @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file', 
172           default=None)
173     @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns', 
174           default='', type="str", action='callback', callback=optparse_listvalue_callback)
175     @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers', 
176           default='', type="str", action='callback', callback=optparse_listvalue_callback)
177     @args('-p', '--pis', dest='pis', metavar='<PIs>', 
178           help='Principal Investigators/Project Managers ', 
179           default='', type="str", action='callback', callback=optparse_listvalue_callback)
180     def register(self, xrn, type=None, url=None, description=None, key=None, slices='', 
181                  pis='', researchers='',email=''):
182         """Create a new Registry record"""
183         record_dict = self._record_dict(xrn=xrn, type=type, url=url, key=key, 
184                                         slices=slices, researchers=researchers, email=email, pis=pis)
185         self.api.manager.Register(self.api, record_dict)         
186
187
188     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
189     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
190     @args('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
191     @args('-d', '--description', dest='description', metavar='<description>',
192           help='Description', default=None)
193     @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
194           default=None)
195     @args('-s', '--slices', dest='slices', metavar='<slices>', help='slice xrns',
196           default='', type="str", action='callback', callback=optparse_listvalue_callback)
197     @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='slice researchers',
198           default='', type="str", action='callback', callback=optparse_listvalue_callback)
199     @args('-p', '--pis', dest='pis', metavar='<PIs>',
200           help='Principal Investigators/Project Managers ',
201           default='', type="str", action='callback', callback=optparse_listvalue_callback)
202     def update(self, xrn, type=None, url=None, description=None, key=None, slices='', 
203                pis='', researchers=''):
204         """Update an existing Registry record""" 
205         record_dict = self._record_dict(xrn=xrn, type=type, url=url, description=description, 
206                                         key=key, slices=slices, researchers=researchers, pis=pis)
207         self.api.manager.Update(self.api, record_dict)
208         
209     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
210     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
211     def remove(self, xrn, type=None):
212         """Remove given object from the registry"""
213         xrn = Xrn(xrn, type)
214         self.api.manager.Remove(self.api, xrn)            
215
216
217     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
218     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
219     def credential(self, xrn, type=None):
220         """Invoke GetCredential"""
221         cred = self.api.manager.GetCredential(self.api, xrn, type, self.api.hrn)
222         print cred
223    
224
225     def import_registry(self):
226         """Run the importer"""
227         from sfa.importer import Importer
228         importer = Importer()
229         importer.run()
230
231     def sync_db(self):
232         """Initialize or upgrade the db"""
233         from sfa.storage.dbschema import DBSchema
234         dbschema=DBSchema()
235         dbschema.init_or_upgrade
236     
237     @args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False,
238           help='Remove all registry records and all files in %s area' % help_basedir)
239     @args('-c', '--certs', dest='certs', metavar='<certs>', action='store_true', default=False,
240           help='Remove all cached certs/gids found in %s' % help_basedir )
241     @args('-0', '--no-reinit', dest='reinit', metavar='<reinit>', action='store_false', default=True,
242           help='Prevents new DB schema from being installed after cleanup')
243     def nuke(self, all=False, certs=False, reinit=True):
244         """Cleanup local registry DB, plus various additional filesystem cleanups optionally"""
245         from sfa.storage.dbschema import DBSchema
246         from sfa.util.sfalogging import _SfaLogger
247         logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
248         logger.setLevelFromOptVerbose(self.api.config.SFA_API_LOGLEVEL)
249         logger.info("Purging SFA records from database")
250         dbschema=DBSchema()
251         dbschema.nuke()
252
253         # for convenience we re-create the schema here, so there's no need for an explicit
254         # service sfa restart
255         # however in some (upgrade) scenarios this might be wrong
256         if reinit:
257             logger.info("re-creating empty schema")
258             dbschema.init_or_upgrade()
259
260         # remove the server certificate and all gids found in /var/lib/sfa/authorities
261         if certs:
262             logger.info("Purging cached certificates")
263             for (dir, _, files) in os.walk('/var/lib/sfa/authorities'):
264                 for file in files:
265                     if file.endswith('.gid') or file == 'server.cert':
266                         path=dir+os.sep+file
267                         os.unlink(path)
268
269         # just remove all files that do not match 'server.key' or 'server.cert'
270         if all:
271             logger.info("Purging registry filesystem cache")
272             preserved_files = [ 'server.key', 'server.cert']
273             for (dir,_,files) in os.walk(Hierarchy().basedir):
274                 for file in files:
275                     if file in preserved_files: continue
276                     path=dir+os.sep+file
277                     os.unlink(path)
278         
279     
280 class CertCommands(Commands):
281     
282     def import_gid(self, xrn):
283         pass
284
285     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
286     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
287     @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
288     def export(self, xrn, type=None, outfile=None):
289         """Fetch an object's GID from the Registry"""  
290         from sfa.storage.alchemy import dbsession
291         from sfa.storage.model import RegRecord
292         hrn = Xrn(xrn).get_hrn()
293         request=dbsession.query(RegRecord).filter_by(hrn=hrn)
294         if type: request = request.filter_by(type=type)
295         record=request.first()
296         if record:
297             gid = GID(string=record.gid)
298         else:
299             # check the authorities hierarchy
300             hierarchy = Hierarchy()
301             try:
302                 auth_info = hierarchy.get_auth_info(hrn)
303                 gid = auth_info.gid_object
304             except:
305                 print "Record: %s not found" % hrn
306                 sys.exit(1)
307         # save to file
308         if not outfile:
309             outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
310         gid.save_to_file(outfile, save_parents=True)
311         
312     @args('-g', '--gidfile', dest='gid', metavar='<gid>', help='path of gid file to display (mandatory)') 
313     def display(self, gidfile):
314         """Print contents of a GID file"""
315         gid_path = os.path.abspath(gidfile)
316         if not gid_path or not os.path.isfile(gid_path):
317             print "No such gid file: %s" % gidfile
318             sys.exit(1)
319         gid = GID(filename=gid_path)
320         gid.dump(dump_parents=True)
321     
322
323 class AggregateCommands(Commands):
324
325     def __init__(self, *args, **kwds):
326         self.api= Generic.the_flavour().make_api(interface='aggregate')
327    
328     def version(self):
329         """Display the Aggregate version"""
330         version = self.api.manager.GetVersion(self.api, {})
331         pprinter.pprint(version)
332
333     def slices(self):
334         """List the running slices at this Aggregate"""
335         print self.api.manager.ListSlices(self.api, [], {})
336
337     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
338     def status(self, xrn):
339         """Display the status of a slice or slivers"""
340         urn = Xrn(xrn, 'slice').get_urn()
341         status = self.api.manager.SliverStatus(self.api, urn, [], {})
342         pprinter.pprint(status)
343  
344     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
345     @args('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>', 
346           default='GENI', help='version/format of the resulting rspec response')  
347     def resources(self, xrn=None, rspec_version='GENI'):
348         """Display the available resources at an aggregate 
349 or the resources allocated by a slice"""  
350         options = {'geni_rspec_version': rspec_version}
351         if xrn:
352             options['geni_slice_urn'] = Xrn(xrn, 'slice').get_urn()
353         print options
354         resources = self.api.manager.ListResources(self.api, [], options)
355         print resources
356         
357     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
358     @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file (mandatory)')  
359     @args('-u', '--user', dest='user', metavar='<user>', help='hrn/urn of slice user (mandatory)')  
360     @args('-k', '--key', dest='key', metavar='<key>', help="path to user's public key file (mandatory)")  
361     def create(self, xrn, rspec, user, key):
362         """Allocate slivers"""
363         xrn = Xrn(xrn, 'slice')
364         slice_urn=xrn.get_urn()
365         rspec_string = open(rspec).read()
366         user_xrn = Xrn(user, 'user')
367         user_urn = user_xrn.get_urn()
368         user_key_string = open(key).read()
369         users = [{'urn': user_urn, 'keys': [user_key_string]}]
370         options={}
371         self.api.manager.CreateSliver(self, slice_urn, [], rspec_string, users, options) 
372
373     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
374     def delete(self, xrn):
375         """Delete slivers""" 
376         self.api.manager.DeleteSliver(self.api, xrn, [], {})
377  
378     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
379     def start(self, xrn):
380         """Start slivers"""
381         self.api.manager.start_slice(self.api, xrn, [])
382
383     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
384     def stop(self, xrn):
385         """Stop slivers"""
386         self.api.manager.stop_slice(self.api, xrn, [])      
387
388     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
389     def reset(self, xrn):
390         """Reset sliver"""
391         self.api.manager.reset_slice(self.api, xrn)
392
393
394 #    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
395 #    @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='request rspec', default=None)
396 #    def ticket(self, xrn, rspec):
397 #        pass
398
399
400
401 class SliceManagerCommands(AggregateCommands):
402     
403     def __init__(self, *args, **kwds):
404         self.api= Generic.the_flavour().make_api(interface='slicemgr')
405
406
407 class SfaAdmin:
408
409     CATEGORIES = {'certificate': CertCommands,
410                   'registry': RegistryCommands,
411                   'aggregate': AggregateCommands,
412                   'slicemgr': SliceManagerCommands}
413
414     # returns (name,class) or (None,None)
415     def find_category (self, input):
416         full_name=Candidates (SfaAdmin.CATEGORIES.keys()).only_match(input)
417         if not full_name: return (None,None)
418         return (full_name,SfaAdmin.CATEGORIES[full_name])
419
420     def summary_usage (self, category=None):
421         print "Usage:", self.script_name + " category command [<options>]"
422         if category and category in SfaAdmin.CATEGORIES: 
423             categories=[category]
424         else:
425             categories=SfaAdmin.CATEGORIES
426         for c in categories:
427             cls=SfaAdmin.CATEGORIES[c]
428             print "==================== category=%s"%c
429             names=cls.__dict__.keys()
430             names.sort()
431             for name in names:
432                 method=cls.__dict__[name]
433                 if name.startswith('_'): continue
434                 margin=15
435                 format="%%-%ds"%margin
436                 print "%-15s"%name,
437                 doc=getattr(method,'__doc__',None)
438                 if not doc: 
439                     print "<missing __doc__>"
440                     continue
441                 lines=[line.strip() for line in doc.split("\n")]
442                 line1=lines.pop(0)
443                 print line1
444                 for extra_line in lines: print margin*" ",extra_line
445         sys.exit(2)
446
447     def main(self):
448         argv = copy.deepcopy(sys.argv)
449         self.script_name = argv.pop(0)
450         # ensure category is specified    
451         if len(argv) < 1:
452             self.summary_usage()
453
454         # ensure category is valid
455         category_input = argv.pop(0)
456         (category_name, category_class) = self.find_category (category_input)
457         if not category_name or not category_class:
458             self.summary_usage(category_name)
459
460         usage = "%%prog %s command [options]" % (category_name)
461         parser = OptionParser(usage=usage)
462     
463         # ensure command is valid      
464         category_instance = category_class()
465         commands = category_instance._get_commands()
466         if len(argv) < 1:
467             # xxx what is this about ?
468             command_name = '__call__'
469         else:
470             command_input = argv.pop(0)
471             command_name = Candidates (commands).only_match (command_input)
472     
473         if command_name and hasattr(category_instance, command_name):
474             command = getattr(category_instance, command_name)
475         else:
476             self.summary_usage(category_name)
477
478         # ensure options are valid
479         options = getattr(command, 'options', [])
480         usage = "%%prog %s %s [options]" % (category_name, command_name)
481         parser = OptionParser(usage=usage)
482         for arg, kwd in options:
483             parser.add_option(*arg, **kwd)
484         (opts, cmd_args) = parser.parse_args(argv)
485         cmd_kwds = vars(opts)
486
487         # dont overrride meth
488         for k, v in cmd_kwds.items():
489             if v is None:
490                 del cmd_kwds[k]
491
492         # execute command
493         try:
494             #print "invoking %s *=%s **=%s"%(command.__name__,cmd_args, cmd_kwds)
495             command(*cmd_args, **cmd_kwds)
496             sys.exit(0)
497         except TypeError:
498             print "Possible wrong number of arguments supplied"
499             #import traceback
500             #traceback.print_exc()
501             print command.__doc__
502             parser.print_help()
503             #raise
504         except Exception:
505             print "Command failed, please check log for more info"
506             raise
507