Skip to content

Commit

Permalink
Add better messaging for exceptions when loading broken inventory fil…
Browse files Browse the repository at this point in the history
…es. Fix: #152
  • Loading branch information
uberspot committed Jan 16, 2019
1 parent 6a20e41 commit 68b9906
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ def main():
help='exit with failure code if warnings exist, default is False')
lint_parser.add_argument('--skip-class-checks',
help='skip checking for unused classes, default is False',
default=from_dot_kapitan('secrets', 'skip-class-checks', False))
default=from_dot_kapitan('lint', 'skip-class-checks', False))
lint_parser.add_argument('--search-secrets',
default=from_dot_kapitan('lint', 'search-secrets', False),
action='store_true',
help='searches for plaintext secrets in inventory, default is False')
lint_parser.add_argument('--secrets-path',
help='set secrets path, default is "./secrets"',
default=from_dot_kapitan('secrets', 'secrets-path', './secrets'))
default=from_dot_kapitan('lint', 'secrets-path', './secrets'))
lint_parser.add_argument('--compiled-path',
default=from_dot_kapitan('lint', 'compiled-path', './compiled'),
help='set compiled path, default is "./compiled"')
Expand Down
6 changes: 3 additions & 3 deletions kapitan/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import logging
import os
import sys
from pprint import pformat
from sys import exit

from kapitan.utils import list_all_paths

Expand All @@ -30,7 +30,7 @@
def start_lint(fail_on_warning, skip_class_checks, inventory_path, search_secrets, secrets_path, compiled_path):
if skip_class_checks and not search_secrets:
logger.info("Nothing to check. Remove --skip-class-checks or add --search-secrets to lint secrets")
exit(0)
sys.exit(0)

status_class_checks = 0
if not skip_class_checks:
Expand All @@ -46,7 +46,7 @@ def start_lint(fail_on_warning, skip_class_checks, inventory_path, search_secret
status_secrets = lint_orphan_secrets(compiled_path, secrets_path)

if fail_on_warning and (status_secrets + status_class_checks) > 0:
exit(1)
sys.exit(1)


def lint_orphan_secrets(compiled_path, secrets_path):
Expand Down
23 changes: 17 additions & 6 deletions kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from kapitan.inputs.jsonnet import Jsonnet
from kapitan import cached

from reclass.errors import ReclassException

logger = logging.getLogger(__name__)


Expand All @@ -60,15 +62,17 @@ def compile_targets(inventory_path, search_paths, output_path, parallel, targets
logger.info("No changes since last compilation.")
return

target_objs = load_target_inventory(inventory_path, updated_targets)

pool = multiprocessing.Pool(parallel)
# append "compiled" to output_path so we can safely overwrite it
compile_path = os.path.join(output_path, "compiled")
worker = partial(compile_target, search_paths=search_paths, compile_path=temp_path, ref_controller=ref_controller,
**kwargs)

try:
target_objs = load_target_inventory(inventory_path, updated_targets)

# append "compiled" to output_path so we can safely overwrite it
compile_path = os.path.join(output_path, "compiled")
worker = partial(compile_target, search_paths=search_paths, compile_path=temp_path, ref_controller=ref_controller,
**kwargs)


if target_objs == []:
raise CompileError("Error: no targets found")
# compile_target() returns None on success
Expand Down Expand Up @@ -97,6 +101,13 @@ def compile_targets(inventory_path, search_paths, output_path, parallel, targets
# Save inventory and folders cache
save_inv_cache(compile_path, targets)
pool.close()

except ReclassException as e:
if isinstance(e, NotFoundError):
logger.error("Inventory reclass error: inventory not found")
else:
logger.error("Inventory reclass error: %s", e.message)
raise InventoryError(e.message)
except Exception as e:
# if compile worker fails, terminate immediately
pool.terminate()
Expand Down

0 comments on commit 68b9906

Please sign in to comment.