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 Record
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', 'simple'), 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 = Record(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 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
68 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
69 def remove(self, xrn, type=None):
71 self.api.manager.Remove(self.api, xrn)
74 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
75 @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
76 def credential(self, xrn, type=None):
77 cred = self.api.manager.GetCredential(self.api, xrn, type, self.api.hrn)
81 class CerficiateCommands(Commands):
83 def import_records(self, xrn):
86 def export(self, xrn):
89 def display(self, xrn):
95 class AggregateCommands(Commands):
97 def __init__(self, *args, **kwds):
98 self.api= Generic.the_flavour().make_api(interface='aggregate')
101 version = self.api.manager.GetVersion(self.api, {})
102 pprinter.pprint(version)
105 print self.api.manager.ListSlices(self.api, [], {})
107 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn')
108 def status(self, xrn):
109 urn = Xrn(xrn, 'slice').get_urn()
110 status = self.api.manager.SliverStatus(self.api, urn, [], {})
111 pprinter.pprint(status)
113 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
114 @args('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>',
115 default='GENI', help='version/format of the resulting rspec response')
116 def resources(self, xrn=None, rspec_version='GENI'):
117 options = {'geni_rspec_version': rspec_version}
119 options['geni_slice_urn'] = xrn
120 resources = self.api.manager.ListResources(self.api, [], options)
121 pprinter.pprint(resources)
123 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
124 @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file')
125 def create(self, xrn, rspec):
128 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
129 def delete(self, xrn):
130 self.api.manager.DeleteSliver(self.api, xrn, [], {})
132 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
133 def start(self, xrn):
134 self.api.manager.start_slice(self.api, xrn, [])
136 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
138 self.api.manager.stop_slice(self.api, xrn, [])
140 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
141 def reset(self, xrn):
142 self.api.manager.reset_slice(self.api, xrn)
145 @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
146 @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='request rspec', default=None)
147 def ticket(self, xrn, rspec):
151 class SliceManagerCommands(AggregateCommands):
153 def __init__(self, *args, **kwds):
154 self.api= Generic.the_flavour().make_api(interface='slicemgr')
157 CATEGORIES = {'registry': RegistryCommands,
158 'aggregate': AggregateCommands,
159 'slicemgr': SliceManagerCommands}
162 argv = copy.deepcopy(sys.argv)
163 script_name = argv.pop(0)
165 print script_name + " category action [<args>]"
166 print "Available categories:"
171 category = argv.pop(0)
172 usage = "%%prog %s action <args> [options]" % (category)
173 parser = OptionParser(usage=usage)
174 command_class = CATEGORIES[category]
175 command_instance = command_class()
176 actions = command_instance._get_commands()
178 if hasattr(command_instance, '__call__'):
180 command = command_instance.__call__
182 print script_name + " category action [<args>]"
183 print "Available actions for %s category:" % category
189 command = getattr(command_instance, action)
191 options = getattr(command, 'options', [])
192 usage = "%%prog %s %s <args> [options]" % (category, action)
193 parser = OptionParser(usage=usage)
194 for arg, kwd in options:
195 parser.add_option(*arg, **kwd)
196 (opts, cmd_args) = parser.parse_args(argv)
197 cmd_kwds = vars(opts)
199 # dont overrride meth
200 for k, v in cmd_kwds.items():
205 command(*cmd_args, **cmd_kwds)
208 print "Possible wrong number of arguments supplied"
209 print command.__doc__
214 print "Command failed, please check log for more info"
218 if __name__ == '__main__':