421 lines
9.0 KiB
Bash
Executable File
421 lines
9.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
SCRIPT_PATH="${BASH_SOURCE[0]}"
|
|
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
|
|
|
|
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 ./local_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}/local_installer.log"
|
|
STATE_FILE="${SCRIPT_DIR}/local_installer.state"
|
|
|
|
mkdir -p "$SCRIPT_DIR"
|
|
touch "$LOG_FILE"
|
|
touch "$STATE_FILE"
|
|
exec > >(tee -a "$LOG_FILE") 2>&1
|
|
|
|
CURRENT_STAGE=""
|
|
CURRENT_INSTALLER=""
|
|
CURRENT_INSTALLER_BASENAME=""
|
|
|
|
SUCCESS_STAGES=()
|
|
SKIPPED_STAGES=()
|
|
FAILED_STAGES=()
|
|
|
|
STAGE_ORDER=(
|
|
"stage_env"
|
|
"stage_wifi"
|
|
"stage_mqtt"
|
|
"stage_z2m"
|
|
"stage_reverse_tunnels"
|
|
"stage_ui"
|
|
"stage_itscreen"
|
|
)
|
|
|
|
is_known_stage() {
|
|
local wanted="$1"
|
|
local stage
|
|
for stage in "${STAGE_ORDER[@]}"; do
|
|
[[ "$stage" == "$wanted" ]] && return 0
|
|
done
|
|
return 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 ./local_installer.sh
|
|
sudo bash ./local_installer.sh --reset-state
|
|
sudo bash ./local_installer.sh --from stage_mqtt
|
|
|
|
Behavior:
|
|
- runs stage installers in fixed order
|
|
- skips missing stage directories
|
|
- skips stages without a detected installer
|
|
- resumes from local_installer.state by default
|
|
- stops on first stage failure
|
|
|
|
Notes:
|
|
- reboot is not required immediately after stage_env
|
|
- final reboot is still recommended after full install so
|
|
LightDM autologin and reverse tunnels apply cleanly
|
|
EOF
|
|
}
|
|
|
|
normalize_stage_key() {
|
|
local stage="$1"
|
|
printf '%s' "${stage//[^a-zA-Z0-9_]/_}"
|
|
}
|
|
|
|
get_state_value() {
|
|
local key="$1"
|
|
if [[ ! -f "$STATE_FILE" ]]; then
|
|
return 0
|
|
fi
|
|
awk -F= -v k="$key" '$1 == k { print substr($0, index($0, "=") + 1); found=1 } END { if (!found) exit 0 }' "$STATE_FILE" | tail -n1
|
|
}
|
|
|
|
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"
|
|
}
|
|
|
|
set_stage_status() {
|
|
local stage="$1"
|
|
local status="$2"
|
|
local key
|
|
key="STATUS_$(normalize_stage_key "$stage")"
|
|
set_state_value "$key" "$status"
|
|
}
|
|
|
|
get_stage_status() {
|
|
local stage="$1"
|
|
local key
|
|
key="STATUS_$(normalize_stage_key "$stage")"
|
|
get_state_value "$key"
|
|
}
|
|
|
|
reset_state() {
|
|
: >"$STATE_FILE"
|
|
log "State reset: $STATE_FILE"
|
|
}
|
|
|
|
stage_candidates() {
|
|
local stage="$1"
|
|
case "$stage" in
|
|
stage_env)
|
|
printf '%s\n' "stage_env_install.sh"
|
|
;;
|
|
stage_wifi)
|
|
printf '%s\n' "stage_wifi_install.sh"
|
|
;;
|
|
stage_mqtt)
|
|
printf '%s\n' "stage_mqtt_install.sh"
|
|
;;
|
|
stage_z2m)
|
|
printf '%s\n' "stage_z2m_install"
|
|
printf '%s\n' "stage_z2m_install.sh"
|
|
;;
|
|
stage_reverse_tunnels)
|
|
printf '%s\n' "stage_reverse_tunnels_install.sh"
|
|
;;
|
|
stage_ui)
|
|
printf '%s\n' "ui_installer.sh"
|
|
printf '%s\n' "stage_ui_install.sh"
|
|
printf '%s\n' "stage_display_addon.sh"
|
|
;;
|
|
stage_itscreen)
|
|
printf '%s\n' "stage_itscreen_install.sh"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
find_stage_installer() {
|
|
local stage="$1"
|
|
local stage_dir="${SCRIPT_DIR}/${stage}"
|
|
local candidate
|
|
local found=()
|
|
|
|
[[ -d "$stage_dir" ]] || return 1
|
|
|
|
while IFS= read -r candidate; do
|
|
[[ -n "$candidate" ]] || continue
|
|
if [[ -f "${stage_dir}/${candidate}" ]]; then
|
|
printf '%s\n' "${stage_dir}/${candidate}"
|
|
return 0
|
|
fi
|
|
done < <(stage_candidates "$stage")
|
|
|
|
while IFS= read -r candidate; do
|
|
found+=("$candidate")
|
|
done < <(
|
|
find "$stage_dir" -maxdepth 1 -type f \
|
|
\( -name '*_install.sh' -o -name '*_installer.sh' -o -name 'stage_*_install' -o -name 'stage_*_install.sh' \) \
|
|
| sort
|
|
)
|
|
|
|
if [[ "${#found[@]}" -gt 0 ]]; then
|
|
printf '%s\n' "${found[0]}"
|
|
return 0
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
record_summary_from_state() {
|
|
local stage
|
|
local status
|
|
SUCCESS_STAGES=()
|
|
SKIPPED_STAGES=()
|
|
FAILED_STAGES=()
|
|
for stage in "${STAGE_ORDER[@]}"; do
|
|
status="$(get_stage_status "$stage")"
|
|
case "$status" in
|
|
success)
|
|
SUCCESS_STAGES+=("$stage")
|
|
;;
|
|
skipped*)
|
|
SKIPPED_STAGES+=("$stage")
|
|
;;
|
|
failed)
|
|
FAILED_STAGES+=("$stage")
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
run_stage() {
|
|
local stage="$1"
|
|
local stage_dir="${SCRIPT_DIR}/${stage}"
|
|
local installer
|
|
local rc
|
|
|
|
if [[ ! -d "$stage_dir" ]]; then
|
|
warn "SKIP: ${stage} not found"
|
|
set_stage_status "$stage" "skipped_stage_missing"
|
|
SKIPPED_STAGES+=("$stage")
|
|
return 0
|
|
fi
|
|
|
|
if ! installer="$(find_stage_installer "$stage")"; then
|
|
warn "SKIP: installer not found for ${stage}"
|
|
set_stage_status "$stage" "skipped_installer_missing"
|
|
SKIPPED_STAGES+=("$stage")
|
|
return 0
|
|
fi
|
|
|
|
CURRENT_STAGE="$stage"
|
|
CURRENT_INSTALLER="$installer"
|
|
CURRENT_INSTALLER_BASENAME="$(basename "$installer")"
|
|
|
|
log "START: ${stage} -> ${CURRENT_INSTALLER_BASENAME}"
|
|
set_state_value "LAST_STAGE" "$stage"
|
|
set_state_value "LAST_INSTALLER" "$CURRENT_INSTALLER_BASENAME"
|
|
set_state_value "LAST_RESULT" "running"
|
|
|
|
set +e
|
|
(
|
|
cd "$stage_dir"
|
|
/bin/bash "./${CURRENT_INSTALLER_BASENAME}"
|
|
)
|
|
rc=$?
|
|
set -e
|
|
|
|
if [[ "$rc" -ne 0 ]]; then
|
|
set_stage_status "$stage" "failed"
|
|
set_state_value "LAST_RESULT" "failed"
|
|
FAILED_STAGES+=("$stage")
|
|
log "FAIL: ${stage} (installer=${CURRENT_INSTALLER_BASENAME}, rc=${rc})"
|
|
return "$rc"
|
|
fi
|
|
|
|
set_stage_status "$stage" "success"
|
|
set_state_value "LAST_RESULT" "success"
|
|
SUCCESS_STAGES+=("$stage")
|
|
log "OK: ${stage}"
|
|
|
|
CURRENT_STAGE=""
|
|
CURRENT_INSTALLER=""
|
|
CURRENT_INSTALLER_BASENAME=""
|
|
return 0
|
|
}
|
|
|
|
print_summary() {
|
|
local stage
|
|
local status
|
|
|
|
echo
|
|
echo "============================================================"
|
|
echo "LOCAL INSTALLER SUMMARY"
|
|
echo "============================================================"
|
|
echo "Log file : ${LOG_FILE}"
|
|
echo "State file : ${STATE_FILE}"
|
|
echo
|
|
|
|
echo "Successful stages:"
|
|
if [[ "${#SUCCESS_STAGES[@]}" -eq 0 ]]; then
|
|
echo " - none"
|
|
else
|
|
for stage in "${SUCCESS_STAGES[@]}"; do
|
|
status="$(get_stage_status "$stage")"
|
|
echo " - ${stage} (${status})"
|
|
done
|
|
fi
|
|
|
|
echo
|
|
echo "Skipped stages:"
|
|
if [[ "${#SKIPPED_STAGES[@]}" -eq 0 ]]; then
|
|
echo " - none"
|
|
else
|
|
for stage in "${SKIPPED_STAGES[@]}"; do
|
|
status="$(get_stage_status "$stage")"
|
|
case "$status" in
|
|
skipped_stage_missing)
|
|
echo " - ${stage} (stage directory not found)"
|
|
;;
|
|
skipped_installer_missing)
|
|
echo " - ${stage} (installer not found)"
|
|
;;
|
|
*)
|
|
echo " - ${stage} (${status})"
|
|
;;
|
|
esac
|
|
done
|
|
fi
|
|
|
|
echo
|
|
echo "Failed stages:"
|
|
if [[ "${#FAILED_STAGES[@]}" -eq 0 ]]; then
|
|
echo " - none"
|
|
else
|
|
for stage in "${FAILED_STAGES[@]}"; do
|
|
echo " - ${stage}"
|
|
done
|
|
fi
|
|
|
|
echo
|
|
echo "Post-install note:"
|
|
echo " - immediate reboot after stage_env is NOT required for the next stages"
|
|
echo " - final reboot is recommended after the full run"
|
|
echo " - reverse tunnels from stage_reverse_tunnels start automatically only after reboot"
|
|
echo "============================================================"
|
|
}
|
|
|
|
FROM_STAGE=""
|
|
RESET_STATE=0
|
|
|
|
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
|
|
|
|
if [[ -n "$FROM_STAGE" ]] && ! is_known_stage "$FROM_STAGE"; then
|
|
die "Unknown stage for --from: ${FROM_STAGE}"
|
|
fi
|
|
|
|
cd "$SCRIPT_DIR"
|
|
|
|
if [[ "$RESET_STATE" -eq 1 ]]; then
|
|
reset_state
|
|
fi
|
|
|
|
log "=== LOCAL INSTALLER START ==="
|
|
log "Project root: ${SCRIPT_DIR}"
|
|
log "Log file: ${LOG_FILE}"
|
|
log "State file: ${STATE_FILE}"
|
|
|
|
STARTED=0
|
|
RUN_FAILED=0
|
|
|
|
for stage in "${STAGE_ORDER[@]}"; do
|
|
status="$(get_stage_status "$stage")"
|
|
|
|
if [[ -n "$FROM_STAGE" && "$STARTED" -eq 0 ]]; then
|
|
if [[ "$stage" == "$FROM_STAGE" ]]; then
|
|
STARTED=1
|
|
else
|
|
continue
|
|
fi
|
|
else
|
|
STARTED=1
|
|
fi
|
|
|
|
if [[ -z "$FROM_STAGE" && "$status" == "success" ]]; then
|
|
log "RESUME: ${stage} already successful, skipping"
|
|
SUCCESS_STAGES+=("$stage")
|
|
continue
|
|
fi
|
|
|
|
echo
|
|
echo ">>> RUNNING ${stage}"
|
|
if ! run_stage "$stage"; then
|
|
RUN_FAILED=1
|
|
break
|
|
fi
|
|
done
|
|
|
|
record_summary_from_state
|
|
print_summary
|
|
|
|
if [[ "$RUN_FAILED" -ne 0 || "${#FAILED_STAGES[@]}" -gt 0 ]]; then
|
|
exit 1
|
|
fi
|
|
|
|
exit 0
|