|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Project N.O.M.A.D. - One-Time Updater Fix Script |
| 4 | +# |
| 5 | +# Script | Project N.O.M.A.D. One-Time Updater Fix Script |
| 6 | +# Version | 1.0.0 |
| 7 | +# Author | Crosstalk Solutions, LLC |
| 8 | +# Website | https://crosstalksolutions.com |
| 9 | +# |
| 10 | +# PURPOSE: |
| 11 | +# This is a one-time migration script. It deploys two fixes to the sidecar |
| 12 | +# updater that cannot be applied through the normal in-app update mechanism: |
| 13 | +# |
| 14 | +# Fix 1 — Sidecar volume write access |
| 15 | +# Removes the :ro (read-only) flag from the sidecar's /opt/project-nomad |
| 16 | +# volume mount in compose.yml. The sidecar must be able to write to |
| 17 | +# compose.yml so it can set the correct Docker image tag when installing |
| 18 | +# RC or stable versions. |
| 19 | +# |
| 20 | +# Fix 2 — RC-aware sidecar watcher |
| 21 | +# Downloads the updated sidecar Dockerfile (adds jq) and update-watcher.sh |
| 22 | +# (reads target_tag from the update request and applies it to compose.yml |
| 23 | +# before pulling images), then rebuilds and restarts the sidecar container. |
| 24 | +# |
| 25 | +# NOTE: The companion fix in the admin service (system_update_service.ts, |
| 26 | +# which writes the target_tag into the update request) ships in the GHCR |
| 27 | +# image and will take effect automatically on the next normal app update. |
| 28 | + |
| 29 | +############################################################################### |
| 30 | +# Color Codes |
| 31 | +############################################################################### |
| 32 | + |
| 33 | +RESET='\033[0m' |
| 34 | +YELLOW='\033[1;33m' |
| 35 | +RED='\033[1;31m' |
| 36 | +GREEN='\033[1;32m' |
| 37 | +WHITE_R='\033[39m' |
| 38 | + |
| 39 | +############################################################################### |
| 40 | +# Constants |
| 41 | +############################################################################### |
| 42 | + |
| 43 | +NOMAD_DIR="/opt/project-nomad" |
| 44 | +COMPOSE_FILE="${NOMAD_DIR}/compose.yml" |
| 45 | +SIDECAR_DIR="${NOMAD_DIR}/sidecar-updater" |
| 46 | +COMPOSE_PROJECT_NAME="project-nomad" |
| 47 | + |
| 48 | +SIDECAR_DOCKERFILE_URL="https://raw.githubusercontent.com/Crosstalk-Solutions/project-nomad/refs/heads/main/install/sidecar-updater/Dockerfile" |
| 49 | +SIDECAR_SCRIPT_URL="https://raw.githubusercontent.com/Crosstalk-Solutions/project-nomad/refs/heads/main/install/sidecar-updater/update-watcher.sh" |
| 50 | + |
| 51 | +############################################################################### |
| 52 | +# Pre-flight Checks |
| 53 | +############################################################################### |
| 54 | + |
| 55 | +check_is_bash() { |
| 56 | + if [[ -z "$BASH_VERSION" ]]; then |
| 57 | + echo -e "${RED}#${RESET} This script must be run with bash." |
| 58 | + echo -e "${RED}#${RESET} Example: bash $(basename "$0")" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + echo -e "${GREEN}#${RESET} Running in bash.\n" |
| 62 | +} |
| 63 | + |
| 64 | +check_confirmation() { |
| 65 | + echo -e "${YELLOW}#${RESET} This is a very specific fix script for a very specific issue. You probably don't need to run this unless you were specifically directed to by the N.O.M.A.D. team." |
| 66 | + echo -e "${YELLOW}#${RESET} Please ensure you have a backup of your data before proceeding." |
| 67 | + read -rp "Do you want to continue? (y/N) " response |
| 68 | + if [[ ! "$response" =~ ^[Yy]$ ]]; then |
| 69 | + echo -e "${RED}#${RESET} Aborting. No changes have been made." |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | + echo -e "${GREEN}#${RESET} Confirmation received. Proceeding with fixes...\n" |
| 73 | +} |
| 74 | + |
| 75 | +check_has_sudo() { |
| 76 | + if sudo -n true 2>/dev/null; then |
| 77 | + echo -e "${GREEN}#${RESET} Sudo permissions confirmed.\n" |
| 78 | + else |
| 79 | + echo -e "${RED}#${RESET} This script requires sudo permissions." |
| 80 | + echo -e "${RED}#${RESET} Example: sudo bash $(basename "$0")" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +check_docker_running() { |
| 86 | + if ! command -v docker &>/dev/null; then |
| 87 | + echo -e "${RED}#${RESET} Docker is not installed. Cannot proceed." |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + if ! systemctl is-active --quiet docker; then |
| 91 | + echo -e "${RED}#${RESET} Docker is not running. Please start Docker and try again." |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + echo -e "${GREEN}#${RESET} Docker is running.\n" |
| 95 | +} |
| 96 | + |
| 97 | +check_compose_file() { |
| 98 | + if [[ ! -f "$COMPOSE_FILE" ]]; then |
| 99 | + echo -e "${RED}#${RESET} compose.yml not found at ${COMPOSE_FILE}." |
| 100 | + echo -e "${RED}#${RESET} Please ensure Project N.O.M.A.D. is installed before running this script." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + echo -e "${GREEN}#${RESET} Found compose.yml at ${COMPOSE_FILE}.\n" |
| 104 | +} |
| 105 | + |
| 106 | +check_sidecar_dir() { |
| 107 | + if [[ ! -d "$SIDECAR_DIR" ]]; then |
| 108 | + echo -e "${RED}#${RESET} Sidecar directory not found at ${SIDECAR_DIR}." |
| 109 | + echo -e "${RED}#${RESET} Please ensure Project N.O.M.A.D. is installed before running this script." |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + echo -e "${GREEN}#${RESET} Found sidecar directory at ${SIDECAR_DIR}.\n" |
| 113 | +} |
| 114 | + |
| 115 | +############################################################################### |
| 116 | +# Fix 1 — Remove :ro from sidecar volume mount |
| 117 | +############################################################################### |
| 118 | + |
| 119 | +backup_compose_file() { |
| 120 | + local backup="${COMPOSE_FILE}.bak.$(date +%Y%m%d%H%M%S)" |
| 121 | + echo -e "${YELLOW}#${RESET} Backing up compose.yml to ${backup}..." |
| 122 | + if cp "$COMPOSE_FILE" "$backup"; then |
| 123 | + echo -e "${GREEN}#${RESET} Backup created at ${backup}.\n" |
| 124 | + else |
| 125 | + echo -e "${RED}#${RESET} Failed to create backup. Aborting." |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +fix_sidecar_volume_mount() { |
| 131 | + # Idempotent: skip if :ro is already absent from the sidecar mount line |
| 132 | + if ! grep -q '/opt/project-nomad:/opt/project-nomad:ro' "$COMPOSE_FILE"; then |
| 133 | + echo -e "${GREEN}#${RESET} Sidecar volume mount is already writable — no change needed.\n" |
| 134 | + return 0 |
| 135 | + fi |
| 136 | + |
| 137 | + echo -e "${YELLOW}#${RESET} Removing :ro restriction from sidecar volume mount in compose.yml..." |
| 138 | + sed -i 's|/opt/project-nomad:/opt/project-nomad:ro.*|/opt/project-nomad:/opt/project-nomad # Writable access required so the updater can set the correct image tag in compose.yml|' "$COMPOSE_FILE" |
| 139 | + |
| 140 | + if grep -q '/opt/project-nomad:/opt/project-nomad:ro' "$COMPOSE_FILE"; then |
| 141 | + echo -e "${RED}#${RESET} Failed to remove :ro from compose.yml. Please update it manually:" |
| 142 | + echo -e "${WHITE_R} - /opt/project-nomad:/opt/project-nomad:ro${RESET} → ${WHITE_R}- /opt/project-nomad:/opt/project-nomad${RESET}" |
| 143 | + exit 1 |
| 144 | + fi |
| 145 | + |
| 146 | + echo -e "${GREEN}#${RESET} Sidecar volume mount updated successfully.\n" |
| 147 | +} |
| 148 | + |
| 149 | +############################################################################### |
| 150 | +# Fix 2 — Download updated sidecar files and rebuild |
| 151 | +############################################################################### |
| 152 | + |
| 153 | +download_updated_sidecar_files() { |
| 154 | + echo -e "${YELLOW}#${RESET} Downloading updated sidecar Dockerfile..." |
| 155 | + if ! curl -fsSL "$SIDECAR_DOCKERFILE_URL" -o "${SIDECAR_DIR}/Dockerfile"; then |
| 156 | + echo -e "${RED}#${RESET} Failed to download sidecar Dockerfile. Check your network connection." |
| 157 | + exit 1 |
| 158 | + fi |
| 159 | + echo -e "${GREEN}#${RESET} Sidecar Dockerfile updated.\n" |
| 160 | + |
| 161 | + echo -e "${YELLOW}#${RESET} Downloading updated update-watcher.sh..." |
| 162 | + if ! curl -fsSL "$SIDECAR_SCRIPT_URL" -o "${SIDECAR_DIR}/update-watcher.sh"; then |
| 163 | + echo -e "${RED}#${RESET} Failed to download update-watcher.sh. Check your network connection." |
| 164 | + exit 1 |
| 165 | + fi |
| 166 | + chmod +x "${SIDECAR_DIR}/update-watcher.sh" |
| 167 | + echo -e "${GREEN}#${RESET} update-watcher.sh updated.\n" |
| 168 | +} |
| 169 | + |
| 170 | +rebuild_sidecar() { |
| 171 | + echo -e "${YELLOW}#${RESET} Rebuilding the updater container (this may take a moment)..." |
| 172 | + if ! docker compose -p "$COMPOSE_PROJECT_NAME" -f "$COMPOSE_FILE" build updater; then |
| 173 | + echo -e "${RED}#${RESET} Failed to rebuild the updater container. See output above for details." |
| 174 | + exit 1 |
| 175 | + fi |
| 176 | + echo -e "${GREEN}#${RESET} Updater container rebuilt successfully.\n" |
| 177 | +} |
| 178 | + |
| 179 | +restart_sidecar() { |
| 180 | + echo -e "${YELLOW}#${RESET} Restarting the updater container..." |
| 181 | + if ! docker compose -p "$COMPOSE_PROJECT_NAME" -f "$COMPOSE_FILE" up -d --force-recreate updater; then |
| 182 | + echo -e "${RED}#${RESET} Failed to restart the updater container." |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | + echo -e "${GREEN}#${RESET} Updater container restarted.\n" |
| 186 | +} |
| 187 | + |
| 188 | +verify_sidecar_running() { |
| 189 | + sleep 3 |
| 190 | + if docker ps --filter "name=nomad_updater" --filter "status=running" --format '{{.Names}}' | grep -q "nomad_updater"; then |
| 191 | + echo -e "${GREEN}#${RESET} Updater container is running.\n" |
| 192 | + else |
| 193 | + echo -e "${RED}#${RESET} Updater container does not appear to be running." |
| 194 | + echo -e "${RED}#${RESET} Check its logs with: docker logs nomad_updater" |
| 195 | + exit 1 |
| 196 | + fi |
| 197 | +} |
| 198 | + |
| 199 | +############################################################################### |
| 200 | +# Main |
| 201 | +############################################################################### |
| 202 | + |
| 203 | +echo -e "${GREEN}#########################################################################${RESET}" |
| 204 | +echo -e "${GREEN}#${RESET} Project N.O.M.A.D. — One-Time Updater Fix Script ${GREEN}#${RESET}" |
| 205 | +echo -e "${GREEN}#########################################################################${RESET}\n" |
| 206 | + |
| 207 | +check_is_bash |
| 208 | +check_has_sudo |
| 209 | +chech_confirmation |
| 210 | +check_docker_running |
| 211 | +check_compose_file |
| 212 | +check_sidecar_dir |
| 213 | + |
| 214 | +echo -e "${YELLOW}#${RESET} Starting Fix 1: Sidecar volume write access...\n" |
| 215 | +backup_compose_file |
| 216 | +fix_sidecar_volume_mount |
| 217 | + |
| 218 | +echo -e "${YELLOW}#${RESET} Starting Fix 2: RC-aware sidecar watcher...\n" |
| 219 | +download_updated_sidecar_files |
| 220 | +rebuild_sidecar |
| 221 | +restart_sidecar |
| 222 | +verify_sidecar_running |
| 223 | + |
| 224 | +echo -e "${GREEN}#########################################################################${RESET}" |
| 225 | +echo -e "${GREEN}#${RESET} All fixes applied successfully!" |
| 226 | +echo -e "${GREEN}#${RESET}" |
| 227 | +echo -e "${GREEN}#${RESET} The updater sidecar can now install RC and stable versions correctly." |
| 228 | +echo -e "${GREEN}#${RESET} The remaining fix (admin service target_tag support) will apply" |
| 229 | +echo -e "${GREEN}#${RESET} automatically the next time you update N.O.M.A.D. via the UI." |
| 230 | +echo -e "${GREEN}#########################################################################${RESET}\n" |
0 commit comments