| 1 | #!/usr/bin/env bash |
| 2 | # Start de self-update en wacht tot GET /api/v1/admin/self-update |
| 3 | # status ok meldt voor CI_COMMIT_SHA. publish_aur heeft needs: [self_update], |
| 4 | # dus die job start pas hierna. Een mislukking of timeout faalt deze job. |
| 5 | # SELF_UPDATE_TIMEOUT is seconden (standaard 300). SELF_UPDATE_INTERVAL |
| 6 | # is de pauze tussen pogingen (standaard 5). |
| 7 | set -euo pipefail |
| 8 | |
| 9 | : "${CI_API_URL:?CI_API_URL ontbreekt}" |
| 10 | : "${BLUEGIT_UPDATE_TOKEN:?BLUEGIT_UPDATE_TOKEN ontbreekt}" |
| 11 | : "${CI_COMMIT_SHA:?CI_COMMIT_SHA ontbreekt}" |
| 12 | |
| 13 | timeout="${SELF_UPDATE_TIMEOUT:-300}" |
| 14 | interval="${SELF_UPDATE_INTERVAL:-5}" |
| 15 | [[ "$timeout" =~ ^[0-9]+$ ]] || timeout=300 |
| 16 | [[ "$interval" =~ ^[0-9]+$ ]] || interval=5 |
| 17 | |
| 18 | api="${CI_API_URL%/}/admin/self-update" |
| 19 | auth=(-H "Authorization: Bearer ${BLUEGIT_UPDATE_TOKEN}") |
| 20 | |
| 21 | curl -fsS --connect-timeout 5 --max-time 20 -X POST "$api" "${auth[@]}" \ |
| 22 | -H "Content-Type: application/json" \ |
| 23 | -d "{\"ref\":\"${CI_COMMIT_SHA}\"}" |
| 24 | |
| 25 | start="$(date +%s)" |
| 26 | while true; do |
| 27 | # Een herstart van de service tijdens pacman -U mag de poll niet stoppen. |
| 28 | body="$(curl -fsS --connect-timeout 5 --max-time 20 "$api" "${auth[@]}" || true)" |
| 29 | # log kan "status" of "sha" bevatten. Alleen de velden ervoor tellen. |
| 30 | prefix="${body%%\"log\":*}" |
| 31 | status="$(printf '%s' "$prefix" | sed -n 's/.*"status"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)" |
| 32 | sha="$(printf '%s' "$prefix" | sed -n 's/.*"sha"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)" |
| 33 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "ok" ]]; then |
| 34 | echo "Self-update ok: $sha" |
| 35 | exit 0 |
| 36 | fi |
| 37 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "failed" ]]; then |
| 38 | echo "Self-update mislukt: $sha" >&2 |
| 39 | exit 1 |
| 40 | fi |
| 41 | now="$(date +%s)" |
| 42 | if (( now - start >= timeout )); then |
| 43 | echo "Self-update timeout na ${timeout}s (status ${status:-onbekend}, sha ${sha:-onbekend})" >&2 |
| 44 | exit 1 |
| 45 | fi |
| 46 | echo "Self-update status ${status:-onbekend} voor ${sha:-onbekend}" |
| 47 | sleep "$interval" |
| 48 | done |