shebangs need to point at python2
[sfa.git] / sfa / importer / __init__.py
1 #!/usr/bin/env python2
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 init_logger, logger as default_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
18
19 # note on logging
20 # it is doubtful that anyone ever used the ability to
21 # pass a logger to this class, and that can probably be
22 # thrown away.
23 # However a quick attempt showed that it seems to
24 # also require changes in the Generic layer
25
26 class Importer:
27
28     def __init__(self, auth_hierarchy=None, logger=None):
29         self.config = Config()
30         if auth_hierarchy is not None:
31             self.auth_hierarchy = auth_hierarchy
32         else:
33             self.auth_hierarchy = Hierarchy()
34         if logger is None:
35             # redirect in sfa-import.log
36             self.logger = default_logger
37             init_logger('import')
38         else:
39             self.logger = logger
40         self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
41 # ugly side effect so that other modules get it right
42         import sfa.util.sfalogging
43         sfa.util.sfalogging.logger = logger
44 #        self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
45
46     # check before creating a RegRecord entry as we run this over and over
47     @staticmethod
48     def record_exists(type, hrn):
49         return (global_dbsession.query(RegRecord)
50                 .filter_by(hrn=hrn, type=type).count() != 0)
51
52     def create_top_level_auth_records(self, hrn):
53         """
54         Create top level db records
55         includes root and sub authorities (local/remote)
56         """
57         # make sure parent exists
58         parent_hrn = get_authority(hrn)
59         if not parent_hrn:
60             parent_hrn = hrn
61         if not parent_hrn == hrn:
62             self.create_top_level_auth_records(parent_hrn)
63
64         # ensure key and cert exists:
65         self.auth_hierarchy.create_top_level_auth(hrn)
66         # create the db record if it doesnt already exist
67         if not self.record_exists('authority', hrn):
68             auth_info = self.auth_hierarchy.get_auth_info(hrn)
69             auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
70                                        authority=get_authority(hrn))
71             auth_record.just_created()
72             global_dbsession.add(auth_record)
73             global_dbsession.commit()
74             self.logger.info(
75                 "SfaImporter: imported authority (parent) %s " % auth_record)
76
77     def create_interface_records(self):
78         """
79         Create a record for each SFA interface
80         """
81         # just create certs for all sfa interfaces even if they
82         # aren't enabled
83         auth_info = self.auth_hierarchy.get_auth_info(
84             self.config.SFA_INTERFACE_HRN)
85         pkey = auth_info.get_pkey_object()
86         hrn = self.config.SFA_INTERFACE_HRN
87         for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
88             urn = hrn_to_urn(hrn, type)
89             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
90             # for now we have to preserve the authority+<> stuff
91             if self.record_exists(type, hrn):
92                 continue
93             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
94                                             authority=get_authority(hrn))
95             interface_record.just_created()
96             global_dbsession.add(interface_record)
97             global_dbsession.commit()
98             self.logger.info("SfaImporter: imported authority (%s) %s " % (
99                 type, interface_record))
100
101     def run(self, options=None):
102         if not self.config.SFA_REGISTRY_ENABLED:
103             self.logger.critical(
104                 "Importer: need SFA_REGISTRY_ENABLED to run import")
105
106         # testbed-neutral : create local certificates and the like
107         auth_hierarchy = Hierarchy()
108         self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
109         self.create_interface_records()
110
111         # testbed-specific
112         testbed_importer = None
113         generic = Generic.the_flavour()
114         importer_class = generic.importer_class()
115         if importer_class:
116             begin_time = datetime.utcnow()
117             self.logger.info(30 * '=')
118             self.logger.info(
119                 "Starting import on %s, using class %s from flavour %s" %
120                 (begin_time, importer_class.__name__, generic.flavour))
121             testbed_importer = importer_class(auth_hierarchy, self.logger)
122             if testbed_importer:
123                 testbed_importer.add_options(options)
124                 testbed_importer.run(options)
125             end_time = datetime.utcnow()
126             duration = end_time - begin_time
127             self.logger.info("Import took %s" % duration)
128             self.logger.info(30 * '=')