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=", "gidhrn=", "gidkeyfile=", "hrn=", "type=", "addresearcher=", "delresearcher=", "dump"]
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 " --gidfile <fn> ... load gid from file"
39 print " --gidhrn <name> ... name to use when creating gid"
40 print " --gidkeyfile <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
69 global gidkeyfile, gidhrn
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 == "--gidhrn":
93 elif name == "--gidkeyfile":
97 elif name == "--type":
99 elif name == "--dump":
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"
118 if record.type == "user":
119 if not geni_info.get("email",None):
120 print "Warning: unknown email in user record"
122 if record.type == "node":
123 if not geni_info.get("ip",None):
124 print "Warning: unknown ip in node record"
125 if not geni_info.get("dns",None):
126 print "Warning: unknown dns in node record"
128 # updates is a list of items to add or remove. If an item starts with "-", then
129 # it will be removed. Otherwise it will be added
130 def update_list(dict, listname, updates):
131 list = dict.get(listname, [])
133 if hrn.startswith("-"):
136 list.delete(real_hrn)
141 dict[listname] = list
146 # if the user didn't tell us to do much of anything, then maybe he needs
148 if (not infile) and (not outfile) and (not dump):
153 str = file(infile, "r").read()
154 record = GeniRecord(string = str)
156 record = GeniRecord()
158 geni_info = record.get_geni_info()
159 geni_info_orig = geni_info.copy()
162 geni_info["email"] = email
168 geni_info["dns"] = dns
177 gid_str = file(gidfile, "r").read()
178 gid = GID(string=gid_str)
181 if gidhrn or gidkeyfile:
183 print "must use --gidkeyfile with --gidhrn"
186 print "must use --gidhrn with --gidkeyfile"
189 geni_info = record.get_geni_info()
190 geni_info["create_gid"] = True
191 geni_info["create_gid_hrn"] = gidhrn
192 geni_info["create_gid_key"] = load_publickey_string(gidkeyfile)
195 update_list(geni_info, "researcher", researcher)
197 if (geni_info != geni_info_orig):
198 record.set_geni_info(geni_info)
206 str = record.save_to_string()
207 file(outfile, "w").write(str)
208 print "wrote record to", outfile
210 if __name__=="__main__":