Ejemplo n.º 1
0
def show_package(name, show_info=False):
    # type: (PackageName, bool) -> None
    try:
        if name.endswith(_pac_ext):
            tar = tarfile.open(name, "r:gz")
            info = tar.extractfile("info")
            if info is None:
                raise PackageException("Failed to extract \"info\"")
            package = parse_package_info(info.read())
        else:
            this_package = read_package_info(name)
            if not this_package:
                raise PackageException("No such package %s." % name)

            package = this_package
            if show_info:
                sys.stdout.write("Package file:                  %s%s\n" %
                                 (package_dir(), name))
    except PackageException:
        raise
    except Exception as e:
        raise PackageException("Cannot open package %s: %s" % (name, e))

    if show_info:
        sys.stdout.write("Name:                          %s\n" %
                         package["name"])
        sys.stdout.write("Version:                       %s\n" %
                         package["version"])
        sys.stdout.write("Packaged on Checkmk Version:   %s\n" %
                         package["version.packaged"])
        sys.stdout.write("Required Checkmk Version:      %s\n" %
                         package["version.min_required"])
        valid_until_text = package[
            "version.usable_until"] or "No version limitation"
        sys.stdout.write("Valid until Checkmk version:   %s\n" %
                         valid_until_text)
        sys.stdout.write("Title:                         %s\n" %
                         package["title"])
        sys.stdout.write("Author:                        %s\n" %
                         package["author"])
        sys.stdout.write("Download-URL:                  %s\n" %
                         package["download_url"])
        sys.stdout.write("Files:                         %s\n" % \
                " ".join([ "%s(%d)" % (part, len(fs)) for part, fs in package["files"].items() ]))
        sys.stdout.write("Description:\n  %s\n" % package["description"])
    else:
        if logger.isEnabledFor(VERBOSE):
            sys.stdout.write("Files in package %s:\n" % name)
            for part in get_package_parts():
                files = package["files"].get(part.ident, [])
                if len(files) > 0:
                    sys.stdout.write("  %s%s%s:\n" %
                                     (tty.bold, part.title, tty.normal))
                    for f in files:
                        sys.stdout.write("    %s\n" % f)
        else:
            for part in get_package_parts():
                for fn in package["files"].get(part.ident, []):
                    sys.stdout.write(part.path + "/" + fn + "\n")
Ejemplo n.º 2
0
def clean_dirs():
    for part in packaging.get_package_parts():
        Path(part.path).mkdir(parents=True, exist_ok=True)

    yield

    for part in packaging.get_package_parts():
        shutil.rmtree(part.path)
Ejemplo n.º 3
0
def test_package_parts():
    assert sorted(packaging.get_package_parts()) == sorted([
        packaging.PackagePart(
            "agent_based", _("Agent based plugins (Checks, Inventory)"),
            str(cmk.utils.paths.local_agent_based_plugins_dir)),
        packaging.PackagePart('checks', _('Legacy check plugins'),
                              str(cmk.utils.paths.local_checks_dir)),
        packaging.PackagePart('notifications', _('Notification scripts'),
                              str(cmk.utils.paths.local_notifications_dir)),
        packaging.PackagePart('inventory', _('Legacy inventory plugins'),
                              str(cmk.utils.paths.local_inventory_dir)),
        packaging.PackagePart('checkman', _("Checks' man pages"),
                              str(cmk.utils.paths.local_check_manpages_dir)),
        packaging.PackagePart('agents', _('Agents'),
                              str(cmk.utils.paths.local_agents_dir)),
        packaging.PackagePart('web', _('GUI extensions'),
                              str(cmk.utils.paths.local_web_dir)),
        packaging.PackagePart('pnp-templates', _('PNP4Nagios templates'),
                              str(cmk.utils.paths.local_pnp_templates_dir)),
        packaging.PackagePart('doc', _('Documentation files'),
                              str(cmk.utils.paths.local_doc_dir)),
        packaging.PackagePart('locales', _('Localizations'),
                              str(cmk.utils.paths.local_locale_dir)),
        packaging.PackagePart('bin', _('Binaries'),
                              str(cmk.utils.paths.local_bin_dir)),
        packaging.PackagePart('lib', _('Libraries'),
                              str(cmk.utils.paths.local_lib_dir)),
        packaging.PackagePart('mibs', _('SNMP MIBs'),
                              str(cmk.utils.paths.local_mib_dir)),
        packaging.PackagePart(
            'alert_handlers', _('Alert handlers'),
            str(cmk.utils.paths.local_share_dir.joinpath('alert_handlers'))),
    ])
Ejemplo n.º 4
0
def package_pack(args: List[str]) -> None:
    if len(args) != 1:
        raise PackageException("Usage: check_mk -P pack NAME")

    # Make sure, user is not in data directories of Checkmk
    abs_curdir = os.path.abspath(os.curdir)
    for directory in [cmk.utils.paths.var_dir] + [
            p.path for p in get_package_parts() + get_config_parts()
    ]:
        if abs_curdir == directory or abs_curdir.startswith(directory + "/"):
            raise PackageException(
                "You are in %s!\n"
                "Please leave the directories of Check_MK before creating\n"
                "a packet file. Foreign files lying around here will mix up things."
                % abs_curdir)

    pacname = args[0]
    package = read_package_info(pacname)
    if not package:
        raise PackageException("Package %s not existing or corrupt." % pacname)
    tarfilename = packaging.format_file_name(name=pacname,
                                             version=package["version"])

    logger.log(VERBOSE, "Packing %s into %s...", pacname, tarfilename)
    with Path(tarfilename).open("wb") as f:
        packaging.write_file(package, cast(BinaryIO, f))
    logger.log(VERBOSE, "Successfully created %s", tarfilename)
Ejemplo n.º 5
0
def package_create(args: List[str]) -> None:
    if len(args) != 1:
        raise PackageException("Usage: check_mk -P create NAME")

    pacname = args[0]
    if read_package_info(pacname):
        raise PackageException("Package %s already existing." % pacname)

    logger.log(VERBOSE, "Creating new package %s...", pacname)
    package = get_initial_package_info(pacname)
    filelists = package["files"]
    num_files = 0
    for part in get_package_parts():
        files = unpackaged_files_in_dir(part.ident, part.path)
        filelists[part.ident] = files
        num_files += len(files)
        if len(files) > 0:
            logger.log(VERBOSE, "  %s%s%s:", tty.bold, part.title, tty.normal)
            for f in files:
                logger.log(VERBOSE, "    %s", f)

    write_package_info(package)
    logger.log(VERBOSE, "New package %s created with %d files.", pacname,
               num_files)
    logger.log(VERBOSE, "Please edit package details in %s%s%s", tty.bold,
               package_dir() / pacname, tty.normal)
Ejemplo n.º 6
0
def test_package_parts():
    assert packaging.get_package_parts() == [
        packaging.PackagePart('checks', 'Checks',
                              'local/share/check_mk/checks'),
        packaging.PackagePart('notifications', 'Notification scripts',
                              'local/share/check_mk/notifications'),
        packaging.PackagePart('inventory', 'Inventory plugins',
                              'local/share/check_mk/inventory'),
        packaging.PackagePart('checkman', "Checks' man pages",
                              'local/share/check_mk/checkman'),
        packaging.PackagePart('agents', 'Agents',
                              'local/share/check_mk/agents'),
        packaging.PackagePart('web', 'Multisite extensions',
                              'local/share/check_mk/web'),
        packaging.PackagePart('pnp-templates', 'PNP4Nagios templates',
                              'local/share/check_mk/pnp-templates'),
        packaging.PackagePart('doc', 'Documentation files',
                              'local/share/doc/check_mk'),
        packaging.PackagePart('locales', 'Localizations',
                              'local/share/check_mk/locale'),
        packaging.PackagePart('bin', 'Binaries', 'local/bin'),
        packaging.PackagePart('lib', 'Libraries', 'local/lib'),
        packaging.PackagePart('mibs', 'SNMP MIBs', 'local/share/snmp/mibs'),
        packaging.PackagePart('alert_handlers', 'Alert handlers',
                              'local/share/check_mk/alert_handlers'),
    ]
Ejemplo n.º 7
0
def show_package(name: PackageName, show_info: bool = False) -> None:
    try:
        if name.endswith(PACKAGE_EXTENSION):
            with tarfile.open(name, "r:gz") as tar:
                info = tar.extractfile("info")
            if info is None:
                raise PackageException('Failed to extract "info"')
            package = parse_package_info(info.read().decode())
        else:
            this_package = read_package_info(name)
            if not this_package:
                raise PackageException("No such package %s." % name)

            package = this_package
            if show_info:
                sys.stdout.write("Package file:                  %s\n" %
                                 (package_dir() / name))
    except PackageException:
        raise
    except Exception as e:
        raise PackageException("Cannot open package %s: %s" % (name, e))

    if show_info:
        sys.stdout.write("Name:                          %s\n" %
                         package["name"])
        sys.stdout.write("Version:                       %s\n" %
                         package["version"])
        sys.stdout.write("Packaged on Checkmk Version:   %s\n" %
                         package["version.packaged"])
        sys.stdout.write("Required Checkmk Version:      %s\n" %
                         package["version.min_required"])
        valid_until_text = package[
            "version.usable_until"] or "No version limitation"
        sys.stdout.write("Valid until Checkmk version:   %s\n" %
                         valid_until_text)
        sys.stdout.write("Title:                         %s\n" %
                         package["title"])
        sys.stdout.write("Author:                        %s\n" %
                         package["author"])
        sys.stdout.write("Download-URL:                  %s\n" %
                         package["download_url"])
        files = " ".join([
            "%s(%d)" % (part, len(fs))
            for part, fs in package["files"].items()
        ])
        sys.stdout.write("Files:                         %s\n" % files)
        sys.stdout.write("Description:\n  %s\n" % package["description"])
    else:
        if logger.isEnabledFor(VERBOSE):
            sys.stdout.write("Files in package %s:\n" % name)
            for part in get_package_parts():
                if part_files := package["files"].get(part.ident, []):
                    sys.stdout.write("  %s%s%s:\n" %
                                     (tty.bold, part.title, tty.normal))
                    for f in part_files:
                        sys.stdout.write("    %s\n" % f)
        else:
Ejemplo n.º 8
0
def test_package_parts():
    assert sorted(packaging.get_package_parts()) == sorted(
        [
            packaging.PackagePart(
                "agent_based",
                _("Agent based plugins (Checks, Inventory)"),
                str(cmk.utils.paths.local_agent_based_plugins_dir),
            ),
            packaging.PackagePart(
                "checks", _("Legacy check plugins"), str(cmk.utils.paths.local_checks_dir)
            ),
            packaging.PackagePart(
                "notifications",
                _("Notification scripts"),
                str(cmk.utils.paths.local_notifications_dir),
            ),
            packaging.PackagePart(
                "inventory", _("Legacy inventory plugins"), str(cmk.utils.paths.local_inventory_dir)
            ),
            packaging.PackagePart(
                "checkman", _("Checks' man pages"), str(cmk.utils.paths.local_check_manpages_dir)
            ),
            packaging.PackagePart("agents", _("Agents"), str(cmk.utils.paths.local_agents_dir)),
            packaging.PackagePart(
                "gui", _("GUI extensions"), str(cmk.utils.paths.local_gui_plugins_dir)
            ),
            packaging.PackagePart(
                "web", _("Legacy GUI extensions"), str(cmk.utils.paths.local_web_dir)
            ),
            packaging.PackagePart(
                "pnp-templates",
                _("PNP4Nagios templates (deprecated)"),
                str(cmk.utils.paths.local_pnp_templates_dir),
            ),
            packaging.PackagePart(
                "doc", _("Documentation files"), str(cmk.utils.paths.local_doc_dir)
            ),
            packaging.PackagePart(
                "locales", _("Localizations"), str(cmk.utils.paths.local_locale_dir)
            ),
            packaging.PackagePart("bin", _("Binaries"), str(cmk.utils.paths.local_bin_dir)),
            packaging.PackagePart("lib", _("Libraries"), str(cmk.utils.paths.local_lib_dir)),
            packaging.PackagePart("mibs", _("SNMP MIBs"), str(cmk.utils.paths.local_mib_dir)),
            packaging.PackagePart(
                "alert_handlers",
                _("Alert handlers"),
                str(cmk.utils.paths.local_share_dir.joinpath("alert_handlers")),
            ),
        ]
    )
Ejemplo n.º 9
0
def test_get_package_parts():
    assert sorted([p.ident for p in packaging.get_package_parts()]) == sorted([
        'agents',
        'alert_handlers',
        'bin',
        'checkman',
        'checks',
        'doc',
        'inventory',
        'lib',
        'locales',
        'mibs',
        'notifications',
        'pnp-templates',
        'web',
    ])
Ejemplo n.º 10
0
def package_find(_no_args: List[str]) -> None:
    first = True
    for part in get_package_parts() + get_config_parts():
        files = unpackaged_files_in_dir(part.ident, part.path)
        if len(files) > 0:
            if first:
                logger.log(VERBOSE, "Unpackaged files:")
                first = False

            logger.log(VERBOSE, "  %s%s%s:", tty.bold, part.title, tty.normal)
            for f in files:
                if logger.isEnabledFor(VERBOSE):
                    logger.log(VERBOSE, "    %s", f)
                else:
                    logger.info("%s/%s", part.path, f)

    if first:
        logger.log(VERBOSE, "No unpackaged files found.")
Ejemplo n.º 11
0
def test_get_package_parts():
    assert sorted([p.ident for p in packaging.get_package_parts()]) == sorted([
        "agent_based",
        "agents",
        "alert_handlers",
        "bin",
        "checkman",
        "checks",
        "doc",
        "inventory",
        "lib",
        "locales",
        "mibs",
        "notifications",
        "pnp-templates",
        "web",
        "gui",
    ])
Ejemplo n.º 12
0
            "%s(%d)" % (part, len(fs))
            for part, fs in package["files"].items()
        ])
        sys.stdout.write("Files:                         %s\n" % files)
        sys.stdout.write("Description:\n  %s\n" % package["description"])
    else:
        if logger.isEnabledFor(VERBOSE):
            sys.stdout.write("Files in package %s:\n" % name)
            for part in get_package_parts():
                if part_files := package["files"].get(part.ident, []):
                    sys.stdout.write("  %s%s%s:\n" %
                                     (tty.bold, part.title, tty.normal))
                    for f in part_files:
                        sys.stdout.write("    %s\n" % f)
        else:
            for part in get_package_parts():
                for fn in package["files"].get(part.ident, []):
                    sys.stdout.write(part.path + "/" + fn + "\n")


def package_create(args: List[str]) -> None:
    if len(args) != 1:
        raise PackageException("Usage: check_mk -P create NAME")

    pacname = args[0]
    if read_package_info(pacname):
        raise PackageException("Package %s already existing." % pacname)

    logger.log(VERBOSE, "Creating new package %s...", pacname)
    package = get_initial_package_info(pacname)
    filelists = package["files"]