Iomatex_OS/compact_installer.sh

306 lines
6.9 KiB
Bash

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_PATH="${SCRIPT_DIR}/$(basename "${BASH_SOURCE[0]}")"
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
if command -v sudo >/dev/null 2>&1; then
exec sudo -E /bin/bash "$SCRIPT_PATH" "$@"
fi
echo "ERROR: root privileges are required. Run: sudo bash ./compact_installer.sh" >&2
exit 1
fi
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export DEBIAN_FRONTEND="${DEBIAN_FRONTEND:-noninteractive}"
LOG_FILE="${SCRIPT_DIR}/compact_installer.log"
STATE_FILE="${SCRIPT_DIR}/compact_installer.state"
STAGE_ORDER=(
"stage_mqtt"
"stage_wifi"
"stage_ui"
"stage_z2m"
"stage_itscreen"
"stage_reverse_tunnels"
)
CURRENT_STAGE=""
CURRENT_INSTALLER=""
RESET_STATE=0
FROM_STAGE=""
START_FROM_STAGE=""
mkdir -p "$SCRIPT_DIR"
touch "$LOG_FILE"
touch "$STATE_FILE"
chmod 0644 "$LOG_FILE" "$STATE_FILE" 2>/dev/null || true
exec > >(tee -a "$LOG_FILE") 2>&1
log() {
printf '[%s] %s\n' "$(date -u +'%F %T UTC')" "$*"
}
warn() {
log "WARN: $*"
}
die() {
log "ERROR: $*"
exit 1
}
on_err() {
local rc="$1"
local line="$2"
if [[ -n "$CURRENT_STAGE" ]]; then
log "ERROR: unexpected failure in ${CURRENT_STAGE} at line ${line} (rc=${rc})"
else
log "ERROR: unexpected failure at line ${line} (rc=${rc})"
fi
}
trap 'on_err "$?" "$LINENO"' ERR
usage() {
cat <<'EOF'
Usage:
sudo bash ./compact_installer.sh
sudo bash ./compact_installer.sh --from stage_ui
sudo bash ./compact_installer.sh --reset-state
Behavior:
- runs stages in this exact order:
stage_mqtt -> stage_wifi -> stage_ui -> stage_z2m -> stage_itscreen -> stage_reverse_tunnels
- after stage_reverse_tunnels, prints reminder about manual VPS bootstrap and reboot
- stops on first failure
EOF
}
set_state_value() {
local key="$1"
local value="$2"
local tmp
tmp="$(mktemp "${STATE_FILE}.tmp.XXXXXX")"
if [[ -f "$STATE_FILE" ]]; then
awk -F= -v k="$key" '$1 != k { print $0 }' "$STATE_FILE" >"$tmp"
fi
printf '%s=%s\n' "$key" "$value" >>"$tmp"
mv -f "$tmp" "$STATE_FILE"
}
get_state_value() {
local key="$1"
[[ -f "$STATE_FILE" ]] || return 0
awk -F= -v k="$key" '$1 == k { print substr($0, index($0, "=") + 1) }' "$STATE_FILE" | tail -n1
}
set_stage_status() {
local stage="$1"
local status="$2"
set_state_value "STATUS_${stage}" "$status"
}
get_stage_status() {
local stage="$1"
get_state_value "STATUS_${stage}"
}
reset_state() {
: >"$STATE_FILE"
log "State reset: $STATE_FILE"
}
is_known_stage() {
local wanted="$1"
local stage
for stage in "${STAGE_ORDER[@]}"; do
[[ "$stage" == "$wanted" ]] && return 0
done
return 1
}
next_stage_after() {
local current="$1"
local seen=0
local stage
for stage in "${STAGE_ORDER[@]}"; do
if [[ "$seen" -eq 0 ]]; then
[[ "$stage" == "$current" ]] && seen=1
continue
fi
printf '%s\n' "$stage"
return 0
done
return 1
}
stage_candidates() {
local stage="$1"
case "$stage" in
stage_mqtt)
printf '%s\n' "stage_mqtt_install.sh"
;;
stage_wifi)
printf '%s\n' "stage_wifi_install.sh"
;;
stage_ui)
printf '%s\n' "ui_installer.sh"
printf '%s\n' "stage_ui_install.sh"
;;
stage_z2m)
printf '%s\n' "stage_z2m_install"
printf '%s\n' "stage_z2m.sh"
;;
stage_itscreen)
printf '%s\n' "stage_itscreen_install.sh"
;;
stage_reverse_tunnels)
printf '%s\n' "stage_reverse_tunnels_install.sh"
;;
esac
}
find_stage_installer() {
local stage="$1"
local stage_dir="${SCRIPT_DIR}/${stage}"
local candidate
[[ -d "$stage_dir" ]] || return 1
while IFS= read -r candidate; do
[[ -n "$candidate" ]] || continue
[[ -f "${stage_dir}/${candidate}" ]] && {
printf '%s\n' "${stage_dir}/${candidate}"
return 0
}
done < <(stage_candidates "$stage")
return 1
}
print_header() {
echo
echo "============================================================"
echo "IOMATEX COMPACT INSTALLER"
echo "============================================================"
echo "Project root : ${SCRIPT_DIR}"
echo "Start from : ${START_FROM_STAGE:-stage_mqtt}"
echo "Log file : ${LOG_FILE}"
echo "State file : ${STATE_FILE}"
echo "Stage order : ${STAGE_ORDER[*]}"
echo "============================================================"
echo
}
run_stage() {
local stage="$1"
local installer=""
local stage_dir="${SCRIPT_DIR}/${stage}"
local rc=0
installer="$(find_stage_installer "$stage")" || die "Installer not found for ${stage}"
CURRENT_STAGE="$stage"
CURRENT_INSTALLER="$(basename "$installer")"
log "START: ${stage} -> ${CURRENT_INSTALLER}"
set_state_value "LAST_STAGE" "$stage"
set_state_value "LAST_INSTALLER" "$CURRENT_INSTALLER"
set_state_value "LAST_RESULT" "running"
set +e
(
cd "$stage_dir"
export UCF_FORCE_CONFFOLD=1
/bin/bash "./${CURRENT_INSTALLER}"
)
rc=$?
set -e
if [[ "$rc" -ne 0 ]]; then
set_stage_status "$stage" "failed"
set_state_value "LAST_RESULT" "failed"
die "Stage ${stage} failed with rc=${rc}"
fi
set_stage_status "$stage" "success"
set_state_value "LAST_RESULT" "success"
log "OK: ${stage}"
CURRENT_STAGE=""
CURRENT_INSTALLER=""
}
print_final_summary() {
echo
echo "============================================================"
echo "COMPACT INSTALLER SUMMARY"
echo "============================================================"
echo "Successful stages:"
local stage
for stage in "${STAGE_ORDER[@]}"; do
if [[ "$(get_stage_status "$stage")" == "success" ]]; then
echo " - ${stage}"
fi
done
echo
echo "After stage_reverse_tunnels:"
echo " - run the VPS bootstrap command printed by stage_reverse_tunnels"
echo " - reboot Edge so enabled reverse tunnel services actually start"
echo "============================================================"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--reset-state)
RESET_STATE=1
shift
;;
--from)
[[ $# -ge 2 ]] || die "--from requires a stage name"
FROM_STAGE="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
die "Unknown argument: $1"
;;
esac
done
[[ -n "$FROM_STAGE" ]] && is_known_stage "$FROM_STAGE" || [[ -z "$FROM_STAGE" ]] || die "Unknown stage: ${FROM_STAGE}"
if [[ "$RESET_STATE" -eq 1 ]]; then
reset_state
fi
log "=== COMPACT INSTALLER START ==="
log "Log file: ${LOG_FILE}"
log "State file: ${STATE_FILE}"
START_FROM_STAGE="${FROM_STAGE:-stage_mqtt}"
print_header
started=0
for stage in "${STAGE_ORDER[@]}"; do
if [[ "$started" -eq 0 ]]; then
if [[ "$stage" == "$START_FROM_STAGE" ]]; then
started=1
else
continue
fi
fi
if [[ -z "$FROM_STAGE" && "$(get_stage_status "$stage")" == "success" ]]; then
log "RESUME: ${stage} already successful, skipping"
continue
fi
echo
echo ">>> RUNNING ${stage}"
run_stage "$stage"
done
print_final_summary
exit 0