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 _SfaLogger
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 = _SfaLogger(
32 logfile='/var/log/sfa_import.log', loggername='importlog')
33 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
34 # ugly side effect so that other modules get it right
35 import sfa.util.sfalogging
36 sfa.util.sfalogging.logger = logger
37 # self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
39 # check before creating a RegRecord entry as we run this over and over
41 def record_exists(type, hrn):
42 return (global_dbsession.query(RegRecord)
43 .filter_by(hrn=hrn, type=type).count() != 0)
45 def create_top_level_auth_records(self, hrn):
47 Create top level db records
48 includes root and sub authorities (local/remote)
50 # make sure parent exists
51 parent_hrn = get_authority(hrn)
54 if not parent_hrn == hrn:
55 self.create_top_level_auth_records(parent_hrn)
57 # ensure key and cert exists:
58 self.auth_hierarchy.create_top_level_auth(hrn)
59 # create the db record if it doesnt already exist
60 if not self.record_exists('authority', hrn):
61 auth_info = self.auth_hierarchy.get_auth_info(hrn)
62 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
63 authority=get_authority(hrn))
64 auth_record.just_created()
65 global_dbsession.add(auth_record)
66 global_dbsession.commit()
68 "SfaImporter: imported authority (parent) %s " % auth_record)
70 def create_sm_client_record(self):
72 Create a user record for the Slicemanager service.
74 hrn = self.interface_hrn + '.slicemanager' # pylint: disable=e1101
75 urn = hrn_to_urn(hrn, 'user')
76 if not self.auth_hierarchy.auth_exists(urn):
77 self.logger.info("SfaImporter: creating Slice Manager user")
78 self.auth_hierarchy.create_auth(urn)
80 if self.record_exists('user', hrn):
82 auth_info = self.auth_hierarchy.get_auth_info(hrn)
83 user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
84 authority=get_authority(hrn))
85 user_record.just_created()
86 global_dbsession.add(user_record)
87 global_dbsession.commit()
89 "SfaImporter: importing user (slicemanager) %s " % user_record)
91 def create_interface_records(self):
93 Create a record for each SFA interface
95 # just create certs for all sfa interfaces even if they
97 auth_info = self.auth_hierarchy.get_auth_info(
98 self.config.SFA_INTERFACE_HRN)
99 pkey = auth_info.get_pkey_object()
100 hrn = self.config.SFA_INTERFACE_HRN
101 for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
102 urn = hrn_to_urn(hrn, type)
103 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
104 # for now we have to preserve the authority+<> stuff
105 if self.record_exists(type, hrn):
107 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
108 authority=get_authority(hrn))
109 interface_record.just_created()
110 global_dbsession.add(interface_record)
111 global_dbsession.commit()
112 self.logger.info("SfaImporter: imported authority (%s) %s " % (
113 type, interface_record))
115 def run(self, options=None):
116 if not self.config.SFA_REGISTRY_ENABLED:
117 self.logger.critical(
118 "Importer: need SFA_REGISTRY_ENABLED to run import")
120 # testbed-neutral : create local certificates and the like
121 auth_hierarchy = Hierarchy()
122 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
123 self.create_interface_records()
126 testbed_importer = None
127 generic = Generic.the_flavour()
128 importer_class = generic.importer_class()
130 begin_time = datetime.utcnow()
131 self.logger.info(30 * '=')
133 "Starting import on %s, using class %s from flavour %s" %
134 (begin_time, importer_class.__name__, generic.flavour))
135 testbed_importer = importer_class(auth_hierarchy, self.logger)
137 testbed_importer.add_options(options)
138 testbed_importer.run(options)
139 end_time = datetime.utcnow()
140 duration = end_time - begin_time
141 self.logger.info("Import took %s" % duration)
142 self.logger.info(30 * '=')