79 lines
3.4 KiB
Bash
79 lines
3.4 KiB
Bash
#!/bin/sh
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
#
|
|
# A client of Ianitor's preview API (docs/previews.md) for a Fabrica pipeline step or a script:
|
|
#
|
|
# scripts/preview.sh open <project> <branch> <upstream> [ttl-seconds] prints the preview's URL
|
|
# scripts/preview.sh close <project> <branch>
|
|
# scripts/preview.sh list
|
|
#
|
|
# IANITOR_URL Ianitor's admin address, e.g. https://ianitor.example.org
|
|
# IANITOR_PREVIEW_TOKEN a token made with `ianitor-control token create --scope previews`
|
|
# (kept in the Custos team vault; never in the repository or a log)
|
|
# IANITOR_CA optional: a CA file for curl (a Domi root, for an internal admin host)
|
|
#
|
|
# `open` waits until the preview is live (its apply kept) and fails with Ianitor's reason
|
|
# otherwise. Needs curl and jq.
|
|
set -eu
|
|
|
|
: "${IANITOR_URL:?set IANITOR_URL}"
|
|
: "${IANITOR_PREVIEW_TOKEN:?set IANITOR_PREVIEW_TOKEN}"
|
|
command -v jq >/dev/null || { echo "preview: needs jq" >&2; exit 2; }
|
|
|
|
api() {
|
|
# The token goes in a header read from stdin, so it never shows in `ps`.
|
|
method=$1; path=$2; body=${3:-}
|
|
set -- -sS -X "$method" -H "Content-Type: application/json" -H @- -w '\n%{http_code}'
|
|
[ -n "${IANITOR_CA:-}" ] && set -- "$@" --cacert "$IANITOR_CA"
|
|
[ -n "$body" ] && set -- "$@" --data "$body"
|
|
printf 'Authorization: Bearer %s\n' "$IANITOR_PREVIEW_TOKEN" | curl "$@" "${IANITOR_URL%/}$path"
|
|
}
|
|
|
|
split() { # sets BODY and CODE from api's output
|
|
CODE=$(printf '%s' "$1" | tail -n1)
|
|
BODY=$(printf '%s' "$1" | sed '$d')
|
|
}
|
|
|
|
fail() {
|
|
echo "preview: $(printf '%s' "$BODY" | jq -r '.error.message // .' 2>/dev/null || printf '%s' "$BODY")" >&2
|
|
exit 1
|
|
}
|
|
|
|
case "${1:-}" in
|
|
open)
|
|
[ $# -ge 4 ] || { echo "usage: $0 open <project> <branch> <upstream> [ttl]" >&2; exit 2; }
|
|
body=$(jq -cn --arg p "$2" --arg b "$3" --arg u "$4" --arg t "${5:-}" \
|
|
'{project:$p, branch:$b, upstream:$u} + (if $t == "" then {} else {ttl: ($t|tonumber)} end)')
|
|
split "$(api POST /api/v1/previews "$body")"
|
|
[ "$CODE" = 202 ] || fail
|
|
host=$(printf '%s' "$BODY" | jq -r .preview.hostname)
|
|
url=$(printf '%s' "$BODY" | jq -r .preview.url)
|
|
i=0
|
|
while :; do
|
|
split "$(api GET "/api/v1/previews/$host")"
|
|
[ "$CODE" = 200 ] || fail
|
|
state=$(printf '%s' "$BODY" | jq -r .preview.state)
|
|
case "$state" in
|
|
live) echo "$url"; exit 0 ;;
|
|
failed) echo "preview: $(printf '%s' "$BODY" | jq -r '.preview.problem // "the apply failed"')" >&2; exit 1 ;;
|
|
esac
|
|
i=$((i + 1)); [ $i -le 150 ] || { echo "preview: not live after 5 minutes" >&2; exit 1; }
|
|
sleep 2
|
|
done
|
|
;;
|
|
close)
|
|
[ $# -eq 3 ] || { echo "usage: $0 close <project> <branch>" >&2; exit 2; }
|
|
split "$(api POST /api/v1/previews/close "$(jq -cn --arg p "$2" --arg b "$3" '{project:$p, branch:$b}')")"
|
|
case "$CODE" in 202) echo "closing"; ;; 404) echo "no preview"; ;; *) fail ;; esac
|
|
;;
|
|
list)
|
|
split "$(api GET /api/v1/previews)"
|
|
[ "$CODE" = 200 ] || fail
|
|
printf '%s' "$BODY" | jq -r '.items[] | "\(.url)\t\(.project)\t\(.branch)\t\(.state)\tuntil \(.expires_at)"'
|
|
;;
|
|
*)
|
|
echo "usage: $0 open|close|list ..." >&2; exit 2 ;;
|
|
esac
|