initial checkin of admin script
[sfa.git] / sfa / client / sfaadmin.py
1 #!/usr/bin/python
2 import sys
3 import copy
4 from sfa.generic import Generic
5 from optparse import OptionParser
6
7 from sfa.util.xrn import Xrn
8
9 def args(*args, **kwargs):
10     def _decorator(func):
11         func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
12         return func
13     return _decorator
14
15 class Commands(object):
16
17     def _get_commands(self):
18         available_methods = []
19         for attrib in dir(self):
20             if callable(getattr(self, attrib)) and not attrib.startswith('_'):
21                 available_methods.append(attrib)
22         return available_methods         
23
24 class RegistryCommands(Commands):
25
26     def __init__(self, *args, **kwds):
27         self.api= Generic.the_flavour().make_api(interface='registry')
28  
29     def version(self): 
30         pass           
31
32     @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn') 
33     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
34     def list(self, xrn, type=None):
35         xrn = Xrn(xrn, type) 
36         records = self.api.manager.List(self.api, xrn.get_hrn())
37         for record in records:
38             if not type or record['type'] == type:
39                 print "%s (%s)" % (record['hrn'], record['type'])
40
41     def show(self, xrn, type=None, full=True):
42         records = self.api.manager.Resolve(self.api, xrn, type, full) 
43
44     def register(self, record):
45         pass
46
47     def update(self, record):
48         pass
49         
50     def remove(self, xrn):            
51         pass
52
53     def credential(self, xrn):
54         pass
55
56
57 class CerficiateCommands(Commands):
58     
59     def import_records(self, xrn):
60         pass
61
62     def export(self, xrn):
63         pass
64
65
66     def display(self, xrn):
67         pass
68     def nuke(self):
69         pass  
70
71 class AggregateCommands(Commands):
72
73     def __init__(self, *args, **kwds):
74         self.api= Generic.the_flavour().make_api(interface='aggregate')
75    
76     def version(self):
77         pass
78
79     def slices(self):
80         pass        
81
82     def status(self, xrn):
83         pass
84  
85     def resources(self, xrn):
86         pass
87
88     def create(self, xrn, rspec):
89         pass
90
91     def delete(self, xrn):
92         pass 
93     
94     def start(self, xrn):
95         pass
96
97     def stop(self, xrn):
98         pass      
99
100     def reset(self, xrn):
101         pass
102
103     def ticket(self):
104         pass
105
106
107 class SliceManagerCommands(AggregateCommands):
108     
109     def __init__(self, *args, **kwds):
110         self.api= Generic().make_api(interface='slicemgr')
111
112
113 CATEGORIES = {'registry': RegistryCommands,
114               'aggregate': AggregateCommands,
115               'slicemgr': SliceManagerCommands}
116
117 def main():
118     argv = copy.deepcopy(sys.argv)
119     script_name = argv.pop(0)
120     if len(argv) < 1:
121         print script_name + " category action [<args>]"
122         print "Available categories:"
123         for k in CATEGORIES:
124             print "\t%s" % k
125         sys.exit(2)
126
127     category = argv.pop(0)
128     usage = "%%prog %s action <args> [options]" % (category)
129     parser = OptionParser(usage=usage)
130     command_class =  CATEGORIES[category]
131     command_instance = command_class()
132     actions = command_instance._get_commands()
133     if len(argv) < 1:
134         if hasattr(command_instance, '__call__'):
135             action = ''
136             command = command_instance.__call__
137         else:
138             print script_name + " category action [<args>]"
139             print "Available actions for %s category:" % category
140             for k in actions:
141                 print "\t%s" % k 
142             sys.exit(2)
143     else:
144         action = argv.pop(0)
145         command = getattr(command_instance, action)
146
147     options = getattr(command, 'options', [])
148     usage = "%%prog %s %s <args> [options]" % (category, action)
149     parser = OptionParser(usage=usage)
150     for arg, kwd in options:
151         parser.add_option(*arg, **kwd)
152     (opts, cmd_args) = parser.parse_args(argv)
153     cmd_kwds = vars(opts)
154
155     # dont overrride meth
156     for k, v in cmd_kwds.items():
157         if v is None:
158             del cmd_kwds[k]
159
160     try:
161         command(*cmd_args, **cmd_kwds)
162         sys.exit(0)
163     except TypeError:
164         print "Possible wrong number of arguments supplied"
165         print command.__doc__
166         parser.print_help()
167         #raise
168     except Exception:
169         print "Command failed, please check log for more info"
170         raise
171
172
173 if __name__ == '__main__':
174     main()
175     
176      
177         
178