Exemplo n.º 1
0
    def __init__(self, instance_name, booth_files_data):
        """
        Create a new BoothEnv

        string|None instance_name -- booth instance name
        dict booth_files_data -- ghost files (config_data, key_data, key_path)
        """
        if (
            "config_data" in booth_files_data
            and "key_data" not in booth_files_data
        ):
            raise LibraryError(
                ReportItem.error(
                    reports.messages.LiveEnvironmentNotConsistent(
                        [file_type_codes.BOOTH_CONFIG],
                        [file_type_codes.BOOTH_KEY],
                    )
                )
            )
        if (
            "config_data" not in booth_files_data
            and "key_data" in booth_files_data
        ):
            raise LibraryError(
                ReportItem.error(
                    reports.messages.LiveEnvironmentNotConsistent(
                        [file_type_codes.BOOTH_KEY],
                        [file_type_codes.BOOTH_CONFIG],
                    )
                )
            )

        self._instance_name = instance_name or constants.DEFAULT_INSTANCE_NAME
        report_list = config_validators.check_instance_name(self._instance_name)
        if report_list:
            raise LibraryError(*report_list)

        self._config_file = FileInstance.for_booth_config(
            f"{self._instance_name}.conf",
            **self._init_file_data(booth_files_data, "config_data"),
        )
        self._key_file = FileInstance.for_booth_key(
            f"{self._instance_name}.key",
            **self._init_file_data(booth_files_data, "key_data"),
        )
        if isinstance(self._key_file.raw_file, raw_file.GhostFile):
            self._key_path = booth_files_data.get("key_path", "")
        else:
            self._key_path = self._key_file.raw_file.metadata.path
Exemplo n.º 2
0
def send_all_config_to_node(
    communicator,
    reporter,
    target_list,
    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
    target_list list -- list of targets to which configs should be delivered
    rewrite_existing -- if True rewrite existing file
    skip_wrong_config -- if True skip local configs that are unreadable
    """
    # TODO adapt to new file transfer framework once it is written
    # TODO the function is not modular enough - it raises LibraryError

    file_list = []
    for conf_file_name in sorted(config_files.get_all_configs_file_names()):
        config_file = FileInstance.for_booth_config(conf_file_name)
        try:
            booth_conf_data = config_file.raw_file.read()
            (
                authfile_name,
                authfile_data,
                authfile_report_list,
            ) = config_files.get_authfile_name_and_data(
                config_file.raw_to_facade(booth_conf_data))
            reporter.report_list(authfile_report_list)
            file_list.append({
                "name": conf_file_name,
                "data": booth_conf_data.decode("utf-8"),
                "is_authfile": False,
            })
            if authfile_name and authfile_data:
                file_list.append({
                    "name":
                    authfile_name,
                    "data":
                    base64.b64encode(authfile_data).decode("utf-8"),
                    "is_authfile":
                    True,
                })
        except RawFileError as e:
            reporter.report(
                raw_file_error_report(
                    e,
                    force_code=report_codes.SKIP_UNREADABLE_CONFIG,
                    is_forced_or_warning=skip_wrong_config,
                ))
        except ParserErrorException as e:
            reporter.report_list(
                config_file.parser_exception_to_report_list(
                    e,
                    force_code=report_codes.SKIP_UNREADABLE_CONFIG,
                    is_forced_or_warning=skip_wrong_config,
                ))
    if reporter.has_errors:
        raise LibraryError()

    if not file_list:
        # no booth configs exist, nothing to be synced
        return

    reporter.report(
        ReportItem.info(reports.messages.BoothConfigDistributionStarted()))
    com_cmd = BoothSaveFiles(reporter,
                             file_list,
                             rewrite_existing=rewrite_existing)
    com_cmd.set_targets(target_list)
    run(communicator, com_cmd)

    if reporter.has_errors:
        raise LibraryError()