| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | ## |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | ## |
| 18 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 19 | import asyncio |
| 20 | import getopt |
| 21 | import logging |
| 22 | import logging.handlers |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 23 | import os |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 24 | import sys |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 25 | from os import path |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 26 | |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 27 | import yaml |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 28 | from osm_common.dbbase import DbException |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 29 | from osm_common.temporal_task_queues.task_queues_mappings import LCM_TASK_QUEUE |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 30 | from osm_lcm.data_utils.database.database import Database |
| 31 | from osm_lcm.data_utils.lcm_config import LcmCfg |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 32 | from osm_lcm.lcm_utils import LcmException |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 33 | from osm_lcm.temporal.juju_paas_activities import ( |
| 34 | JujuPaasConnector, |
| 35 | CreateModelImpl, |
| 36 | CheckCharmStatusImpl, |
| 37 | DeployCharmImpl, |
| 38 | TestVimConnectivityImpl, |
| 39 | ) |
| 40 | |
| 41 | from osm_lcm.temporal.lcm_activities import NsLcmNoOpImpl, UpdateNsLcmOperationStateImpl |
| 42 | from osm_lcm.temporal.lcm_workflows import NsNoOpWorkflowImpl |
| 43 | from osm_lcm.temporal.ns_activities import ( |
| 44 | GetVnfDetailsImpl, |
| 45 | GetNsRecordImpl, |
| 46 | UpdateNsStateImpl, |
| 47 | ) |
| 48 | from osm_lcm.temporal.ns_workflows import NsInstantiateWorkflowImpl |
| 49 | from osm_lcm.temporal.vdu_workflows import VduInstantiateWorkflowImpl |
| 50 | from osm_lcm.temporal.vim_activities import ( |
| 51 | UpdateVimStateImpl, |
| 52 | UpdateVimOperationStateImpl, |
| 53 | DeleteVimRecordImpl, |
| 54 | ) |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 55 | from osm_lcm.temporal.vim_workflows import ( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 56 | VimCreateWorkflowImpl, |
| 57 | VimDeleteWorkflowImpl, |
| 58 | VimUpdateWorkflowImpl, |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 59 | ) |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 60 | from osm_lcm.temporal.vnf_activities import ( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 61 | GetTaskQueueImpl, |
| 62 | GetVnfDescriptorImpl, |
| 63 | GetVnfRecordImpl, |
| 64 | GetVimCloudImpl, |
| 65 | ChangeVnfStateImpl, |
| 66 | SetVnfModelImpl, |
| 67 | ChangeVnfInstantiationStateImpl, |
| 68 | SendNotificationForVnfImpl, |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 69 | ) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 70 | from osm_lcm.temporal.vnf_workflows import ( |
| 71 | VnfInstantiateWorkflowImpl, |
| 72 | VnfPrepareWorkflowImpl, |
| 73 | ) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 74 | from temporalio.client import Client |
| 75 | from temporalio.worker import Worker |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 76 | |
| 77 | |
| 78 | class NGLcm: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 79 | main_config = LcmCfg() |
| 80 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 81 | def __init__(self, config_file): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 82 | """ |
| 83 | Init, Connect to database, filesystem storage, and messaging |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 84 | :param config_file: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 85 | :return: None |
| 86 | """ |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 87 | self.db: Database = None |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 88 | self.logger = logging.getLogger("lcm") |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 89 | self._load_configuration(config_file) |
| 90 | self._configure_logging() |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 91 | |
| 92 | try: |
| 93 | self.db = Database(self.main_config.to_dict()).instance.db |
| Mark Beierl | 90f700d | 2023-02-09 15:01:33 -0500 | [diff] [blame] | 94 | except DbException as e: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 95 | self.logger.critical(str(e), exc_info=True) |
| 96 | raise LcmException(str(e)) |
| 97 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 98 | def _load_configuration(self, config_file): |
| 99 | config = self._read_config_file(config_file) |
| 100 | self.main_config.set_from_dict(config) |
| 101 | self.main_config.transform() |
| 102 | self.main_config.load_from_env() |
| 103 | self.logger.critical("Loaded configuration:" + str(self.main_config.to_dict())) |
| 104 | |
| 105 | def _read_config_file(self, config_file): |
| 106 | try: |
| 107 | with open(config_file) as f: |
| 108 | return yaml.safe_load(f) |
| 109 | except Exception as e: |
| 110 | self.logger.critical("At config file '{}': {}".format(config_file, e)) |
| 111 | exit(1) |
| 112 | |
| 113 | @staticmethod |
| 114 | def _get_log_formatter_simple(): |
| 115 | log_format_simple = ( |
| 116 | "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" |
| 117 | ) |
| 118 | return logging.Formatter(log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S") |
| 119 | |
| 120 | def _create_file_handler(self): |
| 121 | return logging.handlers.RotatingFileHandler( |
| 122 | self.main_config.globalConfig.logfile, |
| 123 | maxBytes=100e6, |
| 124 | backupCount=9, |
| 125 | delay=0, |
| 126 | ) |
| 127 | |
| 128 | def _log_other_modules(self): |
| 129 | for logger in ("message", "database", "storage", "tsdb", "temporal"): |
| 130 | logger_config = self.main_config.to_dict()[logger] |
| 131 | logger_module = logging.getLogger(logger_config["logger_name"]) |
| 132 | if logger_config["logfile"]: |
| 133 | file_handler = logging.handlers.RotatingFileHandler( |
| 134 | logger_config["logfile"], maxBytes=100e6, backupCount=9, delay=0 |
| 135 | ) |
| 136 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 137 | logger_module.addHandler(file_handler) |
| 138 | if logger_config["loglevel"]: |
| 139 | logger_module.setLevel(logger_config["loglevel"]) |
| Mark Beierl | dceed59 | 2023-04-11 21:03:56 +0000 | [diff] [blame] | 140 | logging.getLogger("juju.client.connection").setLevel(logging.CRITICAL) |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 141 | |
| 142 | def _configure_logging(self): |
| 143 | if self.main_config.globalConfig.logfile: |
| 144 | file_handler = self._create_file_handler() |
| 145 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 146 | self.logger.addHandler(file_handler) |
| 147 | |
| 148 | if not self.main_config.globalConfig.to_dict()["nologging"]: |
| 149 | str_handler = logging.StreamHandler() |
| 150 | str_handler.setFormatter(self._get_log_formatter_simple()) |
| 151 | self.logger.addHandler(str_handler) |
| 152 | |
| 153 | if self.main_config.globalConfig.to_dict()["loglevel"]: |
| 154 | self.logger.setLevel(self.main_config.globalConfig.loglevel) |
| 155 | |
| 156 | self._log_other_modules() |
| 157 | self.logger.critical("starting osm/nglcm") |
| 158 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 159 | async def start(self): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 160 | temporal_api = ( |
| 161 | f"{self.main_config.temporal.host}:{str(self.main_config.temporal.port)}" |
| 162 | ) |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 163 | client = await Client.connect(temporal_api) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 164 | |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 165 | paas_connector_instance = JujuPaasConnector(self.db) |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 166 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 167 | workflows = [ |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 168 | NsInstantiateWorkflowImpl, |
| 169 | NsNoOpWorkflowImpl, |
| 170 | VimCreateWorkflowImpl, |
| 171 | VimDeleteWorkflowImpl, |
| 172 | VimUpdateWorkflowImpl, |
| 173 | VduInstantiateWorkflowImpl, |
| 174 | VnfInstantiateWorkflowImpl, |
| 175 | VnfPrepareWorkflowImpl, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 176 | ] |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 177 | activities = [ |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame^] | 178 | UpdateNsStateImpl(self.db).__call__, |
| 179 | GetVnfDetailsImpl(self.db).__call__, |
| 180 | GetNsRecordImpl(self.db).__call__, |
| 181 | UpdateNsLcmOperationStateImpl(self.db).__call__, |
| 182 | NsLcmNoOpImpl().__call__, |
| 183 | CreateModelImpl(self.db, paas_connector_instance).__call__, |
| 184 | DeployCharmImpl(paas_connector_instance).__call__, |
| 185 | CheckCharmStatusImpl(paas_connector_instance).__call__, |
| 186 | TestVimConnectivityImpl(paas_connector_instance).__call__, |
| 187 | UpdateVimOperationStateImpl(self.db).__call__, |
| 188 | UpdateVimStateImpl(self.db).__call__, |
| 189 | DeleteVimRecordImpl(self.db).__call__, |
| 190 | ChangeVnfStateImpl(self.db).__call__, |
| 191 | ChangeVnfInstantiationStateImpl(self.db).__call__, |
| 192 | GetTaskQueueImpl(self.db).__call__, |
| 193 | GetVimCloudImpl(self.db).__call__, |
| 194 | GetVnfDescriptorImpl(self.db).__call__, |
| 195 | GetVnfRecordImpl(self.db).__call__, |
| 196 | SendNotificationForVnfImpl().__call__, |
| 197 | SetVnfModelImpl(self.db).__call__, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 198 | ] |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 199 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 200 | # Check if we are running under a debugger |
| 201 | debug = os.getenv("VSCODE_IPC_HOOK_CLI") is not None |
| 202 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 203 | worker = Worker( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 204 | client, |
| 205 | task_queue=LCM_TASK_QUEUE, |
| 206 | workflows=workflows, |
| 207 | activities=activities, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 208 | debug_mode=debug, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 209 | ) |
| 210 | |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 211 | self.logger.info("Starting LCM temporal worker") |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 212 | await worker.run() |
| 213 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 214 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 215 | if __name__ == "__main__": |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 216 | try: |
| 217 | opts, args = getopt.getopt( |
| 218 | sys.argv[1:], "hc:", ["config=", "help", "health-check"] |
| 219 | ) |
| 220 | # TODO add "log-socket-host=", "log-socket-port=", "log-file=" |
| 221 | config_file = None |
| 222 | for o, a in opts: |
| 223 | if o in ("-c", "--config"): |
| 224 | config_file = a |
| 225 | else: |
| 226 | assert False, "Unhandled option" |
| 227 | |
| 228 | if config_file: |
| 229 | if not path.isfile(config_file): |
| 230 | print( |
| 231 | "configuration file '{}' does not exist".format(config_file), |
| 232 | file=sys.stderr, |
| 233 | ) |
| 234 | exit(1) |
| 235 | else: |
| 236 | for config_file in ( |
| 237 | __file__[: __file__.rfind(".")] + ".cfg", |
| 238 | "./lcm.cfg", |
| 239 | "/etc/osm/lcm.cfg", |
| 240 | ): |
| 241 | print(f"{config_file}") |
| 242 | if path.isfile(config_file): |
| 243 | break |
| 244 | else: |
| 245 | print( |
| 246 | "No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", |
| 247 | file=sys.stderr, |
| 248 | ) |
| 249 | exit(1) |
| 250 | lcm = NGLcm(config_file) |
| 251 | asyncio.run(lcm.start()) |
| 252 | except (LcmException, getopt.GetoptError) as e: |
| 253 | print(str(e), file=sys.stderr) |
| 254 | # usage() |
| 255 | exit(1) |