more pep8-friendly
[sfa.git] / sfa / importer / __init__.py
1 #!/usr/bin/python
2
3 # pylint: disable=c0111, w1201, w0622
4
5 from datetime import datetime
6
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
18
19
20 class Importer:
21
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
26         else:
27             self.auth_hierarchy = Hierarchy()
28         if logger is not None:
29             self.logger = logger
30         else:
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())
38
39     # check before creating a RegRecord entry as we run this over and over
40     @staticmethod
41     def record_exists(type, hrn):
42         return (global_dbsession.query(RegRecord)
43                 .filter_by(hrn=hrn, type=type).count() != 0)
44
45     def create_top_level_auth_records(self, hrn):
46         """
47         Create top level db records
48         includes root and sub authorities (local/remote)
49         """
50         # make sure parent exists
51         parent_hrn = get_authority(hrn)
52         if not parent_hrn:
53             parent_hrn = hrn
54         if not parent_hrn == hrn:
55             self.create_top_level_auth_records(parent_hrn)
56
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()
67             self.logger.info(
68                 "SfaImporter: imported authority (parent) %s " % auth_record)
69
70     def create_sm_client_record(self):
71         """
72         Create a user record for the Slicemanager service.
73         """
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)
79
80         if self.record_exists('user', hrn):
81             return
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()
88         self.logger.info(
89             "SfaImporter: importing user (slicemanager) %s " % user_record)
90
91     def create_interface_records(self):
92         """
93         Create a record for each SFA interface
94         """
95         # just create certs for all sfa interfaces even if they
96         # aren't enabled
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):
106                 continue
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))
114
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")
119
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()
124
125         # testbed-specific
126         testbed_importer = None
127         generic = Generic.the_flavour()
128         importer_class = generic.importer_class()
129         if importer_class:
130             begin_time = datetime.utcnow()
131             self.logger.info(30 * '=')
132             self.logger.info(
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)
136             if testbed_importer:
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 * '=')