def run( ctx, mailgun_api_key, auth, password, keystore_file, scenario_file, notify_tasks, enable_ui, password_file, ): scenario_file = Path(scenario_file.name).absolute() data_path = ctx.obj["data_path"] chain_rpc_urls = ctx.obj["chain_rpc_urls"] log_file_name = construct_log_file_name("run", data_path, scenario_file) configure_logging_for_subcommand(log_file_name) password = get_password(password, password_file) account = get_account(keystore_file, password) notify_tasks_callable = None if notify_tasks is TaskNotifyType.ROCKETCHAT: if "RC_WEBHOOK_URL" not in os.environ: click.secho( "'--notify-tasks rocket-chat' requires env variable 'RC_WEBHOOK_URL' to be set.", fg="red", ) notify_tasks_callable = post_task_state_to_rc log_buffer = None # If the output is a terminal, beautify our output. if enable_ui: log_buffer = attach_urwid_logbuffer() # Dynamically import valid Task classes from sceanrio_player.tasks package. collect_tasks(tasks) # Start our Services service_process = ServiceProcess() service_process.start() # Run the scenario using the configurations passed. try: runner = ScenarioRunner(account, chain_rpc_urls, auth, data_path, scenario_file, notify_tasks_callable) except Exception as e: # log anything that goes wrong during init of the runner and isnt handled. log.exception(e) raise ui = None ui_greenlet = None if enable_ui: ui = ScenarioUI(runner, log_buffer, log_file_name) ui_greenlet = ui.run() success = False try: try: runner.run_scenario() except ScenarioAssertionError as ex: log.error("Run finished", result="assertion errors") send_notification_mail( runner.yaml.settings.notify, f"Assertion mismatch in {scenario_file.name}", str(ex), mailgun_api_key, ) except ScenarioError: log.exception("Run finished", result="scenario error") send_notification_mail( runner.yaml.settings.notify, f"Invalid scenario {scenario_file.name}", traceback.format_exc(), mailgun_api_key, ) else: success = True log.info("Run finished", result="success") send_notification_mail( runner.yaml.settings.notify, f"Scenario successful {scenario_file.name}", "Success", mailgun_api_key, ) except Exception: log.exception("Exception while running scenario") send_notification_mail( runner.yaml.settings.notify, f"Error running scenario {scenario_file.name}", traceback.format_exc(), mailgun_api_key, ) finally: try: if enable_ui and ui: ui.set_success(success) log.warning("Press q to exit") while not ui_greenlet.dead: gevent.sleep(1) service_process.stop() except ServiceProcessException: service_process.kill() finally: runner.node_controller.stop() if ui_greenlet is not None and not ui_greenlet.dead: ui_greenlet.kill(ExitMainLoop) ui_greenlet.join()
def run_( data_path: Path, auth: str, password: Optional[str], keystore_file: str, scenario_file: Path, enable_ui: bool, password_file: str, log_file_name: str, environment: EnvironmentConfig, delete_snapshots: bool, raiden_client: Optional[str], smoketest_deployment_data=None, ) -> None: """Execute a scenario as defined in scenario definition file. (Shared code for `run` and `smoketest` command). Calls :func:`exit` when done, with the following status codes: Exit code 1x There was a problem when starting up the SP, nodes, deploying tokens or setting up services. This points at an issue in the SP and of of its components. Exit code 2x There was an error when parsing or evaluating the given scenario definition file. This may be a syntax- or logic-related issue. Exit code 3x There was an assertion error while executing the scenario. This points to an error in a `raiden` component (the client, services or contracts). """ log.info("Scenario Player version:", version_info=get_complete_spec()) password = get_password(password, password_file) account = get_account(keystore_file, password) log_buffer = None if enable_ui: log_buffer = attach_urwid_logbuffer() # Dynamically import valid Task classes from scenario_player.tasks package. collect_tasks(tasks) # Start our Services report: Dict[str, str] = {} success = Event() success.clear() try: # We need to fix the log stream early in case the UI is active scenario_runner = ScenarioRunner( account=account, auth=auth, data_path=data_path, scenario_file=scenario_file, environment=environment, success=success, smoketest_deployment_data=smoketest_deployment_data, delete_snapshots=delete_snapshots, raiden_client=raiden_client, ) if enable_ui: ui: AbstractContextManager = ScenarioUIManager( scenario_runner, log_buffer, log_file_name, success ) else: ui = nullcontext() log.info("Startup complete") with ui: scenario_runner.run_scenario() except ScenarioAssertionError as ex: log.error("Run finished", result="assertion errors") if hasattr(ex, "exit_code"): exit_code = ex.exit_code else: exit_code = 30 report.update(dict(subject=f"Assertion mismatch in {scenario_file.name}", message=str(ex))) exit(exit_code) except ScenarioError as ex: log.error("Run finished", result="scenario error", message=str(ex)) if hasattr(ex, "exit_code"): exit_code = ex.exit_code else: exit_code = 20 report.update( dict( subject=f"Invalid scenario {scenario_file.name}", message=traceback.format_exc(), ) ) exit(exit_code) except Exception as ex: log.exception("Exception while running scenario") if hasattr(ex, "exit_code"): exit_code = ex.exit_code # type: ignore # pylint: disable=no-member else: exit_code = 10 report.update( dict( subject=f"Error running scenario {scenario_file.name}", message=traceback.format_exc(), ) ) exit(exit_code) else: exit_code = 0 log.info("Run finished", result="success") report.update(dict(subject=f"Scenario successful {scenario_file.name}", message="Success")) log.info("Scenario player unwind complete") exit(exit_code)
def run( ctx, chain, data_path, mailgun_api_key, auth, password, keystore_file, scenario_file, notify_tasks, enable_ui, password_file, ): """Execute a scenario as defined in scenario definition file. Calls :func:`exit` when done, with the following status codes: Exit code 1x There was a problem when starting up the SP, nodes, deploying tokens or setting up services. This points at an issue in the SP and of of its components. Exit code 2x There was an error when parsing or evaluating the given scenario definition file. This may be a syntax- or logic-related issue. Exit code 3x There was an assertion error while executing the scenario. This points to an error in a `raiden` component (the client, services or contracts). """ data_path = Path(data_path) scenario_file = Path(scenario_file.name).absolute() log_file_name = construct_log_file_name("run", data_path, scenario_file) configure_logging_for_subcommand(log_file_name) log.info("Scenario Player version:", version_info=get_complete_spec()) password = get_password(password, password_file) account = get_account(keystore_file, password) notify_tasks_callable = None if notify_tasks is TaskNotifyType.ROCKETCHAT: if "RC_WEBHOOK_URL" not in os.environ: click.secho( "'--notify-tasks rocket-chat' requires env variable 'RC_WEBHOOK_URL' to be set.", fg="red", ) notify_tasks_callable = post_task_state_to_rc log_buffer = None # If the output is a terminal, beautify our output. if enable_ui: log_buffer = attach_urwid_logbuffer() # Dynamically import valid Task classes from sceanrio_player.tasks package. collect_tasks(tasks) # Start our Services service_process = ServiceProcess() service_process.start() # Run the scenario using the configurations passed. try: runner = ScenarioRunner(account, auth, chain, data_path, scenario_file, notify_tasks_callable) except Exception as e: # log anything that goes wrong during init of the runner and isn't handled. log.exception("Error during startup", exception=e) raise ui = None ui_greenlet = None if enable_ui: ui = ScenarioUI(runner, log_buffer, log_file_name) ui_greenlet = ui.run() success = False exit_code = 1 subject = None message = None try: runner.run_scenario() except ScenarioAssertionError as ex: log.error("Run finished", result="assertion errors") if hasattr(ex, "exit_code"): exit_code = ex.exit_code else: exit_code = 30 subject = f"Assertion mismatch in {scenario_file.name}" message = str(ex) except ScenarioError as ex: log.error("Run finished", result="scenario error", message=str(ex)) if hasattr(ex, "exit_code"): exit_code = ex.exit_code else: exit_code = 20 subject = f"Invalid scenario {scenario_file.name}" message = traceback.format_exc() except Exception as ex: log.exception("Exception while running scenario") if hasattr(ex, "exit_code"): exit_code = ex.exit_code else: exit_code = 10 subject = f"Error running scenario {scenario_file.name}" message = traceback.format_exc() else: success = True exit_code = 0 log.info("Run finished", result="success") subject = f"Scenario successful {scenario_file.name}" message = "Success" finally: send_notification_mail( runner.definition.settings.notify, subject or "Logic error in main.py", message or "Message should not be empty.", mailgun_api_key, ) try: if enable_ui and ui: ui.set_success(success) log.warning("Press q to exit") while not ui_greenlet.dead: gevent.sleep(1) service_process.stop() except ServiceProcessException: service_process.kill() finally: runner.node_controller.stop() if ui_greenlet is not None and not ui_greenlet.dead: ui_greenlet.kill(ExitMainLoop) ui_greenlet.join() exit(exit_code)