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)
145 if 'key_ids' in person:
146 key_ids = person["key_ids"]
148 # get the user's private key from the SSH keys they have uploaded
150 keys = shell.GetKeys(pl_auth, key_ids)
152 pkey =convert_public_key(key)
154 # the user has no keys
155 report.trace(" person " + hrn + " does not have a PL public key")
157 # if a key is unavailable, then we still need to put something in the
158 # user's GID. So make one up.
159 pkey = Keypair(create=True)
162 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
163 person_record = table.resolve("user", hrn)
164 if not person_record:
165 report.trace(" inserting user record for " + hrn)
166 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
167 table.insert(person_record)
169 report.trace(" updating user record for " + hrn)
170 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
171 table.update(person_record)
173 def import_slice(parent_hrn, slice):
174 AuthHierarchy = Hierarchy()
175 slicename = slice['name'].split("_",1)[-1]
176 slicename = cleanup_string(slicename)
179 report.error("Import_Slice: failed to parse slice name " + slice['name'])
182 hrn = parent_hrn + "." + slicename
183 report.trace("Import: importing slice " + hrn)
185 table = get_auth_table(parent_hrn)
187 slice_record = table.resolve("slice", hrn)
189 pkey = Keypair(create=True)
190 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
191 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
192 report.trace(" inserting slice record for " + hrn)
193 table.insert(slice_record)
195 def import_node(parent_hrn, node):
196 AuthHierarchy = Hierarchy()
197 nodename = node['hostname'].split(".")[0]
198 nodename = cleanup_string(nodename)
201 report.error("Import_node: failed to parse node name " + node['hostname'])
204 hrn = parent_hrn + "." + nodename
206 # ASN.1 will have problems with hrn's longer than 64 characters
210 report.trace("Import: importing node " + hrn)
212 table = get_auth_table(parent_hrn)
214 node_record = table.resolve("node", hrn)
216 pkey = Keypair(create=True)
217 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
218 node_record = GeniRecord(name=hrn, gid=node_gid, type="node", pointer=node['node_id'])
219 report.trace(" inserting node record for " + hrn)
220 table.insert(node_record)
222 def import_site(parent_hrn, site):
223 AuthHierarchy = Hierarchy()
224 sitename = site['login_base']
225 sitename = cleanup_string(sitename)
227 hrn = parent_hrn + "." + sitename
229 # Hardcode 'internet2' into the hrn for sites hosting
230 # internet2 nodes. This is a special operation for some vini
232 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
233 if sitename.startswith("ii"):
234 sitename = sitename.replace("ii", "")
235 hrn = ".".join([parent_hrn, "internet2", sitename])
236 elif sitename.startswith("nlr"):
237 hrn = ".".join([parent_hrn, "internet2", sitename])
238 sitename = sitename.replace("nlr", "")
240 report.trace("Import_Site: importing site " + hrn)
242 # create the authority
243 if not AuthHierarchy.auth_exists(hrn):
244 AuthHierarchy.create_auth(hrn)
246 auth_info = AuthHierarchy.get_auth_info(hrn)
248 table = get_auth_table(parent_hrn)
250 auth_record = table.resolve("authority", hrn)
252 auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
253 report.trace(" inserting authority record for " + hrn)
254 table.insert(auth_record)
256 if 'person_ids' in site:
257 for person_id in site['person_ids']:
258 persons = shell.GetPersons(pl_auth, [person_id])
261 import_person(hrn, persons[0])
263 report.trace("Failed to import: %s" % persons[0])
264 if 'slice_ids' in site:
265 for slice_id in site['slice_ids']:
266 slices = shell.GetSlices(pl_auth, [slice_id])
269 import_slice(hrn, slices[0])
271 report.trace("Failed to import: %s" % slices[0])
272 if 'node_ids' in site:
273 for node_id in site['node_ids']:
274 nodes = shell.GetNodes(pl_auth, [node_id])
277 import_node(hrn, nodes[0])
279 report.trace("Failed to import: %s" % nodes[0])
281 def create_top_level_auth_records(hrn):
282 parent_hrn = get_authority(hrn)
283 print hrn, ":", parent_hrn
286 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
287 table = get_auth_table(parent_hrn)
289 auth_record = table.resolve("authority", hrn)
291 auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
292 report.trace(" inserting authority record for " + hrn)
293 table.insert(auth_record)
301 AuthHierarchy = Hierarchy()
302 TrustedRoots = TrustedRootList()
304 print "Import: creating top level authorities"
306 if not AuthHierarchy.auth_exists(root_auth):
307 AuthHierarchy.create_auth(root_auth)
309 create_top_level_auth_records(root_auth)
311 if not AuthHierarchy.auth_exists(level1_auth):
312 AuthHierarchy.create_auth(level1_auth)
313 create_top_level_auth_records(level1_auth)
314 import_auth = level1_auth
316 import_auth = root_auth
318 print "Import: adding", root_auth, "to trusted list"
319 root = AuthHierarchy.get_auth_info(root_auth)
320 TrustedRoots.add_gid(root.get_gid_object())
324 sites = shell.GetSites(pl_auth, {'peer_id': None})
325 # create a fake internet2 site first
326 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
327 'login_base': 'internet2', 'site_id': -1}
328 import_site(import_auth, i2site)
331 import_site(import_auth, site)
333 if __name__ == "__main__":