4 # Import PLC records into the SFA database. It is indended that this tool be
5 # run once to create SFA records that reflect the current state of the
8 # The import tool assumes that the existing PLC hierarchy should all be part
9 # of "planetlab.us" (see the root_auth and level1_auth variables below).
11 # Public keys are extracted from the users' SSH keys automatically and used to
12 # create GIDs. This is relatively experimental as a custom tool had to be
13 # written to perform conversion from SSH to OpenSSL format. It only supports
14 # RSA keys at this time, not DSA keys.
22 from sfa.util.record import *
23 from sfa.util.table import SfaTable
24 from sfa.util.xrn import get_leaf, get_authority
25 from sfa.util.plxrn import hostname_to_hrn, slicename_to_hrn, email_to_hrn, hrn_to_pl_slicename
26 from sfa.util.config import Config
27 from sfa.trust.certificate import convert_public_key, Keypair
28 from sfa.trust.trustedroots import *
29 from sfa.trust.hierarchy import *
30 from sfa.util.xrn import Xrn
31 from sfa.trust.gid import create_uuid
34 from sfa.senslab.SenslabImportUsers import *
35 from sfa.senslab.OARrestapi import *
37 from sfa.senslab.SenslabImport import SenslabImport
44 oarserver['ip'] = '10.127.255.254'
45 oarserver['port'] = 80
46 oarserver['uri'] = '/oarapi/resources/full.json'
49 def process_options():
51 (options, args) = getopt.getopt(sys.argv[1:], '', [])
57 def load_keys(filename):
61 execfile(filename, tmp_dict)
62 if 'keys' in tmp_dict:
63 keys = tmp_dict['keys']
68 def save_keys(filename, keys):
69 f = open(filename, 'w')
70 f.write("keys = %s" % str(keys))
77 if not config.SFA_REGISTRY_ENABLED:
79 root_auth = config.SFA_REGISTRY_ROOT_AUTH
80 interface_hrn = config.SFA_INTERFACE_HRN
81 print interface_hrn, root_auth
82 keys_filename = config.config_path + os.sep + 'person_keys.py'
84 sfaImporter = SenslabImport()
85 SenslabUsers = SenslabImportUsers()
87 OARImporter = OARapi()
88 #print '\r\n =====OAR Importer list===== '
89 #for node in OARImporter.OARserver.GetNodes().keys():
90 #print node, OARImporter.OARserver.GetNodes[node]
93 #if config.SFA_API_DEBUG: sfaImporter.logger.setLevelDebug()
94 #shell = sfaImporter.shell
95 #plc_auth = sfaImporter.plc_auth
98 # initialize registry db table
100 if not table.exists():
103 # create root authority
104 sfaImporter.create_top_level_auth_records(root_auth)
105 if not root_auth == interface_hrn:
106 sfaImporter.create_top_level_auth_records(interface_hrn)
108 # create interface records ADDED 12 JUILLET 2011
109 sfaImporter.logger.info("Import: creating interface records")
110 sfaImporter.create_interface_records()
112 # add local root authority's cert to trusted list ADDED 12 JUILLET 2011
113 sfaImporter.logger.info("Import: adding " + interface_hrn + " to trusted list")
114 authority = sfaImporter.AuthHierarchy.get_auth_info(interface_hrn)
115 sfaImporter.TrustedRoots.add_gid(authority.get_gid_object())
118 print "\r\n \r\n create dict of all existing sfa records"
119 # create dict of all existing sfa records
120 existing_records = {}
124 results = table.find()
125 for result in results:
126 existing_records[(result['hrn'], result['type'])] = result
127 existing_hrns.append(result['hrn'])
133 nodes_dict = OARImporter.GetNodes()
134 print "\r\n NODES8DICT ",nodes_dict
136 persons_list = SenslabUsers.GetPersons()
137 print "\r\n PERSONS_LIST ",persons_list
139 keys_list = SenslabUsers.GetKeys()
140 print "\r\n KEYSS_LIST ",keys_list
142 slices_list = SenslabUsers.GetSlices()
143 print "\r\n SLICES_LIST ",slices_list
145 # Get all Senslab sites
146 sites_dict = OARImporter.GetSites()
147 print "\r\n sSITES_DICT" , sites_dict
150 for site in sites_dict:
151 site_hrn = interface_hrn + "." + site['login_base']
152 #sfa_logger().info("Importing site: %s" % site_hrn)
153 print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
154 # import if hrn is not in list of existing hrns or if the hrn exists
155 # but its not a site record
156 if site_hrn not in existing_hrns or \
157 (site_hrn, 'authority') not in existing_records:
158 print "SITE HRN UNKNOWN" , site, site_hrn
159 site_hrn = sfaImporter.import_site(interface_hrn, site)
161 print "\r\n \r\n ===========IMPORT NODE_RECORDS ==========\r\n site %s \r\n \t nodes_dict %s" %(site,nodes_dict)
162 # import node records
163 for node_id in site['node_ids']:
164 #for[node['node_id'] for node in nodes_dict]:
165 #print '\r\n \t **NODE_ID %s node %s '%( node_id, node)
167 for node in nodes_dict:
168 if node_id is node['node_id']:
169 #node = nodes_dict[node_id]
170 print '\r\n \t NODE_ID %s node %s '%( node_id, node)
171 hrn = hostname_to_hrn(interface_hrn, site['login_base'], node['hostname'])
174 if hrn not in existing_hrns or \
175 (hrn, 'node') not in existing_records:
176 print "\t\t NODE HRN NOT in existing records!" ,hrn
177 sfaImporter.import_node(hrn, node)
180 for person in persons_list:
181 hrn = email_to_hrn(site_hrn, person['email'])
182 print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^PERSON hrn %s person %s site hrn %s" %(hrn,person,site_hrn)
183 sfaImporter.import_person( site_hrn, person,keys_list)
186 for slice_id in site['slice_ids']:
187 print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id %s " %(slice_id)
188 for sl in slices_list:
189 if slice_id is sl['slice_id']:
190 #hrn = slicename_to_hrn(interface_hrn, sl['name'])
191 hrn = email_to_hrn(site_hrn, sl['name'])
192 print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s site_hrn %s" %(hrn,site_hrn)
193 if hrn not in existing_hrns or \
194 (hrn, 'slice') not in existing_records:
195 sfaImporter.import_slice(site_hrn, sl)
199 # remove stale records
200 for (record_hrn, type) in existing_records.keys():
201 record = existing_records[(record_hrn, type)]
202 print" \r\n ****record hrn %s \t\t TYPE %s " %(record_hrn,type)
203 # if this is the interface name dont do anything
204 if record_hrn == interface_hrn or \
205 record_hrn == root_auth or \
206 record['peer_authority']:
212 if type == 'authority':
213 for site in sites_dict:
214 print "\t type : authority : ", site
215 site_hrn = interface_hrn + "." + site['login_base']
216 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
218 print "\t \t Found :", found
222 login_base = get_leaf(get_authority(record_hrn))
224 nodename = Xrn.unescape(get_leaf(record_hrn))
225 print "type: node : login_base %s nodename %s" %(login_base, nodename)
226 if login_base in sites_dict:
227 site = sites_dict[login_base]
228 print "\t type node : login base %s site %s" %(login_base, site)
229 for node in nodes_dict.values():
230 tmp_nodename = node['hostname']
231 if tmp_nodename == nodename and \
232 node['site_id'] == site['site_id'] and \
233 node['node_id'] == record['pointer']:
235 print "\t Nodename: %s site id %s node id %s record %s" %( nodename, node['site_id'], node['node_id'],record['pointer'])
241 record_object = existing_records[(record_hrn, type)]
242 print "\t\t NOT FOUND ! "
243 sfaImporter.delete_record(record_hrn, type)
246 sfaImporter.logger.info('Import: saving current pub keys')
247 save_keys(keys_filename, person_keys)
251 if __name__ == "__main__":