4 from sfa.generic import Generic
5 from optparse import OptionParser
7 from sfa.util.xrn import Xrn
8 from sfa.storage.record import SfaRecord
9 from sfa.client.sfi import save_records_to_file
11 def args(*args, **kwargs):
13 func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
17 class Commands(object):
19 def _get_commands(self):
20 available_methods = []
21 for attrib in dir(self):
22 if callable(getattr(self, attrib)) and not attrib.startswith('_'):
23 available_methods.append(attrib)
24 return available_methods
26 class RegistryCommands(Commands):
28 def __init__(self, *args, **kwds):
29 self.api= Generic.the_flavour().make_api(interface='registry')
34 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
35 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
36 def list(self, xrn, type=None):
38 records = self.api.manager.List(self.api, xrn.get_hrn())
39 for record in records:
40 if not type or record['type'] == type:
41 print "%s (%s)" % (record['hrn'], record['type'])
44 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
45 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
46 @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file')
47 @args('-f', '--format', dest='format', metavar='<display>', type='choice',
48 choices=('text', 'xml', 'summary'), help='display record in different formats')
49 def show(self, xrn, type=None, format=None, outfile=None):
50 records = self.api.manager.Resolve(self.api, xrn, type, True)
51 for record in records:
52 sfa_record = SfaRecord(dict=record)
53 sfa_record.dump(format)
55 save_records_to_file(outfile, records)
57 def register(self, record):
60 def update(self, record):
63 def remove(self, xrn):
66 def credential(self, xrn):
70 class CerficiateCommands(Commands):
72 def import_records(self, xrn):
75 def export(self, xrn):
79 def display(self, xrn):
84 class AggregateCommands(Commands):
86 def __init__(self, *args, **kwds):
87 self.api= Generic.the_flavour().make_api(interface='aggregate')
95 def status(self, xrn):
98 def resources(self, xrn):
101 def create(self, xrn, rspec):
104 def delete(self, xrn):
107 def start(self, xrn):
113 def reset(self, xrn):
120 class SliceManagerCommands(AggregateCommands):
122 def __init__(self, *args, **kwds):
123 self.api= Generic().make_api(interface='slicemgr')
126 CATEGORIES = {'registry': RegistryCommands,
127 'aggregate': AggregateCommands,
128 'slicemgr': SliceManagerCommands}
131 argv = copy.deepcopy(sys.argv)
132 script_name = argv.pop(0)
134 print script_name + " category action [<args>]"
135 print "Available categories:"
140 category = argv.pop(0)
141 usage = "%%prog %s action <args> [options]" % (category)
142 parser = OptionParser(usage=usage)
143 command_class = CATEGORIES[category]
144 command_instance = command_class()
145 actions = command_instance._get_commands()
147 if hasattr(command_instance, '__call__'):
149 command = command_instance.__call__
151 print script_name + " category action [<args>]"
152 print "Available actions for %s category:" % category
158 command = getattr(command_instance, action)
160 options = getattr(command, 'options', [])
161 usage = "%%prog %s %s <args> [options]" % (category, action)
162 parser = OptionParser(usage=usage)
163 for arg, kwd in options:
164 parser.add_option(*arg, **kwd)
165 (opts, cmd_args) = parser.parse_args(argv)
166 cmd_kwds = vars(opts)
168 # dont overrride meth
169 for k, v in cmd_kwds.items():
174 command(*cmd_args, **cmd_kwds)
177 print "Possible wrong number of arguments supplied"
178 print command.__doc__
182 print "Command failed, please check log for more info"
186 if __name__ == '__main__':