Skip to content

Commit

Permalink
add provider_label support to execute and extend commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rplevka committed Apr 18, 2024
1 parent 254fc1e commit d04c1d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
37 changes: 31 additions & 6 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def wrapper(*args, **kwargs):
return decorator


def parse_labels(provider_labels):
"""Parse the provided label string and returns labels in a dict."""
return {
label[0]: "=".join(label[1:])
for label in [kv_pair.split("=") for kv_pair in provider_labels.split(",")]
}


class ExceptionHandler(click.Group):
"""Wraps click group to catch and handle raised exceptions."""

Expand Down Expand Up @@ -217,10 +225,7 @@ def checkout(ctx, background, nick, count, args_file, provider_labels, **kwargs)
if args_file:
broker_args["args_file"] = args_file
if provider_labels:
broker_args["provider_labels"] = {
label[0]: "=".join(label[1:])
for label in [kv_pair.split("=") for kv_pair in provider_labels.split(",")]
}
broker_args["provider_labels"] = parse_labels(provider_labels)

# if additional arguments were passed, include them in the broker args
# strip leading -- characters
Expand Down Expand Up @@ -303,13 +308,23 @@ def inventory(details, sync, filter):
@click.option("--all", "all_", is_flag=True, help="Select all VMs")
@click.option("--sequential", is_flag=True, help="Run extends sequentially")
@click.option("--filter", type=str, help="Extend only what matches the specified filter")
@click.option(
"-l",
"--provider-labels",
type=str,
help="A string representing the list"
" of k=v pairs (comma-separated) to be used as provider resource"
" labels (e.g. '-l k1=v1,k2=v2,k3=v3=z4').",
)
@provider_options
def extend(vm, background, all_, sequential, filter, **kwargs):
def extend(vm, provider_labels, background, all_, sequential, filter, **kwargs):
"""Extend a host's lease time.
COMMAND: broker extend <vm hostname>|<vm name>|<local id>|--all
"""
broker_args = helpers.clean_dict(kwargs)
if provider_labels:
broker_args["provider_labels"] = parse_labels(provider_labels)
if background:
helpers.fork_broker()
inventory = helpers.load_inventory(filter=filter)
Expand Down Expand Up @@ -361,14 +376,22 @@ def duplicate(vm, background, count, all_, filter):
type=click.Choice(["merge", "last"]),
help="AnsibleTower: return artifacts associated with the execution.",
)
@click.option(
"-l",
"--provider-labels",
type=str,
help="A string representing the list"
" of k=v pairs (comma-separated) to be used as provider resource"
" labels (e.g. '-l k1=v1,k2=v2,k3=v3=z4').",
)
@click.option(
"--args-file",
type=click.Path(exists=True),
help="A json or yaml file mapping arguments to values",
)
@provider_options
@click.pass_context
def execute(ctx, background, nick, output_format, artifacts, args_file, **kwargs):
def execute(ctx, background, nick, output_format, artifacts, args_file, provider_labels, **kwargs):
"""Execute an arbitrary provider action.
COMMAND: broker execute --workflow "workflow-name" --workflow_arg1 something
Expand All @@ -382,6 +405,8 @@ def execute(ctx, background, nick, output_format, artifacts, args_file, **kwargs
broker_args["artifacts"] = artifacts
if args_file:
broker_args["args_file"] = args_file
if provider_labels:
broker_args["provider_labels"] = parse_labels(provider_labels)
# if additional arguments were passed, include them in the broker args
# strip leading -- characters
broker_args.update(
Expand Down
3 changes: 2 additions & 1 deletion broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def get_inventory(self, user=None):
compiled_host_info = [self._compile_host_info(host) for host in hosts_bar]
return compiled_host_info

def extend(self, target_vm, new_expire_time=None):
def extend(self, target_vm, new_expire_time=None, provider_labels=None):
"""Run the extend workflow with defaults args.
:param target_vm: This should be a host object
Expand All @@ -668,6 +668,7 @@ def extend(self, target_vm, new_expire_time=None):
workflow=settings.ANSIBLETOWER.extend_workflow,
target_vm=target_vm.name,
new_expire_time=new_expire_time or settings.ANSIBLETOWER.get("new_expire_time"),
provider_labels=provider_labels,
)

def provider_help(
Expand Down

0 comments on commit d04c1d7

Please sign in to comment.