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.delete(real_hrn)
138 dict[listname] = list
143 # if the user didn't tell us to do much of anything, then maybe he needs
145 if (not infile) and (not outfile) and (not dump):
150 str = file(infile, "r").read()
151 record = GeniRecord(string = str)
153 record = GeniRecord()
155 geni_info = record.get_geni_info()
156 geni_info_orig = geni_info.copy()
159 geni_info["email"] = email
165 geni_info["dns"] = dns
174 gid_str = file(gidfile, "r").read()
175 gid = GID(string=gid_str)
180 print "You should not use --gidfile and --pubkeyfile together"
184 print "You must specify --hrn when you specify --pubkeyfile"
187 geni_info = record.get_geni_info()
188 geni_info["create_gid"] = True
189 geni_info["create_gid_hrn"] = record.name
190 geni_info["create_gid_key"] = load_publickey_string(pubkeyfile)
193 update_list(geni_info, "researcher", researcher)
195 if (geni_info != geni_info_orig):
196 record.set_geni_info(geni_info)
204 str = record.save_to_string()
205 file(outfile, "w").write(str)
206 print "wrote record to", outfile
208 if __name__=="__main__":