def _process_response(self, response): report = self._get_response_report(response) if report is not None: self._report(report) return target = response.request.target try: parsed_data = json.loads(response.data) self._report( reports_booth.booth_config_accepted_by_node( target.label, list(parsed_data["saved"]))) for filename in list(parsed_data["existing"]): self._report( reports.file_already_exists( "", # TODO specify file type; this will be overhauled to # a generic file transport framework anyway filename, severity=(ReportItemSeverity.WARNING if self._rewrite_existing else ReportItemSeverity.ERROR), forceable=(None if self._rewrite_existing else report_codes.FORCE_FILE_OVERWRITE), node=target.label, )) for file, reason in dict(parsed_data["failed"]).items(): self._report( reports_booth.booth_config_distribution_node_error( target.label, reason, file)) except (KeyError, TypeError, ValueError): self._report(reports.invalid_response_format(target.label))
def pull_config(env, node_name): """ Get config from specified node and save it on local system. It will rewrite existing files. env -- LibraryEnvironment node_name -- string, name of node from which config should be fetched """ name = env.booth.name env.report_processor.process( booth_reports.booth_fetching_config_from_node_started(node_name, name)) com_cmd = BoothGetConfig(env.report_processor, name) com_cmd.set_targets( [env.get_node_target_factory().get_target_from_hostname(node_name)]) # pylint: disable=unsubscriptable-object # In general, pylint is right. And it cannot know in this case code is OK. # It is covered by tests. output = run_and_raise(env.get_node_communicator(), com_cmd)[0][1] try: env.booth.create_config(output["config"]["data"], True) if (output["authfile"]["name"] is not None and output["authfile"]["data"]): env.booth.set_key_path( os.path.join(settings.booth_config_dir, output["authfile"]["name"])) env.booth.create_key( base64.b64decode(output["authfile"]["data"].encode("utf-8")), True) env.report_processor.process( booth_reports.booth_config_accepted_by_node(name_list=[name])) except KeyError: raise LibraryError(reports.invalid_response_format(node_name))
def pull_config(env, node_name, name): """ Get config from specified node and save it on local system. It will rewrite existing files. env -- LibraryEnvironment node_name -- string, name of node from which config should be fetched name -- string, name of booth instance of which config should be fetched """ env.report_processor.process( booth_reports.booth_fetching_config_from_node_started(node_name, name)) output = sync.pull_config_from_node(env.node_communicator(), NodeAddresses(node_name), name) try: env.booth.create_config(output["config"]["data"], True) if (output["authfile"]["name"] is not None and output["authfile"]["data"]): env.booth.set_key_path( os.path.join(settings.booth_config_dir, output["authfile"]["name"])) env.booth.create_key( base64.b64decode(output["authfile"]["data"].encode("utf-8")), True) env.report_processor.process( booth_reports.booth_config_accepted_by_node(name_list=[name])) except KeyError: raise LibraryError(reports.invalid_response_format(node_name))
def test_node(self): self.assert_message_from_report( "node1: Booth configs 'another', 'some' saved", reports.booth_config_accepted_by_node( node="node1", name_list=["some", "another"], ))
def _set_config_on_node( communicator, reporter, node, name, config_data, authfile=None, authfile_data=None ): """ Set booth config for instance 'name' on specified node. communicator -- NodeCommunicator reporter -- report processor node -- NodeAddresses name -- name of booth instance config_data -- booth config as string authfile -- path to authfile authfile_data -- authfile content as bytes """ data = { "config": { "name": "{0}.conf".format(name), "data": config_data } } if authfile is not None and authfile_data is not None: data["authfile"] = { "name": os.path.basename(authfile), "data": base64.b64encode(authfile_data).decode("utf-8") } communicator.call_node( node, "remote/booth_set_config", NodeCommunicator.format_data_dict([("data_json", json.dumps(data))]) ) reporter.process(reports.booth_config_accepted_by_node(node.label, [name]))
def pull_config(env, node_name, name): """ Get config from specified node and save it on local system. It will rewrite existing files. env -- LibraryEnvironment node_name -- string, name of node from which config should be fetched name -- string, name of booth instance of which config should be fetched """ env.report_processor.process( booth_reports.booth_fetching_config_from_node_started(node_name, name) ) output = sync.pull_config_from_node( env.node_communicator(), NodeAddresses(node_name), name ) try: env.booth.create_config(output["config"]["data"], True) if ( output["authfile"]["name"] is not None and output["authfile"]["data"] ): env.booth.set_key_path(os.path.join( settings.booth_config_dir, output["authfile"]["name"] )) env.booth.create_key( base64.b64decode( output["authfile"]["data"].encode("utf-8") ), True ) env.report_processor.process( booth_reports.booth_config_accepted_by_node(name_list=[name]) ) except KeyError: raise LibraryError(reports.invalid_response_format(node_name))
def test_empty_name_list(self): self.assert_message_from_report( "Booth config saved", reports.booth_config_accepted_by_node( name_list=[] ) )
def test_multiple_names(self): self.assert_message_from_report( "Booth configs 'another', 'some' saved", reports.booth_config_accepted_by_node( name_list=["some", "another"], ) )
def test_single_name(self): self.assert_message_from_report( "Booth config 'some' saved", reports.booth_config_accepted_by_node( name_list=["some"] ) )
def pull_config(env, node_name, instance_name=None): """ Get config from specified node and save it on local system. It will rewrite existing files. LibraryEnvironment env string node_name -- name of the node from which the config should be fetched string instance_name -- booth instance name """ report_processor = SimpleReportProcessor(env.report_processor) booth_env = env.get_booth_env(instance_name) instance_name = booth_env.instance_name _ensure_live_env(env, booth_env) env.report_processor.process( booth_reports.booth_fetching_config_from_node_started( node_name, instance_name)) com_cmd = BoothGetConfig(env.report_processor, instance_name) com_cmd.set_targets( [env.get_node_target_factory().get_target_from_hostname(node_name)]) # pylint: disable=unsubscriptable-object # In general, pylint is right. And it cannot know in this case code is OK. # It is covered by tests. output = run_and_raise(env.get_node_communicator(), com_cmd)[0][1] try: # TODO adapt to new file transfer framework once it is written if (output["authfile"]["name"] is not None and output["authfile"]["data"]): authfile_name = output["authfile"]["name"] report_list = config_validators.check_instance_name(authfile_name) if report_list: raise LibraryError(*report_list) booth_key = FileInstance.for_booth_key(authfile_name) booth_key.write_raw(base64.b64decode( output["authfile"]["data"].encode("utf-8")), can_overwrite=True) booth_env.config.write_raw(output["config"]["data"].encode("utf-8"), can_overwrite=True) env.report_processor.process( booth_reports.booth_config_accepted_by_node( name_list=[instance_name])) except RawFileError as e: report_processor.report(raw_file_error_report(e)) except KeyError: raise LibraryError(reports.invalid_response_format(node_name)) if report_processor.has_errors: raise LibraryError()
def pull_config(env, node_name): """ Get config from specified node and save it on local system. It will rewrite existing files. env -- LibraryEnvironment node_name -- string, name of node from which config should be fetched """ name = env.booth.name env.report_processor.process( booth_reports.booth_fetching_config_from_node_started(node_name, name) ) com_cmd = BoothGetConfig(env.report_processor, name) com_cmd.set_targets([ env.get_node_target_factory().get_target_from_hostname(node_name) ]) # pylint: disable=unsubscriptable-object # In general, pylint is right. And it cannot know in this case code is OK. # It is covered by tests. output = run_and_raise(env.get_node_communicator(), com_cmd)[0][1] try: env.booth.create_config(output["config"]["data"], True) if ( output["authfile"]["name"] is not None and output["authfile"]["data"] ): env.booth.set_key_path(os.path.join( settings.booth_config_dir, output["authfile"]["name"] )) env.booth.create_key( base64.b64decode( output["authfile"]["data"].encode("utf-8") ), True ) env.report_processor.process( booth_reports.booth_config_accepted_by_node(name_list=[name]) ) except KeyError: raise LibraryError(reports.invalid_response_format(node_name))
def test_empty_name_list(self): self.assert_message_from_report( "Booth config saved", reports.booth_config_accepted_by_node(name_list=[]))
def test_single_name(self): self.assert_message_from_report( "Booth config 'some' saved", reports.booth_config_accepted_by_node(name_list=["some"]))
def send_all_config_to_node( communicator, reporter, target, rewrite_existing=False, skip_wrong_config=False ): """ Send all booth configs from default booth config directory and theri authfiles to specified node. communicator -- NodeCommunicator reporter -- report processor node -- NodeAddress rewrite_existing -- if True rewrite existing file skip_wrong_config -- if True skip local configs that are unreadable """ config_dict = booth_conf.read_configs(reporter, skip_wrong_config) if not config_dict: return reporter.process(reports.booth_config_distribution_started()) file_list = [] for config, config_data in sorted(config_dict.items()): try: authfile_path = config_structure.get_authfile( config_parser.parse(config_data) ) file_list.append({ "name": config, "data": config_data, "is_authfile": False }) if authfile_path: content = booth_conf.read_authfile(reporter, authfile_path) if not content: continue file_list.append({ "name": os.path.basename(authfile_path), "data": base64.b64encode(content).decode("utf-8"), "is_authfile": True }) except LibraryError: reporter.process(reports.booth_skipping_config( config, "unable to parse config" )) com_cmd = BoothSaveFiles( reporter, file_list, rewrite_existing=rewrite_existing ) com_cmd.set_targets([target]) response = run_and_raise(communicator, com_cmd)[0][1] try: report_list = [] for file in response["existing"]: report_list.append(lib_reports.file_already_exists( None, file, Severities.WARNING if rewrite_existing else Severities.ERROR, ( None if rewrite_existing else report_codes.FORCE_FILE_OVERWRITE ), target.label )) for file, reason in response["failed"].items(): report_list.append(reports.booth_config_distribution_node_error( target.label, reason, file )) reporter.process_list(report_list) reporter.process( reports.booth_config_accepted_by_node(target.label, response["saved"]) ) except (KeyError, ValueError): raise LibraryError(lib_reports.invalid_response_format(target.label))
def _get_success_report(self, node_label): return reports_booth.booth_config_accepted_by_node( node_label, [self._booth_name])
def test_name_booth_and_node(self): self.assert_message_from_report( "node1: Booth config saved", reports.booth_config_accepted_by_node(node="node1", name_list=["booth"]))
def test_defaults(self): self.assert_message_from_report( "Booth config saved", reports.booth_config_accepted_by_node())
def send_all_config_to_node( communicator, reporter, node, rewrite_existing=False, skip_wrong_config=False ): """ Send all booth configs from default booth config directory and theri authfiles to specified node. communicator -- NodeCommunicator reporter -- report processor node -- NodeAddress rewrite_existing -- if True rewrite existing file skip_wrong_config -- if True skip local configs that are unreadable """ config_dict = booth_conf.read_configs(reporter, skip_wrong_config) if not config_dict: return reporter.process(reports.booth_config_distribution_started()) file_list = [] for config, config_data in sorted(config_dict.items()): try: authfile_path = config_structure.get_authfile( config_parser.parse(config_data) ) file_list.append({ "name": config, "data": config_data, "is_authfile": False }) if authfile_path: content = booth_conf.read_authfile(reporter, authfile_path) if not content: continue file_list.append({ "name": os.path.basename(authfile_path), "data": base64.b64encode(content).decode("utf-8"), "is_authfile": True }) except LibraryError: reporter.process(reports.booth_skipping_config( config, "unable to parse config" )) data = [("data_json", json.dumps(file_list))] if rewrite_existing: data.append(("rewrite_existing", "1")) try: response = json.loads(communicator.call_node( node, "remote/booth_save_files", NodeCommunicator.format_data_dict(data) )) report_list = [] for file in response["existing"]: report_list.append(lib_reports.file_already_exists( None, file, Severities.WARNING if rewrite_existing else Severities.ERROR, ( None if rewrite_existing else report_codes.FORCE_FILE_OVERWRITE ), node.label )) for file, reason in response["failed"].items(): report_list.append(reports.booth_config_distribution_node_error( node.label, reason, file )) reporter.process_list(report_list) reporter.process( reports.booth_config_accepted_by_node(node.label, response["saved"]) ) except NodeCommunicationException as e: raise LibraryError(node_communicator_exception_to_report_item(e)) except (KeyError, ValueError): raise LibraryError(lib_reports.invalid_response_format(node.label))