#!/usr/bin/env bash # One-time operator setup: put every app in Base's catalogue on a CARLOS # account, so that Base can install it for a team later. # # scripts/prepare-apps.sh [] # # holds the binaries `make release` built, named # --- (releases/ by default). Give to # pick a build. Without it, the directory must hold exactly one build of each # app, so an old build can't be promoted by accident. # # For each app id in internal/catalog/catalog.json this claims the app name # (unless the account already has it), ships the binary (unless that version # is already shipped), promotes it to the edge channel, and enables # instances. It never runs `carlos deploy`, which would also create an # instance at the app's own host that Base doesn't use. It stops at the first failure. Run it again after a failure: # finished steps are skipped. # # You need `carlos auth login` first, against the console that hosts . set -euo pipefail if [ $# -lt 2 ] || [ $# -gt 3 ]; then echo "usage: $0 []" >&2 exit 2 fi account=$1 releases=$2 want_version=${3:-} root=$(cd -- "$(dirname -- "$0")/.." && pwd) catalog=$root/internal/catalog/catalog.json [ -d "$releases" ] || { echo "release dir not found: $releases" >&2; exit 1; } command -v carlos >/dev/null || { echo "carlos is not on PATH" >&2; exit 1; } # App ids, read from the catalogue so this list can't drift from Base's. ids=$(grep -o '"id": *"[^"]*"' "$catalog" | sed 's/.*: *"\(.*\)"/\1/') [ -n "$ids" ] || { echo "no app ids found in $catalog" >&2; exit 1; } # Decide every binary before touching the account, so ambiguous input # fails with nothing done. declare -A bin_of version_of for id in $ids; do found=() for f in "$releases/$id"-linux-arm64-*; do case $f in *.COMMIT) continue ;; esac [ -f "$f" ] || continue if [ -z "$want_version" ] || [ "${f##*-linux-arm64-}" = "$want_version" ]; then found+=("$f") fi done if [ ${#found[@]} -eq 0 ]; then echo "no $id-linux-arm64-${want_version:-*} binary in $releases" >&2 exit 1 fi if [ ${#found[@]} -gt 1 ]; then echo "$releases holds ${#found[@]} builds of $id; pass the version to use as a third argument:" >&2 printf ' %s\n' "${found[@]##*-linux-arm64-}" >&2 exit 1 fi bin_of[$id]=${found[0]} version_of[$id]=${found[0]##*-linux-arm64-} done # The apps this account already has (the Trash included). A failed listing # stops the script rather than looking like an empty account. apps_out=$(carlos apps list --account "$account") for id in $ids; do echo "== $id" bin=${bin_of[$id]} version=${version_of[$id]} if printf '%s\n' "$apps_out" | awk -v id="$id" '$1 == id { found = 1 } END { exit !found }'; then echo "app name already claimed in $account, skipping" else echo "claim the app name" carlos apps create --app "$id" --account "$account" fi # Is this version already shipped? A failed lookup is not "no". rel_out=$(carlos releases --app "$id" --account "$account") if printf '%s\n' "$rel_out" | awk -v v="$version" '$1 == v { found = 1 } END { exit !found }'; then echo "$version is already shipped" else echo "ship $version" # Uploads are rate limited: wait and retry while the console says so. while :; do if ship_out=$(carlos ship --app "$id" --account "$account" --no-prompt \ --version "$version" "$bin" 2>&1); then printf '%s\n' "$ship_out" break fi printf '%s\n' "$ship_out" >&2 case $ship_out in *"too many uploads"*) sleep "${PREPARE_APPS_RETRY_SLEEP:-10}" ;; *) exit 1 ;; esac done fi echo "promote $version to edge" carlos promote --app "$id" --account "$account" --no-prompt "$version" edge echo "enable instances" carlos instances enable --app "$id" --account "$account" done echo "done: every catalogue app is ready for Base to install"