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

137 lines
3.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
COMMON_ENV_FILE="${ROOT_DIR}/environment.client"
CLIENT_ENV_FILE="${ROOT_DIR}/deploy/client/.env"
MODE="${RPKI_RTR_CLIENT_MODE:-tcp}"
COMPOSE_PROJECT="${RPKI_RTR_CLIENT_COMPOSE_PROJECT:-rpki-rtr-client}"
usage() {
cat <<'EOF'
Usage: scripts/11_start_client.sh [options]
Read root ./environment.client, update deploy/client/.env, then start the RTR debug client.
Options:
--mode MODE Client mode: tcp|tls|ssh. Default: tcp
--env FILE Client common env file. Default: ./environment.client
-h, --help Show this help.
EOF
}
log() {
printf '[11_start_client] %s\n' "$*" >&2
}
stage() {
printf '\n====== %s ======\n' "$*" >&2
}
die() {
printf '[11_start_client] ERROR: %s\n' "$*" >&2
exit 1
}
set_env_var() {
local env_file="$1"
local key="$2"
local value="$3"
local tmp_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}"
}
compose_file_for_mode() {
case "$1" in
tcp) printf '%s\n' "${ROOT_DIR}/deploy/client/docker-compose.tcp.yml" ;;
tls) printf '%s\n' "${ROOT_DIR}/deploy/client/docker-compose.tls.yml" ;;
ssh) printf '%s\n' "${ROOT_DIR}/deploy/client/docker-compose.ssh.yml" ;;
*) die "unsupported mode '$1', expected tcp|tls|ssh" ;;
esac
}
while [ "$#" -gt 0 ]; do
case "$1" in
--mode)
[ "$#" -ge 2 ] || die "--mode requires a value"
MODE="$2"
shift 2
;;
--env)
[ "$#" -ge 2 ] || die "--env requires a value"
COMMON_ENV_FILE="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
done
CLIENT_COMPOSE_FILE="$(compose_file_for_mode "${MODE}")"
stage "Checking Docker"
command -v docker >/dev/null 2>&1 || die "docker command not found"
if docker compose version >/dev/null 2>&1; then
COMPOSE_BIN=(docker compose)
elif command -v docker-compose >/dev/null 2>&1 && docker-compose version >/dev/null 2>&1; then
COMPOSE_BIN=(docker-compose)
else
die "docker compose is not available"
fi
[ -f "${CLIENT_ENV_FILE}" ] || die "deploy client env file not found: ${CLIENT_ENV_FILE}"
[ -f "${CLIENT_COMPOSE_FILE}" ] || die "client compose file not found: ${CLIENT_COMPOSE_FILE}"
stage "Reading Client Environment"
if [ -f "${COMMON_ENV_FILE}" ]; then
updated_count=0
while IFS= read -r line || [ -n "${line}" ]; do
case "${line}" in
''|\#*) continue ;;
*=*)
key="${line%%=*}"
value="${line#*=}"
set_env_var "${CLIENT_ENV_FILE}" "${key}" "${value}"
updated_count=$((updated_count + 1))
;;
esac
done <"${COMMON_ENV_FILE}"
log "updated ${updated_count} client env variable(s)"
else
log "environment.client file not found, skip env update"
fi
stage "Starting RTR Debug Client"
log "mode: ${MODE}"
"${COMPOSE_BIN[@]}" --env-file "${CLIENT_ENV_FILE}" -p "${COMPOSE_PROJECT}" -f "${CLIENT_COMPOSE_FILE}" up -d --no-build rtr-debug-client
log "checking client compose status"
"${COMPOSE_BIN[@]}" --env-file "${CLIENT_ENV_FILE}" -p "${COMPOSE_PROJECT}" -f "${CLIENT_COMPOSE_FILE}" ps rtr-debug-client
stage "Client Started"