oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1#!/usr/bin/env bash
2# One-time operator setup: put every app in Base's catalogue on a CARLOS
3# account, so that Base can install it for a team later.
4#
5# scripts/prepare-apps.sh <account> <release-dir> [<version>]
6#
7# <release-dir> holds the binaries `make release` built, named
8# <app>-<goos>-<goarch>-<version> (releases/ by default). Give <version> to
9# pick a build. Without it, the directory must hold exactly one build of each
10# app, so an old build can't be promoted by accident.
11#
12# For each app id in internal/catalog/catalog.json this claims the app name
13# (unless the account already has it), ships the binary (unless that version
14# is already shipped), promotes it to the edge channel, and enables
15# instances. It never runs `carlos deploy`, which would also create an
16# instance at the app's own host that Base doesn't use. It stops at the first failure. Run it again after a failure:
17# finished steps are skipped.
18#
19# You need `carlos auth login` first, against the console that hosts <account>.
20set -euo pipefail
21
22if [ $# -lt 2 ] || [ $# -gt 3 ]; then
23 echo "usage: $0 <account> <release-dir> [<version>]" >&2
24 exit 2
25fi
26account=$1
27releases=$2
28want_version=${3:-}
29root=$(cd -- "$(dirname -- "$0")/.." && pwd)
30catalog=$root/internal/catalog/catalog.json
31
32[ -d "$releases" ] || { echo "release dir not found: $releases" >&2; exit 1; }
33command -v carlos >/dev/null || { echo "carlos is not on PATH" >&2; exit 1; }
34
35# App ids, read from the catalogue so this list can't drift from Base's.
36ids=$(grep -o '"id": *"[^"]*"' "$catalog" | sed 's/.*: *"\(.*\)"/\1/')
37[ -n "$ids" ] || { echo "no app ids found in $catalog" >&2; exit 1; }
38
39# Decide every binary before touching the account, so ambiguous input
40# fails with nothing done.
41declare -A bin_of version_of
42for id in $ids; do
43 found=()
44 for f in "$releases/$id"-linux-arm64-*; do
45 case $f in *.COMMIT) continue ;; esac
46 [ -f "$f" ] || continue
47 if [ -z "$want_version" ] || [ "${f##*-linux-arm64-}" = "$want_version" ]; then
48 found+=("$f")
49 fi
50 done
51 if [ ${#found[@]} -eq 0 ]; then
52 echo "no $id-linux-arm64-${want_version:-*} binary in $releases" >&2
53 exit 1
54 fi
55 if [ ${#found[@]} -gt 1 ]; then
56 echo "$releases holds ${#found[@]} builds of $id; pass the version to use as a third argument:" >&2
57 printf ' %s\n' "${found[@]##*-linux-arm64-}" >&2
58 exit 1
59 fi
60 bin_of[$id]=${found[0]}
61 version_of[$id]=${found[0]##*-linux-arm64-}
62done
63
64# The apps this account already has (the Trash included). A failed listing
65# stops the script rather than looking like an empty account.
66apps_out=$(carlos apps list --account "$account")
67
68for id in $ids; do
69 echo "== $id"
70 bin=${bin_of[$id]}
71 version=${version_of[$id]}
72
73 if printf '%s\n' "$apps_out" | awk -v id="$id" '$1 == id { found = 1 } END { exit !found }'; then
74 echo "app name already claimed in $account, skipping"
75 else
76 echo "claim the app name"
77 carlos apps create --app "$id" --account "$account"
78 fi
79
80 # Is this version already shipped? A failed lookup is not "no".
81 rel_out=$(carlos releases --app "$id" --account "$account")
82 if printf '%s\n' "$rel_out" | awk -v v="$version" '$1 == v { found = 1 } END { exit !found }'; then
83 echo "$version is already shipped"
84 else
85 echo "ship $version"
86 # Uploads are rate limited: wait and retry while the console says so.
87 while :; do
88 if ship_out=$(carlos ship --app "$id" --account "$account" --no-prompt \
89 --version "$version" "$bin" 2>&1); then
90 printf '%s\n' "$ship_out"
91 break
92 fi
93 printf '%s\n' "$ship_out" >&2
94 case $ship_out in
95 *"too many uploads"*) sleep "${PREPARE_APPS_RETRY_SLEEP:-10}" ;;
96 *) exit 1 ;;
97 esac
98 done
99 fi
100
101 echo "promote $version to edge"
102 carlos promote --app "$id" --account "$account" --no-prompt "$version" edge
103
104 echo "enable instances"
105 carlos instances enable --app "$id" --account "$account"
106done
107echo "done: every catalogue app is ready for Base to install"
108