switched to fully self contained docker
This commit is contained in:
@@ -1,204 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
|
||||
SCRIPT_PATH="${SCRIPT_DIR}/$(basename -- "${BASH_SOURCE[0]}")"
|
||||
|
||||
COLOR_RESET=''
|
||||
COLOR_RED=''
|
||||
COLOR_GREEN=''
|
||||
COLOR_CYAN=''
|
||||
ERROR_RESET=''
|
||||
ERROR_RED=''
|
||||
ERROR_YELLOW=''
|
||||
if [[ -z "${NO_COLOR:-}" ]]; then
|
||||
if [[ -t 1 ]]; then
|
||||
COLOR_RESET=$'\033[0m'
|
||||
COLOR_RED=$'\033[1;31m'
|
||||
COLOR_GREEN=$'\033[1;32m'
|
||||
COLOR_CYAN=$'\033[1;36m'
|
||||
fi
|
||||
if [[ -t 2 ]]; then
|
||||
ERROR_RESET=$'\033[0m'
|
||||
ERROR_RED=$'\033[1;31m'
|
||||
ERROR_YELLOW=$'\033[1;33m'
|
||||
fi
|
||||
fi
|
||||
|
||||
fail() {
|
||||
printf '%sERROR:%s %s\n' "$ERROR_RED" "$ERROR_RESET" "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
trap 'printf "%sERROR:%s broker update failed at line %d.\n" "$ERROR_RED" "$ERROR_RESET" "$LINENO" >&2' ERR
|
||||
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
command -v git >/dev/null 2>&1 || fail "git is required"
|
||||
LOCAL_HOSTNAME="$(hostname -s 2>/dev/null || printf 'localhost')"
|
||||
|
||||
POST_PULL=0
|
||||
LOCAL_ONLY="${TAPM_UPDATE_LOCAL_ONLY:-0}"
|
||||
for argument in "$@"; do
|
||||
case "$argument" in
|
||||
--post-pull) POST_PULL=1 ;;
|
||||
--local-only) LOCAL_ONLY=1 ;;
|
||||
*) fail "unknown argument: ${argument}" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
deploy_local_broker() {
|
||||
command -v docker >/dev/null 2>&1 || fail "docker is required"
|
||||
docker compose version >/dev/null 2>&1 || fail "Docker Compose v2 is required"
|
||||
[[ -f compose.yaml ]] || fail "compose.yaml was not found in ${SCRIPT_DIR}"
|
||||
[[ -f .env ]] || fail ".env was not found in ${SCRIPT_DIR}"
|
||||
|
||||
printf 'Updating local broker at %s%s%s...\n' \
|
||||
"$COLOR_CYAN" "$LOCAL_HOSTNAME" "$COLOR_RESET"
|
||||
|
||||
printf 'Building broker image...\n'
|
||||
docker compose build --quiet broker
|
||||
|
||||
printf 'Applying pending database migrations...\n'
|
||||
docker compose run --rm migrate
|
||||
|
||||
printf 'Starting broker...\n'
|
||||
docker compose up -d broker
|
||||
|
||||
container_id="$(docker compose ps -q broker)"
|
||||
[[ -n "$container_id" ]] || fail "Docker Compose did not return a broker container"
|
||||
|
||||
printf 'Waiting for broker health check'
|
||||
for ((attempt = 1; attempt <= 75; attempt++)); do
|
||||
status="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")"
|
||||
case "$status" in
|
||||
healthy)
|
||||
printf ' %shealthy%s.\n' "$COLOR_GREEN" "$COLOR_RESET"
|
||||
docker compose ps broker
|
||||
printf '%sSUCCESS:%s Local TAPM Deployment Broker at %s%s%s updated successfully.\n' \
|
||||
"$COLOR_GREEN" "$COLOR_RESET" \
|
||||
"$COLOR_CYAN" "$LOCAL_HOSTNAME" "$COLOR_RESET"
|
||||
return 0
|
||||
;;
|
||||
exited | dead | unhealthy)
|
||||
printf ' %s%s%s.\n' "$COLOR_RED" "$status" "$COLOR_RESET"
|
||||
docker compose logs --tail=100 broker
|
||||
fail "broker failed its health check"
|
||||
;;
|
||||
esac
|
||||
printf '.'
|
||||
sleep 2
|
||||
done
|
||||
|
||||
printf ' timed out.\n'
|
||||
docker compose logs --tail=100 broker
|
||||
fail "broker did not become healthy within 150 seconds"
|
||||
}
|
||||
|
||||
read_env_setting() {
|
||||
local key="$1"
|
||||
local value
|
||||
|
||||
value="$(
|
||||
sed -n "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*//p" .env |
|
||||
tail -n 1
|
||||
)"
|
||||
value="${value#"${value%%[![:space:]]*}"}"
|
||||
value="${value%"${value##*[![:space:]]}"}"
|
||||
value="${value#\"}"
|
||||
value="${value%\"}"
|
||||
value="${value#\'}"
|
||||
value="${value%\'}"
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
update_peer_broker() {
|
||||
local peer_list="${TAPM_UPDATE_PEERS:-}"
|
||||
local ssh_user="${TAPM_UPDATE_SSH_USER:-}"
|
||||
local remote_update_path='/opt/idssys/ta-deployment-broker/update.sh'
|
||||
local peer_host
|
||||
local raw_peer
|
||||
local -a peer_hosts
|
||||
local -a ssh_options=(
|
||||
-o BatchMode=yes
|
||||
-o ConnectTimeout=5
|
||||
-o ConnectionAttempts=1
|
||||
-o StrictHostKeyChecking=yes
|
||||
)
|
||||
|
||||
[[ -f .env ]] || fail ".env was not found in ${SCRIPT_DIR}"
|
||||
if [[ -z "$peer_list" ]]; then
|
||||
peer_list="$(read_env_setting TAPM_UPDATE_PEERS)"
|
||||
fi
|
||||
[[ -n "$peer_list" ]] ||
|
||||
fail "TAPM_UPDATE_PEERS is not configured in .env"
|
||||
if [[ -z "$ssh_user" ]]; then
|
||||
ssh_user="$(read_env_setting TAPM_UPDATE_SSH_USER)"
|
||||
fi
|
||||
ssh_user="${ssh_user:-root}"
|
||||
|
||||
IFS=',' read -r -a peer_hosts <<< "$peer_list"
|
||||
for raw_peer in "${peer_hosts[@]}"; do
|
||||
peer_host="${raw_peer#"${raw_peer%%[![:space:]]*}"}"
|
||||
peer_host="${peer_host%"${peer_host##*[![:space:]]}"}"
|
||||
[[ -n "$peer_host" ]] || continue
|
||||
|
||||
printf 'Checking peer broker at %s%s%s...\n' \
|
||||
"$COLOR_CYAN" "$peer_host" "$COLOR_RESET"
|
||||
if ! ssh "${ssh_options[@]}" "${ssh_user}@${peer_host}" true; then
|
||||
printf '%sWARNING:%s peer %s is unavailable over SSH; its update was skipped.\n' \
|
||||
"$ERROR_YELLOW" "$ERROR_RESET" "$peer_host" >&2
|
||||
continue
|
||||
fi
|
||||
if ! ssh "${ssh_options[@]}" "${ssh_user}@${peer_host}" \
|
||||
"TAPM_UPDATE_LOCAL_ONLY=1 ${remote_update_path} --local-only"; then
|
||||
fail "peer ${peer_host} is reachable, but its broker update failed"
|
||||
fi
|
||||
printf '%sSUCCESS:%s Peer broker at %s%s%s completed successfully.\n' \
|
||||
"$COLOR_GREEN" "$COLOR_RESET" "$COLOR_CYAN" "$peer_host" "$COLOR_RESET"
|
||||
done
|
||||
}
|
||||
|
||||
if ((POST_PULL == 0)); then
|
||||
branch="$(git symbolic-ref --quiet --short HEAD)" ||
|
||||
fail "the repository is in detached HEAD state"
|
||||
upstream="$(git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' 2>/dev/null)" ||
|
||||
fail "branch ${branch} does not have an upstream branch"
|
||||
remote="$(git config --get "branch.${branch}.remote")" ||
|
||||
fail "branch ${branch} does not have a configured remote"
|
||||
git diff --quiet && git diff --cached --quiet ||
|
||||
fail "tracked local changes must be committed or stashed before updating"
|
||||
|
||||
printf 'Checking %s against %s...\n' "$branch" "$upstream"
|
||||
git fetch --quiet --prune "$remote"
|
||||
|
||||
local_commit="$(git rev-parse HEAD)"
|
||||
upstream_commit="$(git rev-parse '@{upstream}')"
|
||||
if [[ "$local_commit" == "$upstream_commit" ]]; then
|
||||
printf '%sLocal TAPM Deployment Broker is already up to date.%s\n' \
|
||||
"$COLOR_GREEN" "$COLOR_RESET"
|
||||
elif git merge-base --is-ancestor "$local_commit" "$upstream_commit"; then
|
||||
printf 'Updating TAPM Deployment Broker from %s...\n' "$upstream"
|
||||
git merge --quiet --ff-only "$upstream_commit"
|
||||
|
||||
# Restart from the newly pulled script so updates to this file take effect.
|
||||
if ((LOCAL_ONLY == 1)); then
|
||||
exec "$SCRIPT_PATH" --post-pull --local-only
|
||||
fi
|
||||
exec "$SCRIPT_PATH" --post-pull
|
||||
elif git merge-base --is-ancestor "$upstream_commit" "$local_commit"; then
|
||||
fail "local branch ${branch} is ahead of ${upstream}; refusing to rebuild"
|
||||
else
|
||||
fail "local branch ${branch} has diverged from ${upstream}"
|
||||
fi
|
||||
else
|
||||
deploy_local_broker
|
||||
fi
|
||||
|
||||
if ((LOCAL_ONLY == 0)); then
|
||||
update_peer_broker
|
||||
fi
|
||||
|
||||
printf '%sTAPM Deployment Broker update check completed.%s\n' \
|
||||
"$COLOR_GREEN" "$COLOR_RESET"
|
||||
Reference in New Issue
Block a user