def package_pack(args): # type: (List[str]) -> None if len(args) != 1: raise PackageException("Usage: check_mk -P pack NAME") # Make sure, user is not in data directories of Check_MK 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 = "%s-%s%s" % (pacname, package["version"], _pac_ext) logger.log(VERBOSE, "Packing %s into %s...", pacname, tarfilename) with Path(tarfilename).open("wb") as f: create_mkp_file(package, cast(BinaryIO, f)) logger.log(VERBOSE, "Successfully created %s", tarfilename)
def test_get_optional_package_infos(monkeypatch, tmp_path): mkp_dir = tmp_path.joinpath("optional_packages") mkp_dir.mkdir(parents=True, exist_ok=True) monkeypatch.setattr(cmk.utils.paths, "optional_packages_dir", mkp_dir) # Create package _create_simple_test_package("optional") package_info = _read_package_info("optional") # Write MKP file mkp_path = mkp_dir.joinpath("optional.mkp") with mkp_path.open("wb") as mkp: packaging.create_mkp_file(package_info, mkp) assert packaging.get_optional_package_infos() == {"optional.mkp": package_info}
def test_create_mkp_file(): package_info = _create_simple_test_package("aaa") mkp = BytesIO() packaging.create_mkp_file(package_info, mkp) mkp.seek(0) tar = tarfile.open(fileobj=mkp, mode="r:gz") assert sorted(tar.getnames()) == sorted( ["info", "info.json", "checks.tar"]) info = ast.literal_eval(six.ensure_str(tar.extractfile("info").read())) assert info["name"] == "aaa" info2 = json.loads(tar.extractfile("info.json").read()) assert info2["name"] == "aaa"
def test_install_package_by_path(tmp_path): # Create _create_simple_test_package("aaa") package_info = packaging.read_package_info("aaa") # Write MKP file mkp_path = tmp_path.joinpath("aaa.mkp") with mkp_path.open("wb") as mkp: packaging.create_mkp_file(package_info, mkp) # Remove files from local hierarchy packaging.remove_package(package_info) assert packaging._package_exists("aaa") is False # And now install the package from memory packaging.install_package_by_path(mkp_path) # Check result assert packaging._package_exists("aaa") is True package_info = packaging.read_package_info("aaa") assert package_info["version"] == "1.0" assert package_info["files"]["checks"] == ["aaa"] assert cmk.utils.paths.local_checks_dir.joinpath("aaa").exists()
def test_install_package(): # Create _create_simple_test_package("aaa") package_info = packaging.read_package_info("aaa") # Build MKP in memory mkp = BytesIO() packaging.create_mkp_file(package_info, mkp) mkp.seek(0) # Remove files from local hierarchy packaging.remove_package(package_info) assert packaging._package_exists("aaa") is False # And now install the package from memory packaging.install_package(mkp) # Check result assert packaging._package_exists("aaa") is True package_info = packaging.read_package_info("aaa") assert package_info["version"] == "1.0" assert package_info["files"]["checks"] == ["aaa"] assert cmk.utils.paths.local_checks_dir.joinpath("aaa").exists()