e1c3a289dd704d0132ca973de577d3a0769eda45
[sfa.git] / sfa / importer / __init__.py
1 #!/usr/bin/python
2
3 import sys
4 from datetime import datetime
5
6 from sfa.util.xrn import get_authority, hrn_to_urn
7 from sfa.generic import Generic
8 from sfa.util.config import Config
9 from sfa.util.sfalogging import _SfaLogger
10 from sfa.trust.hierarchy import Hierarchy
11 #from sfa.trust.trustedroots import TrustedRoots
12 from sfa.trust.gid import create_uuid
13 # using global alchemy.session() here is fine 
14 # as importer is on standalone one-shot process
15 from sfa.storage.alchemy import global_dbsession
16 from sfa.storage.model import RegRecord, RegAuthority, RegUser
17 from sfa.trust.certificate import convert_public_key, Keypair
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(logfile='/var/log/sfa_import.log', loggername='importlog')
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())    
37    
38     # check before creating a RegRecord entry as we run this over and over
39     def record_exists (self, type, hrn):
40        return global_dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0 
41
42     def create_top_level_auth_records(self, hrn):
43         """
44         Create top level db records (includes root and sub authorities (local/remote)
45         """
46         # make sure parent exists
47         parent_hrn = get_authority(hrn)
48         if not parent_hrn:
49             parent_hrn = hrn
50         if not parent_hrn == hrn:
51             self.create_top_level_auth_records(parent_hrn)
52
53         # ensure key and cert exists:
54         self.auth_hierarchy.create_top_level_auth(hrn)
55         # create the db record if it doesnt already exist
56         if not self.record_exists ('authority',hrn):
57             auth_info = self.auth_hierarchy.get_auth_info(hrn)
58             auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
59                                        authority=get_authority(hrn))
60             auth_record.just_created()
61             global_dbsession.add (auth_record)
62             global_dbsession.commit()
63             self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)     
64    
65
66     def create_sm_client_record(self):
67         """
68         Create a user record for the Slicemanager service.
69         """
70         hrn = self.interface_hrn + '.slicemanager'
71         urn = hrn_to_urn(hrn, 'user')
72         if not self.auth_hierarchy.auth_exists(urn):
73             self.logger.info("SfaImporter: creating Slice Manager user")
74             self.auth_hierarchy.create_auth(urn)
75
76         if self.record_exists ('user',hrn): return
77         auth_info = self.auth_hierarchy.get_auth_info(hrn)
78         user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
79                               authority=get_authority(hrn))
80         user_record.just_created()
81         global_dbsession.add (user_record)
82         global_dbsession.commit()
83         self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
84
85
86     def create_interface_records(self):
87         """
88         Create a record for each SFA interface
89         """
90         # just create certs for all sfa interfaces even if they
91         # aren't enabled
92         auth_info = self.auth_hierarchy.get_auth_info(self.config.SFA_INTERFACE_HRN)
93         pkey = auth_info.get_pkey_object()
94         hrn=self.config.SFA_INTERFACE_HRN
95         for type in  [ 'authority+sa', 'authority+am', 'authority+sm', ]:
96             urn = hrn_to_urn(hrn, type)
97             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
98             # for now we have to preserve the authority+<> stuff
99             if self.record_exists (type,hrn): continue
100             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
101                                             authority=get_authority(hrn))
102             interface_record.just_created()
103             global_dbsession.add (interface_record)
104             global_dbsession.commit()
105             self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
106  
107     def run(self, options=None):
108         if not self.config.SFA_REGISTRY_ENABLED:
109             self.logger.critical("Importer: need SFA_REGISTRY_ENABLED to run import")
110
111         # testbed-neutral : create local certificates and the like
112         auth_hierarchy = Hierarchy ()
113         self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
114         self.create_interface_records()
115  
116         # testbed-specific
117         testbed_importer = None
118         generic=Generic.the_flavour()
119         importer_class = generic.importer_class()
120         if importer_class:
121             begin_time=datetime.utcnow()
122             self.logger.info (30*'=')
123             self.logger.info ("Starting import on %s, using class %s from flavour %s"%\
124                          (begin_time,importer_class.__name__,generic.flavour))
125             testbed_importer = importer_class (auth_hierarchy, self.logger)
126             if testbed_importer:
127                 testbed_importer.add_options(options)
128                 testbed_importer.run (options)
129             end_time=datetime.utcnow()
130             duration=end_time-begin_time
131             self.logger.info("Import took %s"%duration)
132             self.logger.info (30*'=')