2 from __future__ import with_statement
4 # sfi -- slice-based facility interface
10 from geni.util.cert import Keypair, Certificate
11 from geni.util.credential import Credential
12 from geni.util.geniclient import GeniClient
13 from geni.util.record import GeniRecord
14 from geni.util.gid import GID
15 from geni.util.gid import create_uuid
30 long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "pubkeyfile=", "type=", "addresearcher=", "delresearcher=", "dump", "extractgid="]
33 print "syntax: editRecord.py <options>"
34 print " --help ... show help"
35 print " --infile <name> ... read record from file"
36 print " --outfile <name> ... write record to file"
37 print " --dump ... dump record to stdout"
38 print " --extractgid <fn> ... extract GID to filename"
39 print " --gidfile <fn> ... load gid from file"
40 print " --pubkeyfile <name> ... key to use when creating gid"
41 print " --hrn <name> ... set hrn"
42 print " --type <type> ... set type (user|slice|sa|ma|...)"
43 print " --email <addr> ... user: set email address"
44 print " --ip <addr> ... node: set ip address"
45 print " --dns <hostname> ... node: set hostname"
46 print " --addresearcher <hrn> ... slice: add researcher"
47 print " --delresearcher <hrn> ... slice: delete researcher"
49 def load_publickey_string(fn):
53 # if the filename is a private key file, then extract the public key
54 if "PRIVATE KEY" in key_string:
55 outfn = tempfile.mktemp()
56 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
64 def process_options():
65 global infile, outfile
66 global email, ip, dns, gidfile, hrn, type
68 global dump, extractgid
71 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
79 elif name == "--infile":
81 elif name == "--outfile":
83 elif name == "--email":
89 elif name == "--gidfile":
91 elif name == "--pubkeyfile":
95 elif name == "--type":
97 elif name == "--dump":
99 elif name == "--extractgid":
101 elif name == "--addresearcher":
102 researcher.append(val)
103 elif name == "--delresearcher":
104 researcher.append("-" + val)
106 def errorcheck(record):
107 geni_info = record.get_geni_info()
110 print "Warning: no type specified"
111 if not record.type in ["user", "sa", "ma", "slice", "node"]:
112 print "Warning: unknown record type"
114 print "Warning: unknown record name"
115 if (not record.gid) and (not ("create_gid" in geni_info)):
116 print "Warning: unknown record gid"
117 print " use --hrn and --pubkeyfile to cause a gid to be created"
119 if record.type == "user":
120 if geni_info.get("email", None) == None:
121 print "Warning: unknown email in user record"
123 if record.type == "node":
124 if geni_info.get("ip",None) == None:
125 print "Warning: unknown ip in node record"
126 if geni_info.get("dns",None) == None:
127 print "Warning: unknown dns in node record"
129 # updates is a list of items to add or remove. If an item starts with "-", then
130 # it will be removed. Otherwise it will be added
131 def update_list(dict, listname, updates):
132 list = dict.get(listname, [])
134 if hrn.startswith("-"):
137 list.remove(real_hrn)
139 print "Error:", real_hrn, "is not in researcher list:", list
144 dict[listname] = list
149 # if the user didn't tell us to do much of anything, then maybe he needs
151 if (not infile) and (not outfile) and (not dump) and (extractgid==None):
156 str = file(infile, "r").read()
157 record = GeniRecord(string = str)
159 record = GeniRecord()
161 geni_info = record.get_geni_info()
162 geni_info_orig = geni_info.copy()
165 geni_info["email"] = email
171 geni_info["dns"] = dns
180 gid_str = file(gidfile, "r").read()
181 gid = GID(string=gid_str)
186 print "You should not use --gidfile and --pubkeyfile together"
190 print "You must specify --hrn when you specify --pubkeyfile"
193 geni_info["create_gid"] = True
194 geni_info["create_gid_hrn"] = record.name
195 geni_info["create_gid_key"] = load_publickey_string(pubkeyfile)
198 update_list(geni_info, "researcher", researcher)
200 if (geni_info != geni_info_orig):
201 record.set_geni_info(geni_info)
209 record.get_gid_object().save_to_file(extractgid, save_parents=True)
210 print "write GID to", extractgid
213 str = record.save_to_string()
214 file(outfile, "w").write(str)
215 print "wrote record to", outfile
217 if __name__=="__main__":