468 lines
13 KiB
Bash
Executable File
468 lines
13 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
|
|
LOG_FILE="/var/log/iomatex-edge-bootstrap.log"
|
|
DEFAULT_GIT_URL="${IOMATEX_GIT_URL:-https://git.it-screen.com/An-J/Iomatex_OS.git}"
|
|
DEFAULT_GIT_BRANCH="${IOMATEX_GIT_BRANCH:-ci-cd}"
|
|
DEFAULT_CHECKOUT_DIR="${IOMATEX_GIT_DIR:-/opt/iomatex/Iomatex_OS}"
|
|
|
|
GIT_URL="$DEFAULT_GIT_URL"
|
|
GIT_BRANCH="$DEFAULT_GIT_BRANCH"
|
|
CHECKOUT_DIR="$DEFAULT_CHECKOUT_DIR"
|
|
USE_ALL_STAGES=1
|
|
SELECTED_STAGES=""
|
|
APT_UPDATED=0
|
|
ASKPASS_SCRIPT=""
|
|
GIT_USERNAME="${IOMATEX_GIT_USERNAME:-}"
|
|
GIT_PASSWORD="${IOMATEX_GIT_PASSWORD:-}"
|
|
DEFAULT_PIPELINE_STAGES="stage_env,stage_wifi,stage_mqtt,stage_ui,stage_itscreen,stage_reverse_tunnels"
|
|
|
|
log() {
|
|
printf '[%s] %s\n' "$(date -u +'%F %T UTC')" "$*"
|
|
}
|
|
|
|
die() {
|
|
log "ERROR: $*"
|
|
exit 1
|
|
}
|
|
|
|
spinner() {
|
|
local pid="$1"
|
|
local msg="$2"
|
|
local spin='|/-\'
|
|
local i=0
|
|
local start_ts=0
|
|
local elapsed=0
|
|
local last_heartbeat=0
|
|
local spinner_tty=0
|
|
|
|
start_ts="$(date +%s)"
|
|
|
|
if [[ -w /dev/tty ]]; then
|
|
spinner_tty=1
|
|
fi
|
|
|
|
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
|
|
tput civis >/dev/tty 2>/dev/null || true
|
|
fi
|
|
|
|
while kill -0 "$pid" 2>/dev/null; do
|
|
i=$(((i + 1) % 4))
|
|
elapsed=$(( $(date +%s) - start_ts ))
|
|
if [[ "$spinner_tty" -eq 1 ]]; then
|
|
printf '\r[%c] %s... %ss' "${spin:$i:1}" "$msg" "$elapsed" >/dev/tty
|
|
fi
|
|
if (( elapsed >= last_heartbeat + 15 )); then
|
|
log "HEARTBEAT: ${msg} still running (${elapsed}s)"
|
|
last_heartbeat="$elapsed"
|
|
fi
|
|
sleep 0.2
|
|
done
|
|
|
|
if command -v tput >/dev/null 2>&1 && [[ "$spinner_tty" -eq 1 ]]; then
|
|
tput cnorm >/dev/tty 2>/dev/null || true
|
|
fi
|
|
|
|
wait "$pid"
|
|
local rc=$?
|
|
if [[ "$rc" -eq 0 ]]; then
|
|
if [[ "$spinner_tty" -eq 1 ]]; then
|
|
printf '\r[OK] %s. \n' "$msg" >/dev/tty
|
|
else
|
|
echo "[OK] $msg"
|
|
fi
|
|
else
|
|
if [[ "$spinner_tty" -eq 1 ]]; then
|
|
printf '\r[FAIL:%s] %s. \n' "$rc" "$msg" >/dev/tty
|
|
else
|
|
echo "[FAIL:$rc] $msg"
|
|
fi
|
|
fi
|
|
return "$rc"
|
|
}
|
|
|
|
run_step() {
|
|
local msg="$1"
|
|
shift
|
|
|
|
log "RUN: ${msg} :: $*"
|
|
(
|
|
"$@"
|
|
) >>"$LOG_FILE" 2>&1 &
|
|
local pid=$!
|
|
local rc=0
|
|
|
|
spinner "$pid" "$msg"
|
|
rc=$?
|
|
|
|
if [[ "$rc" -eq 0 ]]; then
|
|
log "OK: ${msg}"
|
|
return 0
|
|
fi
|
|
|
|
log "ERROR: ${msg} failed with rc=${rc}"
|
|
log "Last log lines for ${msg}:"
|
|
tail -n 20 "$LOG_FILE" || true
|
|
return "$rc"
|
|
}
|
|
|
|
cleanup() {
|
|
[[ -n "$ASKPASS_SCRIPT" && -f "$ASKPASS_SCRIPT" ]] && rm -f "$ASKPASS_SCRIPT"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage:
|
|
sudo bash ./install_iomatex_edge.sh
|
|
sudo bash ./install_iomatex_edge.sh --default-only
|
|
sudo bash ./install_iomatex_edge.sh --with-z2m
|
|
sudo bash ./install_iomatex_edge.sh --only stage_env,stage_mqtt,stage_ui
|
|
sudo bash ./install_iomatex_edge.sh --branch <branch>
|
|
sudo bash ./install_iomatex_edge.sh --git-url <url>
|
|
sudo bash ./install_iomatex_edge.sh --checkout-dir <path>
|
|
|
|
Behavior:
|
|
- may be launched from any directory on Edge
|
|
- installs git automatically if it is missing
|
|
- asks once for git username and password/token when credentials are not pre-exported
|
|
- clones or updates the Prod checkout locally
|
|
- then runs local_installer.sh from that local checkout in non-interactive mode
|
|
- installation continues locally after reboot and does not require git re-authentication
|
|
- by default installs the main pipeline without stage_z2m
|
|
EOF
|
|
}
|
|
|
|
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
|
|
if command -v sudo >/dev/null 2>&1; then
|
|
exec sudo -E /bin/bash "$0" "$@"
|
|
fi
|
|
echo "ERROR: root privileges are required. Run: sudo bash ./${SCRIPT_NAME}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p "$(dirname "$LOG_FILE")"
|
|
touch "$LOG_FILE"
|
|
chmod 0644 "$LOG_FILE" 2>/dev/null || true
|
|
exec > >(tee -a "$LOG_FILE") 2>&1
|
|
|
|
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
export DEBIAN_FRONTEND="${DEBIAN_FRONTEND:-noninteractive}"
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--default-only)
|
|
USE_ALL_STAGES=0
|
|
SELECTED_STAGES="stage_env,stage_mqtt,stage_ui,stage_itscreen"
|
|
shift
|
|
;;
|
|
--with-z2m)
|
|
USE_ALL_STAGES=0
|
|
SELECTED_STAGES="stage_env,stage_wifi,stage_mqtt,stage_z2m,stage_ui,stage_itscreen,stage_reverse_tunnels"
|
|
shift
|
|
;;
|
|
--only)
|
|
[[ $# -ge 2 ]] || die "--only requires a value"
|
|
USE_ALL_STAGES=0
|
|
SELECTED_STAGES="$2"
|
|
shift 2
|
|
;;
|
|
--branch)
|
|
[[ $# -ge 2 ]] || die "--branch requires a value"
|
|
GIT_BRANCH="$2"
|
|
shift 2
|
|
;;
|
|
--git-url)
|
|
[[ $# -ge 2 ]] || die "--git-url requires a value"
|
|
GIT_URL="$2"
|
|
shift 2
|
|
;;
|
|
--checkout-dir)
|
|
[[ $# -ge 2 ]] || die "--checkout-dir requires a value"
|
|
CHECKOUT_DIR="$2"
|
|
shift 2
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
die "Unknown argument: $1"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
apt_source_list_files() {
|
|
local path
|
|
[[ -f /etc/apt/sources.list ]] && printf '%s\n' /etc/apt/sources.list
|
|
shopt -s nullglob
|
|
for path in /etc/apt/sources.list.d/*.list; do
|
|
[[ -f "$path" ]] && printf '%s\n' "$path"
|
|
done
|
|
shopt -u nullglob
|
|
}
|
|
|
|
apt_source_deb822_files() {
|
|
local path
|
|
shopt -s nullglob
|
|
for path in /etc/apt/sources.list.d/*.sources; do
|
|
[[ -f "$path" ]] && printf '%s\n' "$path"
|
|
done
|
|
shopt -u nullglob
|
|
}
|
|
|
|
backup_apt_source_files() {
|
|
local path=""
|
|
local backup_path=""
|
|
|
|
while IFS= read -r path; do
|
|
[[ -n "$path" ]] || continue
|
|
backup_path="${path}.bak.iomatex"
|
|
[[ -f "$backup_path" ]] || cp -a "$path" "$backup_path"
|
|
done < <(
|
|
apt_source_list_files
|
|
apt_source_deb822_files
|
|
)
|
|
}
|
|
|
|
disable_apt_translations() {
|
|
cat >/etc/apt/apt.conf.d/99no-translations <<'EOF'
|
|
Acquire::Languages "none";
|
|
EOF
|
|
chmod 0644 /etc/apt/apt.conf.d/99no-translations
|
|
}
|
|
|
|
sanitize_apt_list_files() {
|
|
local path=""
|
|
|
|
while IFS= read -r path; do
|
|
[[ -n "$path" ]] || continue
|
|
sed -i -E 's/^[[:space:]]*deb-src /# deb-src /g' "$path"
|
|
sed -i -E 's|^[[:space:]]*deb .*bullseye-backports.*|# &|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://mirrors\.ustc\.edu\.cn/debian-security|http://deb.debian.org/debian-security|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://mirrors\.aliyun\.com/debian-security|http://deb.debian.org/debian-security|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://[^[:space:]]+/debian-security|http://deb.debian.org/debian-security|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://mirrors\.ustc\.edu\.cn/debian|http://deb.debian.org/debian|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://mirrors\.aliyun\.com/debian|http://deb.debian.org/debian|g' "$path"
|
|
sed -i -E '/^[[:space:]]*(deb|# deb|deb-src|# deb-src)[[:space:]].*bullseye/ s|https?://[^[:space:]]+/debian|http://deb.debian.org/debian|g' "$path"
|
|
done < <(apt_source_list_files)
|
|
}
|
|
|
|
sanitize_apt_deb822_files() {
|
|
local path=""
|
|
local tmp=""
|
|
|
|
while IFS= read -r path; do
|
|
[[ -n "$path" ]] || continue
|
|
if ! grep -qiE '^[[:space:]]*Suites:[[:space:]].*bullseye' "$path"; then
|
|
continue
|
|
fi
|
|
tmp="$(mktemp)"
|
|
awk '
|
|
BEGIN { disabled = 0 }
|
|
/^[[:space:]]*Types:[[:space:]]*/ {
|
|
line = $0
|
|
sub(/^[[:space:]]*Types:[[:space:]]*/, "", line)
|
|
gsub(/deb-src/, "", line)
|
|
gsub(/[[:space:]]+/, " ", line)
|
|
sub(/^ /, "", line)
|
|
sub(/ $/, "", line)
|
|
if (line == "") {
|
|
print "Enabled: no"
|
|
disabled = 1
|
|
} else {
|
|
print "Types: " line
|
|
}
|
|
next
|
|
}
|
|
/^[[:space:]]*Suites:[[:space:]]*/ {
|
|
line = $0
|
|
sub(/^[[:space:]]*Suites:[[:space:]]*/, "", line)
|
|
gsub(/bullseye-backports/, "", line)
|
|
gsub(/[[:space:]]+/, " ", line)
|
|
sub(/^ /, "", line)
|
|
sub(/ $/, "", line)
|
|
if (line == "") {
|
|
if (!disabled) {
|
|
print "Enabled: no"
|
|
disabled = 1
|
|
}
|
|
} else {
|
|
print "Suites: " line
|
|
}
|
|
next
|
|
}
|
|
/^[[:space:]]*URIs:[[:space:]]*/ {
|
|
line = $0
|
|
gsub(/https?:\/\/mirrors\.ustc\.edu\.cn\/debian-security/, "http://deb.debian.org/debian-security", line)
|
|
gsub(/https?:\/\/mirrors\.aliyun\.com\/debian-security/, "http://deb.debian.org/debian-security", line)
|
|
gsub(/https?:\/\/[^[:space:]]+\/debian-security/, "http://deb.debian.org/debian-security", line)
|
|
gsub(/https?:\/\/mirrors\.ustc\.edu\.cn\/debian/, "http://deb.debian.org/debian", line)
|
|
gsub(/https?:\/\/mirrors\.aliyun\.com\/debian/, "http://deb.debian.org/debian", line)
|
|
gsub(/https?:\/\/[^[:space:]]+\/debian/, "http://deb.debian.org/debian", line)
|
|
print line
|
|
next
|
|
}
|
|
{ print }
|
|
' "$path" >"$tmp"
|
|
mv -f "$tmp" "$path"
|
|
done < <(apt_source_deb822_files)
|
|
}
|
|
|
|
sanitize_apt_sources() {
|
|
backup_apt_source_files
|
|
disable_apt_translations
|
|
sanitize_apt_list_files
|
|
sanitize_apt_deb822_files
|
|
log "APT sources normalized: deb-src disabled, bullseye-backports disabled, vendor mirrors replaced with deb.debian.org"
|
|
}
|
|
|
|
apt_update_once() {
|
|
if [[ "$APT_UPDATED" -eq 0 ]]; then
|
|
sanitize_apt_sources
|
|
run_step "APT update" apt-get update
|
|
APT_UPDATED=1
|
|
fi
|
|
}
|
|
|
|
ensure_package() {
|
|
local pkg="$1"
|
|
if dpkg-query -W -f='${Status}\n' "$pkg" 2>/dev/null | grep -qx 'install ok installed'; then
|
|
return 0
|
|
fi
|
|
apt_update_once
|
|
run_step "Install package ${pkg}" apt-get install -y "$pkg"
|
|
}
|
|
|
|
ensure_git_stack() {
|
|
if command -v git >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
log "git is missing; installing git and ca-certificates"
|
|
ensure_package ca-certificates
|
|
ensure_package git
|
|
}
|
|
|
|
strip_git_url_credentials() {
|
|
local url="$1"
|
|
printf '%s\n' "$url" | sed -E 's#^(https?://)[^/@]+@#\1#'
|
|
}
|
|
|
|
prompt_for_git_credentials() {
|
|
if [[ -n "$GIT_USERNAME" && -n "$GIT_PASSWORD" ]]; then
|
|
return 0
|
|
fi
|
|
|
|
printf 'Git username: '
|
|
read -r GIT_USERNAME
|
|
[[ -n "$GIT_USERNAME" ]] || die "git username cannot be empty"
|
|
|
|
printf 'Git password or token: '
|
|
read -rs GIT_PASSWORD
|
|
echo
|
|
[[ -n "$GIT_PASSWORD" ]] || die "git password/token cannot be empty"
|
|
}
|
|
|
|
prepare_askpass() {
|
|
ASKPASS_SCRIPT="$(mktemp /tmp/iomatex-git-askpass.XXXXXX)"
|
|
cat >"$ASKPASS_SCRIPT" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
prompt="${1:-}"
|
|
case "$prompt" in
|
|
*Username*|*username*)
|
|
printf '%s\n' "${IOMATEX_GIT_USERNAME:?}"
|
|
;;
|
|
*Password*|*password*)
|
|
printf '%s\n' "${IOMATEX_GIT_PASSWORD:?}"
|
|
;;
|
|
*)
|
|
printf '%s\n' "${IOMATEX_GIT_PASSWORD:?}"
|
|
;;
|
|
esac
|
|
EOF
|
|
chmod 0700 "$ASKPASS_SCRIPT"
|
|
}
|
|
|
|
run_git_auth() {
|
|
GIT_ASKPASS="$ASKPASS_SCRIPT" \
|
|
GIT_TERMINAL_PROMPT=0 \
|
|
IOMATEX_GIT_USERNAME="$GIT_USERNAME" \
|
|
IOMATEX_GIT_PASSWORD="$GIT_PASSWORD" \
|
|
git "$@"
|
|
}
|
|
|
|
clone_or_update_repo() {
|
|
local expected_remote=""
|
|
local current_remote=""
|
|
|
|
expected_remote="$(strip_git_url_credentials "$GIT_URL")"
|
|
mkdir -p "$(dirname "$CHECKOUT_DIR")"
|
|
|
|
if [[ ! -d "$CHECKOUT_DIR/.git" ]]; then
|
|
[[ ! -e "$CHECKOUT_DIR" || -d "$CHECKOUT_DIR" ]] || die "Checkout path exists and is not a directory: $CHECKOUT_DIR"
|
|
if [[ -d "$CHECKOUT_DIR" && ! -d "$CHECKOUT_DIR/.git" ]]; then
|
|
rm -rf "$CHECKOUT_DIR"
|
|
fi
|
|
log "Cloning ${expected_remote} branch=${GIT_BRANCH} into ${CHECKOUT_DIR}"
|
|
run_step "Clone IOMATEX repo" run_git_auth clone --branch "$GIT_BRANCH" --single-branch "$GIT_URL" "$CHECKOUT_DIR"
|
|
else
|
|
current_remote="$(git -C "$CHECKOUT_DIR" remote get-url origin 2>/dev/null || true)"
|
|
[[ -n "$current_remote" ]] || die "Existing checkout has no origin remote: $CHECKOUT_DIR"
|
|
if [[ "$(strip_git_url_credentials "$current_remote")" != "$expected_remote" ]]; then
|
|
die "Existing checkout remote mismatch: ${current_remote} (expected ${expected_remote})"
|
|
fi
|
|
|
|
log "Updating existing checkout at ${CHECKOUT_DIR}"
|
|
run_step "Fetch IOMATEX repo updates" run_git_auth -C "$CHECKOUT_DIR" fetch --prune origin
|
|
if git -C "$CHECKOUT_DIR" show-ref --verify --quiet "refs/heads/${GIT_BRANCH}"; then
|
|
run_step "Checkout branch ${GIT_BRANCH}" git -C "$CHECKOUT_DIR" checkout "$GIT_BRANCH"
|
|
else
|
|
run_step "Create local branch ${GIT_BRANCH}" git -C "$CHECKOUT_DIR" checkout -B "$GIT_BRANCH" "origin/${GIT_BRANCH}"
|
|
fi
|
|
run_step "Fast-forward branch ${GIT_BRANCH}" git -C "$CHECKOUT_DIR" merge --ff-only "origin/${GIT_BRANCH}"
|
|
fi
|
|
|
|
git -C "$CHECKOUT_DIR" remote set-url origin "$expected_remote"
|
|
}
|
|
|
|
run_local_installer() {
|
|
local installer_path="${CHECKOUT_DIR}/local_installer.sh"
|
|
local args=("$CHECKOUT_DIR" "--non-interactive")
|
|
|
|
[[ -f "$installer_path" ]] || die "local_installer.sh not found in checkout: $installer_path"
|
|
export IOMATEX_FORCE_PASSWORD_CHANGE="${IOMATEX_FORCE_PASSWORD_CHANGE:-0}"
|
|
|
|
if [[ -z "$SELECTED_STAGES" && "$USE_ALL_STAGES" -eq 1 ]]; then
|
|
SELECTED_STAGES="$DEFAULT_PIPELINE_STAGES"
|
|
fi
|
|
|
|
if [[ -n "$SELECTED_STAGES" ]]; then
|
|
args+=("--only" "$SELECTED_STAGES")
|
|
fi
|
|
|
|
log "Starting local installer from ${installer_path}"
|
|
log "Repo URL : ${GIT_URL}"
|
|
log "Branch : ${GIT_BRANCH}"
|
|
log "Checkout : ${CHECKOUT_DIR}"
|
|
if [[ -n "$SELECTED_STAGES" ]]; then
|
|
log "Stages : ${SELECTED_STAGES}"
|
|
else
|
|
log "Stages : ${DEFAULT_PIPELINE_STAGES}"
|
|
fi
|
|
log "Log file : ${LOG_FILE}"
|
|
|
|
exec /bin/bash "$installer_path" "${args[@]}"
|
|
}
|
|
|
|
main() {
|
|
log "=== EDGE BOOTSTRAP START ==="
|
|
ensure_git_stack
|
|
prompt_for_git_credentials
|
|
prepare_askpass
|
|
clone_or_update_repo
|
|
run_local_installer
|
|
}
|
|
|
|
main
|