#!/usr/bin/env bash set -Eeuo pipefail SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" readonly SCRIPT_NAME readonly SERVER_RESPONSE_FILE="/run/tuxflotte/server/response.json" readonly RUNTIME_DIR="/run/tuxflotte/assignment" readonly TEMPLATE_FILE="${RUNTIME_DIR}/template.json" log() { printf '[%s] %s\n' "${SCRIPT_NAME}" "$*" } fatal() { printf '[%s] FEHLER: %s\n' "${SCRIPT_NAME}" "$*" >&2 exit 1 } require_root() { if [[ "${EUID}" -ne 0 ]]; then fatal "Das Profilauswahlmodul muss als root ausgeführt werden." fi } prepare_runtime() { install -d -m 0700 "${RUNTIME_DIR}" rm -f -- "${TEMPLATE_FILE}" } validate_server_response() { [[ -r "${SERVER_RESPONSE_FILE}" ]] || fatal "Serverantwort nicht gefunden: ${SERVER_RESPONSE_FILE}" jq --exit-status . "${SERVER_RESPONSE_FILE}" >/dev/null || fatal "Serverantwort enthält kein gültiges JSON." jq --exit-status ' .success == true and (.templates | type == "array") and (.templates | length > 0) ' "${SERVER_RESPONSE_FILE}" >/dev/null || fatal "Serverantwort enthält keine auswählbaren Bereitstellungsvorlagen." } show_templates() { local index=1 local template log "Verfügbare Bereitstellungsvorlagen:" while IFS= read -r template; do printf '\n' printf ' %d) %s' \ "${index}" \ "$(jq -r '.label' <<<"${template}")" if [[ "$(jq -r '.is_default' <<<"${template}")" == "true" ]]; then printf ' (Standard)' fi printf '\n' printf ' Workspace: %s\n' \ "$(jq -r '.workspace.name' <<<"${template}")" printf ' Backend: %s %s\n' \ "$(jq -r '.backend.name' <<<"${template}")" \ "$(jq -r '.backend.version' <<<"${template}")" ((index += 1)) done < <(jq -c '.templates[]' "${SERVER_RESPONSE_FILE}") } select_template() { local template_count local selection template_count="$(jq '.templates | length' "${SERVER_RESPONSE_FILE}")" while true; do printf '\n' read -r -p "Bereitstellungsvorlage auswählen [1-${template_count}]: " selection if [[ "${selection}" =~ ^[0-9]+$ ]] && ((selection >= 1 && selection <= template_count)); then break fi log "Ungültige Auswahl." done jq \ --argjson index "$((selection - 1))" \ '{ schema_version: 1, template: .templates[$index] }' \ "${SERVER_RESPONSE_FILE}" >"${TEMPLATE_FILE}" chmod 0600 "${TEMPLATE_FILE}" jq --exit-status ' .schema_version == 1 and (.template | type == "object") and (.template.id | type == "string") and (.template.id | length > 0) and (.template.workspace | type == "object") and (.template.backend | type == "object") ' "${TEMPLATE_FILE}" >/dev/null || fatal "Ausgewählte Bereitstellungsvorlage konnte nicht gültig gespeichert werden." log "Bereitstellungsvorlage $(jq -r '.template.id' "${TEMPLATE_FILE}") wurde ausgewählt." log "Auswahl wurde unter ${TEMPLATE_FILE} gespeichert." } main() { require_root prepare_runtime validate_server_response show_templates select_template } main "$@"