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)
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")
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:
def packaging_usage(): sys.stdout.write("""Usage: check_mk [-v] -P|--package COMMAND [ARGS] Available commands are: create NAME ... Collect unpackaged files into new package NAME pack NAME ... Create package file from installed package release NAME ... Drop installed package NAME, release packaged files find ... Find and display unpackaged files list ... List all installed packages list NAME ... List files of installed package list PACK.mkp ... List files of uninstalled package file show NAME ... Show information about installed package show PACK.mkp ... Show information about uninstalled package file install PACK.mkp ... Install or update package from file PACK.mkp remove NAME ... Uninstall package NAME -v enables verbose output Package files are located in %s. """ % package_dir())
def package_dir(): packaging.package_dir().mkdir(parents=True, exist_ok=True) yield shutil.rmtree(str(packaging.package_dir()))
def test_package_dir(): assert isinstance(packaging.package_dir(), Path)
def test_diagnostics_element_local_files_csv_content(tmp_path, _collectors): diagnostics_element = diagnostics.LocalFilesCSVDiagnosticsElement() check_dir = cmk.utils.paths.local_checks_dir def create_test_package(name): check_dir.mkdir(parents=True, exist_ok=True) with check_dir.joinpath(name).open("w", encoding="utf-8") as f: f.write("test-check\n") package_info = packaging.get_initial_package_info(name) package_info["files"] = { "checks": [name], } packaging.create(package_info) packaging.package_dir().mkdir(parents=True, exist_ok=True) name = "test-package-csv" create_test_package(name) tmppath = Path(tmp_path).joinpath("tmp") filepath = next(diagnostics_element.add_or_get_files(tmppath, _collectors)) assert isinstance(filepath, Path) assert filepath == tmppath.joinpath("local_files.csv") column_headers = [ "path", "exists", "package", "author", "description", "download_url", "name", "title", "version", "version.min_required", "version.packaged", "version.usable_until", "permissions", "installed", "optional_packages", "unpackaged", ] with open(filepath, newline="") as csvfile: csvreader = csv.DictReader(csvfile, delimiter=";", quotechar="'") csvdata = {} for row in csvreader: csvdata[row["path"]] = row last_row = row assert sorted(last_row.keys()) == sorted(column_headers) path = check_dir.joinpath(name) assert str(path) in csvdata assert csvdata[str(path)]["permissions"] == "420" assert csvdata[str(path)]["unpackaged"] == "N/A" assert csvdata[str(path)]["exists"] == "file" assert csvdata[str(path)]["installed"] == "YES" shutil.rmtree(str(packaging.package_dir()))
def test_diagnostics_element_local_files_json_content(tmp_path, _collectors): diagnostics_element = diagnostics.LocalFilesJSONDiagnosticsElement() def create_test_package(name): check_dir = cmk.utils.paths.local_checks_dir check_dir.mkdir(parents=True, exist_ok=True) with check_dir.joinpath(name).open("w", encoding="utf-8") as f: f.write("test-check\n") package_info = packaging.get_initial_package_info(name) package_info["files"] = { "checks": [name], } packaging.create(package_info) packaging.package_dir().mkdir(parents=True, exist_ok=True) name = "test-package-json" create_test_package(name) tmppath = Path(tmp_path).joinpath("tmp") filepath = next(diagnostics_element.add_or_get_files(tmppath, _collectors)) assert isinstance(filepath, Path) assert filepath == tmppath.joinpath("local_files.json") info_keys = [ "installed", "unpackaged", "parts", "optional_packages", "disabled_packages", ] content = json.loads(filepath.open().read()) assert sorted(content.keys()) == sorted(info_keys) installed_keys = [name] assert sorted(content["installed"].keys()) == sorted(installed_keys) assert content["installed"][name]["files"] == {"checks": [name]} unpackaged_keys = [ "agent_based", "agents", "alert_handlers", "bin", "checkman", "checks", "doc", "ec_rule_packs", "inventory", "lib", "locales", "mibs", "notifications", "pnp-templates", "web", "gui", ] assert sorted(content["unpackaged"].keys()) == sorted(unpackaged_keys) for key in unpackaged_keys: assert content["unpackaged"][key] == [] parts_keys = [ "agent_based", "agents", "alert_handlers", "bin", "checkman", "checks", "doc", "ec_rule_packs", "inventory", "lib", "locales", "mibs", "notifications", "pnp-templates", "web", "gui", ] assert sorted(content["parts"].keys()) == sorted(parts_keys) part_keys = [ "files", "path", "permissions", "title", ] for key in parts_keys: assert sorted(content["parts"][key].keys()) == sorted(part_keys) if key == "checks": assert content["parts"][key]["files"] == [name] assert content["parts"][key]["permissions"] == [420] else: assert content["parts"][key]["files"] == [] assert content["parts"][key]["permissions"] == [] assert content["optional_packages"] == {} shutil.rmtree(str(packaging.package_dir()))