Example #1
0
def stonith_create(lib, argv, modifiers):
    if modifiers["before"] and modifiers["after"]:
        raise error("you cannot specify both --before and --after{0}".format(
            "" if modifiers["group"] else " and you have to specify --group"
        ))

    if not modifiers["group"]:
        if modifiers["before"]:
            raise error("you cannot use --before without --group")
        elif modifiers["after"]:
            raise error("you cannot use --after without --group")

    if len(argv) < 2:
        usage.stonith(["create"])
        sys.exit(1)

    stonith_id = argv[0]
    stonith_type = argv[1]

    parts = parse_create_args(argv[2:])

    settings = dict(
        allow_absent_agent=modifiers["force"],
        allow_invalid_operation=modifiers["force"],
        allow_invalid_instance_attributes=modifiers["force"],
        ensure_disabled=modifiers["disabled"],
        use_default_operations=not modifiers["no-default-ops"],
        wait=modifiers["wait"],
    )

    if not modifiers["group"]:
        lib.stonith.create(
            stonith_id, stonith_type, parts["op"],
            parts["meta"],
            parts["options"],
            **settings
        )
    else:
        adjacent_resource_id = None
        put_after_adjacent = False
        if modifiers["after"]:
            adjacent_resource_id = modifiers["after"]
            put_after_adjacent = True
        if modifiers["before"]:
            adjacent_resource_id = modifiers["before"]
            put_after_adjacent = False

        lib.stonith.create_in_group(
            stonith_id, stonith_type, modifiers["group"], parts["op"],
            parts["meta"],
            parts["options"],
            adjacent_resource_id=adjacent_resource_id,
            put_after_adjacent=put_after_adjacent,
            **settings
        )
Example #2
0
def process_library_reports(report_item_list):
    """
    report_item_list list of ReportItem
    """
    if not report_item_list:
        raise error("Errors have occurred, therefore pcs is unable to continue")

    critical_error = False
    for report_item in report_item_list:
        if report_item.severity == ReportItemSeverity.WARNING:
            print("Warning: " + build_report_message(report_item))
            continue

        if report_item.severity != ReportItemSeverity.ERROR:
            print(build_report_message(report_item))
            continue

        sys.stderr.write('Error: {0}\n'.format(build_report_message(
            report_item,
            _prepare_force_text(report_item)
        )))
        critical_error = True

    if critical_error:
        sys.exit(1)
Example #3
0
    def apply(next_in_line, env, *args, **kwargs):
        if local_file_path:
            try:
                env.corosync_conf_data = open(local_file_path).read()
            except EnvironmentError as e:
                raise console_report.error("Unable to read {0}: {1}".format(local_file_path, e.strerror))

        result_of_next = next_in_line(env, *args, **kwargs)

        if local_file_path:
            try:
                f = open(local_file_path, "w")
                f.write(env.corosync_conf_data)
                f.close()
            except EnvironmentError as e:
                raise console_report.error("Unable to write {0}: {1}".format(local_file_path, e.strerror))
        return result_of_next
Example #4
0
 def _send(self, report_item_list, print_errors=True):
     errors = []
     for report_item in report_item_list:
         if report_item.severity == ReportItemSeverity.ERROR:
             if print_errors:
                 error(build_report_message(
                     report_item,
                     _prepare_force_text(report_item)
                 ))
             errors.append(report_item)
         elif report_item.severity == ReportItemSeverity.WARNING:
             warn(build_report_message(report_item))
         elif self.debug or report_item.severity != ReportItemSeverity.DEBUG:
             msg = build_report_message(report_item)
             if msg:
                 print(msg)
     return errors
Example #5
0
    def apply(next_in_line, env, *args, **kwargs):
        if local_file_path:
            try:
                env.cluster_conf_data = open(local_file_path).read()
            except EnvironmentError as e:
                raise console_report.error("Unable to read {0}: {1}".format(local_file_path, e.strerror))

        return next_in_line(env, *args, **kwargs)
Example #6
0
def write_env_file(env_file, file_path):
    try:
        f = open(file_path, "wb" if env_file.get("is_binary", False) else "w")
        f.write(env_file["content"])
        f.close()
    except EnvironmentError as e:
        raise console_report.error(
            "Unable to write {0}: {1}".format(file_path, e.strerror)
        )
Example #7
0
def read_env_file(path):
    try:
        return {
            "content": open(path).read() if os.path.isfile(path) else None
        }
    except EnvironmentError as e:
        raise console_report.error(
            "Unable to read {0}: {1}".format(path, e.strerror)
        )
Example #8
0
def read(path, is_binary=False):
    try:
        mode = "rb" if is_binary else "r"
        return {
            "content": open(path, mode).read() if os.path.isfile(path) else None
        }
    except EnvironmentError as e:
        raise console_report.error(
            "Unable to read {0}: {1}".format(path, e.strerror)
        )
Example #9
0
def remove(lib, argv, modifiers):
    """
    Options:
      * -f - CIB file
    """
    modifiers.ensure_only_supported("-f")
    if len(argv) != 2:
        raise CmdLineInputError()
    ticket, resource_id = argv
    if not lib.constraint_ticket.remove(ticket, resource_id):
        raise error("no matching ticket constraint found")
Example #10
0
def process_no_existing_file_expectation(file_role, env_file, file_path):
    if(
        env_file["no_existing_file_expected"]
        and
        os.path.exists(file_path)
    ):
        msg = "{0} {1} already exists".format(file_role, file_path)
        if not env_file["can_overwrite_existing_file"]:
            raise console_report.error(
                "{0}, use --force to override".format(msg)
            )
        console_report.warn(msg)
Example #11
0
    def flush(modified_env):
        if not is_mocked_environment:
            return
        if not modified_env:
            #TODO now this would not happen
            #for more information see comment in
            #pcs.cli.common.lib_wrapper.lib_env_to_cli_env
            raise console_report.error("Error during library communication")

        process_no_existing_file_expectation(
            "booth config file",
            modified_env["config_file"],
            config_path
        )
        process_no_existing_file_expectation(
            "booth key file",
            modified_env["key_file"],
            key_path
        )
        write_env_file(modified_env["key_file"], key_path)
        write_env_file(modified_env["config_file"], config_path)
Example #12
0
def get_capabilities_definition():
    """
    Read and parse capabilities file

    The point is to return all data in python structures for further processing.
    """
    filename = os.path.join(settings.pcsd_exec_location, "capabilities.xml")
    try:
        with open(filename, mode="r") as file_:
            capabilities_xml = xml_fromstring(file_.read())
    except (EnvironmentError, etree.XMLSyntaxError, etree.DocumentInvalid) as e:
        raise error(
            "Cannot read capabilities definition file '{0}': '{1}'"
            .format(filename, str(e))
        )
    capabilities = []
    for feat_xml in capabilities_xml.findall(".//capability"):
        feat = dict(feat_xml.attrib)
        desc = feat_xml.find("./description")
        # dedent and strip remove indentation in the XML file
        feat["description"] = "" if desc is None else dedent(desc.text).strip()
        capabilities.append(feat)
    return capabilities
Example #13
0
def report_missing_file(file_role, file_path):
    console_report.error(
        "{0} '{1}' does not exist".format(file_role, file_path)
    )
Example #14
0
def remove(lib, argv, modificators):
    if len(argv) != 2:
        raise CmdLineInputError()
    ticket, resource_id = argv
    if not lib.constraint_ticket.remove(ticket, resource_id):
        raise error("no matching ticket constraint found")
Example #15
0
def middleware_config(name, config_path, key_path):
    if config_path and not key_path:
        raise console_report.error(
            "With --booth-conf must be specified --booth-key as well"
        )

    if key_path and not config_path:
        raise console_report.error(
            "With --booth-key must be specified --booth-conf as well"
        )

    is_mocked_environment = config_path and key_path

    def create_booth_env():
        if not is_mocked_environment:
            return {"name": name}
        return {
            "name": name,
            "config_file": read_env_file(config_path),
            "key_file": read_env_file(key_path),
            "key_path": key_path,
        }

    def flush(modified_env):
        if not is_mocked_environment:
            return
        if not modified_env:
            #TODO now this would not happen
            #for more information see comment in
            #pcs.cli.common.lib_wrapper.lib_env_to_cli_env
            raise console_report.error("Error during library communication")

        process_no_existing_file_expectation(
            "booth config file",
            modified_env["config_file"],
            config_path
        )
        process_no_existing_file_expectation(
            "booth key file",
            modified_env["key_file"],
            key_path
        )
        write_env_file(modified_env["key_file"], key_path)
        write_env_file(modified_env["config_file"], config_path)

    def apply(next_in_line, env, *args, **kwargs):
        env.booth = create_booth_env()
        try:
            result_of_next = next_in_line(env, *args, **kwargs)
        except LibraryEnvError as e:
            for report in e.args:
                if is_missing_file_report(report, file_role_codes.BOOTH_CONFIG):
                    report_missing_file("Booth config file", config_path)
                    e.sign_processed(report)
                if is_missing_file_report(report, file_role_codes.BOOTH_KEY):
                    report_missing_file("Booth key file", key_path)
                    e.sign_processed(report)
            raise e
        flush(env.booth["modified_env"])
        return result_of_next

    return apply
Example #16
0
def middleware_config(name, config_path, key_path):
    if config_path and not key_path:
        raise console_report.error(
            "With --booth-conf must be specified --booth-key as well"
        )

    if key_path and not config_path:
        raise console_report.error(
            "With --booth-key must be specified --booth-conf as well"
        )

    is_mocked_environment = config_path and key_path

    def create_booth_env():
        if not is_mocked_environment:
            return {"name": name}
        return {
            "name": name,
            "config_file": env_file.read(config_path),
            "key_file": env_file.read(key_path, is_binary=True),
            "key_path": key_path,
        }

    def flush(modified_env):
        if not is_mocked_environment:
            return
        if not modified_env:
            #TODO now this would not happen
            #for more information see comment in
            #pcs.cli.common.lib_wrapper.lib_env_to_cli_env
            raise console_report.error("Error during library communication")

        env_file.process_no_existing_file_expectation(
            "booth config file",
            modified_env["config_file"],
            config_path
        )
        env_file.process_no_existing_file_expectation(
            "booth key file",
            modified_env["key_file"],
            key_path
        )
        env_file.write(modified_env["key_file"], key_path)
        env_file.write(modified_env["config_file"], config_path)

    def apply(next_in_line, env, *args, **kwargs):
        env.booth = create_booth_env()
        try:
            result_of_next = next_in_line(env, *args, **kwargs)
        except LibraryEnvError as err:
            missing_file = env_file.MissingFileCandidateInfo

            env_file.evaluate_for_missing_files(err, [
                missing_file(BOOTH_CONFIG, "Booth config file", config_path),
                missing_file(BOOTH_KEY, "Booth key file", key_path),
            ])
            raise err
        flush(env.booth["modified_env"])
        return result_of_next

    return apply
Example #17
0
def stonith_create(lib, argv, modifiers):
    """
    Options:
      * --before - specified resource inside a group before which new resource
        will be placed inside the group
      * --after - specified resource inside a group after which new resource
        will be placed inside the group
      * --group - specifies group in which resource will be created
      * --force - allow not existing agent, invalid operations or invalid
        instance attributes
      * --disabled - created reource will be disabled
      * --no-default-ops - do not add default operations
      * --wait
      * -f - CIB file
    """
    modifiers.ensure_only_supported(
        "--before", "--after", "--group", "--force", "--disabled",
        "--no-default-ops", "--wait", "-f",
    )
    if modifiers.is_specified("--before") and modifiers.is_specified("--after"):
        raise error("you cannot specify both --before and --after{0}".format(
            "" if modifiers.is_specified("--group")
            else " and you have to specify --group"
        ))

    if not modifiers.is_specified("--group"):
        if modifiers.is_specified("--before"):
            raise error("you cannot use --before without --group")
        if modifiers.is_specified("--after"):
            raise error("you cannot use --after without --group")

    if len(argv) < 2:
        raise CmdLineInputError()

    stonith_id = argv[0]
    stonith_type = argv[1]

    parts = parse_create_args(argv[2:])

    settings = dict(
        allow_absent_agent=modifiers.get("--force"),
        allow_invalid_operation=modifiers.get("--force"),
        allow_invalid_instance_attributes=modifiers.get("--force"),
        ensure_disabled=modifiers.get("--disabled"),
        use_default_operations=not modifiers.get("--no-default-ops"),
        wait=modifiers.get("--wait"),
    )

    if not modifiers.get("--group"):
        lib.stonith.create(
            stonith_id, stonith_type, parts["op"],
            parts["meta"],
            parts["options"],
            **settings
        )
    else:
        adjacent_resource_id = None
        put_after_adjacent = False
        if modifiers.get("--after"):
            adjacent_resource_id = modifiers.get("--after")
            put_after_adjacent = True
        if modifiers.get("--before"):
            adjacent_resource_id = modifiers.get("--before")
            put_after_adjacent = False

        lib.stonith.create_in_group(
            stonith_id, stonith_type, modifiers.get("--group"), parts["op"],
            parts["meta"],
            parts["options"],
            adjacent_resource_id=adjacent_resource_id,
            put_after_adjacent=put_after_adjacent,
            **settings
        )