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