Files
computer-playbook/scripts/setup.sh
T
kevinveenbirkenbach fa5779aab1 refactor(setup): embed setup configuration directly into setup.sh
- Remove setup-related variables from Makefile
- Hard-code paths and scripts inside setup.sh
- Add validation and structured logging
- Compute reserved usernames dynamically

https://chatgpt.com/share/696ffc6d-dd00-800f-8f40-2112155fbeac
2026-01-20 23:06:31 +01:00

109 lines
3.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
echo "🔧 Running project setup (no installation)"
# ------------------------------------------------------------
# Hard-coded configuration (NOT overridable)
# ------------------------------------------------------------
PYTHON="python3"
ROLES_DIR="./roles"
APPLICATIONS_OUT="./group_vars/all/04_applications.yml"
APPLICATIONS_SCRIPT="./cli/setup/applications/__main__.py"
USERS_SCRIPT="./cli/setup/users/__main__.py"
USERS_OUT="./group_vars/all/03_users.yml"
INCLUDES_SCRIPT="./cli/build/role_include/__main__.py"
INCLUDES_OUT_DIR="./tasks/groups"
# ------------------------------------------------------------
# Validation
# ------------------------------------------------------------
require_file() {
local path="$1"
[[ -f "$path" ]] || { echo "❌ File not found: $path" >&2; exit 1; }
}
require_dir() {
local path="$1"
[[ -d "$path" ]] || { echo "❌ Directory not found: $path" >&2; exit 1; }
}
require_cmd() {
command -v "$1" >/dev/null || { echo "❌ Command not found: $1" >&2; exit 1; }
}
require_cmd "${PYTHON}"
require_dir "${ROLES_DIR}"
require_file "${APPLICATIONS_SCRIPT}"
require_file "${USERS_SCRIPT}"
require_file "${INCLUDES_SCRIPT}"
# ------------------------------------------------------------
# Helpers
# ------------------------------------------------------------
log_section() {
echo
echo "------------------------------------------------------------"
echo "$1"
echo "------------------------------------------------------------"
}
compute_reserved_usernames() {
find "${ROLES_DIR}" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' \
| sed -E 's/.*-//' \
| grep -E -x '[a-z0-9]+' \
| sort -u \
| paste -sd, -
}
# ------------------------------------------------------------
# Reserved usernames
# ------------------------------------------------------------
log_section "👤 Reserved usernames"
RESERVED_USERNAMES="$(compute_reserved_usernames)"
echo "Reserved usernames: ${RESERVED_USERNAMES:-<none>}"
# ------------------------------------------------------------
# Users defaults
# ------------------------------------------------------------
log_section "👥 Generating users defaults → ${USERS_OUT}"
"${PYTHON}" "${USERS_SCRIPT}" \
--roles-dir "${ROLES_DIR}" \
--output "${USERS_OUT}" \
--reserved-usernames "${RESERVED_USERNAMES}"
echo "✅ Users defaults written to ${USERS_OUT}"
# ------------------------------------------------------------
# Applications defaults
# ------------------------------------------------------------
log_section "📦 Generating applications defaults → ${APPLICATIONS_OUT}"
"${PYTHON}" "${APPLICATIONS_SCRIPT}" \
--roles-dir "${ROLES_DIR}" \
--output-file "${APPLICATIONS_OUT}"
echo "✅ Applications defaults written to ${APPLICATIONS_OUT}"
# ------------------------------------------------------------
# Role include files
# ------------------------------------------------------------
log_section "🧩 Generating role include files"
mkdir -p "${INCLUDES_OUT_DIR}"
mapfile -t INCLUDE_GROUPS < <("${PYTHON}" -m cli.meta.categories.invokable -s "-")
for grp in "${INCLUDE_GROUPS[@]}"; do
[[ -z "${grp}" ]] && continue
out="${INCLUDES_OUT_DIR}/${grp}roles.yml"
echo "→ Building ${out} (pattern: '${grp}')"
"${PYTHON}" "${INCLUDES_SCRIPT}" "${ROLES_DIR}" -p "${grp}" -o "${out}"
echo " ✅ ${out}"
done
echo
echo "🎉 Project setup completed"