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
10 def args(*args, **kwargs):
12 func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
16 class Commands(object):
18 def _get_commands(self):
19 available_methods = []
20 for attrib in dir(self):
21 if callable(getattr(self, attrib)) and not attrib.startswith('_'):
22 available_methods.append(attrib)
23 return available_methods
25 class RegistryCommands(Commands):
27 def __init__(self, *args, **kwds):
28 self.api= Generic.the_flavour().make_api(interface='registry')
33 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
34 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
35 def list(self, xrn, type=None):
37 records = self.api.manager.List(self.api, xrn.get_hrn())
38 for record in records:
39 if not type or record['type'] == type:
40 print "%s (%s)" % (record['hrn'], record['type'])
43 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
44 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
45 @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file')
46 def show(self, xrn, type=None, full=True):
47 records = self.api.manager.Resolve(self.api, xrn, type, full)
48 for record in records:
49 sfa_record = SfaRecord(record)
50 print sfa_record.dump()
52 def register(self, record):
55 def update(self, record):
58 def remove(self, xrn):
61 def credential(self, xrn):
65 class CerficiateCommands(Commands):
67 def import_records(self, xrn):
70 def export(self, xrn):
74 def display(self, xrn):
79 class AggregateCommands(Commands):
81 def __init__(self, *args, **kwds):
82 self.api= Generic.the_flavour().make_api(interface='aggregate')
90 def status(self, xrn):
93 def resources(self, xrn):
96 def create(self, xrn, rspec):
99 def delete(self, xrn):
102 def start(self, xrn):
108 def reset(self, xrn):
115 class SliceManagerCommands(AggregateCommands):
117 def __init__(self, *args, **kwds):
118 self.api= Generic().make_api(interface='slicemgr')
121 CATEGORIES = {'registry': RegistryCommands,
122 'aggregate': AggregateCommands,
123 'slicemgr': SliceManagerCommands}
126 argv = copy.deepcopy(sys.argv)
127 script_name = argv.pop(0)
129 print script_name + " category action [<args>]"
130 print "Available categories:"
135 category = argv.pop(0)
136 usage = "%%prog %s action <args> [options]" % (category)
137 parser = OptionParser(usage=usage)
138 command_class = CATEGORIES[category]
139 command_instance = command_class()
140 actions = command_instance._get_commands()
142 if hasattr(command_instance, '__call__'):
144 command = command_instance.__call__
146 print script_name + " category action [<args>]"
147 print "Available actions for %s category:" % category
153 command = getattr(command_instance, action)
155 options = getattr(command, 'options', [])
156 usage = "%%prog %s %s <args> [options]" % (category, action)
157 parser = OptionParser(usage=usage)
158 for arg, kwd in options:
159 parser.add_option(*arg, **kwd)
160 (opts, cmd_args) = parser.parse_args(argv)
161 cmd_kwds = vars(opts)
163 # dont overrride meth
164 for k, v in cmd_kwds.items():
169 command(*cmd_args, **cmd_kwds)
172 print "Possible wrong number of arguments supplied"
173 print command.__doc__
177 print "Command failed, please check log for more info"
181 if __name__ == '__main__':