Skip to content

Commit

Permalink
Merge pull request kapicorp#687 from ademariag/ignore-missing
Browse files Browse the repository at this point in the history
Ignore copy file on missing source
  • Loading branch information
uberspot authored Jan 31, 2021
2 parents 1c177d6 + b1a4cd8 commit a6e0ce7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
12 changes: 12 additions & 0 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,18 @@ For Copy, `input_paths` can be either a file or a directory: in case of a direct

*Supported output types*: N/A (no need to specify `output_type`)

Example

```yaml
kapitan:
compile:
- input_type: copy
ignore_missing: true # Do not error if path is missing. Defaults to False
input_paths:
- resources/state/${target_name}/.terraform.lock.hcl
output_path: terraform/
```

### Remove

This input type simply removes files or directories. This can be helpful if you can't control particular files
Expand Down
3 changes: 2 additions & 1 deletion kapitan/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def compile_obj(self, comp_obj, ext_vars, **kwargs):
inputs = list(itertools.chain.from_iterable(globbed_paths))
# remove duplicate inputs
inputs = set(inputs)
if len(inputs) == 0:
ignore_missing = comp_obj.get("ignore_missing", False)
if len(inputs) == 0 and not ignore_missing:
raise CompileError(
"Compile error: {} for target: {} not found in "
"search_paths: {}".format(input_path, ext_vars["target"], self.search_paths)
Expand Down
24 changes: 15 additions & 9 deletions kapitan/inputs/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@


class Copy(InputType):
def __init__(self, compile_path, search_paths, ref_controller):
def __init__(self, compile_path, search_paths, ref_controller, ignore_missing=False):
self.ignore_missing = ignore_missing
super().__init__("copy", compile_path, search_paths, ref_controller)

def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
Expand All @@ -25,17 +26,22 @@ def compile_file(self, file_path, compile_path, ext_vars, **kwargs):
path can be either a file or directory.
"""

# Whether to fail silently if the path does not exists.
ignore_missing = self.ignore_missing
try:
logger.debug("Copying {} to {}.".format(file_path, compile_path))
if os.path.isfile(file_path):
if os.path.isfile(compile_path):
shutil.copy2(file_path, compile_path)
if os.path.exists(file_path):
if os.path.isfile(file_path):
if os.path.isfile(compile_path):
shutil.copy2(file_path, compile_path)
else:
os.makedirs(compile_path, exist_ok=True)
shutil.copy2(file_path, os.path.join(compile_path, os.path.basename(file_path)))
else:
os.makedirs(compile_path, exist_ok=True)
shutil.copy2(file_path, os.path.join(compile_path, os.path.basename(file_path)))
else:
compile_path = os.path.abspath(compile_path) # Resolve relative paths
copy_tree(file_path, compile_path)
compile_path = os.path.abspath(compile_path) # Resolve relative paths
copy_tree(file_path, compile_path)
elif ignore_missing == False:
raise OSError(f"Path {file_path} does not exist and `ignore_missing` is {ignore_missing}")
except OSError as e:
logger.exception(f"Input dir not copied. Error: {e}")

Expand Down
3 changes: 2 additions & 1 deletion kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ def compile_target(target_obj, search_paths, compile_path, ref_controller, **kwa
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)
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":
Expand Down
21 changes: 21 additions & 0 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
compile_path = os.path.join(test_path, "output")
file_path = os.path.join(test_path, "input")
test_file_path = os.path.join(file_path, "test_copy_input")
test_file_missing_path = os.path.join(file_path, "test_copy_input_missing")
test_file_compiled_path = os.path.join(compile_path, "test_copy_input")
test_file_content = """
apiVersion: v1
Expand Down Expand Up @@ -81,6 +82,26 @@ def tearDown(self):
pass


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


class CompileCopyTest(unittest.TestCase):
def setUp(self):
os.chdir(os.path.join(os.getcwd(), "examples", "kubernetes"))
Expand Down

0 comments on commit a6e0ce7

Please sign in to comment.