class CLIRunner: def __init__(self): self.experiment = None self.topology = None self.experiment_config = {} self.environments = Environments() self.broker_interface = BrokerInterface() self.cmds = { "load": self.load, "start": self.start, "stop": self.stop, "install": self.install, "uninstall": self.uninstall, "begin": self.begin, "end": self.end, } self._status = { "load": False, "start": False, "stop": False, "install": False, "uninstall": False, "begin": False, "end": False, } logger.info("CLIRunner init") def get_cmds(self): return list(self.cmds.keys()) def filepath(self, name): filepath = os.path.normpath(os.path.join(os.path.dirname(__file__), name)) return filepath def load_file(self, filename): filepath = self.filepath(filename) data = {} error = "" try: with open(filepath, "+r") as fp: data = json.load(fp) except Exception as e: error = f"Load file error: {repr(e)}" logger.debug(error) else: logger.debug(f"Load file ok") finally: return data, error def load(self, filename): logger.info(f"Load triggered - filename {filename}") ack = True print_cli(f"Loading configuration file at {filename}") data, error = self.load_file(filename) if error: msg = "Configuration not loaded - " + error print_cli(None, err=msg, style="error") else: self.experiment = Experiment("") ack = self.experiment.parse(data) if ack: self.topology = self.experiment.get_topology() self.environments.generate_env_cfgs(self.topology) msg = "Configuration loaded" print_cli(msg, style="normal") else: msg = "Configuration not loaded - Error parsing scenario data" print_cli(None, err=msg, style="error") self._status["load"] = ack logger.info(f"{msg}") return msg async def start(self): logger.info(f"Start triggered") print_cli(f"Starting", style="attention") ack, messages = self.environments.implement_env_cfgs("start") self._status["start"] = ack logger.info(f"{messages}") return ack, messages async def stop(self): logger.info(f"Stop triggered") print_cli(f"Stopping", style="attention") ack, messages = self.environments.implement_env_cfgs("stop") self._status["start"] = not ack self._status["stop"] = ack logger.info(f"{messages}") return messages async def install(self): logger.info(f"install triggered") print_cli(f"Installing", style="attention") ack, messages = self.environments.implement_env_cfgs("install") self._status["install"] = ack logger.info(f"{messages}") return ack, messages async def uninstall(self): logger.info(f"uninstall triggered") print_cli(f"Uninstalling", style="attention") ack, messages = self.environments.implement_env_cfgs("uninstall") self._status["install"] = not ack self._status["uninstall"] = ack logger.info(f"{messages}") return messages async def begin(self): logger.info(f"begin triggered") print_cli(f"Beginning", style="attention") default_env = self.topology.get_default_environment() default_env_components = default_env.get("components") broker_env = default_env_components.get("broker") print_cli(f"Experiment Begin", style="info") scenario = self.experiment.dump() reply, error = await self.broker_interface.begin(broker_env, scenario) ack = False if error else True self._status["begin"] = ack if ack: print_cli(f"Umbra Experiment Ok", style="normal") messages = reply else: print_cli(f"Umbra Experiment Error", style="error") messages = error logger.info(f"{messages}") return ack, messages async def end(self): logger.info(f"end triggered") print_cli(f"Ending", style="attention") default_env = self.topology.get_default_environment() default_env_components = default_env.get("components") broker_env = default_env_components.get("broker") print_cli(f"Experiment End", style="info") scenario = self.experiment.dump() reply, error = await self.broker_interface.end(broker_env, scenario) ack = False if error else True self._status["end"] = ack self._status["begin"] = not ack if ack: print_cli(f"Ended Umbra Experiment", style="normal") messages = reply else: print_cli(f"Ended Umbra Experiment Error", style="error") messages = error logger.info(f"{messages}") return ack, messages def status(self, command): ack = False error = "" if command == "load": ack = not self._status["start"] if not ack: error = "Cannot load - config started - stop it first" if command == "start": ack = self._status["load"] and not self._status["start"] if not ack: error = "Cannot start - config not loaded or config started" if command == "stop": ack = self._status["start"] and not self._status["stop"] if not ack: error = "Cannot stop - config not started or config stopped" if command == "install": pass if command == "uninstall": pass if command == "begin": pass if command == "end": pass return True, error async def execute(self, cmds): cmd = cmds[0] logger.info(f"Executing commands: {cmds}") ok, error = self.status(cmd) if ok: available_cmds = list(self.cmds.keys()) if cmd == "load": if len(cmds) == 2: config_filename = cmds[1] output = self.load(config_filename) return output else: return "Missing config filepath" if cmd in available_cmds: func = self.cmds.get(cmd) output = await func() return output else: output = f"Command not found in {available_cmds}" return output else: return error
class Operator: def __init__(self, info): self.info = info self.experiment = None self.topology = None self.plugins = {} self.events_handler = Handler() self.events_fabric = FabricEvents() self.events_iroha = IrohaEvents() self.events_scenario = ScenarioEvents() self.events_results = {} def parse_bytes(self, msg): msg_dict = {} if type(msg) is bytes: msg_str = msg.decode("utf-8") if msg_str: msg_dict = json.loads(msg_str) return msg_dict def serialize_bytes(self, msg): msg_bytes = b"" if type(msg) is dict: msg_str = json.dumps(msg) msg_bytes = msg_str.encode("utf-8") return msg_bytes async def call_monitor(self, address, data): logger.info(f"Calling Monitor - {address}") directrix = json_format.ParseDict(data, Directrix()) host, port = address.split(":") try: channel = Channel(host, port) stub = MonitorStub(channel) status = await stub.Measure(directrix) except Exception as e: ack = False info = repr(e) logger.info(f"Error - monitor failed - {info}") else: if status.error: ack = False logger.info(f"Monitor error: {status.error}") info = status.error else: ack = True if status.info: info = self.parse_bytes(status.info) else: info = {} logger.info(f"Monitor info: {info}") finally: channel.close() return ack, info def get_monitor_env_address(self, env): envs = self.topology.get_environments() env_data = envs.get(env) env_components = env_data.get("components") env_monitor_component = env_components.get("monitor") env_monitor_address = env_monitor_component.get("address") return env_monitor_address def build_monitor_directrix(self, env, info, action): if action == "start": hosts = info.get("topology").get("hosts") targets = repr(set(hosts.keys())) else: targets = repr(set()) data = { "action": action, "flush": { "live": True, "environment": env, "address": self.info.get("address"), }, "sources": [ { "id": 1, "name": "container", "parameters": { "targets": targets, "duration": "3600", "interval": "5", }, "schedule": {}, }, { "id": 2, "name": "host", "parameters": { "duration": "3600", "interval": "5", }, "schedule": {}, }, ], } return data async def call_monitors(self, stats, action): logger.info(f"Call monitors") all_acks = {} for env, info in stats.items(): data = self.build_monitor_directrix(env, info, action) address = self.get_monitor_env_address(env) ack, info = await self.call_monitor(address, data) all_acks[env] = ack all_monitors_ack = all(all_acks.values()) logger.info( f"Call monitors - action {action} - status: {all_monitors_ack}") return all_monitors_ack async def call_scenario(self, uid, action, topology, address): logger.info(f"Calling Experiment - {action}") scenario = self.serialize_bytes(topology) deploy = Workflow(id=uid, action=action, scenario=scenario) deploy.timestamp.FromDatetime(datetime.now()) host, port = address.split(":") try: channel = Channel(host, port) stub = ScenarioStub(channel) status = await stub.Establish(deploy) except Exception as e: ack = False info = repr(e) logger.info( f"Error - deploy topology in environment failed - exceptio {info}" ) else: if status.error: ack = False logger.info(f"Experiment not deployed error: {status.error}") info = status.error else: ack = True if status.info: info = self.parse_bytes(status.info) else: info = {} logger.info(f"Experiment info: {info}") finally: channel.close() return ack, info async def call_scenarios(self, uid, topology, action): envs = topology.get_environments() topo_envs = topology.build_environments() logger.info(f"Calling scenarios - {action}") logger.info(f"Environment scenarios - {envs}") logger.debug(f"Environment topologies - {topo_envs}") acks = {} envs_topo_info = {} for env in topo_envs: if env in envs: env_data = envs.get(env) env_components = env_data.get("components") scenario_component = env_components.get("scenario") env_address = scenario_component.get("address") env_topo = topo_envs.get(env) ack, topo_info = await self.call_scenario( uid, action, env_topo, env_address) acks[env] = ack envs_topo_info[env] = topo_info if all(acks.values()): logger.info(f"All environment scenarios deployed - {acks}") else: logger.info(f"Environment scenarios error - {acks}") return acks, envs_topo_info def load(self, scenario_message): try: scenario = self.parse_bytes(scenario_message) self.experiment = Experiment("tmp") self.experiment.parse(scenario) topology = self.experiment.get_topology() topology.build() self.topology = topology ack = True except Exception as e: logger.info(f"Could not load scenario - exception {repr(e)}") ack = False finally: return ack async def start(self, uid): topology = self.experiment.get_topology() acks, stats = await self.call_scenarios(uid, topology, "start") info, error = {}, {} if all(acks.values()): all_monitors_ack = await self.call_monitors(stats, "start") info = stats else: error = stats return info, error async def stop(self, uid): topology = self.experiment.get_topology() acks, stats = await self.call_scenarios(uid, topology, "stop") info, error = {}, {} if all(acks.values()): all_monitors_ack = await self.call_monitors(stats, "stop") info = stats else: error = stats return info, error def build_report(self, uid, info, error): info_msg = self.serialize_bytes(info) error_msg = self.serialize_bytes(error) report = Report(id=uid, info=info_msg, error=error_msg) return report async def execute(self, config): uid = config.id action = config.action scenario = config.scenario if self.load(scenario): info, error = {}, {} if action == "start": info, error = await self.start(uid) if not error: await self.call_events(info) elif action == "stop": info, error = await self.stop(uid) else: error = { "Execution error": f"Unkown action ({action}) to execute config" } report = self.build_report(uid, info, error) else: error_msg = "scenario could not be parsed/loaded" report = Report(id=config.id, error=error_msg) return report def config_plugins(self): logger.info("Configuring Umbra plugins") model = self.topology.get_model() umbra = self.topology.get_umbra() umbra_model_cfgs = umbra.get(model) if model == "fabric": logger.info("Configuring Fabric plugin") settings = umbra_model_cfgs.get("settings") configtx = umbra_model_cfgs.get("configtx") configsdk = umbra_model_cfgs.get("configsdk") chaincode = umbra_model_cfgs.get("chaincode") ack_fabric = self.events_fabric.config(settings, configsdk, chaincode, configtx) if ack_fabric: self.plugins["fabric"] = self.events_fabric if model == "iroha": self.events_iroha.config(umbra_model_cfgs) self.plugins["iroha"] = self.events_iroha self.events_scenario.config(self.topology) self.plugins["scenario"] = self.events_scenario async def handle_events(self, events): events_calls = {} for evs in events.values(): evs_formatted = {ev_id: ev for ev_id, ev in evs.items()} events_calls.update(evs_formatted) self.events_results = await self.events_handler.run(events_calls) def schedule_plugins(self): sched_events = {} for name, plugin in self.plugins.items(): logger.info("Scheduling plugin %s events", name) events = self.experiment.events.get_by_category(name) logger.info(f"Scheduling {len(events)} events: {events}") plugin_sched_evs = plugin.schedule(events) sched_events[plugin] = plugin_sched_evs return sched_events async def call_events(self, info_deploy): logger.info("Scheduling events") # info_topology = info_deploy.get("topology") # info_hosts = info_deploy.get("hosts") # topo = self.experiment.get_topology() # topo.fill_config(info_topology) # topo.fill_hosts_config(info_hosts) # self.topology = topo self.config_plugins() sched_events = self.schedule_plugins() # await self.handle_events(sched_events) coro_events = self.handle_events(sched_events) asyncio.create_task(coro_events)