6 from hierarchy import *
11 root_auth = "planetlab"
12 level1_auth = "planetlab.us"
14 def process_options():
17 (options, args) = getopt.getopt(sys.argv[1:], '', [])
25 # get PL account settings from config module
26 pl_auth = get_pl_auth()
28 # connect to planetlab
31 shell = remoteshell.RemoteShell()
34 shell = PLC.Shell.Shell(globals = globals())
36 def get_auth_table(auth_name):
37 auth_info = AuthHierarchy.get_auth_info(auth_name)
39 table = GeniTable(hrn=auth_name,
40 cninfo=auth_info.get_dbinfo())
42 # if the table doesn't exist, then it means we haven't put any records
43 # into this authority yet.
45 if not table.exists():
46 report.trace("Import: creating table for authority " + auth_name)
51 def get_pl_pubkey(key_id):
52 keys = shell.GetKeys(pl_auth, [key_id])
54 key_str = keys[0]['key']
56 # generate temporary files to hold the keys
57 (ssh_f, ssh_fn) = tempfile.mkstemp()
58 ssl_fn = tempfile.mktemp()
60 os.write(ssh_f, key_str)
63 cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
67 # this check leaves the temporary file containing the public key so
68 # that it can be expected to see why it failed.
69 # TODO: for production, cleanup the temporary files
70 if not os.path.exists(ssl_fn):
71 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
75 k.load_pubkey_from_file(ssl_fn)
79 # remove the temporary files
87 def import_person(parent_hrn, person):
88 personname = person['last_name'] + "_" + person['first_name']
90 hrn = parent_hrn + "." + personname
91 report.trace("Import: importing person " + hrn)
93 table = get_auth_table(parent_hrn)
95 person_record = table.resolve("slice", hrn)
97 #pkey = Keypair(create=True)
98 key_ids = person["key_ids"]
100 pkey = get_pl_pubkey(key_ids[0])
102 report.trace(" person " + hrn + " does not have a PL public key")
103 pkey = Keypair(create=True)
104 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
105 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
106 report.trace(" inserting user record for " + hrn)
107 table.insert(person_record)
109 def import_slice(parent_hrn, slice):
110 slicename = slice['name'].split("_",1)[-1]
113 report.error("Import_Slice: failed to parse slice name " + slice['name'])
116 hrn = parent_hrn + "." + slicename
117 report.trace("Import: importing slice " + hrn)
119 table = get_auth_table(parent_hrn)
121 slice_record = table.resolve("slice", hrn)
123 pkey = Keypair(create=True)
124 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
125 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
126 report.trace(" inserting slice record for " + hrn)
127 table.insert(slice_record)
129 def import_site(parent_hrn, site):
130 hrn = parent_hrn + "." + site['login_base']
132 report.trace("Import_Site: importing site " + hrn)
134 # create the authority
135 if not AuthHierarchy.auth_exists(hrn):
136 AuthHierarchy.create_auth(hrn)
138 auth_info = AuthHierarchy.get_auth_info(hrn)
140 table = get_auth_table(parent_hrn)
142 sa_record = table.resolve("sa", hrn)
144 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
145 report.trace(" inserting sa record for " + hrn)
146 table.insert(sa_record)
148 ma_record = table.resolve("ma", hrn)
150 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
151 report.trace(" inserting ma record for " + hrn)
152 table.insert(ma_record)
154 for person_id in site['person_ids']:
155 persons = shell.GetPersons(pl_auth, [person_id])
157 import_person(hrn, persons[0])
159 for slice_id in site['slice_ids']:
160 slices = shell.GetSlices(pl_auth, [slice_id])
162 import_slice(hrn, slices[0])
169 AuthHierarchy = Hierarchy()
170 if not AuthHierarchy.auth_exists(root_auth):
171 AuthHierarchy.create_auth(root_auth)
172 if not AuthHierarchy.auth_exists(level1_auth):
173 AuthHierarchy.create_auth(level1_auth)
177 sites = shell.GetSites(pl_auth)
179 import_site(level1_auth, site)
181 if __name__ == "__main__":