示例#1
0
文件: config.py 项目: dchirikov/pcs
def config_restore_local(infile_name, infile_obj):
    if (
        status.is_service_running("cman")
        or
        status.is_service_running("corosync")
        or
        status.is_service_running("pacemaker")
        or
        status.is_service_running("pacemaker_remote")
    ):
        utils.err(
            "Cluster is currently running on this node. You need to stop "
                "the cluster in order to restore the configuration."
        )

    file_list = config_backup_path_list(with_uid_gid=True)
    tarball_file_list = []
    version = None
    try:
        tarball = tarfile.open(infile_name, "r|*", infile_obj)
        while True:
            # next(tarball) does not work in python2.6
            tar_member_info = tarball.next()
            if tar_member_info is None:
                break
            if tar_member_info.name == "version.txt":
                version_data = tarball.extractfile(tar_member_info)
                version = version_data.read()
                version_data.close()
                continue
            tarball_file_list.append(tar_member_info.name)
        tarball.close()

        required_file_list = [
            tar_path
            for tar_path, path_info in file_list.items()
                if path_info["required"]
        ]
        missing = set(required_file_list) - set(tarball_file_list)
        if missing:
            utils.err(
                "unable to restore the cluster, missing files in backup: %s"
                % ", ".join(missing)
            )

        config_backup_check_version(version)

        if infile_obj:
            infile_obj.seek(0)
        tarball = tarfile.open(infile_name, "r|*", infile_obj)
        while True:
            # next(tarball) does not work in python2.6
            tar_member_info = tarball.next()
            if tar_member_info is None:
                break
            extract_info = None
            path = tar_member_info.name
            while path:
                if path in file_list:
                    extract_info = file_list[path]
                    break
                path = os.path.dirname(path)
            if not extract_info:
                continue
            path_extract = os.path.dirname(extract_info["path"])
            tarball.extractall(path_extract, [tar_member_info])
            path_full = os.path.join(path_extract, tar_member_info.name)
            file_attrs = extract_info["attrs"]
            os.chmod(path_full, file_attrs["mode"])
            os.chown(path_full, file_attrs["uid"], file_attrs["gid"])
        tarball.close()
    except (tarfile.TarError, EnvironmentError) as e:
        utils.err("unable to restore the cluster: %s" % e)

    try:
        sig_path = os.path.join(settings.cib_dir, "cib.xml.sig")
        if os.path.exists(sig_path):
            os.remove(sig_path)
    except EnvironmentError as e:
        utils.err("unable to remove %s: %s" % (sig_path, e))
示例#2
0
def config_restore_local(infile_name, infile_obj):
    if (status.is_service_running("cman")
            or status.is_service_running("corosync")
            or status.is_service_running("pacemaker")
            or status.is_service_running("pacemaker_remote")):
        utils.err(
            "Cluster is currently running on this node. You need to stop "
            "the cluster in order to restore the configuration.")

    file_list = config_backup_path_list(with_uid_gid=True)
    tarball_file_list = []
    version = None
    tmp_dir = None
    try:
        tarball = tarfile.open(infile_name, "r|*", infile_obj)
        while True:
            # next(tarball) does not work in python2.6
            tar_member_info = tarball.next()
            if tar_member_info is None:
                break
            if tar_member_info.name == "version.txt":
                version_data = tarball.extractfile(tar_member_info)
                version = version_data.read()
                version_data.close()
                continue
            tarball_file_list.append(tar_member_info.name)
        tarball.close()

        required_file_list = [
            tar_path for tar_path, path_info in file_list.items()
            if path_info["required"]
        ]
        missing = set(required_file_list) - set(tarball_file_list)
        if missing:
            utils.err(
                "unable to restore the cluster, missing files in backup: %s" %
                ", ".join(missing))

        config_backup_check_version(version)

        if infile_obj:
            infile_obj.seek(0)
        tarball = tarfile.open(infile_name, "r|*", infile_obj)
        while True:
            # next(tarball) does not work in python2.6
            tar_member_info = tarball.next()
            if tar_member_info is None:
                break
            extract_info = None
            path = tar_member_info.name
            while path:
                if path in file_list:
                    extract_info = file_list[path]
                    break
                path = os.path.dirname(path)
            if not extract_info:
                continue
            path_full = None
            if hasattr(extract_info.get("pre_store_call"), '__call__'):
                extract_info["pre_store_call"]()
            if "rename" in extract_info and extract_info["rename"]:
                if tmp_dir is None:
                    tmp_dir = tempfile.mkdtemp()
                tarball.extractall(tmp_dir, [tar_member_info])
                path_full = extract_info["path"]
                shutil.move(os.path.join(tmp_dir, tar_member_info.name),
                            path_full)
            else:
                dir_path = os.path.dirname(extract_info["path"])
                tarball.extractall(dir_path, [tar_member_info])
                path_full = os.path.join(dir_path, tar_member_info.name)
            file_attrs = extract_info["attrs"]
            os.chmod(path_full, file_attrs["mode"])
            os.chown(path_full, file_attrs["uid"], file_attrs["gid"])
        tarball.close()
    except (tarfile.TarError, EnvironmentError, OSError) as e:
        utils.err("unable to restore the cluster: %s" % e)
    finally:
        if tmp_dir:
            shutil.rmtree(tmp_dir, ignore_errors=True)

    try:
        sig_path = os.path.join(settings.cib_dir, "cib.xml.sig")
        if os.path.exists(sig_path):
            os.remove(sig_path)
    except EnvironmentError as e:
        utils.err("unable to remove %s: %s" % (sig_path, e))
def check_cluster_status(test_name, resource_groups_cfg_map, check_daemons):
    """
    Check the status of the pacemaker cluster, create and sent the message
    to the xymon server.
    """
    xymon = pymon.XymonClient(test_name)
    cluster_infos = get_cluster_infos(resource_groups_cfg_map)
    cluster_name = cluster_infos['cluster_name']
    node_name = cluster_infos['node_name']
    resources_match_cfg = cluster_infos['node_resource_groups_match_cfg']
    node_status = (cluster_infos['node_status'] if resources_match_cfg else
                   '{0} (resources have switched)'.format(
                       cluster_infos['node_status']))
    # message - title
    xymon.title('Pacemaker cluster "{0}"'.format(cluster_name))

    # message - cluster node status
    node_color = (pymon.STATUS_OK
                  if node_status == 'online' else pymon.STATUS_CRITICAL)
    xymon.section('Node Status',
                  '{0} - {1} {2}'.format(node_name, node_status, node_color))
    xymon.color = node_color

    # message - cluster nodes
    nodes = sorted(cluster_nodes())
    xymon.section('Cluster Nodes', ', '.join(nodes))

    # message - resources
    resources_summary = (
        '{c[resources_running]} resources running:\n\n'.format(
            c=cluster_infos))
    resources_color = lambda v: \
        pymon.STATUS_OK if v.get('role') == 'Started' else pymon.STATUS_CRITICAL
    resources_list_with_status = '\n'.join(
        ' {0} {1:<22} {2}'.format(resources_color(v), k, v.get(
            'resource_agent'))
        for k, v in cluster_infos['node_resources'].items())
    xymon.section(
        'Cluster Resources', '{0}{1}'.format(resources_summary,
                                             resources_list_with_status))
    if not all([item.split()[0] == pymon.STATUS_OK \
                for item in resources_list_with_status.splitlines()]):
        xymon.color = pymon.STATUS_CRITICAL

    # message - daemons status
    if check_daemons:
        cluster_services = ['corosync', 'pacemaker', 'pcsd']
        service_status = lambda service: ' {0} service {1:<14} {2}\n'.format(*(
            (pymon.STATUS_OK, service, 'active')
            if status.is_service_running(service) else
            (pymon.STATUS_CRITICAL, service, 'inactive')))
        xymon.section(
            'Daemon Status',\
            ''.join(service_status(service) for service in cluster_services))
        if not all(
                status.is_service_running(service)
                for service in cluster_services):
            xymon.color = pymon.STATUS_CRITICAL

    # message - footer
    check_script = os.path.basename(__file__)
    xymon.footer(check_script, __version__)

    xymon.send()