128 lines
3.2 KiB
Bash
Executable File
128 lines
3.2 KiB
Bash
Executable File
#!/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/provisioning"
|
|
readonly STATE_FILE="${RUNTIME_DIR}/state.env"
|
|
|
|
log() {
|
|
printf '[%s] %s\n' "${SCRIPT_NAME}" "$*" >&2
|
|
}
|
|
|
|
fatal() {
|
|
printf '[%s] FEHLER: %s\n' "${SCRIPT_NAME}" "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
require_root() {
|
|
if [[ "${EUID}" -ne 0 ]]; then
|
|
fatal "Das Gerätestatusmodul muss als root ausgeführt werden."
|
|
fi
|
|
}
|
|
|
|
prepare_runtime() {
|
|
install -d \
|
|
--mode=0700 \
|
|
--owner=root \
|
|
--group=root \
|
|
"${RUNTIME_DIR}"
|
|
|
|
rm -f -- "${STATE_FILE}"
|
|
}
|
|
|
|
store_provisioning_state() {
|
|
local continue_provisioning="$1"
|
|
|
|
umask 077
|
|
|
|
printf 'TUXFLOTTE_PROVISIONING_CONTINUE=%s\n' \
|
|
"${continue_provisioning}" >"${STATE_FILE}"
|
|
|
|
chmod 0600 "${STATE_FILE}"
|
|
}
|
|
|
|
validate_server_response() {
|
|
[[ -r "${SERVER_RESPONSE_FILE}" ]] ||
|
|
fatal "Serverantwort nicht gefunden: ${SERVER_RESPONSE_FILE}"
|
|
|
|
jq --exit-status '
|
|
.success == true
|
|
and (.device | type == "object")
|
|
and (.device.registration_status == "existing"
|
|
or .device.registration_status == "registered")
|
|
and (.customer | type == "object")
|
|
' "${SERVER_RESPONSE_FILE}" >/dev/null ||
|
|
fatal "Serverantwort enthält keinen gültigen Gerätestatus."
|
|
}
|
|
|
|
show_device_status() {
|
|
local registration_status
|
|
local hostname
|
|
local organization_name
|
|
|
|
registration_status="$(
|
|
jq -r '.device.registration_status' "${SERVER_RESPONSE_FILE}"
|
|
)"
|
|
|
|
hostname="$(
|
|
jq -r '.device.hostname // "unbekannt"' "${SERVER_RESPONSE_FILE}"
|
|
)"
|
|
|
|
organization_name="$(
|
|
jq -r '.customer.name // "unbekannt"' "${SERVER_RESPONSE_FILE}"
|
|
)"
|
|
|
|
printf '\n'
|
|
|
|
case "${registration_status}" in
|
|
existing)
|
|
printf 'Bekanntes Gerät erkannt\n'
|
|
printf '========================\n\n'
|
|
printf 'Gerät: %s\n' "${hostname}"
|
|
printf 'Organisation: %s\n' "${organization_name}"
|
|
printf '\n'
|
|
printf 'Das Gerät ist bereits registriert.\n'
|
|
;;
|
|
registered)
|
|
printf 'Neues Gerät registriert\n'
|
|
printf '=======================\n\n'
|
|
printf 'Gerät: %s\n' "${hostname}"
|
|
printf 'Organisation: %s\n' "${organization_name}"
|
|
printf '\n'
|
|
printf 'Hinweis:\n'
|
|
printf 'Die Registrierung erfolgte über den temporären Bootstrap-Aktivierungsmechanismus.\n'
|
|
;;
|
|
esac
|
|
}
|
|
|
|
confirm_provisioning() {
|
|
local answer
|
|
|
|
printf '\n'
|
|
read -r -p "Provisionierung fortsetzen? [j/N]: " answer
|
|
|
|
case "${answer}" in
|
|
j|J|ja|JA|Ja)
|
|
store_provisioning_state true
|
|
log "Provisionierung wird fortgesetzt."
|
|
;;
|
|
*)
|
|
store_provisioning_state false
|
|
log "Provisionierung wurde durch den Benutzer beendet."
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main() {
|
|
require_root
|
|
prepare_runtime
|
|
validate_server_response
|
|
show_device_status
|
|
confirm_provisioning
|
|
}
|
|
|
|
main "$@"
|