3 # pylint: disable=c0111, w1201, w0622
5 from datetime import datetime
7 from sfa.util.xrn import get_authority, hrn_to_urn
8 from sfa.generic import Generic
9 from sfa.util.config import Config
10 from sfa.util.sfalogging import logger as import_logger
11 from sfa.trust.hierarchy import Hierarchy
12 # from sfa.trust.trustedroots import TrustedRoots
13 from sfa.trust.gid import create_uuid
14 # using global alchemy.session() here is fine
15 # as importer is on standalone one-shot process
16 from sfa.storage.alchemy import global_dbsession
17 from sfa.storage.model import RegRecord, RegAuthority, RegUser
22 def __init__(self, auth_hierarchy=None, logger=None):
23 self.config = Config()
24 if auth_hierarchy is not None:
25 self.auth_hierarchy = auth_hierarchy
27 self.auth_hierarchy = Hierarchy()
28 if logger is not None:
31 self.logger = import_logger
32 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
33 # ugly side effect so that other modules get it right
34 import sfa.util.sfalogging
35 sfa.util.sfalogging.logger = logger
36 # self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
38 # check before creating a RegRecord entry as we run this over and over
40 def record_exists(type, hrn):
41 return (global_dbsession.query(RegRecord)
42 .filter_by(hrn=hrn, type=type).count() != 0)
44 def create_top_level_auth_records(self, hrn):
46 Create top level db records
47 includes root and sub authorities (local/remote)
49 # make sure parent exists
50 parent_hrn = get_authority(hrn)
53 if not parent_hrn == hrn:
54 self.create_top_level_auth_records(parent_hrn)
56 # ensure key and cert exists:
57 self.auth_hierarchy.create_top_level_auth(hrn)
58 # create the db record if it doesnt already exist
59 if not self.record_exists('authority', hrn):
60 auth_info = self.auth_hierarchy.get_auth_info(hrn)
61 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
62 authority=get_authority(hrn))
63 auth_record.just_created()
64 global_dbsession.add(auth_record)
65 global_dbsession.commit()
67 "SfaImporter: imported authority (parent) %s " % auth_record)
69 def create_sm_client_record(self):
71 Create a user record for the Slicemanager service.
73 hrn = self.interface_hrn + '.slicemanager' # pylint: disable=e1101
74 urn = hrn_to_urn(hrn, 'user')
75 if not self.auth_hierarchy.auth_exists(urn):
76 self.logger.info("SfaImporter: creating Slice Manager user")
77 self.auth_hierarchy.create_auth(urn)
79 if self.record_exists('user', hrn):
81 auth_info = self.auth_hierarchy.get_auth_info(hrn)
82 user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
83 authority=get_authority(hrn))
84 user_record.just_created()
85 global_dbsession.add(user_record)
86 global_dbsession.commit()
88 "SfaImporter: importing user (slicemanager) %s " % user_record)
90 def create_interface_records(self):
92 Create a record for each SFA interface
94 # just create certs for all sfa interfaces even if they
96 auth_info = self.auth_hierarchy.get_auth_info(
97 self.config.SFA_INTERFACE_HRN)
98 pkey = auth_info.get_pkey_object()
99 hrn = self.config.SFA_INTERFACE_HRN
100 for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
101 urn = hrn_to_urn(hrn, type)
102 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
103 # for now we have to preserve the authority+<> stuff
104 if self.record_exists(type, hrn):
106 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
107 authority=get_authority(hrn))
108 interface_record.just_created()
109 global_dbsession.add(interface_record)
110 global_dbsession.commit()
111 self.logger.info("SfaImporter: imported authority (%s) %s " % (
112 type, interface_record))
114 def run(self, options=None):
115 if not self.config.SFA_REGISTRY_ENABLED:
116 self.logger.critical(
117 "Importer: need SFA_REGISTRY_ENABLED to run import")
119 # testbed-neutral : create local certificates and the like
120 auth_hierarchy = Hierarchy()
121 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
122 self.create_interface_records()
125 testbed_importer = None
126 generic = Generic.the_flavour()
127 importer_class = generic.importer_class()
129 begin_time = datetime.utcnow()
130 self.logger.info(30 * '=')
132 "Starting import on %s, using class %s from flavour %s" %
133 (begin_time, importer_class.__name__, generic.flavour))
134 testbed_importer = importer_class(auth_hierarchy, self.logger)
136 testbed_importer.add_options(options)
137 testbed_importer.run(options)
138 end_time = datetime.utcnow()
139 duration = end_time - begin_time
140 self.logger.info("Import took %s" % duration)
141 self.logger.info(30 * '=')