2 # Import PLC records into the Geni database. It is indended that this tool be
3 # run once to create Geni records that reflect the current state of the
6 # The import tool assumes that the existing PLC hierarchy should all be part
7 # of "planetlab.us" (see the root_auth and level1_auth variables below).
9 # Public keys are extracted from the users' SSH keys automatically and used to
10 # create GIDs. This is relatively experimental as a custom tool had to be
11 # written to perform conversion from SSH to OpenSSL format. It only supports
12 # RSA keys at this time, not DSA keys.
20 from trustedroot import *
21 from hierarchy import *
23 from genitable import *
29 # Two authorities are specified: the root authority and the level1 authority.
31 root_auth = "planetlab"
32 level1_auth = "planetlab.us"
34 def process_options():
37 (options, args) = getopt.getopt(sys.argv[1:], '', [])
45 # get PL account settings from config module
46 pl_auth = get_pl_auth()
48 # connect to planetlab
51 shell = remoteshell.RemoteShell()
54 shell = PLC.Shell.Shell(globals = globals())
56 def get_auth_table(auth_name):
57 auth_info = AuthHierarchy.get_auth_info(auth_name)
59 table = GeniTable(hrn=auth_name,
60 cninfo=auth_info.get_dbinfo())
62 # if the table doesn't exist, then it means we haven't put any records
63 # into this authority yet.
65 if not table.exists():
66 report.trace("Import: creating table for authority " + auth_name)
71 def get_pl_pubkey(key_id):
72 keys = shell.GetKeys(pl_auth, [key_id])
74 key_str = keys[0]['key']
76 # generate temporary files to hold the keys
77 (ssh_f, ssh_fn) = tempfile.mkstemp()
78 ssl_fn = tempfile.mktemp()
80 os.write(ssh_f, key_str)
83 cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
87 # this check leaves the temporary file containing the public key so
88 # that it can be expected to see why it failed.
89 # TODO: for production, cleanup the temporary files
90 if not os.path.exists(ssl_fn):
91 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
95 k.load_pubkey_from_file(ssl_fn)
99 # remove the temporary files
107 def import_person(parent_hrn, person):
108 personname = person['last_name'] + "_" + person['first_name']
110 hrn = parent_hrn + "." + personname
111 report.trace("Import: importing person " + hrn)
113 table = get_auth_table(parent_hrn)
115 person_record = table.resolve("slice", hrn)
116 if not person_record:
117 #pkey = Keypair(create=True)
118 key_ids = person["key_ids"]
120 pkey = get_pl_pubkey(key_ids[0])
122 report.trace(" person " + hrn + " does not have a PL public key")
123 pkey = Keypair(create=True)
124 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
125 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
126 report.trace(" inserting user record for " + hrn)
127 table.insert(person_record)
129 def import_slice(parent_hrn, slice):
130 slicename = slice['name'].split("_",1)[-1]
133 report.error("Import_Slice: failed to parse slice name " + slice['name'])
136 hrn = parent_hrn + "." + slicename
137 report.trace("Import: importing slice " + hrn)
139 table = get_auth_table(parent_hrn)
141 slice_record = table.resolve("slice", hrn)
143 pkey = Keypair(create=True)
144 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
145 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
146 report.trace(" inserting slice record for " + hrn)
147 table.insert(slice_record)
149 def import_site(parent_hrn, site):
150 hrn = parent_hrn + "." + site['login_base']
152 report.trace("Import_Site: importing site " + hrn)
154 # create the authority
155 if not AuthHierarchy.auth_exists(hrn):
156 AuthHierarchy.create_auth(hrn)
158 auth_info = AuthHierarchy.get_auth_info(hrn)
160 table = get_auth_table(parent_hrn)
162 sa_record = table.resolve("sa", hrn)
164 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
165 report.trace(" inserting sa record for " + hrn)
166 table.insert(sa_record)
168 ma_record = table.resolve("ma", hrn)
170 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
171 report.trace(" inserting ma record for " + hrn)
172 table.insert(ma_record)
174 for person_id in site['person_ids']:
175 persons = shell.GetPersons(pl_auth, [person_id])
177 import_person(hrn, persons[0])
179 for slice_id in site['slice_ids']:
180 slices = shell.GetSlices(pl_auth, [slice_id])
182 import_slice(hrn, slices[0])
184 def create_top_level_auth_records(hrn):
185 parent_hrn = get_authority(hrn)
187 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
188 table = get_auth_table(parent_hrn)
190 sa_record = table.resolve("sa", hrn)
192 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
193 report.trace(" inserting sa record for " + hrn)
194 table.insert(sa_record)
196 ma_record = table.resolve("ma", hrn)
198 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
199 report.trace(" inserting ma record for " + hrn)
200 table.insert(ma_record)
208 AuthHierarchy = Hierarchy()
209 TrustedRoots = TrustedRootList()
211 print "Import: creating top level authorities"
213 if not AuthHierarchy.auth_exists(root_auth):
214 AuthHierarchy.create_auth(root_auth)
215 #create_top_level_auth_records(root_auth)
216 if not AuthHierarchy.auth_exists(level1_auth):
217 AuthHierarchy.create_auth(level1_auth)
218 create_top_level_auth_records(level1_auth)
220 print "Import: adding", root_auth, "to trusted list"
221 root = AuthHierarchy.get_auth_info(root_auth)
222 TrustedRoots.add_gid(root.get_gid_object())
226 sites = shell.GetSites(pl_auth)
228 import_site(level1_auth, site)
230 if __name__ == "__main__":