|
| 1 | +# vim: set ft=make : |
| 2 | +# Hypercube system migration commands |
| 3 | + |
| 4 | +# Run all pending system migrations |
| 5 | +migrate: |
| 6 | + #!/usr/bin/bash |
| 7 | + set -uo pipefail |
| 8 | + |
| 9 | + MIGRATIONS_DIR="/usr/share/hypercube/migrations" |
| 10 | + STATE_DIR="/var/lib/hypercube/migrations" |
| 11 | + COMPLETED_FILE="$STATE_DIR/completed" |
| 12 | + |
| 13 | + log() { echo "[migrate] $*"; } |
| 14 | + warn() { echo "[migrate] WARNING: $*" >&2; } |
| 15 | + |
| 16 | + mkdir -p "$STATE_DIR" |
| 17 | + touch "$COMPLETED_FILE" |
| 18 | + |
| 19 | + if [[ ! -d "$MIGRATIONS_DIR" ]]; then |
| 20 | + log "No migrations directory found" |
| 21 | + exit 0 |
| 22 | + fi |
| 23 | + |
| 24 | + shopt -s nullglob |
| 25 | + migrations=("$MIGRATIONS_DIR"/*.sh) |
| 26 | + |
| 27 | + if [[ ${#migrations[@]} -eq 0 ]]; then |
| 28 | + log "No migrations found" |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | + |
| 32 | + failed=0 |
| 33 | + for migration in "${migrations[@]}"; do |
| 34 | + name=$(basename "$migration") |
| 35 | + |
| 36 | + if grep -qxF "$name" "$COMPLETED_FILE" 2>/dev/null; then |
| 37 | + continue |
| 38 | + fi |
| 39 | + |
| 40 | + log "Running: $name" |
| 41 | + if bash "$migration"; then |
| 42 | + echo "$name" >> "$COMPLETED_FILE" |
| 43 | + log "$name completed" |
| 44 | + else |
| 45 | + warn "$name FAILED - will retry next boot" |
| 46 | + ((failed++)) || true |
| 47 | + fi |
| 48 | + done |
| 49 | + |
| 50 | + if [[ $failed -gt 0 ]]; then |
| 51 | + warn "$failed migration(s) failed" |
| 52 | + fi |
| 53 | + |
| 54 | +# List all migrations and their status |
| 55 | +migrate-status: |
| 56 | + #!/usr/bin/bash |
| 57 | + set -euo pipefail |
| 58 | + |
| 59 | + MIGRATIONS_DIR="/usr/share/hypercube/migrations" |
| 60 | + STATE_DIR="/var/lib/hypercube/migrations" |
| 61 | + COMPLETED_FILE="$STATE_DIR/completed" |
| 62 | + |
| 63 | + mkdir -p "$STATE_DIR" |
| 64 | + touch "$COMPLETED_FILE" |
| 65 | + |
| 66 | + echo "Hypercube Migrations" |
| 67 | + echo "====================" |
| 68 | + echo "" |
| 69 | + |
| 70 | + pending=0 |
| 71 | + completed=0 |
| 72 | + |
| 73 | + shopt -s nullglob |
| 74 | + for migration in "$MIGRATIONS_DIR"/*.sh; do |
| 75 | + name=$(basename "$migration") |
| 76 | + if grep -qxF "$name" "$COMPLETED_FILE" 2>/dev/null; then |
| 77 | + echo " [done] $name" |
| 78 | + ((completed++)) || true |
| 79 | + else |
| 80 | + echo " [pending] $name" |
| 81 | + ((pending++)) || true |
| 82 | + fi |
| 83 | + done |
| 84 | + |
| 85 | + echo "" |
| 86 | + echo "Total: $completed completed, $pending pending" |
| 87 | + |
| 88 | +# Mark a migration as completed (after manual fix) |
| 89 | +migrate-mark-done NAME: |
| 90 | + #!/usr/bin/bash |
| 91 | + set -euo pipefail |
| 92 | + |
| 93 | + MIGRATIONS_DIR="/usr/share/hypercube/migrations" |
| 94 | + STATE_DIR="/var/lib/hypercube/migrations" |
| 95 | + COMPLETED_FILE="$STATE_DIR/completed" |
| 96 | + NAME="{{ NAME }}" |
| 97 | + |
| 98 | + if [[ ! -f "$MIGRATIONS_DIR/$NAME" ]]; then |
| 99 | + echo "Error: Migration '$NAME' not found" >&2 |
| 100 | + echo "Run 'ujust migrate-status' to see available migrations" |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + mkdir -p "$STATE_DIR" |
| 105 | + touch "$COMPLETED_FILE" |
| 106 | + |
| 107 | + if grep -qxF "$NAME" "$COMPLETED_FILE" 2>/dev/null; then |
| 108 | + echo "Migration '$NAME' already marked as done" |
| 109 | + else |
| 110 | + echo "$NAME" >> "$COMPLETED_FILE" |
| 111 | + echo "Migration '$NAME' marked as done" |
| 112 | + fi |
| 113 | + |
| 114 | +# Reset a migration to pending (will re-run on next boot) |
| 115 | +migrate-reset NAME: |
| 116 | + #!/usr/bin/bash |
| 117 | + set -euo pipefail |
| 118 | + |
| 119 | + MIGRATIONS_DIR="/usr/share/hypercube/migrations" |
| 120 | + STATE_DIR="/var/lib/hypercube/migrations" |
| 121 | + COMPLETED_FILE="$STATE_DIR/completed" |
| 122 | + NAME="{{ NAME }}" |
| 123 | + |
| 124 | + if [[ ! -f "$MIGRATIONS_DIR/$NAME" ]]; then |
| 125 | + echo "Error: Migration '$NAME' not found" >&2 |
| 126 | + echo "Run 'ujust migrate-status' to see available migrations" |
| 127 | + exit 1 |
| 128 | + fi |
| 129 | + |
| 130 | + if grep -qxF "$NAME" "$COMPLETED_FILE" 2>/dev/null; then |
| 131 | + tmp=$(mktemp) |
| 132 | + grep -vxF "$NAME" "$COMPLETED_FILE" > "$tmp" || true |
| 133 | + sudo mv "$tmp" "$COMPLETED_FILE" |
| 134 | + echo "Migration '$NAME' reset to pending" |
| 135 | + else |
| 136 | + echo "Migration '$NAME' is already pending" |
| 137 | + fi |
| 138 | + |
| 139 | +# Force re-run a specific migration now |
| 140 | +migrate-rerun NAME: |
| 141 | + #!/usr/bin/bash |
| 142 | + set -euo pipefail |
| 143 | + |
| 144 | + MIGRATIONS_DIR="/usr/share/hypercube/migrations" |
| 145 | + STATE_DIR="/var/lib/hypercube/migrations" |
| 146 | + COMPLETED_FILE="$STATE_DIR/completed" |
| 147 | + NAME="{{ NAME }}" |
| 148 | + |
| 149 | + if [[ ! -f "$MIGRATIONS_DIR/$NAME" ]]; then |
| 150 | + echo "Error: Migration '$NAME' not found" >&2 |
| 151 | + echo "Run 'ujust migrate-status' to see available migrations" |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | + |
| 155 | + mkdir -p "$STATE_DIR" |
| 156 | + touch "$COMPLETED_FILE" |
| 157 | + |
| 158 | + echo "Running migration: $NAME" |
| 159 | + if sudo bash "$MIGRATIONS_DIR/$NAME"; then |
| 160 | + if ! grep -qxF "$NAME" "$COMPLETED_FILE" 2>/dev/null; then |
| 161 | + echo "$NAME" >> "$COMPLETED_FILE" |
| 162 | + fi |
| 163 | + echo "Migration '$NAME' completed" |
| 164 | + else |
| 165 | + echo "Migration '$NAME' FAILED" >&2 |
| 166 | + exit 1 |
| 167 | + fi |
0 commit comments