131 lines
3.9 KiB
Bash
Executable File
131 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
APP_DIR="${APP_DIR:-/opt/anal-front}"
|
|
COMPOSE_FILE="${COMPOSE_FILE:-$APP_DIR/compose.yml}"
|
|
ENV_FILE="${ENV_FILE:-$APP_DIR/.env}"
|
|
IMAGE_ENV_FILE="${IMAGE_ENV_FILE:-$APP_DIR/releases/current.env}"
|
|
HEALTH_URL="${HEALTH_URL:-http://10.10.0.124:18011/}"
|
|
DEFAULT_BACKEND_UPSTREAM="${DEFAULT_BACKEND_UPSTREAM:-http://10.10.0.124:18010}"
|
|
DEFAULT_BACKEND_HOST_HEADER="${DEFAULT_BACKEND_HOST_HEADER:-10.10.0.124}"
|
|
ANAL_FRONT_WEB_IMAGE="${1:-${ANAL_FRONT_WEB_IMAGE:-}}"
|
|
|
|
if [[ -z "$ANAL_FRONT_WEB_IMAGE" ]]; then
|
|
echo "ANAL_FRONT_WEB_IMAGE argument or environment variable is required" >&2
|
|
exit 1
|
|
fi
|
|
|
|
install -d -m 0770 "$APP_DIR" "$APP_DIR/releases"
|
|
|
|
if [[ ! -f "$ENV_FILE" ]]; then
|
|
umask 077
|
|
cat >"$ENV_FILE" <<EOF
|
|
BACKEND_UPSTREAM=$DEFAULT_BACKEND_UPSTREAM
|
|
BACKEND_HOST_HEADER=$DEFAULT_BACKEND_HOST_HEADER
|
|
FRONTEND_BIND_HOST=10.10.0.124
|
|
FRONTEND_PORT=18011
|
|
EOF
|
|
chmod 0600 "$ENV_FILE"
|
|
else
|
|
umask 077
|
|
current_backend_upstream="$(
|
|
awk -F= '/^BACKEND_UPSTREAM=/{sub(/^BACKEND_UPSTREAM=/, ""); print; exit}' "$ENV_FILE"
|
|
)"
|
|
desired_backend_upstream="$current_backend_upstream"
|
|
if [[ -z "$desired_backend_upstream" || "$desired_backend_upstream" == "https://anal-back.dev.nii-ecos.ru" ]]; then
|
|
desired_backend_upstream="$DEFAULT_BACKEND_UPSTREAM"
|
|
fi
|
|
current_backend_host_header="$(
|
|
awk -F= '/^BACKEND_HOST_HEADER=/{sub(/^BACKEND_HOST_HEADER=/, ""); print; exit}' "$ENV_FILE"
|
|
)"
|
|
desired_backend_host_header="$current_backend_host_header"
|
|
if [[ -z "$desired_backend_host_header" || "$desired_backend_host_header" == "anal-back.dev.nii-ecos.ru" ]]; then
|
|
desired_backend_host_header="$DEFAULT_BACKEND_HOST_HEADER"
|
|
fi
|
|
|
|
tmp_env_file="$(mktemp)"
|
|
backend_line_written=0
|
|
backend_host_header_written=0
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
if [[ "$line" == BACKEND_UPSTREAM=* ]]; then
|
|
printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file"
|
|
backend_line_written=1
|
|
continue
|
|
fi
|
|
if [[ "$line" == BACKEND_HOST_HEADER=* ]]; then
|
|
printf 'BACKEND_HOST_HEADER=%s\n' "$desired_backend_host_header" >>"$tmp_env_file"
|
|
backend_host_header_written=1
|
|
continue
|
|
fi
|
|
printf '%s\n' "$line" >>"$tmp_env_file"
|
|
done <"$ENV_FILE"
|
|
|
|
if [[ "$backend_line_written" -eq 0 ]]; then
|
|
printf 'BACKEND_UPSTREAM=%s\n' "$desired_backend_upstream" >>"$tmp_env_file"
|
|
fi
|
|
if [[ "$backend_host_header_written" -eq 0 ]]; then
|
|
printf 'BACKEND_HOST_HEADER=%s\n' "$desired_backend_host_header" >>"$tmp_env_file"
|
|
fi
|
|
|
|
mv "$tmp_env_file" "$ENV_FILE"
|
|
chmod 0600 "$ENV_FILE"
|
|
fi
|
|
|
|
umask 027
|
|
cat >"$IMAGE_ENV_FILE" <<EOF
|
|
ANAL_FRONT_WEB_IMAGE=$ANAL_FRONT_WEB_IMAGE
|
|
EOF
|
|
chmod 0640 "$IMAGE_ENV_FILE"
|
|
|
|
compose() {
|
|
docker compose \
|
|
--env-file "$ENV_FILE" \
|
|
--env-file "$IMAGE_ENV_FILE" \
|
|
-f "$COMPOSE_FILE" \
|
|
"$@"
|
|
}
|
|
|
|
wait_for_service_health() {
|
|
local service="$1"
|
|
local container_id=""
|
|
local health_status=""
|
|
|
|
for _ in $(seq 1 120); do
|
|
container_id="$(compose ps -q "$service" 2>/dev/null || true)"
|
|
if [[ -n "$container_id" ]]; then
|
|
health_status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id" 2>/dev/null || true)"
|
|
if [[ "$health_status" == "healthy" || "$health_status" == "running" ]]; then
|
|
return 0
|
|
fi
|
|
fi
|
|
sleep 2
|
|
done
|
|
|
|
echo "Service $service did not become healthy" >&2
|
|
compose ps >&2 || true
|
|
compose logs --tail=100 "$service" >&2 || true
|
|
exit 1
|
|
}
|
|
|
|
compose config >/dev/null
|
|
if [[ "$ANAL_FRONT_WEB_IMAGE" == */* ]]; then
|
|
compose pull web
|
|
else
|
|
echo "Using local image $ANAL_FRONT_WEB_IMAGE"
|
|
fi
|
|
compose up -d --remove-orphans web
|
|
wait_for_service_health web
|
|
|
|
for _ in $(seq 1 60); do
|
|
if curl -fsS "$HEALTH_URL" >/dev/null; then
|
|
compose ps
|
|
exit 0
|
|
fi
|
|
sleep 2
|
|
done
|
|
|
|
echo "Anal Front did not become healthy at $HEALTH_URL" >&2
|
|
compose ps >&2 || true
|
|
compose logs --tail=100 web >&2 || true
|
|
exit 1
|