added redeemticket
[sfa.git] / cmdline / genicli.py
index 610f9ab..caaf084 100644 (file)
@@ -5,8 +5,9 @@ import sys
 import os
 from cert import *
 from geniclient import *
+from geniticket import *
 
-long_opts = ["keyfile=", "help", "outfile=", "credfile=", "username=", "email="]
+long_opts = ["keyfile=", "help", "outfile=", "credfile=", "ticketfile=", "username=", "email=", "ip=", "dns=", "dump_parents", "server="]
 
 # default command line options
 username = "client"
@@ -18,12 +19,17 @@ key_file = None
 cred_file = None
 cert_file = None
 out_file = None
+ticket_file = None
 
+ip = None
+dns = None
 email = None
 uuid = None
 gid_pkey_fn = None
 gid_fn = None
 
+dump_parents = False
+
 leaf_name = None
 server_url = "https://localhost:12345/"
 
@@ -35,10 +41,15 @@ def showhelp():
    print "syntax: cli <options> command <args>"
    print "options:"
    print "    --username       ... username (or hrn) of user making call"
-   print "    --outfile        ... save response to a file"
+   print "    --outfile       ... save response to a file"
    print "    --credfile       ... credential of user making call (or 'None')"
    print "    --keyfile        ... private key file of user making call"
-   print "    --email          ... email address"
+   print "    --ticketfile     ... filename of ticket (for redeemticket)"
+   print "    --email          ... email address (for registering users)"
+   print "    --ip             ... IP address (for registering nodes)"
+   print "    --dns            ... DNS address (for registering nodes)"
+   print "    --dump_parents   ... dump parents"
+   print "    --server         ... geni server (registry/component) to connect to"
    print "commands:"
    print "    resolve <hrn>"
    print "    dumpCredential"
@@ -48,14 +59,20 @@ def showhelp():
    print "    createGid <hrn> <uuid|None> <pubkey_fn>"
    print "    register <type> <hrn> <gid_filename>"
    print "    remove <type> <hrn>"
+   print "    update <type> <hrn>"
+   print "    startSlice"
+   print "    stopSlice"
+   print "    listSlices"
 
 def process_options():
    global username
    global opname
    global type, hrn
    global cert_file, cred_file
-   global key_file, out_file
-   global uuid, pkey_fn, gid_fn, email, gid_pkey_fn
+   global key_file, out_file, ticket_file
+   global uuid, pkey_fn, gid_fn, email, gid_pkey_fn, ip, dns
+   global dump_parents
+   global server_url
 
    (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
    for opt in options:
@@ -75,8 +92,18 @@ def process_options():
            cred_file = val
        elif name == "--keyfile":
            key_file = val
+       elif name == "--ticketfile":
+           ticket_file = val
        elif name == "--email":
            email = val
+       elif name == "--ip":
+           ip = val
+       elif name == "--dns":
+           dns = val
+       elif name == "--dump_parents":
+           dump_parents = True
+       elif name == "--server":
+           server_url = val
 
    if not args:
        print "no operation specified"
@@ -120,6 +147,18 @@ def process_options():
        type = args[1]
        hrn = args[2]
 
+   elif opname == "update":
+       if len(args) < 3:
+           print "syntax: update <type> <hrn>"
+       type = args[1]
+       hrn = args[2]
+
+   elif opname == "getTicket":
+       if len(args) < 2:
+           print "syntax: getTicket <hrn>"
+           sys.exit(-1)
+       hrn = args[1]
+
    leaf_name = get_leaf(username)
 
    if cert_file == None:
@@ -132,6 +171,7 @@ def process_options():
        cred_file = leaf_name + ".cred"
 
 def show_options():
+   print "   server:", server_url
    print " username:", username
    print "cert_file:", cert_file
    print " key_file:", key_file
@@ -208,7 +248,8 @@ def main():
        client = GeniClient(server_url, key_file, cert_file)
 
    # if a cred_file was specified, then load the credential
-   if (cred_file=="None") or (opname == "help") or (opname == "createKey"):
+   if (cred_file=="None") or (opname == "help") or (opname == "createKey") or \
+      (opname == "redeemTicket"):
       cred = None
    else:
       cred = Credential(filename = cred_file)
@@ -227,7 +268,7 @@ def main():
       if result:
           for record in result:
               print "RESULT:"
-              record.dump()
+              record.dump(dump_parents=dump_parents)
       else:
           print "NO RESULT"
 
@@ -235,7 +276,7 @@ def main():
       result = client.get_credential(cred, type, hrn)
       if result:
           print "RESULT:"
-          result.dump()
+          result.dump(dump_parents=dump_parents)
           if out_file:
               file(out_file, "w").write(result.save_to_string(save_parents=True))
       else:
@@ -246,7 +287,7 @@ def main():
       if result:
           for record in result:
               print "RESULT:"
-              record.dump()
+              record.dump(dump_parents=dump_parents)
       else:
           print "NO RESULT"
 
@@ -257,7 +298,7 @@ def main():
        gid = client.create_gid(cred, hrn, uuid, pkey_string)
        if gid:
            print "RESULT:"
-           gid.dump()
+           gid.dump(dump_parents=dump_parents)
            if out_file:
                file(out_file,"w").write(gid.save_to_string(save_parents=True))
        else:
@@ -269,6 +310,15 @@ def main():
            if not email:
                print "ERROR: must specify --email <addr> when registering users"
            geni_info['email'] = email
+
+       if type == "node":
+           if not ip:
+               print "ERROR: must specify --ip <addr> when registering nodes"
+           geni_info['ip'] = ip
+           if not dns:
+               print "ERROR: must specify --dns <addr> when registering nodes"
+           geni_info['dns'] = dns
+
        gid = GID(filename=gid_fn)
        record = GeniRecord(name=hrn, gid=gid, type=type, pointer=-1)
        record.set_geni_info(geni_info)
@@ -291,6 +341,64 @@ def main():
        for record in matching_records:
            client.remove(cred,record)
 
+   elif (opname == "update"):
+       record_list = client.resolve(cred, hrn)
+       if not record_list:
+           print "no records match hrn"
+
+       matching_records = []
+       for record in record_list:
+           if record.get_type() == type:
+               matching_records.append(record)
+
+       if not matching_records:
+           print "records match hrn, but no records match type"
+
+       for record in matching_records:
+           geni_info = record.get_geni_info()
+
+           if email:
+               geni_info['email'] = email
+           if ip:
+               geni_info['ip'] = ip
+           if dns:
+               geni_info['dns'] = dns
+
+           client.update(cred, record)
+
+   elif (opname == "stopSlice"):
+       client.stop_slice(cred)
+
+   elif (opname == "startSlice"):
+       client.start_slice(cred)
+
+   elif (opname == "resetSlice"):
+       client.reset_slice(cred)
+
+   elif (opname == "deleteSlice"):
+       client.delete_slice(cred)
+
+   elif (opname == "listSlices"):
+       result = client.list_slices(cred)
+       print "RESULT:"
+       print "\n".join(result)
+       if out_file:
+           file(out_file,"w").write("\n".join(result))
+
+   elif (opname == "getTicket"):
+      result = client.get_ticket(cred, hrn, {})
+      if result:
+          print "RESULT:"
+          result.dump(dump_parents=dump_parents)
+          if out_file:
+              file(out_file,"w").write(result.save_to_string(save_parents=True))
+      else:
+          print "NO RESULT"
+
+   elif (opname == "redeemTicket"):
+       ticket = Ticket(filename = ticket_file)
+       result = client.redeem_ticket(ticket)
+
    else:
       print "unknown operation: " + opname