Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add contextual awareness to host creation #208

Merged
merged 3 commits into from
May 1, 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
7 changes: 6 additions & 1 deletion broker/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def __init__(self, hostname=None, name=None, from_dict=False, **kwargs):
else:
self.hostname = hostname or kwargs.get("ip", None)
if not self.hostname:
raise HostError("Host must be constructed with a hostname or ip")
# check to see if we're being reconstructued, likely for checkin
import inspect
if any(f.function == "reconstruct_host" for f in inspect.stack()):
logger.debug("Ignoring missing hostname and ip for checkin reconstruction.")
else:
raise HostError("Host must be constructed with a hostname or ip")
self.name = name
self.username = kwargs.get("username", settings.HOST_USERNAME)
self.password = kwargs.get("password", settings.HOST_PASSWORD)
Expand Down
5 changes: 3 additions & 2 deletions broker/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ def _validate_settings(self, instance_name=None):
if not inst_vals.get("override_envars"):
# if a provider instance doesn't want to override envars, load them
settings.execute_loaders(loaders=[dynaconf.loaders.env_loader])

settings.validators.extend([v for v in self._validators if v not in settings.validators])
new_validators = [v for v in self._validators if v not in settings.validators]
logger.debug(f"Adding new validators: {[v.names[0] for v in new_validators]}")
settings.validators.extend(new_validators)
# use selective validation to only validate the instance settings
try:
settings.validators.validate(only=section_name)
Expand Down
43 changes: 28 additions & 15 deletions broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,31 @@ def _translate_inventory(self, inventory):
provider="AnsibleTower",
message=f"Unknown AnsibleTower inventory by id {inventory}",
)
if inventory_info := self.v2.inventory.get(search=inventory):
if inventory_info.count > 1:
raise exceptions.ProviderError(
provider="AnsibleTower",
message=f"Ambigious AnsibleTower inventory name {inventory}",
)
elif inventory_info.count == 1:
inv_struct = inventory_info.results.pop()
return inv_struct.id
else:
raise exceptions.ProviderError(
provider="AnsibleTower",
message=f"Unknown AnsibleTower inventory {inventory}",
)
elif isinstance(inventory, str):
if inventory_info := self.v2.inventory.get(search=inventory):
if inventory_info.count > 1:
raise exceptions.ProviderError(
provider="AnsibleTower",
message=f"Ambigious AnsibleTower inventory name {inventory}",
)
elif inventory_info.count == 1:
inv_struct = inventory_info.results.pop()
return inv_struct.id
else:
raise exceptions.ProviderError(
provider="AnsibleTower",
message=f"Unknown AnsibleTower inventory {inventory}",
)
elif inv_id := getattr(inventory, "id", None):
return inv_id
elif inv_name := getattr(inventory, "name", None):
return inv_name
else:
caller_context = inspect.stack()[1][0].f_locals
raise exceptions.ProviderError(
provider="AnsibleTower",
message=f"Ambiguous AnsibleTower inventory {inventory} passed from {caller_context}",
)

def _merge_artifacts(self, at_object, strategy="last", artifacts=None):
"""Gather and merge all artifacts associated with an object and its children
Expand Down Expand Up @@ -549,7 +560,9 @@ def get_inventory(self, user=None):
for inv in invs:
inv_hosts = inv.get_related("hosts", page_size=200).results
hosts.extend(inv_hosts)
return [self._compile_host_info(host) for host in hosts]
with click.progressbar(hosts, label='Compiling host information') as hosts_bar:
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):
"""Run the extend workflow with defaults args
Expand Down
2 changes: 1 addition & 1 deletion broker/providers/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def construct_host(self, provider_params, host_classes, **kwargs):

def nick_help(self, **kwargs):
"""Useful information about container images"""
results_limit = kwargs.get("results_limit", settings.CONTAINER.results_limit)
results_limit = kwargs.get("results_limit", settings.container.results_limit)
if image := kwargs.get("container_host"):
logger.info(
f"Information for {image} container-host:\n"
Expand Down