GENI_REGISTRY_ENABLE should be GENI_REGISTRY_ENABLED
[sfa.git] / cmdline / genicli.py
index 610f9ab..b4aff04 100644 (file)
@@ -3,10 +3,13 @@
 import getopt
 import sys
 import os
-from cert import *
-from geniclient import *
+from geni.util.cert import *
+from geni.util.geniclient import *
+from geni.util.geniticket import *
 
-long_opts = ["keyfile=", "help", "outfile=", "credfile=", "username=", "email="]
+long_opts = ["keyfile=", "help", "outfile=", "credfile=", "ticketfile=",
+             "username=", "email=", "ip=", "dns=", "dump_parents", "server=",
+             "filter=", "short"]
 
 # default command line options
 username = "client"
@@ -18,11 +21,20 @@ key_file = None
 cred_file = None
 cert_file = None
 out_file = None
+ticket_file = None
 
+short = False
+ip = None
+dns = None
 email = None
 uuid = None
 gid_pkey_fn = None
 gid_fn = None
+filter = None
+
+dump_fn = None
+
+dump_parents = False
 
 leaf_name = None
 server_url = "https://localhost:12345/"
@@ -35,27 +47,45 @@ 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 "    --filter <type>  ... filter the results of a list operation (user | slice | node ...)"
+   print "    --short          ... list records in short format (name only)"
    print "commands:"
    print "    resolve <hrn>"
-   print "    dumpCredential"
+   print "    dumpCredential <filename>"
+   print "    dumpGid <filename>"
    print "    getCredential <type> <hrn>"
+   print "    list <hrn>"
    print "    start <hrn>"
    print "    createKey <filename>"
    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
+   global filter
+   global short
+   global dump_fn
 
    (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
    for opt in options:
@@ -75,8 +105,22 @@ 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
+       elif name == "--filter":
+           filter = val
+       elif name == "--short":
+           short = True
 
    if not args:
        print "no operation specified"
@@ -97,6 +141,13 @@ def process_options():
        type = args[1]
        hrn = args[2]
 
+   elif opname == "list":
+       if len(args) < 2:
+           print "syntax: list <hrn>"
+           sys.exit(-1)
+       hrn = args[1]
+
+
    elif opname == "createGid":
        if len(args) < 4:
            print "syntax: createGid <hrn> <uuid|None> <pubkey_fn>"
@@ -120,6 +171,24 @@ 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]
+
+   elif opname == "dumpGid":
+       if len(args) < 2:
+           print "syntax: dumpGid <filename>"
+           sys.exit(-1)
+       dump_fn = args[1]
+
    leaf_name = get_leaf(username)
 
    if cert_file == None:
@@ -132,6 +201,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
@@ -148,6 +218,10 @@ def get_authority(x):
 def dumpCredential():
    pass
 
+def dumpGid():
+   gid = GID(filename = dump_fn)
+   gid.dump()
+
 # creates a self-signed certificate and private key
 def createKey():
    k = Keypair(create=True)
@@ -193,7 +267,7 @@ def main():
 
    # if the operation is not a local operation, then create a geniclient to
    # talk to the server
-   if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey"):
+   if (opname != "dumpCredential") and (opname != "help") and (opname != "createKey") and (opname != "dumpGid"):
        if not os.path.exists(key_file):
            print "key file", key_file, "does not exist"
            sys.exit(-1)
@@ -208,7 +282,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") or (opname == "dumpCredential") or (opname == "dumpGid"):
       cred = None
    else:
       cred = Credential(filename = cred_file)
@@ -216,6 +291,9 @@ def main():
    if opname == "dumpCredential":
       dumpCredential()
 
+   elif opname == "dumpGid":
+      dumpGid()
+
    elif opname == "help":
       showhelp()
 
@@ -227,7 +305,7 @@ def main():
       if result:
           for record in result:
               print "RESULT:"
-              record.dump()
+              record.dump(dump_parents=dump_parents)
       else:
           print "NO RESULT"
 
@@ -235,18 +313,23 @@ 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:
           print "NO RESULT"
 
    elif (opname == "list"):
-      result = client.list(cred)
+      result = client.list(cred, hrn)
       if result:
+          if filter:
+              result = [r for r in result if r.type==filter]
+          print "RESULT:"
           for record in result:
-              print "RESULT:"
-              record.dump()
+              if short:
+                  print "  ", record.get_name()
+              else:
+                  record.dump(dump_parents=dump_parents)
       else:
           print "NO RESULT"
 
@@ -257,7 +340,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 +352,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)
@@ -276,6 +368,9 @@ def main():
        result = client.register(cred, record)
 
    elif (opname == "remove"):
+       client.remove(cred, type, hrn)
+
+   elif (opname == "update"):
        record_list = client.resolve(cred, hrn)
        if not record_list:
            print "no records match hrn"
@@ -289,7 +384,49 @@ def main():
            print "records match hrn, but no records match type"
 
        for record in matching_records:
-           client.remove(cred,record)
+           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