| 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 | LOG="${BLUEGIT_STATE_DIR}/update.log" |
| 16 | sha="" |
| 17 | |
| 18 | valid_ref() { |
| 19 | local ref="$1" |
| 20 | if [[ -z "$ref" ]]; then |
| 21 | return 0 |
| 22 | fi |
| 23 | [[ "$ref" != *..* && "$ref" != *'@{'* ]] || return 1 |
| 24 | [[ "$ref" =~ ^[0-9A-Za-z][0-9A-Za-z._/-]{0,199}$ ]] |
| 25 | } |
| 26 | |
| 27 | # git_repo DIR ARGS... |
| 28 | # De bare repo kan van een andere uid zijn. Git weigert die ook voor root. |
| 29 | git_repo() { |
| 30 | local repo="$1" |
| 31 | shift |
| 32 | git -c safe.directory="$repo" -c "safe.directory=*" -C "$repo" "$@" |
| 33 | } |
| 34 | |
| 35 | # Zelfde tijdstempel als scripts/aur-publish-local.sh: UTC-committertijd. |
| 36 | # Een falende git mag de stempel niet leeg laten: set -e stopt niet |
| 37 | # op een command substitution zonder ||. |
| 38 | pkgver_of() { |
| 39 | local repo="$1" commit="$2" version |
| 40 | version="$(TZ=UTC git_repo "$repo" show -s --date=format-local:%Y%m%d%H%M%S --format=%cd "$commit")" || return 1 |
| 41 | printf '%s\n' "$version" |
| 42 | } |
| 43 | |
| 44 | resolve_sha() { |
| 45 | local repo="$1" ref="$2" |
| 46 | if [[ -z "$ref" ]]; then |
| 47 | ref="HEAD" |
| 48 | fi |
| 49 | git_repo "$repo" rev-parse --verify "${ref}^{commit}" |
| 50 | } |
| 51 | |
| 52 | # Leegt het log en stuurt stdout en stderr erheen én naar het journal. |
| 53 | # De bluegit-user leest dit bestand via GET; het journal niet. |
| 54 | open_log() { |
| 55 | mkdir -p "$BLUEGIT_STATE_DIR" |
| 56 | : > "$LOG" |
| 57 | chmod 644 "$LOG" |
| 58 | exec > >(tee -a "$LOG") 2>&1 |
| 59 | } |
| 60 | |
| 61 | write_status() { |
| 62 | local result="$1" commit="$2" err="${3:-}" |
| 63 | local now tmp |
| 64 | now="$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 65 | err="${err//$'\n'/ }" |
| 66 | err="${err//\"/}" |
| 67 | mkdir -p "$BLUEGIT_STATE_DIR" |
| 68 | tmp="$(mktemp "${BLUEGIT_STATE_DIR}/.update-status.XXXXXX")" |
| 69 | printf '{"time":"%s","sha":"%s","result":"%s","error":"%s"}\n' "$now" "$commit" "$result" "$err" > "$tmp" |
| 70 | chmod 644 "$tmp" |
| 71 | mv -f "$tmp" "$STATUS" |
| 72 | } |
| 73 | |
| 74 | wait_until_idle() { |
| 75 | local deadline body n poll |
| 76 | poll="$BLUEGIT_UPDATE_POLL" |
| 77 | deadline=$((SECONDS + BLUEGIT_UPDATE_TIMEOUT)) |
| 78 | while (( SECONDS < deadline )); do |
| 79 | body="$(curl -fsS -H "Authorization: Bearer ${BLUEGIT_UPDATE_TOKEN}" "${BLUEGIT_UPDATE_URL}/api/v1/admin/self-update" || true)" |
| 80 | n="$(printf '%s' "$body" | sed -n 's/.*"running":[[:space:]]*\([0-9][0-9]*\).*/\1/p' | head -n 1)" |
| 81 | if [[ "$n" == "0" ]]; then |
| 82 | return 0 |
| 83 | fi |
| 84 | echo "Steve heeft nog ${n:-onbekend} lopende jobs." |
| 85 | sleep "$poll" |
| 86 | done |
| 87 | echo "Timeout: Steve heeft nog lopende jobs." >&2 |
| 88 | return 1 |
| 89 | } |
| 90 | |
| 91 | prepare_build() { |
| 92 | local repo="$1" commit="$2" dest="$3" version sum |
| 93 | version="$(pkgver_of "$repo" "$commit")" || return 1 |
| 94 | [[ "$version" =~ ^[0-9]{14}$ ]] || { echo "pkgver is geen tijdstempel: $version" >&2; return 1; } |
| 95 | mkdir -p "$dest" |
| 96 | git_repo "$repo" archive --format=tar.gz --prefix="bluegit-${commit}/" -o "$dest/bluegit-${commit}.tar.gz" "$commit" |
| 97 | git_repo "$repo" show "${commit}:packaging/aur/PKGBUILD" > "$dest/PKGBUILD" |
| 98 | git_repo "$repo" show "${commit}:packaging/aur/bluegit.install" > "$dest/bluegit.install" |
| 99 | sum="$(sha256sum "$dest/bluegit-${commit}.tar.gz" | awk '{print $1}')" |
| 100 | sed -i "s/^pkgver=.*/pkgver=${version}/" "$dest/PKGBUILD" |
| 101 | sed -i "s/^pkgrel=.*/pkgrel=1/" "$dest/PKGBUILD" |
| 102 | sed -i "s/^_commit=.*/_commit=${commit}/" "$dest/PKGBUILD" |
| 103 | sed -i "s|^sha256sums=.*|sha256sums=('${sum}')|" "$dest/PKGBUILD" |
| 104 | sed -i "s|^source=.*|source=('bluegit-${commit}.tar.gz')|" "$dest/PKGBUILD" |
| 105 | printf '%s\n' "$version" |
| 106 | } |
| 107 | |
| 108 | # runuser houdt de cwd van de unit. Die is /, dus makepkg meldt |
| 109 | # "PKGBUILD does not exist" tenzij we eerst naar de werkmap gaan. |
| 110 | build_package() { |
| 111 | local work="$1" |
| 112 | cd "$work" |
| 113 | runuser -u "$BLUEGIT_BUILD_USER" -- env \ |
| 114 | HOME="/var/lib/${BLUEGIT_BUILD_USER}" \ |
| 115 | BUILDDIR="$work/build" \ |
| 116 | SRCDEST="$work" \ |
| 117 | PKGDEST="$work/pkg" \ |
| 118 | makepkg --noconfirm --nosign |
| 119 | } |
| 120 | |
| 121 | main() { |
| 122 | local ref repo work version built |
| 123 | : "${BLUEGIT_UPDATE_TOKEN:?BLUEGIT_UPDATE_TOKEN ontbreekt}" |
| 124 | : "${BLUEGIT_UPDATE_REPO:?BLUEGIT_UPDATE_REPO ontbreekt}" |
| 125 | repo="$BLUEGIT_UPDATE_REPO" |
| 126 | [[ -d "$repo" ]] || { echo "Bare repo niet gevonden: $repo" >&2; exit 1; } |
| 127 | command -v git >/dev/null |
| 128 | command -v curl >/dev/null |
| 129 | command -v makepkg >/dev/null |
| 130 | command -v pacman >/dev/null |
| 131 | command -v runuser >/dev/null |
| 132 | id "$BLUEGIT_BUILD_USER" >/dev/null 2>&1 || { echo "Bouwgebruiker $BLUEGIT_BUILD_USER ontbreekt." >&2; exit 1; } |
| 133 | |
| 134 | mkdir -p "$BLUEGIT_STATE_DIR" |
| 135 | exec 9>"${BLUEGIT_STATE_DIR}/update.lock" |
| 136 | if ! flock -n 9; then |
| 137 | echo "Er loopt al een self-update." >&2 |
| 138 | exit 1 |
| 139 | fi |
| 140 | open_log |
| 141 | trap on_exit EXIT |
| 142 | |
| 143 | ref="" |
| 144 | if [[ -f "$REQUEST" ]]; then |
| 145 | ref="$(tr -d '[:space:]' < "$REQUEST")" |
| 146 | fi |
| 147 | valid_ref "$ref" || { echo "Ongeldige ref." >&2; exit 1; } |
| 148 | sha="$(resolve_sha "$repo" "$ref")" || exit 1 |
| 149 | echo "Self-update naar $sha" |
| 150 | write_status running "$sha" "" |
| 151 | |
| 152 | if ! wait_until_idle; then |
| 153 | write_status failed "$sha" "jobs still running" |
| 154 | exit 1 |
| 155 | fi |
| 156 | |
| 157 | work="$(mktemp -d /var/tmp/bluegit-update.XXXXXX)" |
| 158 | chmod 755 "$work" |
| 159 | version="$(prepare_build "$repo" "$sha" "$work")" || { write_status failed "$sha" "prepare"; exit 1; } |
| 160 | echo "pkgver $version" |
| 161 | mkdir -p "$work/pkg" |
| 162 | chown -R "$BLUEGIT_BUILD_USER:$BLUEGIT_BUILD_USER" "$work" |
| 163 | build_package "$work" |
| 164 | chown -R root:root "$work/pkg" |
| 165 | |
| 166 | if ! wait_until_idle; then |
| 167 | write_status failed "$sha" "jobs still running" |
| 168 | exit 1 |
| 169 | fi |
| 170 | |
| 171 | shopt -s nullglob |
| 172 | built=("$work"/pkg/bluegit-*.pkg.tar.*) |
| 173 | shopt -u nullglob |
| 174 | if [[ ${#built[@]} -ne 1 ]]; then |
| 175 | echo "Geen uniek pakket in $work/pkg" >&2 |
| 176 | write_status failed "$sha" "package missing" |
| 177 | exit 1 |
| 178 | fi |
| 179 | pacman -U --noconfirm "${built[0]}" |
| 180 | write_status ok "$sha" "" |
| 181 | trap - EXIT |
| 182 | rm -rf "$work" || true |
| 183 | work="" |
| 184 | echo "Self-update geïnstalleerd: $sha ($version)" |
| 185 | } |
| 186 | |
| 187 | on_exit() { |
| 188 | local rc=$? |
| 189 | if [[ "$rc" -ne 0 ]]; then |
| 190 | write_status failed "${sha:-}" "exit ${rc}" || true |
| 191 | fi |
| 192 | if [[ -n "${work:-}" && -d "${work:-}" ]]; then |
| 193 | rm -rf "$work" || true |
| 194 | fi |
| 195 | exit "$rc" |
| 196 | } |
| 197 | |
| 198 | if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
| 199 | main "$@" |
| 200 | fi |