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

BUG: fixes for inventory management bugs #25

Merged
merged 2 commits into from
Apr 11, 2024
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
39 changes: 36 additions & 3 deletions scripts/add_to_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@

import argparse
from pathlib import Path
from typing import Any, Literal, Union

from ruamel.yaml import YAML

yaml = None
_Inventory = dict[str, dict]
# The yaml has top-level group names as strings
# Each group will either have hosts or references to children groups in a hierarchy
# Elements within these sections are dicts, with the keys being the hostnames
# The sub-dict values either point to None or contain config options (Any)
# A group's hosts dict might be missing entirely if there are no hosts in that group
# In these cases, the sub-group is replaced by a None due to the yaml syntax
# Children groups will always be populated
# Unions are used explicitly for py39 compat, future doesn't apply to aliases
_Inventory = dict[
str,
Union[
dict[
Literal["hosts"],
Union[
dict[str, Any],
None,
]
],
dict[
Literal["children"],
dict[str, Any]
],
]
]


def get_parser() -> argparse.ArgumentParser:
Expand Down Expand Up @@ -65,10 +89,19 @@ def host_in_inventory(hostname: str, inventory: _Inventory) -> bool:

def add_host_to_group(hostname: str, group: str, inventory: _Inventory) -> None:
"""Add hostname to the inventory under the selected group."""
hosts_in_group = list(inventory[group]["hosts"])
group_dict = inventory[group]
try:
hosts_dict = group_dict["hosts"]
except KeyError:
raise ValueError(f"Group {group} is not a hosts group.")
if hosts_dict is None:
hosts_dict = {}
hosts_in_group = list(hosts_dict)
hosts_in_group.append(hostname)
hosts_in_group.sort()
inventory[group]["hosts"] = {name: None for name in hosts_in_group}
inventory[group]["hosts"] = {
name: hosts_dict.get(name, None) for name in hosts_in_group
}


def get_group_options(inventory: _Inventory) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion scripts/bootstrap_plc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if grep -q "${HOSTNAME}:" "${INVENTORY_PATH}"; then
echo "Found ${HOSTNAME} in ${INVENTORY_PATH}."
else
# Add PLC to inventory
python "${THIS_DIR}"/add_to_inventory.py "${1}"
python "${THIS_DIR}"/add_to_inventory.py "${HOSTNAME}"
fi

# Create vars, if they do not already exist
Expand Down