def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write items in path as jinja2 rendered files to compile_path. path can be either a file or directory. kwargs: reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled """ reveal = kwargs.get('reveal', False) target_name = kwargs.get('target_name', None) # set ext_vars and inventory for jinja2 context context = ext_vars.copy() context["inventory"] = inventory(self.search_paths, target_name) context["inventory_global"] = inventory(self.search_paths, None) jinja2_filters = kwargs.get('jinja2_filters') for item_key, item_value in render_jinja2(file_path, context, jinja2_filters=jinja2_filters).items(): full_item_path = os.path.join(compile_path, item_key) os.makedirs(os.path.dirname(full_item_path), exist_ok=True) with CompiledFile(full_item_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name) as fp: fp.write(item_value["content"]) mode = item_value["mode"] os.chmod(full_item_path, mode) logger.debug("Wrote %s with mode %.4o", full_item_path, mode)
def test_inventory_context(self): with tempfile.NamedTemporaryFile() as f: f.write("{{inventory.parameters.cluster.name}}".encode("UTF-8")) cluster_name = "minikube" target_name = "minikube-es" inv = inventory(["examples/kubernetes"], target_name) context = {"inventory": inv} f.seek(0) self.assertEqual(render_jinja2_file(f.name, context), cluster_name)
def test_inventory_global_context(self): with tempfile.NamedTemporaryFile() as f: target_name = "minikube-es" f.write("{{inventory_global[\"%s\"].parameters.cluster.name}}".encode("UTF-8") % target_name.encode("UTF-8")) cluster_name = "minikube" inv_global = inventory("examples/kubernetes", None) context = {"inventory_global": inv_global} f.seek(0) self.assertEqual(render_jinja2_file(f.name, context), cluster_name)
def compile_target_file(target_file, search_path, compile_path, **kwargs): """ Loads target file, compiles file (by scanning search_path) and writes to compile_path """ target_obj = load_target(target_file) target_name = target_obj["vars"]["target"] compile_obj = target_obj["compile"] ext_vars = target_obj["vars"] for obj in compile_obj: if obj["type"] == "jsonnet": compile_file_sp = os.path.join(search_path, obj["path"]) if os.path.exists(compile_file_sp): _compile_path = os.path.join(compile_path, target_name, obj["name"]) os.makedirs(_compile_path) logger.debug("Compiling %s", compile_file_sp) compile_jsonnet(compile_file_sp, _compile_path, search_path, ext_vars, output=obj["output"], **kwargs) else: raise IOError("Path not found in search_path: %s" % obj["path"]) if obj["type"] == "jinja2": compile_path_sp = os.path.join(search_path, obj["path"]) if os.path.exists(compile_path_sp): _compile_path = os.path.join(compile_path, target_name, obj["name"]) os.makedirs(_compile_path) # copy ext_vars to dedicated jinja2 context so we can update it ctx = ext_vars.copy() ctx["inventory"] = inventory(search_path, target_name) ctx["inventory_global"] = inventory(search_path, None) compile_jinja2(compile_path_sp, ctx, _compile_path, **kwargs) else: raise IOError("Path not found in search_path: %s" % obj["path"]) logger.info("Compiled %s", target_file)
def test_custom_filter_jinja2(self): with tempfile.NamedTemporaryFile() as f: f.write("{{ inventory.parameters.cluster.name | custom_jinja2_filter }}".encode("UTF-8")) cluster_name = "minikube" target_name = "minikube-es" inv = inventory(["examples/kubernetes"], target_name) context = {"inventory": inv} f.seek(0) actual_output = render_jinja2_file( f.name, context, "./examples/kubernetes/lib/custom_jinja2_filter.py" ) expected_output = base64_encode(cluster_name) self.assertEqual(actual_output, expected_output)
def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write items in path as jinja2 rendered files to compile_path. path can be either a file or directory. kwargs: reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled """ reveal = kwargs.get("reveal", False) target_name = kwargs.get("target_name", None) # set ext_vars and inventory for jinja2 context context = ext_vars.copy() context["inventory"] = inventory(self.search_paths, target_name) context["inventory_global"] = inventory(self.search_paths, None) context["input_params"] = self.input_params # reset between each compile if jinja2 component is used multiple times self.input_params = {} jinja2_filters = kwargs.get("jinja2_filters") for item_key, item_value in render_jinja2( file_path, context, jinja2_filters=jinja2_filters, search_paths=self.search_paths).items(): full_item_path = os.path.join(compile_path, item_key) with CompiledFile(full_item_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name) as fp: fp.write(item_value["content"]) mode = item_value["mode"] os.chmod(full_item_path, mode) logger.debug("Wrote %s with mode %.4o", full_item_path, mode)
def compile_target(target_obj, search_paths, compile_path, **kwargs): """Compiles target_obj and writes to compile_path""" start = time.time() ext_vars = target_obj["vars"] target_name = ext_vars["target"] compile_obj = target_obj["compile"] for obj in compile_obj: input_type = obj["input_type"] input_paths = obj["input_paths"] output_path = obj["output_path"] if input_type == "jsonnet": _compile_path = os.path.join(compile_path, target_name, output_path) # support writing to an already existent dir try: os.makedirs(_compile_path) except OSError as ex: # If directory exists, pass if ex.errno == errno.EEXIST: pass output_type = obj[ "output_type"] # output_type is mandatory in jsonnet for input_path in input_paths: jsonnet_file_found = False for path in search_paths: compile_file_sp = os.path.join(path, input_path) if os.path.exists(compile_file_sp): jsonnet_file_found = True logger.debug("Compiling %s", compile_file_sp) try: compile_jsonnet(compile_file_sp, _compile_path, search_paths, ext_vars, output=output_type, target_name=target_name, **kwargs) except CompileError as e: logger.error( "Compile error: failed to compile target: %s", target_name) raise e if not jsonnet_file_found: logger.error( "Compile error: %s for target: %s not found in " + "search_paths: %s", input_path, target_name, search_paths) raise CompileError() if input_type == "jinja2": _compile_path = os.path.join(compile_path, target_name, output_path) # support writing to an already existent dir try: os.makedirs(_compile_path) except OSError as ex: # If directory exists, pass if ex.errno == errno.EEXIST: pass for input_path in input_paths: jinja2_file_found = False for path in search_paths: compile_path_sp = os.path.join(path, input_path) if os.path.exists(compile_path_sp): jinja2_file_found = True # copy ext_vars to dedicated jinja2 context so we can update it ctx = ext_vars.copy() ctx["inventory"] = inventory(search_paths, target_name) ctx["inventory_global"] = inventory(search_paths, None) try: compile_jinja2(compile_path_sp, ctx, _compile_path, target_name=target_name, **kwargs) except CompileError as e: logger.error( "Compile error: failed to compile target: %s", target_name) raise e if not jinja2_file_found: logger.error( "Compile error: %s for target: %s not found in " + "search_paths: %s", input_path, target_name, search_paths) raise CompileError() logger.info("Compiled %s (%.2fs)", target_name, time.time() - start)
def test_inventory_all_targets(self): inv = inventory(["examples/kubernetes"], None) self.assertNotEqual(inv.get("minikube-es"), None)
def test_inventory_target(self): inv = inventory(["examples/kubernetes"], "minikube-es") self.assertEqual(inv["parameters"]["cluster"]["name"], "minikube")