Skip to content

Commit

Permalink
Selective compilation of targets
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianchifor committed Mar 16, 2018
1 parent 7f3b746 commit 9f57d55
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ optional arguments:
--quiet set quiet mode, only critical output
--output-path PATH set output path, default is "."
--target-path PATH set target path, default is "./targets"
--targets TARGETS [TARGETS ...], -t TARGETS [TARGETS ...]
targets to compile, default is all
--parallelism INT, -p INT
Number of concurrent compile processes, default is 4
--secrets-path SECRETS_PATH
Expand Down
4 changes: 3 additions & 1 deletion kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def main():
compile_parser.add_argument('--target-path', type=str, default='targets',
metavar='PATH',
help='set target path, default is "./targets"')
compile_parser.add_argument('--targets', '-t', help='targets to compile, default is all',
type=str, nargs='+', default=[], metavar='TARGET')
compile_parser.add_argument('--parallelism', '-p', type=int,
default=4, metavar='INT',
help='Number of concurrent compile processes, default is 4')
Expand Down Expand Up @@ -156,7 +158,7 @@ def main():
gpg_obj = secret_gpg_backend()
if args.target_path:
compile_targets(args.target_path, args.inventory_path, search_path,
args.output_path, args.parallelism,
args.output_path, args.parallelism, args.targets,
prune=(not args.no_prune), secrets_path=args.secrets_path,
secrets_reveal=args.reveal, gpg_obj=gpg_obj)
else:
Expand Down
39 changes: 30 additions & 9 deletions kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
logger = logging.getLogger(__name__)


def compile_targets(target_path, inventory_path, search_path, output_path, parallel, **kwargs):
def compile_targets(target_path, inventory_path, search_path, output_path, parallel, targets, **kwargs):
"""
Searches and loads target files in target_path and runs compile_target_file() on a
multiprocessing pool with parallel number of processes.
Expand All @@ -52,18 +52,33 @@ def compile_targets(target_path, inventory_path, search_path, output_path, paral
# append "compiled" to output_path so we can safely overwrite it
compile_path = os.path.join(output_path, "compiled")
worker = partial(compile_target, search_path=search_path, compile_path=temp_path, **kwargs)
target_objs = load_target_files(target_path)
target_objs.extend(load_target_inventory(inventory_path))

target_objs = load_target_inventory(inventory_path, targets)
if not targets:
target_objs.extend(load_target_files(target_path))

try:
if target_objs == []:
logger.error("Error: no targets found")
raise KapitanError("Error: no targets found")
pool.map(worker, target_objs)

if os.path.exists(compile_path):
shutil.rmtree(compile_path)
# on success, copy temp_path into compile_path
shutil.copytree(temp_path, compile_path)
logger.debug("Copied %s into %s", temp_path, compile_path)
# if '-t' is set on compile, only override selected targets
if targets:
for target in targets:
compile_path_target = os.path.join(compile_path, target)
temp_path_target = os.path.join(temp_path, target)

shutil.rmtree(compile_path_target)
shutil.copytree(temp_path_target, compile_path_target)
logger.debug("Copied %s into %s", temp_path_target, compile_path_target)
# otherwise override all targets
else:
shutil.rmtree(compile_path)
shutil.copytree(temp_path, compile_path)
logger.debug("Copied %s into %s", temp_path, compile_path)

except Exception as e:
# if compile worker fails, terminate immediately
pool.terminate()
Expand Down Expand Up @@ -100,11 +115,17 @@ def load_target_files(target_path):
return map(load_target_file, target_files)


def load_target_inventory(inventory_path):
def load_target_inventory(inventory_path, targets):
"retuns a list of target objects from the inventory"
target_objs = []
inv = inventory_reclass(inventory_path)
for target_name in inv['nodes']:

targets_list = inv['nodes']
# if '-t' is set on compile, only loop through selected targets
if targets:
targets_list = targets

for target_name in targets_list:
try:
target_obj = inv['nodes'][target_name]['parameters']['kapitan']
valid_target_obj(target_obj)
Expand Down

0 comments on commit 9f57d55

Please sign in to comment.