Blueforce-Linux/scripts/install/installer/modules/04-hostname.sh
2026-09-17 13:22:26 +03:00

51 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Set hostname bf-<dealer-id> from validated dealer number.
# Idempotent: safe to re-run. Supports --check for read-only verification.
# Exit codes: 0 = OK, 3 = SKIP (optional step, nothing to do).
set -euo pipefail
MODULE="04-hostname"
LOG_FILE="${LOG_FILE:-/var/log/blueforce-install.log}"
DEALER_ID="${DEALER_ID:-}"
BF_HOSTNAME="${BF_HOSTNAME:-}"
BF_DEVICE="${BF_DEVICE:-}"
STATE_DIR="${STATE_DIR:-/var/lib/blueforce}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck source=../identity.sh
source "$SCRIPT_DIR/identity.sh"
log() {
local msg="$1"
mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) [04-hostname] $msg" | tee -a "$LOG_FILE"
}
CHECK_MODE=0
if [[ "${1:-}" == "--check" ]]; then CHECK_MODE=1; fi
[[ -n "$DEALER_ID" ]] || { log "FAIL: DEALER_ID env is empty"; echo "FAIL: DEALER_ID env is empty" >&2; exit 1; }
bf_valid_dealer_id "$DEALER_ID" || { log "FAIL: bad DEALER_ID format: $DEALER_ID"; echo "FAIL: bad DEALER_ID" >&2; exit 1; }
TARGET="$(bf_hostname "$DEALER_ID")"
if [[ "$CHECK_MODE" -eq 1 ]]; then
[[ "$(hostname)" == "$TARGET" ]] || { log "FAIL(check): hostname is $(hostname), want $TARGET"; exit 1; }
log "OK(check): hostname is $TARGET"
exit 0
fi
if [[ "$(hostname)" == "$TARGET" ]]; then
log "hostname already $TARGET, keeping"
else
hostnamectl set-hostname "$TARGET"
if grep -q "127.0.1.1" /etc/hosts; then
sed -i "s/^127.0.1.1.*/127.0.1.1\t$TARGET/" /etc/hosts
else
echo -e "127.0.1.1\t$TARGET" >> /etc/hosts
fi
log "hostname set to $TARGET"
fi
mkdir -p "$STATE_DIR"
echo "$DEALER_ID" > "$STATE_DIR/dealer-id"
echo "$TARGET" > "$STATE_DIR/hostname"
echo "$(bf_device_id "$DEALER_ID")" > "$STATE_DIR/device-id"
bf_write_identity "$DEALER_ID" || { log "FAIL: dealer identity could not be locked"; exit 1; }
log "OK: identity files written ($(bf_device_id "$DEALER_ID") / $TARGET)"