fix in logging: actually store importer logs in /var/log
[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 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_sm_client_record(self):
78         """
79         Create a user record for the Slicemanager service.
80         """
81         hrn = self.interface_hrn + '.slicemanager'      # pylint: disable=e1101
82         urn = hrn_to_urn(hrn, 'user')
83         if not self.auth_hierarchy.auth_exists(urn):
84             self.logger.info("SfaImporter: creating Slice Manager user")
85             self.auth_hierarchy.create_auth(urn)
86
87         if self.record_exists('user', hrn):
88             return
89         auth_info = self.auth_hierarchy.get_auth_info(hrn)
90         user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
91                               authority=get_authority(hrn))
92         user_record.just_created()
93         global_dbsession.add(user_record)
94         global_dbsession.commit()
95         self.logger.info(
96             "SfaImporter: importing user (slicemanager) %s " % user_record)
97
98     def create_interface_records(self):
99         """
100         Create a record for each SFA interface
101         """
102         # just create certs for all sfa interfaces even if they
103         # aren't enabled
104         auth_info = self.auth_hierarchy.get_auth_info(
105             self.config.SFA_INTERFACE_HRN)
106         pkey = auth_info.get_pkey_object()
107         hrn = self.config.SFA_INTERFACE_HRN
108         for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
109             urn = hrn_to_urn(hrn, type)
110             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
111             # for now we have to preserve the authority+<> stuff
112             if self.record_exists(type, hrn):
113                 continue
114             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
115                                             authority=get_authority(hrn))
116             interface_record.just_created()
117             global_dbsession.add(interface_record)
118             global_dbsession.commit()
119             self.logger.info("SfaImporter: imported authority (%s) %s " % (
120                 type, interface_record))
121
122     def run(self, options=None):
123         if not self.config.SFA_REGISTRY_ENABLED:
124             self.logger.critical(
125                 "Importer: need SFA_REGISTRY_ENABLED to run import")
126
127         # testbed-neutral : create local certificates and the like
128         auth_hierarchy = Hierarchy()
129         self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
130         self.create_interface_records()
131
132         # testbed-specific
133         testbed_importer = None
134         generic = Generic.the_flavour()
135         importer_class = generic.importer_class()
136         if importer_class:
137             begin_time = datetime.utcnow()
138             self.logger.info(30 * '=')
139             self.logger.info(
140                 "Starting import on %s, using class %s from flavour %s" %
141                 (begin_time, importer_class.__name__, generic.flavour))
142             testbed_importer = importer_class(auth_hierarchy, self.logger)
143             if testbed_importer:
144                 testbed_importer.add_options(options)
145                 testbed_importer.run(options)
146             end_time = datetime.utcnow()
147             duration = end_time - begin_time
148             self.logger.info("Import took %s" % duration)
149             self.logger.info(30 * '=')