#!/usr/bin/env bash # shellcheck shell=bash # # Installer logging and command execution. # # Every operation performed by the deterministic backend goes through run_cmd # (or run_cmd_capture), which records the command, its output and its exit # status. This makes failures diagnosable and keeps destructive behaviour # auditable. # # Secrets are scrubbed from all log output. declare -g PHOEBUS_LOG_FILE="${PHOEBUS_LOG_FILE:-}" declare -g PHOEBUS_LOG_ECHO="${PHOEBUS_LOG_ECHO:-no}" declare -ga SECRET_VALUES=() # secret_register VALUE - remember a secret so it is never written to logs secret_register() { [[ -n "${1-}" ]] && SECRET_VALUES+=("$1") } # redact_scrub TEXT - replace every known secret with redact_scrub() { local text="$1" secret for secret in "${SECRET_VALUES[@]}"; do [[ -n "$secret" ]] && text="${text//"$secret"/}" done printf '%s' "$text" } log_init() { local dir dir="${LOG_DIR:-/var/log/ai-installer}" if ! mkdir -p "$dir" 2>/dev/null || [[ ! -w "$dir" ]]; then dir="${STATE_DIR:-/tmp/ai-installer}/logs" mkdir -p "$dir" 2>/dev/null || true fi PHOEBUS_LOG_FILE="${PHOEBUS_LOG_FILE:-$dir/install.log}" : >"$PHOEBUS_LOG_FILE" 2>/dev/null || PHOEBUS_LOG_FILE=/dev/null log_line INFO "=== ${DISTRO_NAME:-Phoebus OS} installer started ===" log_line INFO "log file: $PHOEBUS_LOG_FILE" } log_line() { local level="$1" message="${2-}" local ts ts="$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date)" message="$(redact_scrub "$message")" printf '%s [%s] %s\n' "$ts" "$level" "$message" >>"$PHOEBUS_LOG_FILE" 2>/dev/null || true if [[ "$PHOEBUS_LOG_ECHO" == "yes" ]]; then printf '%s [%s] %s\n' "$ts" "$level" "$message" >&2 fi } log_debug() { log_line DEBUG "$@"; } log_info() { log_line INFO "$@"; } log_warn() { log_line WARN "$@"; } log_error() { log_line ERROR "$@"; } log_section() { log_line INFO "--- $* ---"; } # _cmd_to_string ARGS... - printable, redacted representation of a command _cmd_to_string() { local out="" arg for arg in "$@"; do out+="$(printf '%q' "$arg") " done redact_scrub "${out% }" } # run_cmd DESCRIPTION ARGS... # Execute a command, streaming stdout/stderr to the log. Honours DRY_RUN. run_cmd() { local desc="$1"; shift local printable status printable="$(_cmd_to_string "$@")" if config_bool DRY_RUN; then log_line INFO "[dry-run] $desc :: $printable" return 0 fi log_line INFO "$desc :: $printable" "$@" >>"$PHOEBUS_LOG_FILE" 2>&1 status=$? log_line INFO "$desc :: exit status $status" return $status } # run_cmd_capture DESCRIPTION ARGS... # Like run_cmd but stdout is stored in RUN_OUTPUT and not streamed. stderr # still goes to the log. Never logs the captured stdout (may contain secrets). run_cmd_capture() { local desc="$1"; shift local printable status RUN_OUTPUT="" printable="$(_cmd_to_string "$@")" if config_bool DRY_RUN; then log_line INFO "[dry-run] $desc (capture) :: $printable" return 0 fi log_line DEBUG "$desc (capture) :: $printable" RUN_OUTPUT="$("$@" 2>>"$PHOEBUS_LOG_FILE")" status=$? log_line DEBUG "$desc :: exit status $status" return $status } # run_cmd_quiet DESCRIPTION ARGS... - output fully suppressed run_cmd_quiet() { local desc="$1"; shift local printable status printable="$(_cmd_to_string "$@")" if config_bool DRY_RUN; then log_line INFO "[dry-run] $desc :: $printable" return 0 fi log_line DEBUG "$desc (quiet) :: $printable" "$@" >>"$PHOEBUS_LOG_FILE" 2>&1 status=$? log_line DEBUG "$desc :: exit status $status" return $status } # require_cmd NAME... - fail fast when a required tool is unavailable require_cmd() { local missing=() name for name in "$@"; do command -v "$name" >/dev/null 2>&1 || missing+=("$name") done if ((${#missing[@]})); then log_error "missing required command(s): ${missing[*]}" return 1 fi return 0 } # log_tail [N] - last N lines of the log (for the TUI) log_tail() { local n="${1:-40}" tail -n "$n" "$PHOEBUS_LOG_FILE" 2>/dev/null }