5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 import eventlet # noqa
18 from eventlet import wsgi # noqa
19 eventlet.monkey_patch() # noqa
27 from cmframework.utils import cmbackendpluginclient
28 from cmframework.utils import cmlogger
29 from cmframework.utils import cmbackendhandler
30 from cmframework.utils import cmactivationstatehandler
31 from cmframework.utils import cmalarmhandler
32 from cmframework.utils import cmsnapshothandler
33 from cmframework.server import cmargs
34 from cmframework.server import cmprocessor
35 from cmframework.server import cmrestapifactory
36 from cmframework.server import cmwsgihandler
37 from cmframework.server import cmvalidator
38 from cmframework.server import cmactivator
39 from cmframework.server import cmactivateserverhandler
40 from cmframework.server import cmchangemonitor
41 from cmframework.utils.cmansibleinventory import AnsibleInventory
47 parser = cmargs.CMArgsParser('cmserver')
48 parser.parse(sys.argv[1:])
50 # Read configuration names which should be masked in logging
51 mask_names = _read_maskable_names()
53 # initialize the logger
54 _ = cmlogger.CMLogger(parser.get_log_dest(),
56 parser.get_log_level(),
59 logging.info('CM server is starting up')
61 # initialize the change monitor object
62 changemonitor = cmchangemonitor.CMChangeMonitor()
65 logging.info('Initializing backend handler')
67 backend_args['uri'] = parser.get_backend_uri()
68 backend = cmbackendhandler.CMBackendHandler(parser.get_backend_api(), **backend_args)
70 # construct the plugin client library
71 logging.info('Initialize plugin client library')
72 plugin_client = cmbackendpluginclient.CMBackendPluginClient(parser.get_backend_api(),
75 # load activation state handler
76 logging.info('Initializing activation state handler')
77 activationstatehandler_args = {}
78 activationstatehandler_args['uri'] = parser.get_activationstate_handler_uri()
79 activationstate_handler = cmactivationstatehandler.CMActivationStateHandler(
80 parser.get_activationstate_handler_api(), **activationstatehandler_args)
82 # initialize validator
83 logging.info('Initializing validator')
84 validator = cmvalidator.CMValidator(parser.get_validators(), plugin_client)
86 # initializing activation handling process
87 logging.info('Initializing activator')
88 activator = cmactivator.CMActivator(parser.get_activator_workers())
90 # initialize activator rmq handler
91 if not parser.get_disable_remote_activation():
92 from cmframework.server import cmactivatermqhandler
93 logging.info('Initializing activator rmq handler')
94 activatermqhandler = cmactivatermqhandler.CMActivateRMQHandler(parser.get_rmq_ip(),
95 parser.get_rmq_port())
96 activator.add_handler(activatermqhandler)
98 # initialize activator server handler
99 logging.info('Initializing activator server handler')
100 activateserverhandler = cmactivateserverhandler.CMActivateServerHandler(
101 parser.get_activators(), plugin_client, changemonitor, activationstate_handler)
102 activator.add_handler(activateserverhandler)
105 logging.info('Starting activation handling process')
108 # start alarm handler
109 logging.info('Starting alarm handler process')
110 alarmhandler = cmalarmhandler.AlarmHandler()
111 alarmhandler.set_library_impl(parser.get_alarmhandler_api())
114 # load snapshot handler
115 logging.info('Initializing snapshot handler')
116 snapshothandler_args = {}
117 snapshothandler_args['uri'] = parser.get_snapshot_handler_uri()
118 snapshot_handler = cmsnapshothandler.CMSnapshotHandler(
119 parser.get_snapshot_handler_api(), **snapshothandler_args)
121 # initialize processor
122 logging.info('Initializing CM processor')
123 processor = cmprocessor.CMProcessor(backend,
127 activationstate_handler,
130 if not parser.is_install_phase():
131 # generate inventory file
132 logging.info('Generate inventory file')
133 properties = backend.get_properties('.*')
134 inventory = AnsibleInventory(properties, parser.get_inventory_handlers())
135 inventory_data = inventory.generate_inventory()
136 with open(parser.get_inventory_data(), 'w') as inventory_file:
137 inventory_file.write(json.dumps(inventory_data, indent=4, sort_keys=True))
139 # publish full activate request to on-line activators
140 logging.info('Ask on-line activators to full translate')
141 processor.activate(startup_activation=True)
143 # remove inventory file
144 logging.info('Remove inventory file')
146 os.remove(parser.get_inventory_data())
150 # initialize rest api factory
151 logging.info('Initializing REST API factory')
152 base_url = 'http://' + parser.get_ip() + ':' + \
153 str(parser.get_port()) + '/cm/'
154 rest_api_factory = cmrestapifactory.CMRestAPIFactory(processor, base_url)
156 # initialize wsgi handler
157 logging.info('Initializing the WSGI handler')
158 wsgihandler = cmwsgihandler.CMWSGIHandler(rest_api_factory)
160 # start the http server
161 logging.info('Start listening to http requests')
162 wsgi.server(eventlet.listen((parser.get_ip(), parser.get_port())), wsgihandler)
163 except KeyboardInterrupt as exp:
164 logging.info('CM server shutting down')
166 except Exception as exp: # pylint: disable=broad-except
167 logging.error('Got exception %s', str(exp))
168 traceback.print_exc()
172 def _read_maskable_names():
173 MASK_DIR = '/etc/cmframework/masks.d/'
175 filenames = [MASK_DIR + f for f in os.listdir(MASK_DIR)]
176 for filename in filenames:
177 config = ConfigParser.SafeConfigParser()
178 config.read(filename)
179 names = json.loads(config.get('Passwords', 'names'))
180 all_names.extend(names)
184 if __name__ == "__main__":