|
| 1 | +#!/bin/bash |
| 2 | +# Helper functions and variables for the VMDisk backup/restore demo. |
| 3 | +# Source this file in other scripts: source "$(dirname "$0")/00-helpers.sh" |
| 4 | + |
| 5 | +# ANSI color codes |
| 6 | +export RED='\033[0;31m' |
| 7 | +export GREEN='\033[0;32m' |
| 8 | +export YELLOW='\033[1;33m' |
| 9 | +export MAGENTA='\033[0;35m' |
| 10 | +export CYAN='\033[0;36m' |
| 11 | +export WHITE='\033[1;37m' |
| 12 | +export NC='\033[0m' |
| 13 | +export BOLD='\033[1m' |
| 14 | + |
| 15 | +# Default settings |
| 16 | +export NAMESPACE="${NAMESPACE:-tenant-root}" |
| 17 | +# The platform ships the SeaweedFS-backed BackupStorageLocation as cozy-default |
| 18 | +# (packages/system/backupstrategy-controller). Point the demo/e2e strategy at it |
| 19 | +# by default so the flow works out of the box on a backups-enabled cluster; |
| 20 | +# override for an external S3 whose BSL carries a different name. |
| 21 | +export BACKUP_STORAGE_LOCATION="${BACKUP_STORAGE_LOCATION:-cozy-default}" |
| 22 | + |
| 23 | +# Logging functions (output to stderr to avoid polluting captured output) |
| 24 | +log_info() { echo -e "${CYAN}ℹ${NC} $*" >&2; } |
| 25 | +log_success() { echo -e "${GREEN}✔${NC} $*" >&2; } |
| 26 | +log_warning() { echo -e "${YELLOW}⚠${NC} $*" >&2; } |
| 27 | +log_error() { echo -e "${RED}✖${NC} $*" >&2; } |
| 28 | +log_step() { echo -e "\n${MAGENTA}${BOLD}▶ $*${NC}" >&2; } |
| 29 | +log_command() { echo -e "${WHITE} \$ $*${NC}" >&2; } |
| 30 | + |
| 31 | +separator() { |
| 32 | + echo -e "\n${CYAN}────────────────────────────────────────────────────────────${NC}\n" >&2 |
| 33 | +} |
| 34 | + |
| 35 | +print_header() { |
| 36 | + local title="$1" |
| 37 | + echo -e "\n${MAGENTA}${BOLD}== ${title} ==${NC}\n" >&2 |
| 38 | +} |
| 39 | + |
| 40 | +# Wait for a resource field to reach a desired value. |
| 41 | +# Args: type name jsonpath desired [namespace] [timeout] [fail_value] |
| 42 | +wait_for_field() { |
| 43 | + local resource_type="$1" |
| 44 | + local resource_name="$2" |
| 45 | + local jsonpath="$3" |
| 46 | + local desired="$4" |
| 47 | + local namespace="${5:-}" |
| 48 | + local timeout="${6:-300}" |
| 49 | + # Optional terminal-failure value: when the field reaches it, stop |
| 50 | + # immediately instead of polling out the timeout. Fail fast, fail loud. |
| 51 | + local fail_value="${7:-}" |
| 52 | + |
| 53 | + log_command "waiting for $resource_type/$resource_name $jsonpath == '$desired'" |
| 54 | + |
| 55 | + local elapsed=0 |
| 56 | + local ns_flag="" |
| 57 | + [[ -n "$namespace" ]] && ns_flag="-n $namespace" |
| 58 | + |
| 59 | + while true; do |
| 60 | + local current |
| 61 | + # shellcheck disable=SC2086 |
| 62 | + current=$(kubectl get "$resource_type" "$resource_name" $ns_flag -o jsonpath="$jsonpath" 2>/dev/null || true) |
| 63 | + if [[ "$current" == "$desired" ]]; then |
| 64 | + log_success "$resource_type/$resource_name reached '$desired'" |
| 65 | + return 0 |
| 66 | + fi |
| 67 | + if [[ -n "$fail_value" && "$current" == "$fail_value" ]]; then |
| 68 | + log_error "$resource_type/$resource_name reached terminal failure state '$fail_value'" |
| 69 | + return 1 |
| 70 | + fi |
| 71 | + if [[ $elapsed -ge $timeout ]]; then |
| 72 | + log_error "Timeout waiting for $resource_type/$resource_name (current: '$current', expected: '$desired')" |
| 73 | + return 1 |
| 74 | + fi |
| 75 | + sleep 5 |
| 76 | + elapsed=$((elapsed + 5)) |
| 77 | + echo -n "." >&2 |
| 78 | + done |
| 79 | +} |
0 commit comments