| 1 | #!/usr/bin/env bash |
| 2 | # Wacht tot GET /api/v1/admin/self-update status ok meldt voor CI_COMMIT_SHA. |
| 3 | # publish_aur roept dit aan het begin aan. needs: [self_update] houdt die |
| 4 | # job tegen tot de POST klaar is; daarna herstart de service en pas dan |
| 5 | # draait deze poll. Een mislukking of timeout faalt publish_aur. |
| 6 | # SELF_UPDATE_TIMEOUT is seconden (standaard 300). SELF_UPDATE_INTERVAL |
| 7 | # is de pauze tussen pogingen (standaard 5). |
| 8 | set -euo pipefail |
| 9 | |
| 10 | : "${CI_API_URL:?CI_API_URL ontbreekt}" |
| 11 | : "${BLUEGIT_UPDATE_TOKEN:?BLUEGIT_UPDATE_TOKEN ontbreekt}" |
| 12 | : "${CI_COMMIT_SHA:?CI_COMMIT_SHA ontbreekt}" |
| 13 | |
| 14 | timeout="${SELF_UPDATE_TIMEOUT:-300}" |
| 15 | interval="${SELF_UPDATE_INTERVAL:-5}" |
| 16 | [[ "$timeout" =~ ^[0-9]+$ ]] || timeout=300 |
| 17 | [[ "$interval" =~ ^[0-9]+$ ]] || interval=5 |
| 18 | |
| 19 | if ! command -v jq >/dev/null 2>&1; then |
| 20 | if command -v pacman >/dev/null 2>&1; then |
| 21 | pacman -Sy --noconfirm --needed jq >&2 |
| 22 | elif command -v apk >/dev/null 2>&1; then |
| 23 | apk add --no-cache jq >&2 |
| 24 | else |
| 25 | echo "jq ontbreekt" >&2 |
| 26 | exit 1 |
| 27 | fi |
| 28 | fi |
| 29 | |
| 30 | api="${CI_API_URL%/}/admin/self-update" |
| 31 | auth=(-H "Authorization: Bearer ${BLUEGIT_UPDATE_TOKEN}") |
| 32 | |
| 33 | start="$(date +%s)" |
| 34 | while true; do |
| 35 | # Een korte storing tijdens de herstart mag de poll niet stoppen. |
| 36 | # log staat live als eerste key en bevat de tekst "status" en "sha". |
| 37 | body="$(curl -fsS --connect-timeout 5 --max-time 20 "$api" "${auth[@]}" || true)" |
| 38 | status="" |
| 39 | sha="" |
| 40 | if parsed="$(printf '%s' "$body" | jq -er '[.status // "", .sha // ""] | @tsv' 2>/dev/null)"; then |
| 41 | status="${parsed%%$'\t'*}" |
| 42 | sha="${parsed#*$'\t'}" |
| 43 | fi |
| 44 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "ok" ]]; then |
| 45 | echo "self-update ok voor ${CI_COMMIT_SHA}" |
| 46 | exit 0 |
| 47 | fi |
| 48 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "failed" ]]; then |
| 49 | echo "Self-update mislukt: $sha" >&2 |
| 50 | exit 1 |
| 51 | fi |
| 52 | now="$(date +%s)" |
| 53 | if (( now - start >= timeout )); then |
| 54 | echo "Self-update timeout na ${timeout}s (status ${status:-onbekend}, sha ${sha:-onbekend})" >&2 |
| 55 | exit 1 |
| 56 | fi |
| 57 | echo "Self-update status ${status:-onbekend} voor ${sha:-onbekend}" |
| 58 | sleep "$interval" |
| 59 | done |