4 from pprint import pformat
5 from sfa.generic import Generic
6 from optparse import OptionParser
7 from pprint import PrettyPrinter
8 from sfa.util.xrn import Xrn
9 from sfa.storage.record import SfaRecord
10 from sfa.client.sfi import save_records_to_file
11 pprinter = PrettyPrinter(indent=4)
14 def args(*args, **kwargs):
16 func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
20 class Commands(object):
22 def _get_commands(self):
23 available_methods = []
24 for attrib in dir(self):
25 if callable(getattr(self, attrib)) and not attrib.startswith('_'):
26 available_methods.append(attrib)
27 return available_methods
29 class RegistryCommands(Commands):
31 def __init__(self, *args, **kwds):
32 self.api= Generic.the_flavour().make_api(interface='registry')
35 version = self.api.manager.GetVersion(self.api, {})
36 pprinter.pprint(version)
38 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
39 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
40 def list(self, xrn, type=None):
42 records = self.api.manager.List(self.api, xrn.get_hrn())
43 for record in records:
44 if not type or record['type'] == type:
45 print "%s (%s)" % (record['hrn'], record['type'])
48 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
49 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
50 @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file')
51 @args('-f', '--format', dest='format', metavar='<display>', type='choice',
52 choices=('text', 'xml', 'summary'), help='display record in different formats')
53 def show(self, xrn, type=None, format=None, outfile=None):
54 records = self.api.manager.Resolve(self.api, xrn, type, True)
55 for record in records:
56 sfa_record = SfaRecord(dict=record)
57 sfa_record.dump(format)
59 save_records_to_file(outfile, records)
61 def register(self, record):
64 def update(self, record):
67 def remove(self, xrn):
70 def credential(self, xrn):
76 class CerficiateCommands(Commands):
78 def import_records(self, xrn):
81 def export(self, xrn):
84 def display(self, xrn):
90 class AggregateCommands(Commands):
92 def __init__(self, *args, **kwds):
93 self.api= Generic.the_flavour().make_api(interface='aggregate')
96 version = self.api.manager.GetVersion(self.api, {})
97 pprinter.pprint(version)
100 print self.api.manager.ListSlices(self.api, [], {})
102 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
103 def status(self, xrn):
104 urn = Xrn(xrn, 'slice').get_urn()
105 status = self.api.manager.SliverStatus(self.api, urn, [], {})
106 pprinter.pprint(status)
108 def resources(self, xrn):
111 def create(self, xrn, rspec):
114 def delete(self, xrn):
117 def start(self, xrn):
123 def reset(self, xrn):
130 class SliceManagerCommands(AggregateCommands):
132 def __init__(self, *args, **kwds):
133 self.api= Generic().make_api(interface='slicemgr')
136 CATEGORIES = {'registry': RegistryCommands,
137 'aggregate': AggregateCommands,
138 'slicemgr': SliceManagerCommands}
141 argv = copy.deepcopy(sys.argv)
142 script_name = argv.pop(0)
144 print script_name + " category action [<args>]"
145 print "Available categories:"
150 category = argv.pop(0)
151 usage = "%%prog %s action <args> [options]" % (category)
152 parser = OptionParser(usage=usage)
153 command_class = CATEGORIES[category]
154 command_instance = command_class()
155 actions = command_instance._get_commands()
157 if hasattr(command_instance, '__call__'):
159 command = command_instance.__call__
161 print script_name + " category action [<args>]"
162 print "Available actions for %s category:" % category
168 command = getattr(command_instance, action)
170 options = getattr(command, 'options', [])
171 usage = "%%prog %s %s <args> [options]" % (category, action)
172 parser = OptionParser(usage=usage)
173 for arg, kwd in options:
174 parser.add_option(*arg, **kwd)
175 (opts, cmd_args) = parser.parse_args(argv)
176 cmd_kwds = vars(opts)
178 # dont overrride meth
179 for k, v in cmd_kwds.items():
184 command(*cmd_args, **cmd_kwds)
187 print "Possible wrong number of arguments supplied"
188 print command.__doc__
193 print "Command failed, please check log for more info"
197 if __name__ == '__main__':