6 from trustedroot import *
7 from hierarchy import *
9 from genitable import *
14 root_auth = "planetlab"
15 level1_auth = "planetlab.us"
17 def process_options():
20 (options, args) = getopt.getopt(sys.argv[1:], '', [])
28 # get PL account settings from config module
29 pl_auth = get_pl_auth()
31 # connect to planetlab
34 shell = remoteshell.RemoteShell()
37 shell = PLC.Shell.Shell(globals = globals())
39 def get_auth_table(auth_name):
40 auth_info = AuthHierarchy.get_auth_info(auth_name)
42 table = GeniTable(hrn=auth_name,
43 cninfo=auth_info.get_dbinfo())
45 # if the table doesn't exist, then it means we haven't put any records
46 # into this authority yet.
48 if not table.exists():
49 report.trace("Import: creating table for authority " + auth_name)
54 def get_pl_pubkey(key_id):
55 keys = shell.GetKeys(pl_auth, [key_id])
57 key_str = keys[0]['key']
59 # generate temporary files to hold the keys
60 (ssh_f, ssh_fn) = tempfile.mkstemp()
61 ssl_fn = tempfile.mktemp()
63 os.write(ssh_f, key_str)
66 cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
70 # this check leaves the temporary file containing the public key so
71 # that it can be expected to see why it failed.
72 # TODO: for production, cleanup the temporary files
73 if not os.path.exists(ssl_fn):
74 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
78 k.load_pubkey_from_file(ssl_fn)
82 # remove the temporary files
90 def import_person(parent_hrn, person):
91 personname = person['last_name'] + "_" + person['first_name']
93 hrn = parent_hrn + "." + personname
94 report.trace("Import: importing person " + hrn)
96 table = get_auth_table(parent_hrn)
98 person_record = table.resolve("slice", hrn)
100 #pkey = Keypair(create=True)
101 key_ids = person["key_ids"]
103 pkey = get_pl_pubkey(key_ids[0])
105 report.trace(" person " + hrn + " does not have a PL public key")
106 pkey = Keypair(create=True)
107 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
108 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
109 report.trace(" inserting user record for " + hrn)
110 table.insert(person_record)
112 def import_slice(parent_hrn, slice):
113 slicename = slice['name'].split("_",1)[-1]
116 report.error("Import_Slice: failed to parse slice name " + slice['name'])
119 hrn = parent_hrn + "." + slicename
120 report.trace("Import: importing slice " + hrn)
122 table = get_auth_table(parent_hrn)
124 slice_record = table.resolve("slice", hrn)
126 pkey = Keypair(create=True)
127 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
128 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
129 report.trace(" inserting slice record for " + hrn)
130 table.insert(slice_record)
132 def import_site(parent_hrn, site):
133 hrn = parent_hrn + "." + site['login_base']
135 report.trace("Import_Site: importing site " + hrn)
137 # create the authority
138 if not AuthHierarchy.auth_exists(hrn):
139 AuthHierarchy.create_auth(hrn)
141 auth_info = AuthHierarchy.get_auth_info(hrn)
143 table = get_auth_table(parent_hrn)
145 sa_record = table.resolve("sa", hrn)
147 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
148 report.trace(" inserting sa record for " + hrn)
149 table.insert(sa_record)
151 ma_record = table.resolve("ma", hrn)
153 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
154 report.trace(" inserting ma record for " + hrn)
155 table.insert(ma_record)
157 for person_id in site['person_ids']:
158 persons = shell.GetPersons(pl_auth, [person_id])
160 import_person(hrn, persons[0])
162 for slice_id in site['slice_ids']:
163 slices = shell.GetSlices(pl_auth, [slice_id])
165 import_slice(hrn, slices[0])
167 def create_top_level_auth_records(hrn):
168 parent_hrn = get_authority(hrn)
170 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
171 table = get_auth_table(parent_hrn)
173 sa_record = table.resolve("sa", hrn)
175 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
176 report.trace(" inserting sa record for " + hrn)
177 table.insert(sa_record)
179 ma_record = table.resolve("ma", hrn)
181 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
182 report.trace(" inserting ma record for " + hrn)
183 table.insert(ma_record)
191 AuthHierarchy = Hierarchy()
192 TrustedRoots = TrustedRootList()
194 print "Import: creating top level authorities"
196 if not AuthHierarchy.auth_exists(root_auth):
197 AuthHierarchy.create_auth(root_auth)
198 #create_top_level_auth_records(root_auth)
199 if not AuthHierarchy.auth_exists(level1_auth):
200 AuthHierarchy.create_auth(level1_auth)
201 create_top_level_auth_records(level1_auth)
203 print "Import: adding", root_auth, "to trusted list"
204 root = AuthHierarchy.get_auth_info(root_auth)
205 TrustedRoots.add_gid(root.get_gid_object())
209 sites = shell.GetSites(pl_auth)
211 import_site(level1_auth, site)
213 if __name__ == "__main__":