1 # command line interface
7 from geniclient import *
9 long_opts = ["keyfile=", "help", "outfile=", "credfile=", "username=", "email=", "ip=", "dns=", "dump_parents", "server="]
11 # default command line options
32 server_url = "https://localhost:12345/"
35 parts = hrn.split(".")
\r
39 print "syntax: cli <options> command <args>"
41 print " --username ... username (or hrn) of user making call"
42 print " --outfile ... save response to a file"
43 print " --credfile ... credential of user making call (or 'None')"
44 print " --keyfile ... private key file of user making call"
45 print " --email ... email address (for registering users)"
46 print " --ip ... IP address (for registering nodes)"
47 print " --dns ... DNS address (for registering nodes)"
48 print " --dump_parents ... dump parents"
49 print " --server ... geni server (registry/component) to connect to"
51 print " resolve <hrn>"
52 print " dumpCredential"
53 print " getCredential <type> <hrn>"
55 print " createKey <filename>"
56 print " createGid <hrn> <uuid|None> <pubkey_fn>"
57 print " register <type> <hrn> <gid_filename>"
58 print " remove <type> <hrn>"
59 print " update <type> <hrn>"
63 def process_options():
67 global cert_file, cred_file
68 global key_file, out_file
69 global uuid, pkey_fn, gid_fn, email, gid_pkey_fn, ip, dns
73 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
81 elif name == "--username":
83 elif name == "--outfile":
85 elif name == "--credfile":
87 elif name == "--certfile":
89 elif name == "--keyfile":
91 elif name == "--email":
97 elif name == "--dump_parents":
99 elif name == "--server":
103 print "no operation specified"
108 if opname == "resolve":
110 print "syntax: resolve <hrn>"
114 elif opname == "getCredential":
116 print "syntax: getcredential <type> <hrn>"
121 elif opname == "createGid":
123 print "syntax: createGid <hrn> <uuid|None> <pubkey_fn>"
129 gid_pkey_fn = args[3]
131 elif opname == "register":
133 print "syntax: register <type> <hrn> <gid_filename>"
138 elif opname == "remove":
140 print "syntax: remove <type> <hrn>"
144 elif opname == "update":
146 print "syntax: update <type> <hrn>"
150 leaf_name = get_leaf(username)
152 if cert_file == None:
153 cert_file = leaf_name + ".cert"
156 key_file = leaf_name + ".pkey"
158 if cred_file == None:
159 cred_file = leaf_name + ".cred"
162 print " server:", server_url
163 print " username:", username
164 print "cert_file:", cert_file
165 print " key_file:", key_file
166 print "cred_file:", cred_file
167 print "operation:", opname
170 print " out_file:", out_file
172 def get_authority(x):
174 return ".".join(parts[:3])
176 def dumpCredential():
179 # creates a self-signed certificate and private key
181 k = Keypair(create=True)
188 ik = Keypair(create=True)
191 print "writing private key to", key_file
192 k.save_to_file(key_file)
194 #cert = Certificate(subject=username)
196 #cert.set_issuer(ik, iname)
198 #print "writing self-signed cert to", cert_file
199 #cert.save_to_file(cert_file)
201 def load_publickey_string(fn):
203 key_string = f.read()
205 # if the filename is a private key file, then extract the public key
206 if "PRIVATE KEY" in key_string:
207 outfn = tempfile.mktemp()
208 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
211 key_string = f.read()
222 # if the operation is not a local operation, then create a geniclient to
224 if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey"):
225 if not os.path.exists(key_file):
226 print "key file", key_file, "does not exist"
228 if not os.path.exists(cert_file):
229 k = Keypair(filename = key_file)
230 cert = Certificate(subject=username)
232 cert.set_issuer(k, username)
234 print "writing self-signed cert to", cert_file
235 cert.save_to_file(cert_file)
236 client = GeniClient(server_url, key_file, cert_file)
238 # if a cred_file was specified, then load the credential
239 if (cred_file=="None") or (opname == "help") or (opname == "createKey"):
242 cred = Credential(filename = cred_file)
244 if opname == "dumpCredential":
247 elif opname == "help":
250 elif opname == "createKey":
253 elif (opname == "resolve"):
254 result = client.resolve(cred, hrn)
256 for record in result:
258 record.dump(dump_parents=dump_parents)
262 elif (opname == "getCredential"):
263 result = client.get_credential(cred, type, hrn)
266 result.dump(dump_parents=dump_parents)
268 file(out_file, "w").write(result.save_to_string(save_parents=True))
272 elif (opname == "list"):
273 result = client.list(cred)
275 for record in result:
277 record.dump(dump_parents=dump_parents)
281 elif (opname == "createGid"):
282 # try loading it from a private or a public key file
283 pkey_string = load_publickey_string(gid_pkey_fn)
285 gid = client.create_gid(cred, hrn, uuid, pkey_string)
288 gid.dump(dump_parents=dump_parents)
290 file(out_file,"w").write(gid.save_to_string(save_parents=True))
294 elif (opname == "register"):
298 print "ERROR: must specify --email <addr> when registering users"
299 geni_info['email'] = email
303 print "ERROR: must specify --ip <addr> when registering nodes"
306 print "ERROR: must specify --dns <addr> when registering nodes"
307 geni_info['dns'] = dns
309 gid = GID(filename=gid_fn)
310 record = GeniRecord(name=hrn, gid=gid, type=type, pointer=-1)
311 record.set_geni_info(geni_info)
313 result = client.register(cred, record)
315 elif (opname == "remove"):
316 record_list = client.resolve(cred, hrn)
318 print "no records match hrn"
320 matching_records = []
321 for record in record_list:
322 if record.get_type() == type:
323 matching_records.append(record)
325 if not matching_records:
326 print "records match hrn, but no records match type"
328 for record in matching_records:
329 client.remove(cred,record)
331 elif (opname == "update"):
332 record_list = client.resolve(cred, hrn)
334 print "no records match hrn"
336 matching_records = []
337 for record in record_list:
338 if record.get_type() == type:
339 matching_records.append(record)
341 if not matching_records:
342 print "records match hrn, but no records match type"
344 for record in matching_records:
345 geni_info = record.get_geni_info()
348 geni_info['email'] = email
352 geni_info['dns'] = dns
354 client.update(cred, record)
356 elif (opname == "stopSlice"):
357 client.stop_slice(cred)
359 elif (opname == "startSlice"):
360 client.start_slice(cred)
363 print "unknown operation: " + opname
365 if __name__=="__main__":