add --extractgid option
[sfa.git] / cmdline / editRecord.py
index 74d5d58..be18c31 100644 (file)
@@ -6,12 +6,15 @@ from __future__ import with_statement
 import sys
 import os, os.path
 import getopt
-from util.cert import Keypair, Certificate
-from util.credential import Credential
-from util.geniclient import GeniClient
-from util.record import GeniRecord
-from util.gid import GID
-
+import tempfile
+from geni.util.cert import Keypair, Certificate
+from geni.util.credential import Credential
+from geni.util.geniclient import GeniClient
+from geni.util.record import GeniRecord
+from geni.util.gid import GID
+from geni.util.gid import create_uuid
+
+pubkeyfile = None
 infile = None
 outfile = None
 gidfile = None
@@ -21,9 +24,10 @@ dns = None
 hrn = None
 type = None
 dump = False
+extractgid = None
 researcher = []
 
-long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "type=", "addresearcher=", "delresearcher=", "dump"]
+long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "pubkeyfile=", "type=", "addresearcher=", "delresearcher=", "dump", "extractgid="]
 
 def showhelp():
    print "syntax: editRecord.py <options>"
@@ -31,7 +35,9 @@ def showhelp():
    print "    --infile <name>       ... read record from file"
    print "    --outfile <name>      ... write record to file"
    print "    --dump                ... dump record to stdout"
+   print "    --extractgid <fn>     ... extract GID to filename"
    print "    --gidfile <fn>        ... load gid from file"
+   print "    --pubkeyfile <name>   ... key to use when creating gid"
    print "    --hrn <name>          ... set hrn"
    print "    --type <type>         ... set type (user|slice|sa|ma|...)"
    print "    --email <addr>        ... user: set email address"
@@ -40,11 +46,27 @@ def showhelp():
    print "    --addresearcher <hrn> ... slice: add researcher"
    print "    --delresearcher <hrn> ... slice: delete researcher"
 
+def load_publickey_string(fn):
+   f = file(fn,"r")
+   key_string = f.read()
+
+   # if the filename is a private key file, then extract the public key
+   if "PRIVATE KEY" in key_string:
+       outfn = tempfile.mktemp()
+       cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
+       os.system(cmd)
+       f = file(outfn, "r")
+       key_string = f.read()
+       os.remove(outfn)
+
+   return key_string
+
 def process_options():
    global infile, outfile
    global email, ip, dns, gidfile, hrn, type
    global researcher
-   global dump
+   global dump, extractgid
+   global pubkeyfile
 
    (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
    for opt in options:
@@ -66,12 +88,16 @@ def process_options():
            dns = val
        elif name == "--gidfile":
            gidfile = val
+       elif name == "--pubkeyfile":
+           pubkeyfile = val
        elif name == "--hrn":
            hrn = val
        elif name == "--type":
            type = val
        elif name == "--dump":
            dump = True
+       elif name == "--extractgid":
+           extractgid = val
        elif name == "--addresearcher":
            researcher.append(val)
        elif name == "--delresearcher":
@@ -86,17 +112,18 @@ def errorcheck(record):
        print "Warning: unknown record type"
    if not record.name:
        print "Warning: unknown record name"
-   if not record.gid:
+   if (not record.gid) and (not ("create_gid" in geni_info)):
        print "Warning: unknown record gid"
+       print "   use --hrn and --pubkeyfile to cause a gid to be created"
 
    if record.type == "user":
-       if not geni_info.get("email",None):
+       if geni_info.get("email", None) == None:
            print "Warning: unknown email in user record"
 
    if record.type == "node":
-       if not geni_info.get("ip",None):
+       if geni_info.get("ip",None) == None:
            print "Warning: unknown ip in node record"
-       if not geni_info.get("dns",None):
+       if geni_info.get("dns",None) == None:
            print "Warning: unknown dns in node record"
 
 # updates is a list of items to add or remove. If an item starts with "-", then
@@ -107,7 +134,9 @@ def update_list(dict, listname, updates):
        if hrn.startswith("-"):
            real_hrn = hrn[1:]
            if real_hrn in list:
-               list.delete(real_hrn)
+               list.remove(real_hrn)
+           else:
+               print "Error:", real_hrn, "is not in researcher list:", list 
        else:
            if not hrn in list:
                list.append(hrn)
@@ -119,7 +148,7 @@ def main():
 
    # if the user didn't tell us to do much of anything, then maybe he needs
    # some help
-   if (not infile) and (not outfile) and (not dump):
+   if (not infile) and (not outfile) and (not dump) and (extractgid==None):
        showhelp()
        return
 
@@ -152,6 +181,19 @@ def main():
        gid = GID(string=gid_str)
        record.set_gid(gid)
 
+   if pubkeyfile:
+       if gidfile:
+           print "You should not use --gidfile and --pubkeyfile together"
+           sys.exit(-1)
+
+       if not record.name:
+           print "You must specify --hrn when you specify --pubkeyfile"
+           sys.exit(-1)
+
+       geni_info["create_gid"] = True
+       geni_info["create_gid_hrn"] = record.name
+       geni_info["create_gid_key"] = load_publickey_string(pubkeyfile)
+
    if researcher:
        update_list(geni_info, "researcher", researcher)
 
@@ -163,6 +205,10 @@ def main():
    if dump:
        record.dump(False)
 
+   if extractgid:
+       record.get_gid_object().save_to_file(extractgid, save_parents=True)
+       print "write GID to", extractgid
+
    if outfile:
        str = record.save_to_string()
        file(outfile, "w").write(str)