5 # Import PLC records into the Geni database. It is indended that this tool be
6 # run once to create Geni records that reflect the current state of the
9 # The import tool assumes that the existing PLC hierarchy should all be part
10 # of "planetlab.us" (see the root_auth and level1_auth variables below).
12 # Public keys are extracted from the users' SSH keys automatically and used to
13 # create GIDs. This is relatively experimental as a custom tool had to be
14 # written to perform conversion from SSH to OpenSSL format. It only supports
15 # RSA keys at this time, not DSA keys.
22 from geni.util.cert import *
23 from geni.util.trustedroot import *
24 from geni.util.hierarchy import *
25 from geni.util.record import *
26 from geni.util.genitable import *
27 from geni.util.misc import *
28 from geni.util.config import *
30 # get PL account settings from config module
31 pl_auth = get_pl_auth()
33 # connect to planetlab
35 from geni.util import remoteshell
36 shell = remoteshell.RemoteShell()
39 shell = PLC.Shell.Shell(globals = globals())
42 # Two authorities are specified: the root authority and the level1 authority.
47 #root_auth = "planetlab"
48 #level1_auth = "planetlab.us"
51 root_auth = config.GENI_REGISTRY_ROOT_AUTH
52 level1_auth = config.GENI_REGISTRY_LEVEL1_AUTH
53 if not level1_auth or level1_auth in ['']:
57 if isinstance(str, unicode):
58 return str.encode("ascii", "ignore")
62 def cleanup_string(str):
63 # pgsql has a fit with strings that have high ascii in them, so filter it
64 # out when generating the hrns.
72 str = str.replace(" ", "_")
73 str = str.replace(".", "_")
74 str = str.replace("(", "_")
75 str = str.replace("'", "_")
76 str = str.replace(")", "_")
77 str = str.replace('"', "_")
80 def process_options():
83 (options, args) = getopt.getopt(sys.argv[1:], '', [])
91 # get PL account settings from config module
92 pl_auth = get_pl_auth()
94 # connect to planetlab
96 from geni.util import remoteshell
97 shell = remoteshell.RemoteShell()
100 shell = PLC.Shell.Shell(globals = globals())
104 def get_auth_table(auth_name):
105 AuthHierarchy = Hierarchy()
106 auth_info = AuthHierarchy.get_auth_info(auth_name)
108 table = GeniTable(hrn=auth_name,
109 cninfo=auth_info.get_dbinfo())
111 # if the table doesn't exist, then it means we haven't put any records
112 # into this authority yet.
114 if not table.exists():
115 report.trace("Import: creating table for authority " + auth_name)
120 def person_to_hrn(parent_hrn, person):
121 # the old way - Lastname_Firstname
122 #personname = person['last_name'] + "_" + person['first_name']
124 # the new way - use email address up to the "@"
125 personname = person['email'].split("@")[0]
127 personname = cleanup_string(personname)
129 hrn = parent_hrn + "." + personname
132 def import_person(parent_hrn, person):
133 AuthHierarchy = Hierarchy()
134 hrn = person_to_hrn(parent_hrn, person)
136 # ASN.1 will have problems with hrn's longer than 64 characters
140 report.trace("Import: importing person " + hrn)
142 table = get_auth_table(parent_hrn)
144 person_record = table.resolve("user", hrn)
145 if not person_record:
147 if 'key_ids' in person:
148 key_ids = person["key_ids"]
151 # get the user's private key from the SSH keys they have uploaded
153 keys = shell.GetKeys(pl_auth, key_ids)
155 pkey =convert_public_key(key)
157 # the user has no keys
158 report.trace(" person " + hrn + " does not have a PL public key")
161 # if a key is unavailable, then we still need to put something in the
162 # user's GID. So make one up.
164 pkey = Keypair(create=True)
166 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
167 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
168 report.trace(" inserting user record for " + hrn)
169 table.insert(person_record)
171 key_ids = person["key_ids"]
174 pkey = get_pl_pubkey(key_ids[0])
175 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
176 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
177 report.trace(" updating user record for " + hrn)
178 table.update(person_record)
180 def import_slice(parent_hrn, slice):
181 AuthHierarchy = Hierarchy()
182 slicename = slice['name'].split("_",1)[-1]
183 slicename = cleanup_string(slicename)
186 report.error("Import_Slice: failed to parse slice name " + slice['name'])
189 hrn = parent_hrn + "." + slicename
190 report.trace("Import: importing slice " + hrn)
192 table = get_auth_table(parent_hrn)
194 slice_record = table.resolve("slice", hrn)
196 pkey = Keypair(create=True)
197 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
198 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
199 report.trace(" inserting slice record for " + hrn)
200 table.insert(slice_record)
202 def import_node(parent_hrn, node):
203 AuthHierarchy = Hierarchy()
204 nodename = node['hostname'].split(".")[0]
205 nodename = cleanup_string(nodename)
208 report.error("Import_node: failed to parse node name " + node['hostname'])
211 hrn = parent_hrn + "." + nodename
213 # ASN.1 will have problems with hrn's longer than 64 characters
217 report.trace("Import: importing node " + hrn)
219 table = get_auth_table(parent_hrn)
221 node_record = table.resolve("node", hrn)
223 pkey = Keypair(create=True)
224 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
225 node_record = GeniRecord(name=hrn, gid=node_gid, type="node", pointer=node['node_id'])
226 report.trace(" inserting node record for " + hrn)
227 table.insert(node_record)
229 def import_site(parent_hrn, site):
230 AuthHierarchy = Hierarchy()
231 sitename = site['login_base']
232 sitename = cleanup_string(sitename)
234 hrn = parent_hrn + "." + sitename
236 # Hardcode 'internet2' into the hrn for sites hosting
237 # internet2 nodes. This is a special operation for some vini
239 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
240 if sitename.startswith("ii"):
241 sitename = sitename.replace("ii", "")
242 hrn = ".".join([parent_hrn, "internet2", sitename])
243 elif sitename.startswith("nlr"):
244 hrn = ".".join([parent_hrn, "internet2", sitename])
245 sitename = sitename.replace("nlr", "")
247 report.trace("Import_Site: importing site " + hrn)
249 # create the authority
250 if not AuthHierarchy.auth_exists(hrn):
251 AuthHierarchy.create_auth(hrn)
253 auth_info = AuthHierarchy.get_auth_info(hrn)
255 table = get_auth_table(parent_hrn)
257 auth_record = table.resolve("authority", hrn)
259 auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
260 report.trace(" inserting authority record for " + hrn)
261 table.insert(auth_record)
263 if 'person_ids' in site:
264 for person_id in site['person_ids']:
265 persons = shell.GetPersons(pl_auth, [person_id])
268 import_person(hrn, persons[0])
270 report.trace("Failed to import: %s" % persons[0])
271 if 'slice_ids' in site:
272 for slice_id in site['slice_ids']:
273 slices = shell.GetSlices(pl_auth, [slice_id])
276 import_slice(hrn, slices[0])
278 report.trace("Failed to import: %s" % slices[0])
279 if 'node_ids' in site:
280 for node_id in site['node_ids']:
281 nodes = shell.GetNodes(pl_auth, [node_id])
284 import_node(hrn, nodes[0])
286 report.trace("Failed to import: %s" % nodes[0])
288 def create_top_level_auth_records(hrn):
289 parent_hrn = get_authority(hrn)
290 print hrn, ":", parent_hrn
293 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
294 table = get_auth_table(parent_hrn)
296 auth_record = table.resolve("authority", hrn)
298 auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
299 report.trace(" inserting authority record for " + hrn)
300 table.insert(auth_record)
308 AuthHierarchy = Hierarchy()
309 TrustedRoots = TrustedRootList()
311 print "Import: creating top level authorities"
313 if not AuthHierarchy.auth_exists(root_auth):
314 AuthHierarchy.create_auth(root_auth)
316 create_top_level_auth_records(root_auth)
318 if not AuthHierarchy.auth_exists(level1_auth):
319 AuthHierarchy.create_auth(level1_auth)
320 create_top_level_auth_records(level1_auth)
321 import_auth = level1_auth
323 import_auth = root_auth
325 print "Import: adding", root_auth, "to trusted list"
326 root = AuthHierarchy.get_auth_info(root_auth)
327 TrustedRoots.add_gid(root.get_gid_object())
331 sites = shell.GetSites(pl_auth, {'peer_id': None})
332 # create a fake internet2 site first
333 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
334 'login_base': 'internet2', 'site_id': -1}
335 import_site(import_auth, i2site)
338 import_site(import_auth, site)
340 if __name__ == "__main__":