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
29 long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "pubkeyfile=", "type=", "addresearcher=", "delresearcher=", "dump"]
32 print "syntax: editRecord.py <options>"
33 print " --help ... show help"
34 print " --infile <name> ... read record from file"
35 print " --outfile <name> ... write record to file"
36 print " --dump ... dump record to stdout"
37 print " --gidfile <fn> ... load gid from file"
38 print " --pubkeyfile <name> ... key to use when creating gid"
39 print " --hrn <name> ... set hrn"
40 print " --type <type> ... set type (user|slice|sa|ma|...)"
41 print " --email <addr> ... user: set email address"
42 print " --ip <addr> ... node: set ip address"
43 print " --dns <hostname> ... node: set hostname"
44 print " --addresearcher <hrn> ... slice: add researcher"
45 print " --delresearcher <hrn> ... slice: delete researcher"
47 def load_publickey_string(fn):
51 # if the filename is a private key file, then extract the public key
52 if "PRIVATE KEY" in key_string:
53 outfn = tempfile.mktemp()
54 cmd = "openssl rsa -in " + fn + " -pubout -outform PEM -out " + outfn
62 def process_options():
63 global infile, outfile
64 global email, ip, dns, gidfile, hrn, type
69 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
77 elif name == "--infile":
79 elif name == "--outfile":
81 elif name == "--email":
87 elif name == "--gidfile":
89 elif name == "--pubkeyfile":
93 elif name == "--type":
95 elif name == "--dump":
97 elif name == "--addresearcher":
98 researcher.append(val)
99 elif name == "--delresearcher":
100 researcher.append("-" + val)
102 def errorcheck(record):
103 geni_info = record.get_geni_info()
106 print "Warning: no type specified"
107 if not record.type in ["user", "sa", "ma", "slice", "node"]:
108 print "Warning: unknown record type"
110 print "Warning: unknown record name"
111 if (not record.gid) and (not ("create_gid" in geni_info)):
112 print "Warning: unknown record gid"
113 print " use --hrn and --pubkeyfile to cause a gid to be created"
115 if record.type == "user":
116 if not geni_info.get("email",None):
117 print "Warning: unknown email in user record"
119 if record.type == "node":
120 if not geni_info.get("ip",None):
121 print "Warning: unknown ip in node record"
122 if not geni_info.get("dns",None):
123 print "Warning: unknown dns in node record"
125 # updates is a list of items to add or remove. If an item starts with "-", then
126 # it will be removed. Otherwise it will be added
127 def update_list(dict, listname, updates):
128 list = dict.get(listname, [])
130 if hrn.startswith("-"):
133 list.remove(real_hrn)
135 print "Error:", real_hrn, "is not in researcher list:", list
140 dict[listname] = list
145 # if the user didn't tell us to do much of anything, then maybe he needs
147 if (not infile) and (not outfile) and (not dump):
152 str = file(infile, "r").read()
153 record = GeniRecord(string = str)
155 record = GeniRecord()
157 geni_info = record.get_geni_info()
158 geni_info_orig = geni_info.copy()
161 geni_info["email"] = email
167 geni_info["dns"] = dns
176 gid_str = file(gidfile, "r").read()
177 gid = GID(string=gid_str)
182 print "You should not use --gidfile and --pubkeyfile together"
186 print "You must specify --hrn when you specify --pubkeyfile"
189 geni_info = record.get_geni_info()
190 geni_info["create_gid"] = True
191 geni_info["create_gid_hrn"] = record.name
192 geni_info["create_gid_key"] = load_publickey_string(pubkeyfile)
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__":