Пример #1
0
def cluster_reload(argv):
    if len(argv) != 1 or argv[0] != "corosync":
        usage.cluster(["reload"])
        exit(1)

    output, retval = utils.reloadCorosync()
    if retval != 0 or "invalid option" in output:
        utils.err(output.rstrip())
    print("Corosync reloaded")
Пример #2
0
def cluster_report(argv):
    if len(argv) != 1:
        usage.cluster(["report"])
        sys.exit(1)

    outfile = argv[0]
    dest_outfile = outfile + ".tar.bz2"
    if os.path.exists(dest_outfile):
        if "--force" not in utils.pcs_options:
            utils.err(dest_outfile +
                      " already exists, use --force to overwrite")
        else:
            try:
                os.remove(dest_outfile)
            except OSError as e:
                utils.err("Unable to remove " + dest_outfile + ": " +
                          e.strerror)
    crm_report_opts = []

    crm_report_opts.append("-f")
    if "--from" in utils.pcs_options:
        crm_report_opts.append(utils.pcs_options["--from"])
        if "--to" in utils.pcs_options:
            crm_report_opts.append("-t")
            crm_report_opts.append(utils.pcs_options["--to"])
    else:
        yesterday = datetime.datetime.now() - datetime.timedelta(1)
        crm_report_opts.append(yesterday.strftime("%Y-%m-%d %H:%M"))

    crm_report_opts.append(outfile)
    output, retval = utils.run([settings.crm_report] + crm_report_opts)
    if (retval != 0 and
            "ERROR: Cannot determine nodes; specify --nodes or --single-node"
            in output):
        utils.err("cluster is not configured on this node")
    newoutput = ""
    for line in output.split("\n"):
        if line.startswith("cat:") or line.startswith(
                "grep") or line.startswith("grep") or line.startswith("tail"):
            continue
        if "We will attempt to remove" in line:
            continue
        if "-p option" in line:
            continue
        if "However, doing" in line:
            continue
        if "to diagnose" in line:
            continue
        if "--dest" in line:
            line = line.replace("--dest", "<dest>")
        newoutput = newoutput + line + "\n"
    if retval != 0:
        utils.err(newoutput)
    print(newoutput)
Пример #3
0
def cluster_uidgid(argv, silent_list=False):
    if len(argv) == 0:
        found = False
        uid_gid_files = os.listdir(settings.corosync_uidgid_dir)
        for ug_file in uid_gid_files:
            uid_gid_dict = utils.read_uid_gid_file(ug_file)
            if "uid" in uid_gid_dict or "gid" in uid_gid_dict:
                line = "UID/GID: uid="
                if "uid" in uid_gid_dict:
                    line += uid_gid_dict["uid"]
                line += " gid="
                if "gid" in uid_gid_dict:
                    line += uid_gid_dict["gid"]

                print(line)
                found = True
        if not found and not silent_list:
            print("No uidgids configured in cluster.conf")
        return

    command = argv.pop(0)
    uid = ""
    gid = ""

    if (command == "add" or command == "rm") and len(argv) > 0:
        for arg in argv:
            if arg.find('=') == -1:
                utils.err(
                    "uidgid options must be of the form uid=<uid> gid=<gid>")

            (k, v) = arg.split('=', 1)
            if k != "uid" and k != "gid":
                utils.err("%s is not a valid key, you must use uid or gid" % k)

            if k == "uid":
                uid = v
            if k == "gid":
                gid = v
        if uid == "" and gid == "":
            utils.err("you must set either uid or gid")

        if command == "add":
            utils.write_uid_gid_file(uid, gid)
        elif command == "rm":
            retval = utils.remove_uid_gid_file(uid, gid)
            if retval == False:
                utils.err("no uidgid files with uid=%s and gid=%s found" %
                          (uid, gid))

    else:
        usage.cluster(["uidgid"])
        exit(1)
Пример #4
0
def cluster_get_corosync_conf(argv):
    if utils.is_rhel6():
        utils.err("corosync.conf is not supported on CMAN clusters")

    if len(argv) > 1:
        usage.cluster()
        exit(1)

    if len(argv) == 0:
        print(utils.getCorosyncConf().rstrip())
        return

    node = argv[0]
    retval, output = utils.getCorosyncConfig(node)
    if retval != 0:
        utils.err(output)
    else:
        print(output.rstrip())
Пример #5
0
def cluster_verify(argv):
    if len(argv) > 1:
        usage.cluster("verify")
        raise SystemExit(1)

    if argv:
        filename = argv[0]
        if not utils.usefile:
            #We must operate on given cib everywhere.
            utils.usefile = True
            utils.filename = filename
        elif os.path.abspath(filename) == os.path.abspath(utils.filename):
            warn("File '{0}' specified twice".format(
                os.path.abspath(filename)))
        else:
            raise error(
                "Ambiguous cib filename specification: '{0}' vs  -f '{1}'".
                format(filename, utils.filename))

    lib = utils.get_library_wrapper()
    try:
        lib.cluster.verify(verbose="-V" in utils.pcs_options)
    except LibraryError as e:
        utils.process_library_reports(e.args)
Пример #6
0
def get_cib(argv):
    if len(argv) > 2:
        usage.cluster(["cib"])
        sys.exit(1)

    filename = None
    scope = None
    for arg in argv:
        if "=" not in arg:
            filename = arg
        else:
            arg_name, arg_value = arg.split("=", 1)
            if arg_name == "scope" and "--config" not in utils.pcs_options:
                if not utils.is_valid_cib_scope(arg_value):
                    utils.err("invalid CIB scope '%s'" % arg_value)
                else:
                    scope = arg_value
            else:
                usage.cluster(["cib"])
                sys.exit(1)
    if "--config" in utils.pcs_options:
        scope = "configuration"

    if not filename:
        print(utils.get_cib(scope).rstrip())
    else:
        try:
            f = open(filename, 'w')
            output = utils.get_cib(scope)
            if output != "":
                f.write(output)
            else:
                utils.err("No data in the CIB")
        except IOError as e:
            utils.err("Unable to write to file '%s', %s" %
                      (filename, e.strerror))
Пример #7
0
def cluster_edit(argv):
    if 'EDITOR' in os.environ:
        if len(argv) > 1:
            usage.cluster(["edit"])
            sys.exit(1)

        scope = None
        scope_arg = ""
        for arg in argv:
            if "=" not in arg:
                usage.cluster(["edit"])
                sys.exit(1)
            else:
                arg_name, arg_value = arg.split("=", 1)
                if arg_name == "scope" and "--config" not in utils.pcs_options:
                    if not utils.is_valid_cib_scope(arg_value):
                        utils.err("invalid CIB scope '%s'" % arg_value)
                    else:
                        scope_arg = arg
                        scope = arg_value
                else:
                    usage.cluster(["edit"])
                    sys.exit(1)
        if "--config" in utils.pcs_options:
            scope = "configuration"
            # Leave scope_arg empty as cluster_push will pick up a --config
            # option from utils.pcs_options
            scope_arg = ""

        editor = os.environ['EDITOR']
        tempcib = tempfile.NamedTemporaryFile(mode="w+", suffix=".pcs")
        cib = utils.get_cib(scope)
        tempcib.write(cib)
        tempcib.flush()
        try:
            subprocess.call([editor, tempcib.name])
        except OSError:
            utils.err("unable to open file with $EDITOR: " + editor)

        tempcib.seek(0)
        newcib = "".join(tempcib.readlines())
        if newcib == cib:
            print("CIB not updated, no changes detected")
        else:
            cluster_push([arg for arg in [tempcib.name, scope_arg] if arg])

    else:
        utils.err("$EDITOR environment variable is not set")
Пример #8
0
from pcs import (
    cluster,
    pcsd,
    resource,
    status,
    usage,
)
import pcs.cli.cluster.command as cluster_command
from pcs.cli.common.routing import create_router


cluster_cmd = create_router(
    {
        "help": lambda lib, argv, modifiers: usage.cluster(argv),
        "setup": cluster.cluster_setup,
        "sync": create_router(
            {
                "corosync": cluster.sync_nodes,
            },
            ["cluster", "sync"],
            default_cmd="corosync",
        ),
        "status": status.cluster_status,
        "pcsd-status": status.cluster_pcsd_status,
        "certkey": pcsd.pcsd_certkey,
        "auth": cluster.cluster_auth_cmd,
        "start": cluster.cluster_start_cmd,
        "stop": cluster.cluster_stop_cmd,
        "kill": cluster.kill_cluster,
        "enable": cluster.cluster_enable_cmd,
        "disable": cluster.cluster_disable_cmd,
Пример #9
0
def pcsd_status(lib: Any, argv: Sequence[str],
                modifiers: InputModifiers) -> None:
    deprecation_warning(
        "This command is deprecated and will be removed. "
        "Please use 'pcs pcsd status' or 'pcs status pcsd' instead.")
    try:
        return pcsd.pcsd_status_cmd(lib, argv, modifiers)
    except CmdLineInputError as e:
        return exit_on_cmdline_input_error(e, "pcsd", ["status"])


cluster_cmd = create_router(
    {
        "help":
        lambda lib, argv, modifiers: print(usage.cluster(argv)),
        "setup":
        cluster.cluster_setup,
        "config":
        create_router(
            {
                "show": cluster.config_show,
                "update": cluster.config_update,
            },
            ["cluster", "config"],
            default_cmd="show",
        ),
        "authkey":
        create_router(
            {"corosync": cluster.authkey_corosync},
            ["cluster", "authkey"],
Пример #10
0
def cluster_push(argv):
    if len(argv) > 2:
        usage.cluster(["cib-push"])
        sys.exit(1)

    filename = None
    scope = None
    timeout = None
    diff_against = None

    if "--wait" in utils.pcs_options:
        timeout = utils.validate_wait_get_timeout()
    for arg in argv:
        if "=" not in arg:
            filename = arg
        else:
            arg_name, arg_value = arg.split("=", 1)
            if arg_name == "scope":
                if "--config" in utils.pcs_options:
                    utils.err("Cannot use both scope and --config")
                if not utils.is_valid_cib_scope(arg_value):
                    utils.err("invalid CIB scope '%s'" % arg_value)
                else:
                    scope = arg_value
            elif arg_name == "diff-against":
                diff_against = arg_value
            else:
                usage.cluster(["cib-push"])
                sys.exit(1)
    if "--config" in utils.pcs_options:
        scope = "configuration"
    if diff_against and scope:
        utils.err("Cannot use both scope and diff-against")
    if not filename:
        usage.cluster(["cib-push"])
        sys.exit(1)

    try:
        new_cib_dom = xml.dom.minidom.parse(filename)
        if scope and not new_cib_dom.getElementsByTagName(scope):
            utils.err("unable to push cib, scope '%s' not present in new cib" %
                      scope)
    except (EnvironmentError, xml.parsers.expat.ExpatError) as e:
        utils.err("unable to parse new cib: %s" % e)

    if diff_against:
        try:
            xml.dom.minidom.parse(diff_against)
        except (EnvironmentError, xml.parsers.expat.ExpatError) as e:
            utils.err("unable to parse original cib: %s" % e)
        runner = utils.cmd_runner()
        command = [
            "crm_diff", "--original", diff_against, "--new", filename,
            "--no-version"
        ]
        patch, error, dummy_retval = runner.run(command)
        # dummy_retval == 1 means one of two things:
        # a) an error has occured
        # b) --original and --new differ
        # therefore it's of no use to see if an error occurred
        if error.strip():
            utils.err("unable to diff the CIBs:\n" + error)
        if not patch.strip():
            print(
                "The new CIB is the same as the original CIB, nothing to push."
            )
            sys.exit(0)

        command = ["cibadmin", "--patch", "--xml-pipe"]
        output, error, retval = runner.run(command, patch)
        if retval != 0:
            utils.err("unable to push cib\n" + error + output)

    else:
        command = ["cibadmin", "--replace", "--xml-file", filename]
        if scope:
            command.append("--scope=%s" % scope)
        output, retval = utils.run(command)
        if retval != 0:
            utils.err("unable to push cib\n" + output)

    print("CIB updated")

    if "--wait" not in utils.pcs_options:
        return
    cmd = ["crm_resource", "--wait"]
    if timeout:
        cmd.extend(["--timeout", str(timeout)])
    output, retval = utils.run(cmd)
    if retval != 0:
        msg = []
        if retval == settings.pacemaker_wait_timeout_status:
            msg.append("waiting timeout")
        if output:
            msg.append("\n" + output)
        utils.err("\n".join(msg).strip())
Пример #11
0
def cluster_cmd(argv):
    if len(argv) == 0:
        usage.cluster()
        exit(1)

    sub_cmd = argv.pop(0)
    if (sub_cmd == "help"):
        usage.cluster([" ".join(argv)] if argv else [])
    elif (sub_cmd == "setup"):
        try:
            cluster_setup(utils.get_library_wrapper(), argv,
                          utils.get_modifiers())
        except LibraryError as e:
            process_library_reports(e.args)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "cluster", sub_cmd)
    elif (sub_cmd == "sync"):
        sync_nodes(utils.get_corosync_conf_facade().get_nodes_names(),
                   utils.getCorosyncConf())
    elif (sub_cmd == "status"):
        status.cluster_status(argv)
    elif (sub_cmd == "pcsd-status"):
        status.cluster_pcsd_status(argv)
    elif (sub_cmd == "certkey"):
        cluster_certkey(argv)
    elif (sub_cmd == "auth"):
        try:
            cluster_auth_cmd(utils.get_library_wrapper(), argv,
                             utils.get_modifiers())
        except LibraryError as e:
            process_library_reports(e.args)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "cluster", sub_cmd)
    elif (sub_cmd == "start"):
        if "--all" in utils.pcs_options:
            if argv:
                utils.err(ERR_NODE_LIST_AND_ALL_MUTUALLY_EXCLUSIVE)
            start_cluster_all()
        else:
            start_cluster(argv)
    elif (sub_cmd == "stop"):
        if "--all" in utils.pcs_options:
            if argv:
                utils.err(ERR_NODE_LIST_AND_ALL_MUTUALLY_EXCLUSIVE)
            stop_cluster_all()
        else:
            stop_cluster(argv)
    elif (sub_cmd == "kill"):
        kill_cluster(argv)
    elif (sub_cmd == "standby"):
        try:
            node.node_standby_cmd(utils.get_library_wrapper(), argv,
                                  utils.get_modifiers(), True)
        except LibraryError as e:
            utils.process_library_reports(e.args)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "node", "standby")
    elif (sub_cmd == "unstandby"):
        try:
            node.node_standby_cmd(utils.get_library_wrapper(), argv,
                                  utils.get_modifiers(), False)
        except LibraryError as e:
            utils.process_library_reports(e.args)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "node", "unstandby")
    elif (sub_cmd == "enable"):
        if "--all" in utils.pcs_options:
            if argv:
                utils.err(ERR_NODE_LIST_AND_ALL_MUTUALLY_EXCLUSIVE)
            enable_cluster_all()
        else:
            enable_cluster(argv)
    elif (sub_cmd == "disable"):
        if "--all" in utils.pcs_options:
            if argv:
                utils.err(ERR_NODE_LIST_AND_ALL_MUTUALLY_EXCLUSIVE)
            disable_cluster_all()
        else:
            disable_cluster(argv)
    elif (sub_cmd == "remote-node"):
        try:
            cluster_remote_node(argv)
        except LibraryError as e:
            utils.process_library_reports(e.args)
    elif (sub_cmd == "cib"):
        get_cib(argv)
    elif (sub_cmd == "cib-push"):
        cluster_push(argv)
    elif (sub_cmd == "cib-upgrade"):
        utils.cluster_upgrade()
    elif (sub_cmd == "edit"):
        cluster_edit(argv)
    elif (sub_cmd == "node"):
        node_command_map = {
            "add":
            node_add,
            "add-guest":
            cluster_command.node_add_guest,
            "add-outside":
            node_add_outside_cluster,
            "add-remote":
            cluster_command.node_add_remote,
            "clear":
            cluster_command.node_clear,
            "remove":
            node_remove,
            "remove-guest":
            cluster_command.node_remove_guest,
            "remove-remote":
            cluster_command.create_node_remove_remote(
                resource.resource_remove),
        }
        if argv and argv[0] in node_command_map:
            try:
                node_command_map[argv[0]](utils.get_library_wrapper(),
                                          argv[1:], utils.get_modifiers())
            except LibraryError as e:
                process_library_reports(e.args)
            except CmdLineInputError as e:
                utils.exit_on_cmdline_input_errror(e, "cluster",
                                                   "node " + argv[0])
        else:
            usage.cluster(["node"])
            sys.exit(1)
    elif (sub_cmd == "uidgid"):
        cluster_uidgid(argv)
    elif (sub_cmd == "corosync"):
        cluster_get_corosync_conf(argv)
    elif (sub_cmd == "reload"):
        cluster_reload(argv)
    elif (sub_cmd == "destroy"):
        try:
            cluster_destroy(argv)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "cluster", sub_cmd)
    elif (sub_cmd == "verify"):
        cluster_verify(argv)
    elif (sub_cmd == "report"):
        cluster_report(argv)
    elif (sub_cmd == "quorum"):
        if argv and argv[0] == "unblock":
            quorum.quorum_unblock_cmd(argv[1:])
        else:
            usage.cluster()
            sys.exit(1)
    elif (sub_cmd == "remove_nodes_from_cib"):
        try:
            remove_nodes_from_cib(
                utils.get_library_wrapper(),
                argv,
                utils.get_modifiers(),
            )
        except LibraryError as e:
            process_library_reports(e.args)
        except CmdLineInputError as e:
            utils.exit_on_cmdline_input_errror(e, "cluster", sub_cmd)
    else:
        usage.cluster()
        sys.exit(1)