| 1 | #!/usr/bin/env bash |
| 2 | # Bouwt BlueGit uit de lokale bare repo en installeert dat pakket. |
| 3 | # Draait als root via bluegit-update.service. De bouw zelf doet bluegit-build. |
| 4 | # Bij een fout blijft het geïnstalleerde pakket staan. Journal houdt de uitvoer. |
| 5 | set -euo pipefail |
| 6 | |
| 7 | : "${BLUEGIT_STATE_DIR:=/var/lib/bluegit}" |
| 8 | : "${BLUEGIT_UPDATE_URL:=http://127.0.0.1:8080}" |
| 9 | : "${BLUEGIT_BUILD_USER:=bluegit-build}" |
| 10 | : "${BLUEGIT_UPDATE_POLL:=2}" |
| 11 | : "${BLUEGIT_UPDATE_TIMEOUT:=1800}" |
| 12 | |
| 13 | REQUEST="${BLUEGIT_STATE_DIR}/update-request" |
| 14 | STATUS="${BLUEGIT_STATE_DIR}/update-status.json" |
| 15 | sha="" |
| 16 | |
| 17 | valid_ref() { |
| 18 | local ref="$1" |
| 19 | if [[ -z "$ref" ]]; then |
| 20 | return 0 |
| 21 | fi |
| 22 | [[ "$ref" != *..* && "$ref" != *'@{'* ]] || return 1 |
| 23 | [[ "$ref" =~ ^[0-9A-Za-z][0-9A-Za-z._/-]{0,199}$ ]] |
| 24 | } |
| 25 | |
| 26 | # Zelfde tijdstempel als scripts/aur-publish-local.sh: UTC-committertijd. |
| 27 | pkgver_of() { |
| 28 | local repo="$1" commit="$2" |
| 29 | TZ=UTC git -C "$repo" show -s --date=format-local:%Y%m%d%H%M%S --format=%cd "$commit" |
| 30 | } |
| 31 | |
| 32 | resolve_sha() { |
| 33 | local repo="$1" ref="$2" |
| 34 | if [[ -z "$ref" ]]; then |
| 35 | ref="HEAD" |
| 36 | fi |
| 37 | git -C "$repo" rev-parse --verify "${ref}^{commit}" |
| 38 | } |
| 39 | |
| 40 | write_status() { |
| 41 | local result="$1" commit="$2" err="${3:-}" |
| 42 | local now tmp |
| 43 | now="$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 44 | err="${err//$'\n'/ }" |
| 45 | err="${err//\"/}" |
| 46 | mkdir -p "$BLUEGIT_STATE_DIR" |
| 47 | tmp="$(mktemp "${BLUEGIT_STATE_DIR}/.update-status.XXXXXX")" |
| 48 | printf '{"time":"%s","sha":"%s","result":"%s","error":"%s"}\n' "$now" "$commit" "$result" "$err" > "$tmp" |
| 49 | chmod 644 "$tmp" |
| 50 | mv -f "$tmp" "$STATUS" |
| 51 | } |
| 52 | |
| 53 | wait_until_idle() { |
| 54 | local deadline body n poll |
| 55 | poll="$BLUEGIT_UPDATE_POLL" |
| 56 | deadline=$((SECONDS + BLUEGIT_UPDATE_TIMEOUT)) |
| 57 | while (( SECONDS < deadline )); do |
| 58 | body="$(curl -fsS -H "Authorization: Bearer ${BLUEGIT_UPDATE_TOKEN}" "${BLUEGIT_UPDATE_URL}/api/v1/admin/self-update" || true)" |
| 59 | n="$(printf '%s' "$body" | sed -n 's/.*"running":[[:space:]]*\([0-9][0-9]*\).*/\1/p' | head -n 1)" |
| 60 | if [[ "$n" == "0" ]]; then |
| 61 | return 0 |
| 62 | fi |
| 63 | echo "Steve heeft nog ${n:-onbekend} lopende jobs." |
| 64 | sleep "$poll" |
| 65 | done |
| 66 | echo "Timeout: Steve heeft nog lopende jobs." >&2 |
| 67 | return 1 |
| 68 | } |
| 69 | |
| 70 | prepare_build() { |
| 71 | local repo="$1" commit="$2" dest="$3" version sum |
| 72 | version="$(pkgver_of "$repo" "$commit")" |
| 73 | [[ "$version" =~ ^[0-9]{14}$ ]] || { echo "pkgver is geen tijdstempel: $version" >&2; return 1; } |
| 74 | mkdir -p "$dest" |
| 75 | git -C "$repo" archive --format=tar.gz --prefix="bluegit-${commit}/" -o "$dest/bluegit-${commit}.tar.gz" "$commit" |
| 76 | git -C "$repo" show "${commit}:packaging/aur/PKGBUILD" > "$dest/PKGBUILD" |
| 77 | git -C "$repo" show "${commit}:packaging/aur/bluegit.install" > "$dest/bluegit.install" |
| 78 | sum="$(sha256sum "$dest/bluegit-${commit}.tar.gz" | awk '{print $1}')" |
| 79 | sed -i "s/^pkgver=.*/pkgver=${version}/" "$dest/PKGBUILD" |
| 80 | sed -i "s/^pkgrel=.*/pkgrel=1/" "$dest/PKGBUILD" |
| 81 | sed -i "s/^_commit=.*/_commit=${commit}/" "$dest/PKGBUILD" |
| 82 | sed -i "s|^sha256sums=.*|sha256sums=('${sum}')|" "$dest/PKGBUILD" |
| 83 | sed -i "s|^source=.*|source=('bluegit-${commit}.tar.gz')|" "$dest/PKGBUILD" |
| 84 | printf '%s\n' "$version" |
| 85 | } |
| 86 | |
| 87 | main() { |
| 88 | local ref repo work version built |
| 89 | : "${BLUEGIT_UPDATE_TOKEN:?BLUEGIT_UPDATE_TOKEN ontbreekt}" |
| 90 | : "${BLUEGIT_UPDATE_REPO:?BLUEGIT_UPDATE_REPO ontbreekt}" |
| 91 | repo="$BLUEGIT_UPDATE_REPO" |
| 92 | [[ -d "$repo" ]] || { echo "Bare repo niet gevonden: $repo" >&2; exit 1; } |
| 93 | command -v git >/dev/null |
| 94 | command -v curl >/dev/null |
| 95 | command -v makepkg >/dev/null |
| 96 | command -v pacman >/dev/null |
| 97 | command -v runuser >/dev/null |
| 98 | id "$BLUEGIT_BUILD_USER" >/dev/null 2>&1 || { echo "Bouwgebruiker $BLUEGIT_BUILD_USER ontbreekt." >&2; exit 1; } |
| 99 | |
| 100 | mkdir -p "$BLUEGIT_STATE_DIR" |
| 101 | exec 9>"${BLUEGIT_STATE_DIR}/update.lock" |
| 102 | if ! flock -n 9; then |
| 103 | echo "Er loopt al een self-update." >&2 |
| 104 | exit 1 |
| 105 | fi |
| 106 | trap on_exit EXIT |
| 107 | |
| 108 | ref="" |
| 109 | if [[ -f "$REQUEST" ]]; then |
| 110 | ref="$(tr -d '[:space:]' < "$REQUEST")" |
| 111 | fi |
| 112 | valid_ref "$ref" || { echo "Ongeldige ref." >&2; exit 1; } |
| 113 | sha="$(resolve_sha "$repo" "$ref")" |
| 114 | echo "Self-update naar $sha" |
| 115 | write_status running "$sha" "" |
| 116 | |
| 117 | if ! wait_until_idle; then |
| 118 | write_status failed "$sha" "jobs still running" |
| 119 | exit 1 |
| 120 | fi |
| 121 | |
| 122 | work="$(mktemp -d /var/tmp/bluegit-update.XXXXXX)" |
| 123 | chmod 755 "$work" |
| 124 | version="$(prepare_build "$repo" "$sha" "$work")" |
| 125 | echo "pkgver $version" |
| 126 | mkdir -p "$work/pkg" |
| 127 | chown -R "$BLUEGIT_BUILD_USER:$BLUEGIT_BUILD_USER" "$work" |
| 128 | runuser -u "$BLUEGIT_BUILD_USER" -- env \ |
| 129 | HOME="/var/lib/${BLUEGIT_BUILD_USER}" \ |
| 130 | BUILDDIR="$work/build" \ |
| 131 | SRCDEST="$work" \ |
| 132 | PKGDEST="$work/pkg" \ |
| 133 | makepkg --noconfirm --nosign |
| 134 | chown -R root:root "$work/pkg" |
| 135 | |
| 136 | if ! wait_until_idle; then |
| 137 | write_status failed "$sha" "jobs still running" |
| 138 | exit 1 |
| 139 | fi |
| 140 | |
| 141 | shopt -s nullglob |
| 142 | built=("$work"/pkg/bluegit-*.pkg.tar.*) |
| 143 | shopt -u nullglob |
| 144 | if [[ ${#built[@]} -ne 1 ]]; then |
| 145 | echo "Geen uniek pakket in $work/pkg" >&2 |
| 146 | write_status failed "$sha" "package missing" |
| 147 | exit 1 |
| 148 | fi |
| 149 | pacman -U --noconfirm "${built[0]}" |
| 150 | write_status ok "$sha" "" |
| 151 | trap - EXIT |
| 152 | rm -rf "$work" || true |
| 153 | work="" |
| 154 | echo "Self-update geïnstalleerd: $sha ($version)" |
| 155 | } |
| 156 | |
| 157 | on_exit() { |
| 158 | local rc=$? |
| 159 | if [[ "$rc" -ne 0 ]]; then |
| 160 | write_status failed "${sha:-}" "exit ${rc}" || true |
| 161 | fi |
| 162 | if [[ -n "${work:-}" && -d "${work:-}" ]]; then |
| 163 | rm -rf "$work" || true |
| 164 | fi |
| 165 | exit "$rc" |
| 166 | } |
| 167 | |
| 168 | if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
| 169 | main "$@" |
| 170 | fi |