|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Host-side helper that runs one opt-in smoke case of plan-20261001 on a |
| 3 | +# temporarily switched mega2 and always restores the default stack |
| 4 | +# (ADR-BB-06). |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# scripts/bb_optin_run.sh <none|gc|local> <script> <case> <log> |
| 8 | +# scripts/bb_optin_run.sh --selftest <none|gc|local> <log> |
| 9 | +# |
| 10 | +# Modes (mega2 is recreated with --force-recreate): |
| 11 | +# none overlay docker/docker-compose-storage-only.auth-none.yml (push_auth=none) |
| 12 | +# gc --env-file config/compose.env.storage-only.artifacts-gc |
| 13 | +# local --env-file config/compose.env.storage-only.local (local object storage) |
| 14 | +# |
| 15 | +# The case runs inside the `interop-smoke` service with its opt-in switch set |
| 16 | +# (MEGA2_SMOKE_AUTH_NONE / MEGA2_SMOKE_ARTIFACTS_GC / MEGA2_SMOKE_LOCAL_STORAGE) |
| 17 | +# and its output is tee'd to <log>. An EXIT trap recreates mega2 with the |
| 18 | +# default configuration and compares its compose config hash with the one |
| 19 | +# recorded before the switch. |
| 20 | +# |
| 21 | +# --selftest runs a stub instead of a smoke script: it prints |
| 22 | +# `bb-optin-selftest-case-ran`, reads the artifacts discovery document and |
| 23 | +# prints `bb-optin-selftest-signed_url_put=<value>`, then exits 1. |
| 24 | +# |
| 25 | +# Exit codes: 0 = case passed and stack restored; 1 = case failed and stack |
| 26 | +# restored; 2 = another helper run holds the stack lock (nothing switched). |
| 27 | +# |
| 28 | +# Only one helper run may own the mega2-trunk stack at a time: a lock |
| 29 | +# directory is held from the initial config hash through restoration. |
| 30 | +set -euo pipefail |
| 31 | + |
| 32 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 33 | +cd "$ROOT" |
| 34 | + |
| 35 | +COMPOSE=(docker compose -p mega2-trunk -f docker/docker-compose-storage-only.yml) |
| 36 | +CONFIG_DIR="config" |
| 37 | + |
| 38 | +selftest=0 |
| 39 | +if [ "${1:-}" = "--selftest" ]; then |
| 40 | + selftest=1 |
| 41 | + mode="${2:-}" |
| 42 | + script="" |
| 43 | + case_name="selftest" |
| 44 | + log="${3:-}" |
| 45 | +else |
| 46 | + mode="${1:-}" |
| 47 | + script="${2:-}" |
| 48 | + case_name="${3:-}" |
| 49 | + log="${4:-}" |
| 50 | +fi |
| 51 | + |
| 52 | +switched_args=() |
| 53 | +switch_var="" |
| 54 | +case "$mode" in |
| 55 | + none) |
| 56 | + switched_args=(-f docker/docker-compose-storage-only.auth-none.yml) |
| 57 | + switch_var=MEGA2_SMOKE_AUTH_NONE |
| 58 | + ;; |
| 59 | + gc) |
| 60 | + switched_args=(--env-file "$CONFIG_DIR/compose.env.storage-only.artifacts-gc") |
| 61 | + switch_var=MEGA2_SMOKE_ARTIFACTS_GC |
| 62 | + ;; |
| 63 | + local) |
| 64 | + switched_args=(--env-file "$CONFIG_DIR/compose.env.storage-only.local") |
| 65 | + switch_var=MEGA2_SMOKE_LOCAL_STORAGE |
| 66 | + ;; |
| 67 | + *) |
| 68 | + echo "usage: $0 <none|gc|local> <script> <case> <log> | --selftest <mode> <log>" >&2 |
| 69 | + exit 64 |
| 70 | + ;; |
| 71 | +esac |
| 72 | + |
| 73 | +LOCK_DIR="${TMPDIR:-/tmp}/mega2-trunk-optin.lock" |
| 74 | +if ! mkdir "$LOCK_DIR" 2>/dev/null; then |
| 75 | + echo "bb-optin: another helper run holds $LOCK_DIR; nothing switched" >&2 |
| 76 | + exit 2 |
| 77 | +fi |
| 78 | +trap 'rmdir "$LOCK_DIR"' EXIT |
| 79 | + |
| 80 | +mkdir -p "$(dirname "$log")" |
| 81 | +umask 077 |
| 82 | +: > "$log" |
| 83 | +chmod 600 "$log" |
| 84 | +say() { echo "$*" | tee -a "$log"; } |
| 85 | + |
| 86 | +config_hash() { |
| 87 | + "$@" config --hash mega2 | awk '{print $2}' |
| 88 | +} |
| 89 | + |
| 90 | +default_hash=$(config_hash "${COMPOSE[@]}") |
| 91 | +say "bb-optin: config hash before: $default_hash" |
| 92 | + |
| 93 | +# shellcheck disable=SC2317,SC2329 # invoked indirectly via `trap restore EXIT` |
| 94 | +restore() { |
| 95 | + local rc=$? |
| 96 | + set +e |
| 97 | + "${COMPOSE[@]}" up -d --wait --no-deps --force-recreate mega2 >> "$log" 2>&1 |
| 98 | + local up_rc=$? |
| 99 | + local after |
| 100 | + after=$(config_hash "${COMPOSE[@]}") |
| 101 | + if [ "$up_rc" -eq 0 ] && [ "$after" = "$default_hash" ]; then |
| 102 | + say "bb-optin: restored mega2 (config hash match)" |
| 103 | + else |
| 104 | + say "bb-optin: restore FAILED (up exit $up_rc, hash $after, want $default_hash)" |
| 105 | + fi |
| 106 | + rmdir "$LOCK_DIR" |
| 107 | + exit "$rc" |
| 108 | +} |
| 109 | +trap restore EXIT |
| 110 | + |
| 111 | +"${COMPOSE[@]}" "${switched_args[@]}" up -d --wait --no-deps --force-recreate mega2 >> "$log" 2>&1 |
| 112 | +switched_hash=$(config_hash "${COMPOSE[@]}" "${switched_args[@]}") |
| 113 | +if [ "$switched_hash" = "$default_hash" ]; then |
| 114 | + say "bb-optin: switch did not change the mega2 config (hash $switched_hash)" |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | +say "bb-optin: switched config hash $default_hash != $switched_hash" |
| 118 | + |
| 119 | +set +e |
| 120 | +if [ "$selftest" -eq 1 ]; then |
| 121 | + # shellcheck disable=SC2016 # the script body is expanded inside the container |
| 122 | + "${COMPOSE[@]}" --profile interop exec -T interop-smoke bash -c ' |
| 123 | + echo bb-optin-selftest-case-ran |
| 124 | + v=$(curl -fsS http://127.0.0.1:9000/api/v1/repos/bb-selftest/artifacts/discovery | jq -r .transfers.signed_url_put) |
| 125 | + echo "bb-optin-selftest-signed_url_put=$v" |
| 126 | + exit 1' 2>&1 | tee -a "$log" |
| 127 | +else |
| 128 | + "${COMPOSE[@]}" --profile interop exec -T -e "$switch_var=1" -e "MEGA2_SMOKE_CASE=$case_name" \ |
| 129 | + interop-smoke bash "/repo/scripts/$script" 2>&1 | tee -a "$log" |
| 130 | +fi |
| 131 | +case_rc=${PIPESTATUS[0]} |
| 132 | +set -e |
| 133 | +exit "$case_rc" |
0 commit comments