542ee7a13d35ff3f688343e30cf551d4ce2e0d02
[sfa.git] / registry / import.py
1 import getopt
2 import sys
3 import tempfile
4
5 from cert import *
6 from hierarchy import *
7 from record import *
8
9 shell = None
10
11 root_auth = "planetlab"
12 level1_auth = "planetlab.us"
13
14 def process_options():
15    global hrn
16
17    (options, args) = getopt.getopt(sys.argv[1:], '', [])
18    for opt in options:
19        name = opt[0]
20        val = opt[1]
21
22 def connect_shell():
23     global pl_auth, shell
24
25     # get PL account settings from config module
26     pl_auth = get_pl_auth()
27
28     # connect to planetlab
29     if "Url" in pl_auth:
30         import remoteshell
31         shell = remoteshell.RemoteShell()
32     else:
33         import PLC.Shell
34         shell = PLC.Shell.Shell(globals = globals())
35
36 def get_auth_table(auth_name):
37     auth_info = AuthHierarchy.get_auth_info(auth_name)
38
39     table = GeniTable(hrn=auth_name,
40                       cninfo=auth_info.get_dbinfo())
41
42     # if the table doesn't exist, then it means we haven't put any records
43     # into this authority yet.
44
45     if not table.exists():
46         report.trace("Import: creating table for authority " + auth_name)
47         table.create()
48
49     return table
50
51 def get_pl_pubkey(key_id):
52     keys = shell.GetKeys(pl_auth, [key_id])
53     if keys:
54         key_str = keys[0]['key']
55
56         # generate temporary files to hold the keys
57         (ssh_f, ssh_fn) = tempfile.mkstemp()
58         ssl_fn = tempfile.mktemp()
59
60         os.write(ssh_f, key_str)
61         os.close(ssh_f)
62
63         cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
64         print cmd
65         os.system(cmd)
66
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)
72             return None
73
74         k = Keypair()
75         k.load_pubkey_from_file(ssl_fn)
76
77         #sys.exit(-1)
78
79         # remove the temporary files
80         os.remove(ssh_fn)
81         os.remove(ssl_fn)
82
83         return k
84     else:
85         return None
86
87 def import_person(parent_hrn, person):
88     personname = person['last_name'] + "_" + person['first_name']
89
90     hrn = parent_hrn + "." + personname
91     report.trace("Import: importing person " + hrn)
92
93     table = get_auth_table(parent_hrn)
94
95     person_record = table.resolve("slice", hrn)
96     if not person_record:
97         #pkey = Keypair(create=True)
98         key_ids = person["key_ids"]
99         if key_ids:
100             pkey = get_pl_pubkey(key_ids[0])
101         else:
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)
108
109 def import_slice(parent_hrn, slice):
110     slicename = slice['name'].split("_",1)[-1]
111
112     if not slicename:
113         report.error("Import_Slice: failed to parse slice name " + slice['name'])
114         return
115
116     hrn = parent_hrn + "." + slicename
117     report.trace("Import: importing slice " + hrn)
118
119     table = get_auth_table(parent_hrn)
120
121     slice_record = table.resolve("slice", hrn)
122     if not slice_record:
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)
128
129 def import_site(parent_hrn, site):
130     hrn = parent_hrn + "." + site['login_base']
131
132     report.trace("Import_Site: importing site " + hrn)
133
134     # create the authority
135     if not AuthHierarchy.auth_exists(hrn):
136         AuthHierarchy.create_auth(hrn)
137
138     auth_info = AuthHierarchy.get_auth_info(hrn)
139
140     table = get_auth_table(parent_hrn)
141
142     sa_record = table.resolve("sa", hrn)
143     if not sa_record:
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)
147
148     ma_record = table.resolve("ma", hrn)
149     if not ma_record:
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)
153
154     for person_id in site['person_ids']:
155         persons = shell.GetPersons(pl_auth, [person_id])
156         if persons:
157             import_person(hrn, persons[0])
158
159     for slice_id in site['slice_ids']:
160         slices = shell.GetSlices(pl_auth, [slice_id])
161         if slices:
162             import_slice(hrn, slices[0])
163
164 def main():
165     global AuthHierarchy
166
167     process_options()
168
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)
174
175     connect_shell()
176
177     sites = shell.GetSites(pl_auth)
178     for site in sites:
179         import_site(level1_auth, site)
180
181 if __name__ == "__main__":
182     main()