Esempio n. 1
0
    def setUp(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass

        self.copy_compiler = Copy(compile_path, search_path, ref_controller)
Esempio n. 2
0
class RemoveTest(unittest.TestCase):
    def setUp(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass

        self.copy_compiler = Copy(compile_path, search_path, ref_controller)
        self.remove_compiler = Remove(compile_path, search_path, ref_controller)

    def test_remove_file_folder(self):
        test_dirs_bootstrap_helper()
        self.copy_compiler.compile_file(test_file_path, compile_path, None)

        self.assertTrue(os.path.exists(test_file_compiled_path))
        self.remove_compiler.compile_file(test_file_compiled_path, compile_path, None)
        self.assertFalse(os.path.exists(test_file_compiled_path))

    def test_remove_folder_folder(self):
        test_dirs_bootstrap_helper()
        self.copy_compiler.compile_file(file_path, compile_path, None)

        self.assertTrue(os.path.exists(compile_path))
        self.remove_compiler.compile_file(compile_path, compile_path, None)
        self.assertFalse(os.path.exists(compile_path))

    def tearDown(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass
Esempio n. 3
0
class CopyTest(unittest.TestCase):
    def setUp(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass

        self.copy_compiler = Copy(compile_path, search_path, ref_controller)

    def test_copy_file_folder(self):
        test_dirs_bootstrap_helper()
        self.copy_compiler.compile_file(test_file_path, compile_path, None)
        self.test_file_hash = hashlib.sha1(test_file_content.encode()).digest()
        with open(test_file_compiled_path) as f:
            test_file_compiled_hash = hashlib.sha1(f.read().encode()).digest()
            self.assertEqual(self.test_file_hash, test_file_compiled_hash)

    def test_copy_folder_folder(self):
        test_dirs_bootstrap_helper()
        self.copy_compiler.compile_file(file_path, compile_path, None)
        file_path_hash = directory_hash(file_path)
        compile_path_hash = directory_hash(compile_path)
        self.assertEqual(file_path_hash, compile_path_hash)

    def tearDown(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass
Esempio n. 4
0
def compile_target(target_obj,
                   search_paths,
                   compile_path,
                   ref_controller,
                   globals_cached=None,
                   **kwargs):
    """Compiles target_obj and writes to compile_path"""
    start = time.time()
    compile_objs = target_obj["compile"]
    ext_vars = target_obj["vars"]
    target_name = ext_vars["target"]

    if globals_cached:
        cached.from_dict(globals_cached)

    for comp_obj in compile_objs:
        input_type = comp_obj["input_type"]
        output_path = comp_obj["output_path"]

        if input_type == "jinja2":
            input_compiler = Jinja2(compile_path, search_paths, ref_controller)
            if "input_params" in comp_obj:
                input_compiler.set_input_params(comp_obj["input_params"])
        elif input_type == "jsonnet":
            input_compiler = Jsonnet(compile_path, search_paths,
                                     ref_controller)
        elif input_type == "kadet":
            input_compiler = Kadet(compile_path, search_paths, ref_controller)
            if "input_params" in comp_obj:
                input_compiler.set_input_params(comp_obj["input_params"])
        elif input_type == "helm":
            input_compiler = Helm(compile_path, search_paths, ref_controller,
                                  comp_obj)
        elif input_type == "copy":
            ignore_missing = comp_obj.get("ignore_missing", False)
            input_compiler = Copy(compile_path, search_paths, ref_controller,
                                  ignore_missing)
        elif input_type == "remove":
            input_compiler = Remove(compile_path, search_paths, ref_controller)
        elif input_type == "external":
            input_compiler = External(compile_path, search_paths,
                                      ref_controller)
            if "args" in comp_obj:
                input_compiler.set_args(comp_obj["args"])
            if "env_vars" in comp_obj:
                input_compiler.set_env_vars(comp_obj["env_vars"])
        else:
            err_msg = 'Invalid input_type: "{}". Supported input_types: jsonnet, jinja2, kadet, helm, copy, remove, external'
            raise CompileError(err_msg.format(input_type))

        # logger.info("about to compile %s ", target_obj["target_full_path"])
        input_compiler.make_compile_dirs(target_name, output_path)
        input_compiler.compile_obj(comp_obj, ext_vars, **kwargs)

    logger.info("Compiled %s (%.2fs)", target_obj["target_full_path"],
                time.time() - start)
Esempio n. 5
0
def compile_target(target_obj, search_paths, compile_path, ref_controller,
                   **kwargs):
    """Compiles target_obj and writes to compile_path"""
    start = time.time()
    compile_objs = target_obj["compile"]
    ext_vars = target_obj["vars"]
    target_name = ext_vars["target"]

    for comp_obj in compile_objs:
        input_type = comp_obj["input_type"]
        output_path = comp_obj["output_path"]

        if input_type == "jinja2":
            input_compiler = Jinja2(compile_path, search_paths, ref_controller)
        elif input_type == "jsonnet":
            input_compiler = Jsonnet(compile_path, search_paths,
                                     ref_controller)
        elif input_type == "kadet":
            input_compiler = Kadet(compile_path, search_paths, ref_controller)
            if "input_params" in comp_obj:
                input_compiler.set_input_params(comp_obj["input_params"])
        elif input_type == "helm":
            input_compiler = Helm(compile_path, search_paths, ref_controller)
            if "helm_values" in comp_obj:
                input_compiler.dump_helm_values(comp_obj["helm_values"])
            if "helm_params" in comp_obj:
                input_compiler.set_helm_params(comp_obj["helm_params"])
            if "helm_values_files" in comp_obj:
                input_compiler.set_helm_values_files(
                    comp_obj["helm_values_files"])
            if "kube_version" in comp_obj:
                input_compiler.set_kube_version(comp_obj["kube_version"])
        elif input_type == "copy":
            input_compiler = Copy(compile_path, search_paths, ref_controller)
        elif input_type == "remove":
            input_compiler = Remove(compile_path, search_paths, ref_controller)
        elif input_type == "external":
            input_compiler = External(compile_path, search_paths,
                                      ref_controller)
            if "args" in comp_obj:
                input_compiler.set_args(comp_obj["args"])
            if "env_vars" in comp_obj:
                input_compiler.set_env_vars(comp_obj["env_vars"])
        else:
            err_msg = 'Invalid input_type: "{}". Supported input_types: jsonnet, jinja2, kadet, helm, copy, remove, external'
            raise CompileError(err_msg.format(input_type))

        input_compiler.make_compile_dirs(target_name, output_path)
        input_compiler.compile_obj(comp_obj, ext_vars, **kwargs)

    logger.info("Compiled %s (%.2fs)", target_obj["target_full_path"],
                time.time() - start)
Esempio n. 6
0
class CopyMissingFileTest(unittest.TestCase):
    def setUp(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass

        self.copy_compiler = Copy(compile_path,
                                  search_path,
                                  ref_controller,
                                  ignore_missing=True)

    def test_copy_missing_path_folder(self):
        test_dirs_bootstrap_helper()
        self.copy_compiler.compile_file(test_file_missing_path, compile_path,
                                        None)

    def tearDown(self):
        try:
            shutil.rmtree(test_path)
        except FileNotFoundError:
            pass
Esempio n. 7
0
def compile_target(target_obj, search_paths, compile_path, ref_controller, inventory_path, **kwargs):
    """Compiles target_obj and writes to compile_path"""
    start = time.time()
    compile_objs = target_obj["compile"]
    ext_vars = target_obj["vars"]
    target_name = ext_vars["target"]

    jinja2_compiler = Jinja2(compile_path, search_paths, ref_controller, inventory_path)
    jsonnet_compiler = Jsonnet(compile_path, search_paths, ref_controller)
    kadet_compiler = Kadet(compile_path, search_paths, ref_controller)
    helm_compiler = Helm(compile_path, search_paths, ref_controller)
    copy_compiler = Copy(compile_path, search_paths, ref_controller)
    remove_compiler = Remove(compile_path, search_paths, ref_controller)

    for comp_obj in compile_objs:
        input_type = comp_obj["input_type"]
        output_path = comp_obj["output_path"]
        if input_type == "jinja2":
            input_compiler = jinja2_compiler
        elif input_type == "jsonnet":
            input_compiler = jsonnet_compiler
        elif input_type == "kadet":
            input_compiler = kadet_compiler
            if "input_params" in comp_obj:
                kadet_compiler.set_input_params(comp_obj["input_params"])
        elif input_type == "helm":
            if "helm_values" in comp_obj:
                helm_compiler.dump_helm_values(comp_obj["helm_values"])
            if "helm_params" in comp_obj:
                helm_compiler.set_helm_params(comp_obj["helm_params"])
            if "helm_values_files" in comp_obj:
                helm_compiler.set_helm_values_files(comp_obj["helm_values_files"])
            input_compiler = helm_compiler
        elif input_type == "copy":
            input_compiler = copy_compiler
        elif input_type == "remove":
            input_compiler = remove_compiler
        else:
            err_msg = (
                'Invalid input_type: "{}". Supported input_types: jsonnet, jinja2, kadet, helm, copy, remove'
            )
            raise CompileError(err_msg.format(input_type))

        input_compiler.make_compile_dirs(target_name, output_path)
        input_compiler.compile_obj(comp_obj, ext_vars, **kwargs)

    logger.info("Compiled %s (%.2fs)", target_obj["target_full_path"], time.time() - start)