2 from __future__ import with_statement
4 # sfi -- slice-based facility interface
8 #from cert import Keypair, Certificate
9 from optparse import OptionParser
10 #from util.geniclient import GeniClient
12 sfi_dir = os.path.expanduser("~/.sfi/")
17 # Establish Connection to SliceMgr and Registry Servers
19 def set_servers(options):
23 # Set SliceMgr and Registry URLs
24 if (options.sm is not None):
26 elif ("SM" in os.environ):
29 print "No Known Slice Manager"
31 if (options.registry is not None):
32 registry = options.registry
33 elif ("REGISTRY" in os.environ):
34 registry = os.environ["REGISTRY"]
36 print "No Known Registry"
39 print "Contacting Slice Manager at:", sm
40 print "Contacting Registry at:", registry
42 # SliceMgr and Registry may be available on the same server
43 # if (sm == registry):
44 # sm_chan = GeniClient(sm, key_file, cert_file)
47 # sm_chan = GeniClient(sm, key_file, cert_file)
48 # reg_chan = GeniClient(registry, key_file, cert_file)
52 # Get file names for various credentials and specs
54 # Establishes limiting conventions
55 # - conflates MAs and SAs
56 # - assumes a single user per working directory
57 # - assumes last token in slice name is unique
59 # Bootstraps credentials (not done yet)
63 parts = name.split(".")
66 def get_user_cred_fn():
67 file = os.path.join(sfi_dir, os.environ["USER"] + ".cred")
68 if (os.path.isfile(file)):
71 print "bootstrap user credential here"
73 def get_auth_cred_fn():
74 file = os.path.join(sfi_dir, "auth.cred")
75 if (os.path.isfile(file)):
78 print "bootstrap authority credential here"
80 def get_slice_cred_fn(name):
81 file = os.path.join(sfi_dir, "slice_" + get_leaf(name) + ".cred")
82 if (os.path.isfile(file)):
85 print "bootstrap slice credential here"
87 def get_rspec_fn(rspec):
88 if (os.path.isabs(rspec)):
91 file = os.path.join(sfi_dir, rspec)
92 if (os.path.isfile(file)):
95 print "No such rspec file"
98 def get_record_fn(record):
99 if (os.path.isabs(record)):
102 file = os.path.join(sfi_dir, record)
103 if (os.path.isfile(file)):
106 print "No such registry record file"
110 # Generate sub-command parser
112 def create_cmd_parser(command):
113 cmdargs = {"list": "name",
116 "add": "name record",
117 "update": "name record",
121 "create": "name rspec",
127 if command not in cmdargs:
128 print "Invalid command\n"
129 print "Commands:list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset"
132 parser = OptionParser(usage="sfi [sfi_options] %s [options] %s" \
133 % (command, cmdargs[command]))
134 if command in ("nodes", "resources"):
135 parser.add_option("-f", "--format", dest="format",type="choice",
136 help="output format (dns|ip|hrn|rspec)",default="rspec",
137 choices=("dns","ip","hrn","rspec"))
138 elif command in ("list", "show", "remove"):
139 parser.add_option("-t", "--type", dest="type",type="choice",
140 help="type filter (user|slice|sa|ma|node|aggregate)",
141 choices=("user","slice","sa","ma","node","aggregate", "all"),
146 # Main: parse arguments and dispatch to command
152 # Generate command line parser
153 parser = OptionParser(usage="sfi [options] command [command_options] [command_args]",
154 description="Commands: list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset")
155 parser.add_option("-r", "--registry", dest="registry",
156 help="root registry", metavar="URL", default=None)
157 parser.add_option("-s", "--slicemgr", dest="sm",
158 help="slice manager", metavar="URL", default=None)
159 parser.add_option("-d", "--dir", dest="dir",
160 help="working directory", metavar="PATH", default = sfi_dir)
161 parser.add_option("-v", "--verbose",
162 action="store_true", dest="verbose", default=False,
164 parser.disable_interspersed_args()
165 (options, args) = parser.parse_args()
167 (cmd_opts, cmd_args) = create_cmd_parser(command).parse_args(args[1:])
169 print options.registry,options.sm,options.dir,options.verbose
171 if command in ("nodes", "resources"):
172 print cmd_opts.format
173 elif command in ("list","show","remove"):
179 # Dispatch to selected command
181 globals()[command](cmd_opts, cmd_args)
183 print "Command not found:", command
188 # Following functions implement the commands
189 # todo: make sure args exist
191 # First, the Registry-related commands
194 # list entires in named authority registry
195 def list(opts, args):
197 cred_file = get_user_cred_fn()
198 with open(cred_file) as f:
199 credential = f.read()
200 print "list:", opts.type, args[0], reg_chan, credential
201 # result = reg_chan.list(credential, args[0])
202 # ...filter output based on opts.type...
205 # show named registry record
206 def show(opts, args):
207 # pretty print or return record xml?
209 cred_file = get_user_cred_fn()
210 with open(cred_file) as f:
211 credential = f.read()
212 print "show:", opts.type, args[0], reg_chan, credential
213 # result = reg_chan.resolve(credential, args[0])
214 # ...filter output based on opts.type...
217 # removed named registry record
218 def remove(opts, args):
220 cred_file = get_auth_cred_fn()
221 with open(cred_file) as f:
222 credential = f.read()
223 print "remove:", opts.type, args[0], reg_chan, credential
224 # ...first retrieve named record...
225 # results = reg_chan.resolve(credential, args[0])
226 # ...filter desired record from result using opts.type
227 # ...use that record to call remove...
228 # result = reg_chan.remove(credential, record)
231 # add named registry record
234 cred_file = get_auth_cred_fn()
235 with open(cred_file) as f:
236 credential = f.read()
237 rec_file = get_record_fn(args[1])
238 with open(rec_file) as g:
240 print "add:", record, reg_chan, credential
241 # result = reg_chan.register(credential, record)
244 # update named registry entry
245 def update(opts, args):
247 cred_file = get_auth_cred_fn()
248 with open(cred_file) as f:
249 credential = f.read()
250 rec_file = get_record_fn(args[1])
251 with open(rec_file) as g:
253 print "update:", record, reg_chan, credential
254 # result = reg_chan.update(credential, record)
258 # Second, the Slice-related commands
261 # list available nodes
262 def nodes(opts, args):
264 cred_file = get_user_cred_fn()
265 with open(cred_file) as f:
266 credential = f.read()
267 if (args[0] is None):
271 print "nodes:", opts.format, context, sm_chan, credential
272 # result = sm_chan.list_nodes(credential, context)
273 # ...format output based on opts.format...
276 # list instantiated slices
277 def slices(opts, args):
279 cred_file = get_user_cred_fn()
280 with open(cred_file) as f:
281 credential = f.read()
282 print "slices:", sm_chan, credential
283 # result = sm_chan.list_slices(credential)
286 # show rspec for named slice
287 def resources(opts, args):
289 cred_file = get_slice_cred_fn(args[0])
290 with open(cred_file) as f:
291 credential = f.read()
292 print "resources:", opts.format, args[0], sm_chan, credential
293 # result = sm_chan.get_resources(credential, args[0])
294 # ...format output based on opts.format...
297 # created named slice with given rspec
298 def create(opts, args):
300 cred_file = get_slice_cred_fn(args[0])
301 with open(cred_file) as f:
302 credential = f.read()
303 rspec_file = get_rspec_fn(args[1])
304 with open(rspec_file) as g:
306 print "create:", args[0], rspec, sm_chan, credential
307 # result = sm_chan.instantiate(credential, rspec)
311 def delete(opts, args):
313 cred_file = get_slice_cred_fn(args[0])
314 with open(cred_file) as f:
315 credential = f.read()
316 print "delete:", args[0], sm_chan, credential
317 # result = sm_chan.delete_slice(credential)
321 def start(opts, args):
323 cred_file = get_slice_cred_fn(args[0])
324 with open(cred_file) as f:
325 credential = f.read()
326 print "start:", args[0], sm_chan, credential
327 # result = sm_chan.start_slice(credential)
331 def stop(opts, args):
333 cred_file = get_slice_cred_fn(args[0])
334 with open(cred_file) as f:
335 credential = f.read()
336 print "stop:", args[0], sm_chan, credential
337 # result = sm_chan.stop_slice(credential)
341 def reset(opts, args):
343 cred_file = get_slice_cred_fn(args[0])
344 with open(cred_file) as f:
345 credential = f.read()
346 print "reset:", args[0], sm_chan, credential
347 # result = sm_chan.reset_slice(credential)
351 if __name__=="__main__":