|
| 1 | +#!/usr/bin/env bash |
| 2 | +# plugins/dso/scripts/cutover-tickets-migration.sh |
| 3 | +# Phase-gate skeleton for the tickets migration cutover. |
| 4 | +# |
| 5 | +# Phases (in order): validate, snapshot, migrate, verify, finalize |
| 6 | +# Constant names: PRE_FLIGHT, SNAPSHOT, MIGRATE, VERIFY, FINALIZE |
| 7 | +# |
| 8 | +# Usage: cutover-tickets-migration.sh [--dry-run] [--repo-root=PATH] [--help] |
| 9 | +# |
| 10 | +# Environment variables: |
| 11 | +# CUTOVER_LOG_DIR Directory for timestamped log file (default: /tmp) |
| 12 | +# CUTOVER_STATE_FILE Path for the run state file (default: /tmp/cutover-tickets-migration-state.json) |
| 13 | +# CUTOVER_PHASE_EXIT_OVERRIDE "PHASE_NAME=EXIT_CODE" — inject a failure for testing |
| 14 | +# |
| 15 | +# Exit codes: 0=success, 1=error |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Phase constants (ordered) |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +readonly PHASES=( validate snapshot migrate verify finalize ) |
| 23 | +# Canonical constant aliases (for --help display) |
| 24 | +# PRE_FLIGHT=validate SNAPSHOT=snapshot MIGRATE=migrate VERIFY=verify FINALIZE=finalize |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# Argument parsing |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +_DRY_RUN="false" |
| 30 | +_REPO_ROOT="" |
| 31 | + |
| 32 | +for _arg in "$@"; do |
| 33 | + case "$_arg" in |
| 34 | + --help) |
| 35 | + cat <<'USAGE' |
| 36 | +Usage: cutover-tickets-migration.sh [--dry-run] [--repo-root=PATH] [--help] |
| 37 | +
|
| 38 | + --dry-run Execute phase stubs but skip state-file writes and |
| 39 | + any git-modifying actions. Prefixes output with [DRY RUN]. |
| 40 | + --repo-root=PATH Override the git repo root (default: git rev-parse --show-toplevel). |
| 41 | + --help Print this usage message and exit. |
| 42 | +
|
| 43 | +Phases (run in order): |
| 44 | + 1. validate (alias: PRE_FLIGHT) — pre-flight checks |
| 45 | + 2. snapshot (alias: SNAPSHOT) — snapshot current ticket state |
| 46 | + 3. migrate (alias: MIGRATE) — migrate ticket format |
| 47 | + 4. verify (alias: VERIFY) — verify migration results |
| 48 | + 5. finalize (alias: FINALIZE / REFERENCE_UPDATE / CLEANUP) — update references and clean up |
| 49 | +
|
| 50 | +Environment variables: |
| 51 | + CUTOVER_LOG_DIR Log directory (default: /tmp) |
| 52 | + CUTOVER_STATE_FILE State file path (default: /tmp/cutover-tickets-migration-state.json) |
| 53 | + CUTOVER_PHASE_EXIT_OVERRIDE Inject a phase failure, e.g. "MIGRATE=1" (for testing only) |
| 54 | +
|
| 55 | +USAGE |
| 56 | + exit 0 |
| 57 | + ;; |
| 58 | + --dry-run) |
| 59 | + _DRY_RUN="true" |
| 60 | + ;; |
| 61 | + --repo-root=*) |
| 62 | + _REPO_ROOT="${_arg#--repo-root=}" |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "ERROR: Unknown argument: $_arg" >&2 |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | + esac |
| 69 | +done |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# Resolve REPO_ROOT |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | +if [[ -z "$_REPO_ROOT" ]]; then |
| 75 | + if ! _REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null); then |
| 76 | + echo "ERROR: Not a git repository and --repo-root not supplied." >&2 |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +fi |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# Log file setup |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | +: "${CUTOVER_LOG_DIR:=/tmp}" |
| 85 | +_LOG_TIMESTAMP=$(date +%Y-%m-%dT%H-%M-%S) |
| 86 | +_LOG_FILE="${CUTOVER_LOG_DIR}/cutover-${_LOG_TIMESTAMP}.log" |
| 87 | + |
| 88 | +# Re-exec the script under tee to capture all output to the log file while |
| 89 | +# also printing to stdout. PIPESTATUS[0] preserves the real exit code. |
| 90 | +# Guard with _CUTOVER_LOGGING to prevent infinite re-exec. |
| 91 | +if [[ -z "${_CUTOVER_LOGGING:-}" ]]; then |
| 92 | + export _CUTOVER_LOGGING=1 |
| 93 | + mkdir -p "$CUTOVER_LOG_DIR" |
| 94 | + bash "$0" "$@" 2>&1 | tee -a "$_LOG_FILE" |
| 95 | + exit "${PIPESTATUS[0]}" |
| 96 | +fi |
| 97 | + |
| 98 | +# --------------------------------------------------------------------------- |
| 99 | +# State file |
| 100 | +# --------------------------------------------------------------------------- |
| 101 | +: "${CUTOVER_STATE_FILE:=/tmp/cutover-tickets-migration-state.json}" |
| 102 | + |
| 103 | +_state_append_phase() { |
| 104 | + local phase="$1" |
| 105 | + if [[ "$_DRY_RUN" == "true" ]]; then |
| 106 | + return 0 |
| 107 | + fi |
| 108 | + # Append completed phase to state file |
| 109 | + if [[ ! -f "$CUTOVER_STATE_FILE" ]]; then |
| 110 | + printf '{"completed_phases":["%s"]}\n' "$phase" > "$CUTOVER_STATE_FILE" |
| 111 | + else |
| 112 | + # Use python3 for reliable JSON update (stdlib, no new deps) |
| 113 | + python3 - "$CUTOVER_STATE_FILE" "$phase" <<'PYEOF' |
| 114 | +import sys, json |
| 115 | +path, phase = sys.argv[1], sys.argv[2] |
| 116 | +with open(path) as fh: |
| 117 | + data = json.load(fh) |
| 118 | +data.setdefault("completed_phases", []).append(phase) |
| 119 | +with open(path, "w") as fh: |
| 120 | + json.dump(data, fh) |
| 121 | + fh.write("\n") |
| 122 | +PYEOF |
| 123 | + fi |
| 124 | +} |
| 125 | + |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | +# Test injection hook: CUTOVER_PHASE_EXIT_OVERRIDE |
| 128 | +# Format: "PHASE_NAME=EXIT_CODE", e.g., "MIGRATE=1" or "PRE_FLIGHT=1" |
| 129 | +# --------------------------------------------------------------------------- |
| 130 | +_check_override() { |
| 131 | + local phase_lower="$1" |
| 132 | + local phase_upper |
| 133 | + phase_upper=$(echo "$phase_lower" | tr '[:lower:]' '[:upper:]') |
| 134 | + if [[ -n "${CUTOVER_PHASE_EXIT_OVERRIDE:-}" ]]; then |
| 135 | + local override_phase override_code resolved_upper |
| 136 | + override_phase="${CUTOVER_PHASE_EXIT_OVERRIDE%%=*}" |
| 137 | + override_code="${CUTOVER_PHASE_EXIT_OVERRIDE##*=}" |
| 138 | + # Resolve constant aliases to their canonical uppercase phase name |
| 139 | + case "$override_phase" in |
| 140 | + PRE_FLIGHT) resolved_upper="VALIDATE" ;; |
| 141 | + SNAPSHOT) resolved_upper="SNAPSHOT" ;; |
| 142 | + MIGRATE) resolved_upper="MIGRATE" ;; |
| 143 | + VERIFY) resolved_upper="VERIFY" ;; |
| 144 | + FINALIZE|REFERENCE_UPDATE|CLEANUP) resolved_upper="FINALIZE" ;; |
| 145 | + *) resolved_upper="$override_phase" ;; |
| 146 | + esac |
| 147 | + if [[ "$resolved_upper" == "$phase_upper" ]]; then |
| 148 | + return "$override_code" |
| 149 | + fi |
| 150 | + fi |
| 151 | + return 0 |
| 152 | +} |
| 153 | + |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | +# Phase handler stubs |
| 156 | +# (Actual migration logic added by sibling stories w21-7mlx, w21-wbqz, w21-25mq) |
| 157 | +# --------------------------------------------------------------------------- |
| 158 | + |
| 159 | +_phase_validate() { |
| 160 | + echo "Running phase: validate" |
| 161 | + _check_override "validate" |
| 162 | +} |
| 163 | + |
| 164 | +_phase_snapshot() { |
| 165 | + echo "Running phase: snapshot" |
| 166 | + _check_override "snapshot" |
| 167 | +} |
| 168 | + |
| 169 | +_phase_migrate() { |
| 170 | + echo "Running phase: migrate" |
| 171 | + _check_override "migrate" |
| 172 | +} |
| 173 | + |
| 174 | +_phase_verify() { |
| 175 | + echo "Running phase: verify" |
| 176 | + _check_override "verify" |
| 177 | +} |
| 178 | + |
| 179 | +_phase_finalize() { |
| 180 | + echo "Running phase: finalize" |
| 181 | + _check_override "finalize" |
| 182 | +} |
| 183 | + |
| 184 | +# --------------------------------------------------------------------------- |
| 185 | +# Dry-run wrapper: prefix every line of a phase's output with [DRY RUN] |
| 186 | +# --------------------------------------------------------------------------- |
| 187 | +_run_phase_dry() { |
| 188 | + local phase="$1" |
| 189 | + local phase_fn="_phase_${phase}" |
| 190 | + # Run in subshell, capture output, prefix each line |
| 191 | + local _out |
| 192 | + _out=$("$phase_fn" 2>&1) || return $? |
| 193 | + while IFS= read -r _line; do |
| 194 | + echo "[DRY RUN] $_line" |
| 195 | + done <<< "$_out" |
| 196 | +} |
| 197 | + |
| 198 | +# --------------------------------------------------------------------------- |
| 199 | +# Phase gate loop |
| 200 | +# --------------------------------------------------------------------------- |
| 201 | +echo "cutover-tickets-migration: starting (dry_run=${_DRY_RUN})" |
| 202 | + |
| 203 | +for _phase in "${PHASES[@]}"; do |
| 204 | + if [[ "$_DRY_RUN" == "true" ]]; then |
| 205 | + "_run_phase_dry" "$_phase" || { _rc=$?; echo "[DRY RUN] ERROR: phase ${_phase} failed (exit ${_rc}) — see ${_LOG_FILE}" >&2; exit "$_rc"; } |
| 206 | + else |
| 207 | + "_phase_${_phase}" || { _rc=$?; echo "ERROR: phase ${_phase} failed — see ${_LOG_FILE}" >&2; exit "$_rc"; } |
| 208 | + _state_append_phase "$_phase" |
| 209 | + fi |
| 210 | +done |
| 211 | + |
| 212 | +echo "cutover-tickets-migration: all phases complete" |
0 commit comments