rpki/scripts/00_0_install.sh
2026-07-14 14:44:40 +08:00

204 lines
5.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
IMAGE_DIR="${ROOT_DIR}/images"
SERVER_ENV_FILE="${ROOT_DIR}/deploy/server/.env"
CONSOLE_ENV_FILE="${ROOT_DIR}/deploy/console/.env"
SERVER_TAR="${RPKI_RTR_SERVER_IMAGE_TAR:-}"
CLIENT_TAR="${RPKI_RTR_CLIENT_IMAGE_TAR:-}"
CONSOLE_TAR="${RPKI_RTR_CONSOLE_IMAGE_TAR:-}"
usage() {
cat <<'EOF'
Usage: scripts/00_0_install.sh [options]
Load offline Docker image tarballs from ./images, then run scripts/00_1_pre_check.sh.
Options:
--server-tar FILE Server image tarball. Env: RPKI_RTR_SERVER_IMAGE_TAR
--client-tar FILE Debug client image tarball. Env: RPKI_RTR_CLIENT_IMAGE_TAR
--console-tar FILE Console image tarball. Env: RPKI_RTR_CONSOLE_IMAGE_TAR
-h, --help Show this help.
Default tarballs under ./images:
server: rpki-rtr-*.tar, excluding rpki-rtr-debug-client-*.tar
client: rpki-rtr-debug-client-*.tar
console: rpki-console-ui-*.tar
EOF
}
log() {
printf '[00_install] %s\n' "$*" >&2
}
stage() {
printf '\n====== %s ======\n' "$*" >&2
}
die() {
printf '[00_install] ERROR: %s\n' "$*" >&2
exit 1
}
set_env_var() {
local env_file="$1"
local key="$2"
local value="$3"
local tmp_file
[ -f "${env_file}" ] || die "env file not found: ${env_file}"
tmp_file="${env_file}.tmp.$$"
awk -v key="${key}" -v value="${value}" '
BEGIN { done = 0 }
$0 ~ "^[[:space:]]*#?[[:space:]]*" key "=" {
if (!done) {
print key "=" value
done = 1
}
next
}
{ print }
END {
if (!done) {
print key "=" value
}
}
' "${env_file}" >"${tmp_file}"
mv "${tmp_file}" "${env_file}"
}
while [ "$#" -gt 0 ]; do
case "$1" in
--server-tar)
[ "$#" -ge 2 ] || die "--server-tar requires a value"
SERVER_TAR="$2"
shift 2
;;
--client-tar)
[ "$#" -ge 2 ] || die "--client-tar requires a value"
CLIENT_TAR="$2"
shift 2
;;
--console-tar)
[ "$#" -ge 2 ] || die "--console-tar requires a value"
CONSOLE_TAR="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
done
resolve_tar() {
local path="$1"
if [ -f "${path}" ]; then
cd "$(dirname "${path}")" && printf '%s/%s\n' "$(pwd -P)" "$(basename "${path}")"
return 0
fi
if [ -f "${ROOT_DIR}/${path}" ]; then
cd "$(dirname "${ROOT_DIR}/${path}")" && printf '%s/%s\n' "$(pwd -P)" "$(basename "${path}")"
return 0
fi
return 1
}
load_image() {
local label="$1"
local tar_path="$2"
local output image
[ -f "${tar_path}" ] || die "${label} image tarball not found: ${tar_path}"
output="$(docker load -i "${tar_path}" 2>&1)" || {
printf '%s\n' "${output}" >&2
die "failed to load ${label} image from ${tar_path}"
}
printf '%s\n' "${output}" >&2
image="$(printf '%s\n' "${output}" | sed -n 's/^Loaded image: //p' | tail -n 1)"
[ -n "${image}" ] || die "cannot determine ${label} image name from docker load output"
printf '%s\n' "${image}"
}
find_single_tar() {
local label="$1"
local include_pattern="$2"
local exclude_pattern="${3:-}"
local matches=()
local file
while IFS= read -r -d '' file; do
matches+=("${file}")
done < <(
if [ -n "${exclude_pattern}" ]; then
find "${IMAGE_DIR}" -maxdepth 1 -type f -name "${include_pattern}" ! -name "${exclude_pattern}" -print0 | sort -z
else
find "${IMAGE_DIR}" -maxdepth 1 -type f -name "${include_pattern}" -print0 | sort -z
fi
)
case "${#matches[@]}" in
0)
die "no ${label} image tarball found under ${IMAGE_DIR} matching ${include_pattern}"
;;
1)
printf '%s\n' "${matches[0]}"
;;
*)
printf '[00_install] ERROR: multiple %s image tarballs found:\n' "${label}" >&2
printf ' %s\n' "${matches[@]}" >&2
die "please specify --${label}-tar or RPKI_RTR_${label^^}_IMAGE_TAR"
;;
esac
}
stage "Checking Docker"
command -v docker >/dev/null 2>&1 || die "docker command not found"
[ -d "${IMAGE_DIR}" ] || die "image directory not found: ${IMAGE_DIR}"
[ -f "${SERVER_ENV_FILE}" ] || die "server env file not found: ${SERVER_ENV_FILE}"
if [ -n "${SERVER_TAR}" ]; then
SERVER_TAR="$(resolve_tar "${SERVER_TAR}")" || die "server image tarball not found: ${SERVER_TAR}"
else
SERVER_TAR="$(find_single_tar server 'rpki-rtr-*.tar' 'rpki-rtr-debug-client-*.tar')"
fi
if [ -n "${CLIENT_TAR}" ]; then
CLIENT_TAR="$(resolve_tar "${CLIENT_TAR}")" || die "client image tarball not found: ${CLIENT_TAR}"
else
CLIENT_TAR="$(find_single_tar client 'rpki-rtr-debug-client-*.tar')"
fi
if [ -n "${CONSOLE_TAR}" ]; then
CONSOLE_TAR="$(resolve_tar "${CONSOLE_TAR}")" || die "console image tarball not found: ${CONSOLE_TAR}"
else
CONSOLE_TAR="$(find_single_tar console 'rpki-console-ui-*.tar')"
fi
stage "Loading Docker Images"
SERVER_IMAGE="$(load_image server "${SERVER_TAR}")"
CLIENT_IMAGE="$(load_image client "${CLIENT_TAR}")"
CONSOLE_IMAGE="$(load_image console "${CONSOLE_TAR}")"
stage "Updating Deploy Env"
set_env_var "${SERVER_ENV_FILE}" RPKI_RTR_SERVER_IMAGE "${SERVER_IMAGE}"
set_env_var "${CONSOLE_ENV_FILE}" RPKI_CONSOLE_UI_IMAGE "${CONSOLE_IMAGE}"
stage "Running Pre-Check"
RPKI_RTR_SERVER_IMAGE="${SERVER_IMAGE}" \
RPKI_RTR_CLIENT_IMAGE="${CLIENT_IMAGE}" \
bash "${SCRIPT_DIR}/00_1_pre_check.sh" || die "pre-check failed"
stage "Install Finished"