1 # command line interface
7 from geniclient import *
8 from geniticket import *
10 long_opts = ["keyfile=", "help", "outfile=", "credfile=", "ticketfile=",
11 "username=", "email=", "ip=", "dns=", "dump_parents", "server=",
14 # default command line options
37 server_url = "https://localhost:12345/"
40 parts = hrn.split(".")
\r
44 print "syntax: cli <options> command <args>"
46 print " --username ... username (or hrn) of user making call"
47 print " --outfile ... save response to a file"
48 print " --credfile ... credential of user making call (or 'None')"
49 print " --keyfile ... private key file of user making call"
50 print " --ticketfile ... filename of ticket (for redeemticket)"
51 print " --email ... email address (for registering users)"
52 print " --ip ... IP address (for registering nodes)"
53 print " --dns ... DNS address (for registering nodes)"
54 print " --dump_parents ... dump parents"
55 print " --server ... geni server (registry/component) to connect to"
56 print " --filter <type> ... filter the results of a list operation (user | slice | node ...)"
58 print " resolve <hrn>"
59 print " dumpCredential"
60 print " getCredential <type> <hrn>"
62 print " createKey <filename>"
63 print " createGid <hrn> <uuid|None> <pubkey_fn>"
64 print " register <type> <hrn> <gid_filename>"
65 print " remove <type> <hrn>"
66 print " update <type> <hrn>"
71 def process_options():
75 global cert_file, cred_file
76 global key_file, out_file, ticket_file
77 global uuid, pkey_fn, gid_fn, email, gid_pkey_fn, ip, dns
82 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
90 elif name == "--username":
92 elif name == "--outfile":
94 elif name == "--credfile":
96 elif name == "--certfile":
98 elif name == "--keyfile":
100 elif name == "--ticketfile":
102 elif name == "--email":
106 elif name == "--dns":
108 elif name == "--dump_parents":
110 elif name == "--server":
112 elif name == "--filter":
116 print "no operation specified"
121 if opname == "resolve":
123 print "syntax: resolve <hrn>"
127 elif opname == "getCredential":
129 print "syntax: getcredential <type> <hrn>"
134 elif opname == "createGid":
136 print "syntax: createGid <hrn> <uuid|None> <pubkey_fn>"
142 gid_pkey_fn = args[3]
144 elif opname == "register":
146 print "syntax: register <type> <hrn> <gid_filename>"
151 elif opname == "remove":
153 print "syntax: remove <type> <hrn>"
157 elif opname == "update":
159 print "syntax: update <type> <hrn>"
163 elif opname == "getTicket":
165 print "syntax: getTicket <hrn>"
169 leaf_name = get_leaf(username)
171 if cert_file == None:
172 cert_file = leaf_name + ".cert"
175 key_file = leaf_name + ".pkey"
177 if cred_file == None:
178 cred_file = leaf_name + ".cred"
181 print " server:", server_url
182 print " username:", username
183 print "cert_file:", cert_file
184 print " key_file:", key_file
185 print "cred_file:", cred_file
186 print "operation:", opname
189 print " out_file:", out_file
191 def get_authority(x):
193 return ".".join(parts[:3])
195 def dumpCredential():
198 # creates a self-signed certificate and private key
200 k = Keypair(create=True)
207 ik = Keypair(create=True)
210 print "writing private key to", key_file
211 k.save_to_file(key_file)
213 #cert = Certificate(subject=username)
215 #cert.set_issuer(ik, iname)
217 #print "writing self-signed cert to", cert_file
218 #cert.save_to_file(cert_file)
220 def load_publickey_string(fn):
222 key_string = f.read()
224 # if the filename is a private key file, then extract the public key
225 if "PRIVATE KEY" in key_string:
226 outfn = tempfile.mktemp()
227 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
230 key_string = f.read()
241 # if the operation is not a local operation, then create a geniclient to
243 if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey"):
244 if not os.path.exists(key_file):
245 print "key file", key_file, "does not exist"
247 if not os.path.exists(cert_file):
248 k = Keypair(filename = key_file)
249 cert = Certificate(subject=username)
251 cert.set_issuer(k, username)
253 print "writing self-signed cert to", cert_file
254 cert.save_to_file(cert_file)
255 client = GeniClient(server_url, key_file, cert_file)
257 # if a cred_file was specified, then load the credential
258 if (cred_file=="None") or (opname == "help") or (opname == "createKey") or \
259 (opname == "redeemTicket"):
262 cred = Credential(filename = cred_file)
264 if opname == "dumpCredential":
267 elif opname == "help":
270 elif opname == "createKey":
273 elif (opname == "resolve"):
274 result = client.resolve(cred, hrn)
276 for record in result:
278 record.dump(dump_parents=dump_parents)
282 elif (opname == "getCredential"):
283 result = client.get_credential(cred, type, hrn)
286 result.dump(dump_parents=dump_parents)
288 file(out_file, "w").write(result.save_to_string(save_parents=True))
292 elif (opname == "list"):
293 result = client.list(cred)
296 result = [r for r in result if r.type==filter]
297 for record in result:
299 record.dump(dump_parents=dump_parents)
303 elif (opname == "createGid"):
304 # try loading it from a private or a public key file
305 pkey_string = load_publickey_string(gid_pkey_fn)
307 gid = client.create_gid(cred, hrn, uuid, pkey_string)
310 gid.dump(dump_parents=dump_parents)
312 file(out_file,"w").write(gid.save_to_string(save_parents=True))
316 elif (opname == "register"):
320 print "ERROR: must specify --email <addr> when registering users"
321 geni_info['email'] = email
325 print "ERROR: must specify --ip <addr> when registering nodes"
328 print "ERROR: must specify --dns <addr> when registering nodes"
329 geni_info['dns'] = dns
331 gid = GID(filename=gid_fn)
332 record = GeniRecord(name=hrn, gid=gid, type=type, pointer=-1)
333 record.set_geni_info(geni_info)
335 result = client.register(cred, record)
337 elif (opname == "remove"):
338 record_list = client.resolve(cred, hrn)
340 print "no records match hrn"
342 matching_records = []
343 for record in record_list:
344 if record.get_type() == type:
345 matching_records.append(record)
347 if not matching_records:
348 print "records match hrn, but no records match type"
350 for record in matching_records:
351 client.remove(cred,record)
353 elif (opname == "update"):
354 record_list = client.resolve(cred, hrn)
356 print "no records match hrn"
358 matching_records = []
359 for record in record_list:
360 if record.get_type() == type:
361 matching_records.append(record)
363 if not matching_records:
364 print "records match hrn, but no records match type"
366 for record in matching_records:
367 geni_info = record.get_geni_info()
370 geni_info['email'] = email
374 geni_info['dns'] = dns
376 client.update(cred, record)
378 elif (opname == "stopSlice"):
379 client.stop_slice(cred)
381 elif (opname == "startSlice"):
382 client.start_slice(cred)
384 elif (opname == "resetSlice"):
385 client.reset_slice(cred)
387 elif (opname == "deleteSlice"):
388 client.delete_slice(cred)
390 elif (opname == "listSlices"):
391 result = client.list_slices(cred)
393 print "\n".join(result)
395 file(out_file,"w").write("\n".join(result))
397 elif (opname == "getTicket"):
398 result = client.get_ticket(cred, hrn, {})
401 result.dump(dump_parents=dump_parents)
403 file(out_file,"w").write(result.save_to_string(save_parents=True))
407 elif (opname == "redeemTicket"):
408 ticket = Ticket(filename = ticket_file)
409 result = client.redeem_ticket(ticket)
412 print "unknown operation: " + opname
414 if __name__=="__main__":