74 lines
2.4 KiB
Bash
74 lines
2.4 KiB
Bash
#!/usr/bin/env bash
|
|
# Shared helpers for the brick-by-brick deploy. Sourced by deploy.sh.
|
|
# Logging, test gates, health waits, and a docker shim that works whether or not
|
|
# the current shell is in the `docker` group.
|
|
|
|
set -uo pipefail
|
|
|
|
# ---- colors ----------------------------------------------------------------
|
|
if [ -t 1 ]; then
|
|
C_R=$'\e[31m'; C_G=$'\e[32m'; C_Y=$'\e[33m'; C_B=$'\e[36m'; C_D=$'\e[2m'; C_0=$'\e[0m'
|
|
else
|
|
C_R=; C_G=; C_Y=; C_B=; C_D=; C_0=
|
|
fi
|
|
log() { printf '%s\n' "$*"; }
|
|
info() { printf '%s\n' "${C_B}»${C_0} $*"; }
|
|
ok() { printf '%s\n' "${C_G}✓${C_0} $*"; }
|
|
warn() { printf '%s\n' "${C_Y}!${C_0} $*"; }
|
|
err() { printf '%s\n' "${C_R}✗${C_0} $*" >&2; }
|
|
brick(){ printf '\n%s\n' "${C_B}━━━ CĂRĂMIDĂ: $* ━━━${C_0}"; }
|
|
die() { err "$*"; err "Deploy oprit (gate). Repară și re-rulează — e idempotent."; exit 1; }
|
|
|
|
# ---- docker shim (portable: plain docker, else `sg docker`) -----------------
|
|
USE_SG=0
|
|
if ! docker ps >/dev/null 2>&1; then
|
|
if command -v sg >/dev/null 2>&1 && sg docker -c 'docker ps' >/dev/null 2>&1; then
|
|
USE_SG=1
|
|
fi
|
|
fi
|
|
docker() {
|
|
if [ "$USE_SG" = 1 ]; then
|
|
local a args=""
|
|
for a in "$@"; do args+=" $(printf '%q' "$a")"; done
|
|
sg docker -c "docker$args"
|
|
else
|
|
command docker "$@"
|
|
fi
|
|
}
|
|
|
|
# ---- compose wrapper -------------------------------------------------------
|
|
# COMPOSE is exported by deploy.sh
|
|
dc() { docker compose -f "$COMPOSE" "$@"; }
|
|
|
|
# ---- health / test helpers -------------------------------------------------
|
|
# wait_health <url> <timeout_s> <label>
|
|
wait_health() {
|
|
local url=$1 timeout=${2:-120} label=${3:-$1} i=0
|
|
printf ' %s' "aștept $label "
|
|
while [ "$i" -lt "$timeout" ]; do
|
|
if [ "$(curl -s -m5 -o /dev/null -w '%{http_code}' "$url" 2>/dev/null)" = "200" ]; then
|
|
printf ' %sup%s\n' "$C_G" "$C_0"; return 0
|
|
fi
|
|
printf '.'; sleep 3; i=$((i+3))
|
|
done
|
|
printf '\n'; return 1
|
|
}
|
|
|
|
# gate <label> <test-command...>
|
|
# Runs the test; on non-zero exit, STOPS the whole deploy.
|
|
gate() {
|
|
local label=$1; shift
|
|
printf ' test: %-22s ' "$label"
|
|
local out rc
|
|
out=$("$@" 2>&1); rc=$?
|
|
if [ "$rc" -eq 0 ]; then
|
|
ok "PASS ${C_D}${out}${C_0}"
|
|
return 0
|
|
fi
|
|
err "FAIL"
|
|
printf '%s\n' "$out" | sed 's/^/ /'
|
|
die "Testul '$label' nu a trecut."
|
|
}
|
|
|
|
# json_field <url> <python-expr-on-d> -> prints value, exit 1 on error
|
|
post_json() { curl -s -m"${4:-60}" "$1" -H 'Content-Type: application/json' -d "$2"; }
|