Use the canonical inventory loader across all profile pipelines
This commit is contained in:
parent
336bb10397
commit
ee72a7e6a3
|
|
@ -1,272 +1,9 @@
|
|||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2034,SC2043,SC2140
|
||||
|
||||
load_homelab_inventory_defaults() {
|
||||
local inventory_file="${HOMELAB_INVENTORY_FILE:-${REPO_ROOT}/homelab.yml}"
|
||||
local rendered_defaults
|
||||
|
||||
if [[ ! -s "${inventory_file}" ]] || ! command -v python3 >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
rendered_defaults="$(python3 - "${inventory_file}" <<'PY'
|
||||
import re
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import sys
|
||||
|
||||
inventory_file = sys.argv[1]
|
||||
mapping = {
|
||||
"domain.base": "LAB_DOMAIN",
|
||||
"domain.public_url": "LAB_PUBLIC_URL",
|
||||
"domain.gitea_url": "LAB_GITEA_ROOT_URL",
|
||||
"network.lan_cidr": "LAB_LAN_CIDR",
|
||||
"network.lan_ip_prefix": "LAB_LAN_IP_PREFIX",
|
||||
"network.metallb.traefik_ip": "LAB_TRAEFIK_LB_IP",
|
||||
"network.tailscale_cgnat_cidr": "LAB_TAILSCALE_CGNAT_CIDR",
|
||||
"cluster.active_profile": "LAB_CLUSTER_PROFILE",
|
||||
"hosts.debian.user": "LAB_DEBIAN_USER",
|
||||
"hosts.debian.lan_ip": "LAB_DEBIAN_LAN_IP",
|
||||
"hosts.debian.tailscale_ip": "LAB_DEBIAN_TAILSCALE_IP",
|
||||
"hosts.debian.docker_root": "LAB_DEBIAN_DOCKER_ROOT",
|
||||
"hosts.rpi4.user": "LAB_RPI_USER",
|
||||
"hosts.rpi4.lan_ip": "LAB_RPI_HOST",
|
||||
"hosts.rpi4.tailscale_ip": "LAB_RPI_TAILSCALE_IP",
|
||||
"hosts.rpi4.docker_root": "LAB_RPI_DOCKER_ROOT",
|
||||
"hosts.rpi4.docker_nvme_root": "LAB_RPI_DOCKER_NVME_ROOT",
|
||||
"hosts.rpi4.docker_fallback_root": "LAB_RPI_DOCKER_FALLBACK_ROOT",
|
||||
"hosts.opi5_pimox.user": "LAB_PIMOX_USER",
|
||||
"hosts.opi5_pimox.lan_ip": "LAB_PIMOX_HOST",
|
||||
"hosts.opi5_pimox.bridge": "LAB_PIMOX_BRIDGE",
|
||||
"hosts.opi5_pimox.worker_storage": "LAB_PIMOX_WORKER_STORAGE",
|
||||
"hosts.oci_edge.user": "LAB_EDGE_USER",
|
||||
"hosts.oci_edge.public_ip": "LAB_EDGE_HOST",
|
||||
"hosts.oci_edge.install_dir": "LAB_EDGE_INSTALL_DIR",
|
||||
"services.gitea.http_port": "LAB_GITEA_HTTP_PORT",
|
||||
"services.gitea.ssh_port": "LAB_GITEA_SSH_PORT",
|
||||
"services.gitea.root_url": "LAB_GITEA_ROOT_URL",
|
||||
"services.heimdall.install_dir": "LAB_HEIMDALL_INSTALL_DIR",
|
||||
"services.heimdall.http_port": "LAB_HEIMDALL_HTTP_PORT",
|
||||
"services.heimdall.public_url": "LAB_HEIMDALL_PUBLIC_URL",
|
||||
"services.homepage.http_port": "LAB_HOMEPAGE_HTTP_PORT",
|
||||
"services.homepage.public_url": "LAB_HOMEPAGE_PUBLIC_URL",
|
||||
"services.dex.install_dir": "LAB_DEX_INSTALL_DIR",
|
||||
"services.dex.http_port": "LAB_DEX_HTTP_PORT",
|
||||
"services.dex.telemetry_port": "LAB_DEX_TELEMETRY_PORT",
|
||||
"services.dex.public_url": "LAB_DEX_PUBLIC_URL",
|
||||
"services.jellyfin.http_port": "LAB_JELLYFIN_HTTP_PORT",
|
||||
"services.jellyfin.public_url": "LAB_JELLYFIN_PUBLIC_URL",
|
||||
"services.jeannie_api.http_port": "LAB_JEANNIE_API_BACKEND_PORT",
|
||||
"services.ollama.bind_address": "LAB_OLLAMA_BIND_ADDRESS",
|
||||
"services.ollama.models_dir": "LAB_OLLAMA_MODELS_DIR",
|
||||
"services.ollama.igpu_enable": "LAB_OLLAMA_IGPU_ENABLE",
|
||||
"services.rpi_dns.pihole_web_port": "PIHOLE_WEB_PORT",
|
||||
"services.rpi_dns.uptime_kuma_port": "UPTIME_KUMA_PORT",
|
||||
"ai_gateway.provider": "LAB_AI_GATEWAY_PROVIDER",
|
||||
"ai_gateway.enabled": "LAB_BACKSTAGE_BRAIN_ENABLED",
|
||||
"ai_gateway.url": "LAB_AI_GATEWAY_URL",
|
||||
"ai_gateway.model": "LAB_AI_GATEWAY_MODEL",
|
||||
"ai_gateway.timeout_seconds": "LAB_AI_GATEWAY_TIMEOUT_SECONDS",
|
||||
"ai_gateway.knowledge_index_dir": "LAB_AI_KNOWLEDGE_INDEX_DIR",
|
||||
"pimox.worker_base_vmid": "LAB_PIMOX_WORKER_BASE_VMID",
|
||||
"pimox.default_worker_count": "LAB_PIMOX_WORKER_COUNT",
|
||||
}
|
||||
|
||||
def flatten(document, prefix=""):
|
||||
values = {}
|
||||
if isinstance(document, dict):
|
||||
for key, value in document.items():
|
||||
path = f"{prefix}.{key}" if prefix else str(key)
|
||||
if isinstance(value, dict):
|
||||
values.update(flatten(value, path))
|
||||
elif isinstance(value, list):
|
||||
values[path] = ",".join(str(item) for item in value)
|
||||
elif value is not None:
|
||||
values[path] = str(value)
|
||||
return values
|
||||
|
||||
|
||||
def parse_simple_inventory(path):
|
||||
stack = []
|
||||
values = {}
|
||||
pattern = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*):(?:\s*(.*?))?\s*$")
|
||||
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
for raw_line in handle:
|
||||
if not raw_line.strip() or raw_line.lstrip().startswith("#") or raw_line.lstrip().startswith("- "):
|
||||
continue
|
||||
match = pattern.match(raw_line.rstrip("\n"))
|
||||
if not match:
|
||||
continue
|
||||
indent = len(match.group(1))
|
||||
key = match.group(2)
|
||||
value = (match.group(3) or "").strip()
|
||||
while stack and stack[-1][0] >= indent:
|
||||
stack.pop()
|
||||
current_path = ".".join([item[1] for item in stack] + [key])
|
||||
if value == "":
|
||||
stack.append((indent, key))
|
||||
continue
|
||||
if " #" in value:
|
||||
value = value.split(" #", 1)[0].strip()
|
||||
values[current_path] = value.strip("\"'")
|
||||
return values
|
||||
|
||||
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
values = parse_simple_inventory(inventory_file)
|
||||
else:
|
||||
with open(inventory_file, encoding="utf-8") as handle:
|
||||
values = flatten(yaml.safe_load(handle) or {})
|
||||
|
||||
for path, env_name in mapping.items():
|
||||
value = values.get(path)
|
||||
if value:
|
||||
print(f": ${{{env_name}:={shlex.quote(value)}}}")
|
||||
|
||||
cluster_profile = os.environ.get("LAB_CLUSTER_PROFILE") or values.get("cluster.active_profile") or "debian"
|
||||
print(f": ${{LAB_CLUSTER_PROFILE:={shlex.quote(cluster_profile)}}}")
|
||||
profile_prefix = f"cluster.profiles.{cluster_profile}"
|
||||
profile_mapping = {
|
||||
"control_plane.node_name": ("LAB_CLUSTER_CONTROL_PLANE_NODE_NAME", None),
|
||||
"control_plane.lan_ip": ("LAB_CLUSTER_CONTROL_PLANE_LAN_IP", "hosts.debian.lan_ip"),
|
||||
"control_plane.user": ("LAB_CLUSTER_CONTROL_PLANE_USER", "hosts.debian.user"),
|
||||
"control_plane.ssh_key_path": ("LAB_CLUSTER_CONTROL_PLANE_SSH_KEY_PATH", None),
|
||||
"control_plane.kubeconfig": ("LAB_KUBECONFIG_PATH", "hosts.debian.kubeconfig"),
|
||||
"control_plane.kubeconfig_owner": ("LAB_CLUSTER_KUBECONFIG_OWNER", None),
|
||||
"registry.endpoint": ("LAB_REGISTRY_ENDPOINT", "services.local_registry.endpoint"),
|
||||
"provisioning.host": ("LAB_CLUSTER_PROVISIONING_HOST", "hosts.debian.lan_ip"),
|
||||
"provisioning.user": ("LAB_CLUSTER_PROVISIONING_USER", "hosts.debian.user"),
|
||||
"http.host": ("LAB_CLUSTER_HTTP_HOST", "hosts.debian.lan_ip"),
|
||||
"gitops.repo_url": ("LAB_GITOPS_REPO_URL", "services.gitea.ssh_remote"),
|
||||
"platform.calico_operator_node_name": ("LAB_CALICO_OPERATOR_NODE_NAME", None),
|
||||
"pimox.host": ("LAB_CLUSTER_PIMOX_HOST", "hosts.opi5_pimox.lan_ip"),
|
||||
"pimox.user": ("LAB_CLUSTER_PIMOX_USER", "hosts.opi5_pimox.user"),
|
||||
"pimox.bridge": ("LAB_CLUSTER_PIMOX_BRIDGE", "hosts.opi5_pimox.bridge"),
|
||||
"pimox.storage": ("LAB_CLUSTER_PIMOX_STORAGE", "hosts.opi5_pimox.worker_storage"),
|
||||
"pimox.control_plane_vmid": ("LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID", None),
|
||||
"pimox.worker_count": ("LAB_CLUSTER_PIMOX_WORKER_COUNT", "pimox.default_worker_count"),
|
||||
"pimox.worker_static_ips": ("LAB_CLUSTER_PIMOX_WORKER_STATIC_IPS", None),
|
||||
}
|
||||
for relative_path, (env_name, fallback_path) in profile_mapping.items():
|
||||
value = values.get(f"{profile_prefix}.{relative_path}")
|
||||
if not value and fallback_path:
|
||||
value = values.get(fallback_path)
|
||||
if not value and relative_path == "control_plane.node_name" and cluster_profile == "debian":
|
||||
value = "debian"
|
||||
if not value and relative_path == "platform.calico_operator_node_name":
|
||||
value = values.get(f"{profile_prefix}.control_plane.node_name")
|
||||
if not value and cluster_profile == "debian":
|
||||
value = "debian"
|
||||
if value:
|
||||
print(f": ${{{env_name}:={shlex.quote(value)}}}")
|
||||
|
||||
debian_ip = values.get("hosts.debian.lan_ip")
|
||||
gitea_http = values.get("services.gitea.http_port")
|
||||
if debian_ip:
|
||||
print(f": ${{LAB_GITEA_HOST:={shlex.quote(debian_ip)}}}")
|
||||
if debian_ip and gitea_http:
|
||||
print(f": ${{LAB_GITEA_LOCAL_URL:={shlex.quote(f'http://{debian_ip}:{gitea_http}/')}}}")
|
||||
gitea_ts = values.get("hosts.debian.tailscale_ip")
|
||||
if gitea_ts:
|
||||
print(f": ${{LAB_GITEA_TAILSCALE_IP:={shlex.quote(gitea_ts)}}}")
|
||||
subdomains = values.get("domain.subdomains")
|
||||
if subdomains:
|
||||
if isinstance(subdomains, str):
|
||||
subdomain_values = [item.strip() for item in subdomains.split(",") if item.strip()]
|
||||
else:
|
||||
subdomain_values = list(subdomains)
|
||||
if subdomain_values:
|
||||
print(f": ${{LAB_ADDITIONAL_SERVER_NAMES_JSON:={shlex.quote(json.dumps(subdomain_values))}}}")
|
||||
PY
|
||||
)"
|
||||
|
||||
if [[ -n "${rendered_defaults}" ]]; then
|
||||
eval "${rendered_defaults}"
|
||||
fi
|
||||
}
|
||||
|
||||
export_if_unset() {
|
||||
local name="$1"
|
||||
local value="$2"
|
||||
|
||||
if [[ -z "${value}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "${!name:-}" ]]; then
|
||||
printf -v "${name}" '%s' "${value}"
|
||||
fi
|
||||
# shellcheck disable=SC2163
|
||||
export "${name?}"
|
||||
}
|
||||
|
||||
export_homelab_inventory_tf_vars() {
|
||||
export_if_unset TF_VAR_kubeconfig_path "${LAB_KUBECONFIG_PATH:-}"
|
||||
export_if_unset TF_VAR_control_plane_node_name "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-debian}"
|
||||
export_if_unset TF_VAR_control_plane_advertise_address "${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-${LAB_DEBIAN_LAN_IP:-}}"
|
||||
export_if_unset TF_VAR_control_plane_host "${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-${LAB_DEBIAN_LAN_IP:-}}"
|
||||
export_if_unset TF_VAR_control_plane_user "${LAB_CLUSTER_CONTROL_PLANE_USER:-${LAB_DEBIAN_USER:-}}"
|
||||
export_if_unset TF_VAR_control_plane_ssh_key_path "${LAB_CLUSTER_CONTROL_PLANE_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}"
|
||||
export_if_unset TF_VAR_kubeconfig_owner "${LAB_CLUSTER_KUBECONFIG_OWNER:-}"
|
||||
export_if_unset TF_VAR_registry_endpoint "${LAB_REGISTRY_ENDPOINT:-}"
|
||||
export_if_unset TF_VAR_provisioning_host "${LAB_CLUSTER_PROVISIONING_HOST:-${LAB_DEBIAN_LAN_IP:-}}"
|
||||
export_if_unset TF_VAR_provisioning_user "${LAB_CLUSTER_PROVISIONING_USER:-${LAB_DEBIAN_USER:-}}"
|
||||
export_if_unset TF_VAR_http_host "${LAB_CLUSTER_HTTP_HOST:-${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-${LAB_DEBIAN_LAN_IP:-}}}"
|
||||
export_if_unset TF_VAR_pimox_host "${LAB_CLUSTER_PIMOX_HOST:-${LAB_PIMOX_HOST:-}}"
|
||||
export_if_unset TF_VAR_pimox_user "${LAB_CLUSTER_PIMOX_USER:-${LAB_PIMOX_USER:-}}"
|
||||
export_if_unset TF_VAR_pimox_worker_storage "${LAB_CLUSTER_PIMOX_STORAGE:-${LAB_PIMOX_WORKER_STORAGE:-}}"
|
||||
export_if_unset TF_VAR_pimox_template_bridge "${LAB_CLUSTER_PIMOX_BRIDGE:-${LAB_PIMOX_BRIDGE:-}}"
|
||||
export_if_unset TF_VAR_pimox_template_vmid "${LAB_PIMOX_TEMPLATE_VMID:-}"
|
||||
export_if_unset TF_VAR_pimox_template_build_user "${LAB_CLUSTER_PROVISIONING_USER:-${LAB_DEBIAN_USER:-}}"
|
||||
export_if_unset TF_VAR_pimox_template_guest_ip_prefix "${LAB_LAN_IP_PREFIX:-}"
|
||||
export_if_unset TF_VAR_edge_host "${LAB_EDGE_HOST:-}"
|
||||
export_if_unset TF_VAR_edge_user "${LAB_EDGE_USER:-}"
|
||||
# OCI foundation installs the public half of this key on recreated edge
|
||||
# instances. Keep the edge provisioner on the matching private key rather
|
||||
# than silently falling back to a stale/default path.
|
||||
export_if_unset TF_VAR_edge_ssh_key_path "${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-}}"
|
||||
export_if_unset TF_VAR_edge_install_dir "${LAB_EDGE_INSTALL_DIR:-}"
|
||||
local edge_tailscale_auth_key="${LAB_EDGE_TAILSCALE_AUTH_KEY:-}"
|
||||
if [[ -z "${edge_tailscale_auth_key}" ]]; then
|
||||
local edge_tailscale_credentials_file="${LAB_EDGE_TAILSCALE_OAUTH_CREDENTIAL_FILE:-${HOME}/tailscaled.txt}"
|
||||
if [[ -r "${edge_tailscale_credentials_file}" ]]; then
|
||||
edge_tailscale_auth_key="$(sed -n 's/^secret:[[:space:]]*//p' "${edge_tailscale_credentials_file}" | head -n 1)"
|
||||
fi
|
||||
fi
|
||||
export_if_unset TF_VAR_edge_tailscale_auth_key "${edge_tailscale_auth_key}"
|
||||
export_if_unset TF_VAR_edge_tailscale_tag "${LAB_EDGE_TAILSCALE_TAG:-tag:homelab-edge}"
|
||||
export_if_unset TF_VAR_server_name "${LAB_DOMAIN:-}"
|
||||
export_if_unset TF_VAR_additional_server_names "${LAB_ADDITIONAL_SERVER_NAMES_JSON:-}"
|
||||
export_if_unset TF_VAR_jeannie_api_metrics_target "${LAB_JEANNIE_API_METRICS_TARGET:-api.${LAB_DOMAIN:-lab2025.duckdns.org}:443}"
|
||||
export_if_unset TF_VAR_jeannie_api_metrics_token "${JEANNIE_SERVER_TOKEN:-${JEANNIE_REMOTE_TOKEN:-}}"
|
||||
export_if_unset TF_VAR_backend_host "${LAB_TRAEFIK_LB_IP:-}"
|
||||
export_if_unset TF_VAR_gitea_backend_host "${LAB_DEBIAN_TAILSCALE_IP:-}"
|
||||
export_if_unset TF_VAR_gitea_backend_port "${LAB_GITEA_HTTP_PORT:-}"
|
||||
export_if_unset TF_VAR_heimdall_backend_host "${LAB_HEIMDALL_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
|
||||
export_if_unset TF_VAR_heimdall_backend_port "${LAB_HEIMDALL_HTTP_PORT:-}"
|
||||
export_if_unset TF_VAR_homepage_backend_host "${LAB_HOMEPAGE_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
|
||||
export_if_unset TF_VAR_homepage_backend_port "${LAB_HOMEPAGE_HTTP_PORT:-}"
|
||||
export_if_unset TF_VAR_oidc_backend_host "${LAB_DEX_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
|
||||
export_if_unset TF_VAR_oidc_backend_port "${LAB_DEX_HTTP_PORT:-}"
|
||||
export_if_unset TF_VAR_grafana_oidc_url "${LAB_DEX_PUBLIC_URL:-}"
|
||||
export_if_unset TF_VAR_grafana_public_url "https://grafana.${LAB_DOMAIN:-}"
|
||||
export_if_unset TF_VAR_gitea_public_url "${LAB_GITEA_ROOT_URL:-}"
|
||||
export_if_unset TF_VAR_gitea_auth_source_name "${LAB_GITEA_DEX_SOURCE_NAME:-dex}"
|
||||
export_if_unset TF_VAR_jellyfin_backend_host "${LAB_JELLYFIN_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}"
|
||||
export_if_unset TF_VAR_jellyfin_backend_port "${LAB_JELLYFIN_HTTP_PORT:-}"
|
||||
export_if_unset TF_VAR_jeannie_api_backend_host "${LAB_JEANNIE_API_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-${LAB_DEBIAN_LAN_IP:-}}}"
|
||||
export_if_unset TF_VAR_jeannie_api_backend_port "${LAB_JEANNIE_API_BACKEND_PORT:-8787}"
|
||||
export_if_unset TF_VAR_gitops_repo_url "${LAB_GITOPS_REPO_URL:-}"
|
||||
export_if_unset TF_VAR_calico_operator_node_name "${LAB_CALICO_OPERATOR_NODE_NAME:-${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-}}"
|
||||
export_if_unset TF_VAR_worker_tailscale_enabled "${LAB_PIMOX_WORKER_TAILSCALE_ENABLED:-}"
|
||||
export_if_unset TF_VAR_worker_tailscale_accept_routes "${LAB_PIMOX_WORKER_TAILSCALE_ACCEPT_ROUTES:-}"
|
||||
export_if_unset TF_VAR_worker_tailscale_pod_egress_snat "${LAB_PIMOX_WORKER_TAILSCALE_POD_EGRESS_SNAT:-}"
|
||||
export_if_unset LAB_PIMOX_WORKER_COUNT "${LAB_CLUSTER_PIMOX_WORKER_COUNT:-}"
|
||||
export_if_unset LAB_PIMOX_WORKER_STATIC_IPS "${LAB_CLUSTER_PIMOX_WORKER_STATIC_IPS:-}"
|
||||
}
|
||||
# Keep one inventory loader and Terraform variable exporter for every caller.
|
||||
# shellcheck source=lib/jeannie/env.sh
|
||||
source "${BASH_SOURCE[0]%/*}/env.sh"
|
||||
|
||||
configure_platform_grafana_sso() {
|
||||
local namespace="${TF_VAR_grafana_namespace:-monitoring}"
|
||||
|
|
|
|||
Loading…
Reference in New Issue