Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow for additional arguments to be passed to provider help commands #193

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
signal.signal(signal.SIGINT, helpers.handle_keyboardinterrupt)


def loggedcli(*cli_args, **cli_kwargs):
"""Updates the cli.command wrapper function in order to add logging"""
def loggedcli(group=None, *cli_args, **cli_kwargs):
"""Updates the group command wrapper function in order to add logging"""
if not group:
group = cli # default to the main cli group

def decorator(func):
@cli.command(*cli_args, **cli_kwargs)
@group.command(*cli_args, **cli_kwargs)
@wraps(func)
def wrapper(*args, **kwargs):
logger.log(LOG_LEVEL.TRACE.value, f"Calling {func=}(*{args=} **{kwargs=}")
Expand Down Expand Up @@ -72,9 +74,23 @@ def populate_providers(click_group):
"""
for prov, prov_class in (pairs for pairs in PROVIDERS.items()):

@click_group.command(name=prov, hidden=prov_class.hidden)
def provider_cmd(*args, **kwargs): # the actual subcommand
@loggedcli(
group=click_group,
name=prov,
hidden=prov_class.hidden,
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
)
@click.pass_context
def provider_cmd(ctx, *args, **kwargs): # the actual subcommand
"""Get information about a provider's actions"""
# if additional arguments were passed, include them in the broker args
# strip leading -- characters
kwargs.update(
{
(key[2:] if key.startswith("--") else key): val
for key, val in zip(ctx.args[::2], ctx.args[1::2])
}
)
broker_inst = Broker(**kwargs)
broker_inst.nick_help()

Expand Down
13 changes: 12 additions & 1 deletion broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,15 @@ def _get_expire_date(self, host_id):
return None

def _compile_host_info(self, host):
# attempt to get the hostname from the host variables and then facts
if not (hostname := host.variables.get("fqdn")):
if hasattr(host_facts := host.related.ansible_facts.get(), "results"):
hostname = host_facts.results[0].ansible_facts.get("ansible_fqdn")
host_info = {
"name": host.name,
"type": host.type,
"hostname": host.variables.get("fqdn"),
"hostname": hostname,
"ip": host.variables.get("ansible_host"),
"tower_inventory": self._translate_inventory(host.inventory),
"_broker_provider": "AnsibleTower",
"_broker_provider_instance": self.instance,
Expand Down Expand Up @@ -480,8 +485,14 @@ def execute(self, **kwargs):
payload = {}
if inventory := kwargs.pop("inventory", None):
payload["inventory"] = inventory
logger.info(
f"Using tower inventory: {self._translate_inventory(inventory)}"
)
elif self.inventory:
payload["inventory"] = self.inventory
logger.info(
f"Using tower inventory: {self._translate_inventory(self.inventory)}"
)
else:
logger.info("No inventory specified, Ansible Tower will use a default.")
payload["extra_vars"] = str(kwargs)
Expand Down