1 # command line interface
7 from geniclient import *
8 from geniticket import *
10 long_opts = ["keyfile=", "help", "outfile=", "credfile=", "ticketfile=", "username=", "email=", "ip=", "dns=", "dump_parents", "server="]
12 # default command line options
34 server_url = "https://localhost:12345/"
37 parts = hrn.split(".")
\r
41 print "syntax: cli <options> command <args>"
43 print " --username ... username (or hrn) of user making call"
44 print " --outfile ... save response to a file"
45 print " --credfile ... credential of user making call (or 'None')"
46 print " --keyfile ... private key file of user making call"
47 print " --ticketfile ... filename of ticket (for redeemticket)"
48 print " --email ... email address (for registering users)"
49 print " --ip ... IP address (for registering nodes)"
50 print " --dns ... DNS address (for registering nodes)"
51 print " --dump_parents ... dump parents"
52 print " --server ... geni server (registry/component) to connect to"
54 print " resolve <hrn>"
55 print " dumpCredential"
56 print " getCredential <type> <hrn>"
58 print " createKey <filename>"
59 print " createGid <hrn> <uuid|None> <pubkey_fn>"
60 print " register <type> <hrn> <gid_filename>"
61 print " remove <type> <hrn>"
62 print " update <type> <hrn>"
67 def process_options():
71 global cert_file, cred_file
72 global key_file, out_file, ticket_file
73 global uuid, pkey_fn, gid_fn, email, gid_pkey_fn, ip, dns
77 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
85 elif name == "--username":
87 elif name == "--outfile":
89 elif name == "--credfile":
91 elif name == "--certfile":
93 elif name == "--keyfile":
95 elif name == "--ticketfile":
97 elif name == "--email":
101 elif name == "--dns":
103 elif name == "--dump_parents":
105 elif name == "--server":
109 print "no operation specified"
114 if opname == "resolve":
116 print "syntax: resolve <hrn>"
120 elif opname == "getCredential":
122 print "syntax: getcredential <type> <hrn>"
127 elif opname == "createGid":
129 print "syntax: createGid <hrn> <uuid|None> <pubkey_fn>"
135 gid_pkey_fn = args[3]
137 elif opname == "register":
139 print "syntax: register <type> <hrn> <gid_filename>"
144 elif opname == "remove":
146 print "syntax: remove <type> <hrn>"
150 elif opname == "update":
152 print "syntax: update <type> <hrn>"
156 elif opname == "getTicket":
158 print "syntax: getTicket <hrn>"
162 leaf_name = get_leaf(username)
164 if cert_file == None:
165 cert_file = leaf_name + ".cert"
168 key_file = leaf_name + ".pkey"
170 if cred_file == None:
171 cred_file = leaf_name + ".cred"
174 print " server:", server_url
175 print " username:", username
176 print "cert_file:", cert_file
177 print " key_file:", key_file
178 print "cred_file:", cred_file
179 print "operation:", opname
182 print " out_file:", out_file
184 def get_authority(x):
186 return ".".join(parts[:3])
188 def dumpCredential():
191 # creates a self-signed certificate and private key
193 k = Keypair(create=True)
200 ik = Keypair(create=True)
203 print "writing private key to", key_file
204 k.save_to_file(key_file)
206 #cert = Certificate(subject=username)
208 #cert.set_issuer(ik, iname)
210 #print "writing self-signed cert to", cert_file
211 #cert.save_to_file(cert_file)
213 def load_publickey_string(fn):
215 key_string = f.read()
217 # if the filename is a private key file, then extract the public key
218 if "PRIVATE KEY" in key_string:
219 outfn = tempfile.mktemp()
220 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
223 key_string = f.read()
234 # if the operation is not a local operation, then create a geniclient to
236 if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey"):
237 if not os.path.exists(key_file):
238 print "key file", key_file, "does not exist"
240 if not os.path.exists(cert_file):
241 k = Keypair(filename = key_file)
242 cert = Certificate(subject=username)
244 cert.set_issuer(k, username)
246 print "writing self-signed cert to", cert_file
247 cert.save_to_file(cert_file)
248 client = GeniClient(server_url, key_file, cert_file)
250 # if a cred_file was specified, then load the credential
251 if (cred_file=="None") or (opname == "help") or (opname == "createKey") or \
252 (opname == "redeemTicket"):
255 cred = Credential(filename = cred_file)
257 if opname == "dumpCredential":
260 elif opname == "help":
263 elif opname == "createKey":
266 elif (opname == "resolve"):
267 result = client.resolve(cred, hrn)
269 for record in result:
271 record.dump(dump_parents=dump_parents)
275 elif (opname == "getCredential"):
276 result = client.get_credential(cred, type, hrn)
279 result.dump(dump_parents=dump_parents)
281 file(out_file, "w").write(result.save_to_string(save_parents=True))
285 elif (opname == "list"):
286 result = client.list(cred)
288 for record in result:
290 record.dump(dump_parents=dump_parents)
294 elif (opname == "createGid"):
295 # try loading it from a private or a public key file
296 pkey_string = load_publickey_string(gid_pkey_fn)
298 gid = client.create_gid(cred, hrn, uuid, pkey_string)
301 gid.dump(dump_parents=dump_parents)
303 file(out_file,"w").write(gid.save_to_string(save_parents=True))
307 elif (opname == "register"):
311 print "ERROR: must specify --email <addr> when registering users"
312 geni_info['email'] = email
316 print "ERROR: must specify --ip <addr> when registering nodes"
319 print "ERROR: must specify --dns <addr> when registering nodes"
320 geni_info['dns'] = dns
322 gid = GID(filename=gid_fn)
323 record = GeniRecord(name=hrn, gid=gid, type=type, pointer=-1)
324 record.set_geni_info(geni_info)
326 result = client.register(cred, record)
328 elif (opname == "remove"):
329 record_list = client.resolve(cred, hrn)
331 print "no records match hrn"
333 matching_records = []
334 for record in record_list:
335 if record.get_type() == type:
336 matching_records.append(record)
338 if not matching_records:
339 print "records match hrn, but no records match type"
341 for record in matching_records:
342 client.remove(cred,record)
344 elif (opname == "update"):
345 record_list = client.resolve(cred, hrn)
347 print "no records match hrn"
349 matching_records = []
350 for record in record_list:
351 if record.get_type() == type:
352 matching_records.append(record)
354 if not matching_records:
355 print "records match hrn, but no records match type"
357 for record in matching_records:
358 geni_info = record.get_geni_info()
361 geni_info['email'] = email
365 geni_info['dns'] = dns
367 client.update(cred, record)
369 elif (opname == "stopSlice"):
370 client.stop_slice(cred)
372 elif (opname == "startSlice"):
373 client.start_slice(cred)
375 elif (opname == "resetSlice"):
376 client.reset_slice(cred)
378 elif (opname == "deleteSlice"):
379 client.delete_slice(cred)
381 elif (opname == "listSlices"):
382 result = client.list_slices(cred)
384 print "\n".join(result)
386 file(out_file,"w").write("\n".join(result))
388 elif (opname == "getTicket"):
389 result = client.get_ticket(cred, hrn, {})
392 result.dump(dump_parents=dump_parents)
394 file(out_file,"w").write(result.save_to_string(save_parents=True))
398 elif (opname == "redeemTicket"):
399 ticket = Ticket(filename = ticket_file)
400 result = client.redeem_ticket(ticket)
403 print "unknown operation: " + opname
405 if __name__=="__main__":