1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Config executor library."""
16 from importlib import import_module
17 from logging.config import dictConfig
18 from logging import getLogger
21 from .parser import Parser
22 from .serializer import Serializer
27 Executor class reponsible for executing configuration.
29 def __init__(self, configuration_file):
33 :param configuration_file: Telemetry configuration file path.
34 :type configuration_file: str
36 self.parser = Parser(configuration_file)
37 self.log = self.parser.config[u"logging"]
38 self.programs = self.parser.config[u"programs"]
39 self.scheduler = self.parser.config[u"scheduler"]
43 def execute(self, hook=None):
45 Main executor function will run programs from all bundles in a loop.
53 :param hook: Process ID or socket to attach. None by default.
56 for program in self.programs:
57 serializer = Serializer()
59 package = program[u"name"]
60 name = f"telemetry.{package}"
61 package = package.replace("_", " ").title().replace(" ", "")
62 module = import_module(
66 bundle = getattr(module, package)(
68 serializer=serializer,
71 bundle.attach(duration=self.scheduler[u"duration"])
75 except (ImportError, AttributeError) as exc:
77 f"Error executing bundle {package!r}! - {exc}"
81 def execute_daemon(self, hook=None):
83 Daemon executor will execute endless loop.
85 :param hook: Process ID to attach. None by default.
89 self.execute(hook=hook)
92 class ExecutorError(Exception):
94 Creates a Executor Error Exception. This exception is supposed to handle
95 all the errors raised during executing.
97 def __init__(self, message):
99 Execute Error Excpetion init.
101 :param message: Exception error message.
105 self.message = message
106 getLogger(__name__).error(message)