From 58bef0ab2576312f3ae2091fa50773d61397426e Mon Sep 17 00:00:00 2001 From: Till JS Date: Sun, 29 Mar 2026 19:16:49 +0200 Subject: [PATCH] fix: rewrite startup.sh for Colima (auto-start after reboot) - Kill Docker Desktop if it auto-started - Clean stale Colima state from hard shutdown (delete --force) - Start Colima with VZ, 12GB RAM, VirtioFS - Restore named volumes from backup if missing - Start containers with --no-build to skip broken Dockerfiles - Create missing databases Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/mac-mini/startup.sh | 111 +++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/scripts/mac-mini/startup.sh b/scripts/mac-mini/startup.sh index 98c81280e..2852be517 100755 --- a/scripts/mac-mini/startup.sh +++ b/scripts/mac-mini/startup.sh @@ -1,11 +1,12 @@ #!/bin/bash # ManaCore Mac Mini Startup Script -# This script is called by launchd on boot to start all services +# Called by launchd on boot — starts Colima + all containers +# +# LaunchAgent: ~/Library/LaunchAgents/com.manacore.docker-startup.plist -set -e +set -uo pipefail -# Ensure PATH includes docker -export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH" +export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" LOG_FILE="/tmp/manacore-startup.log" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -20,49 +21,91 @@ log() { log "=== ManaCore Startup Script ===" log "Project root: $PROJECT_ROOT" -# Wait for Docker to be ready -log "Waiting for Docker..." -MAX_WAIT=120 -WAITED=0 -while ! docker info >/dev/null 2>&1; do +# ─── Kill Docker Desktop if it auto-started ─── +if pgrep -f "Docker.app" >/dev/null 2>&1; then + log "Docker Desktop detected, stopping..." + osascript -e 'quit app "Docker"' 2>/dev/null + sleep 5 + pkill -f "Docker.app" 2>/dev/null || true + sleep 3 + log "Docker Desktop stopped" +fi + +# ─── Start Colima ─── +if colima status 2>/dev/null | grep -q "running"; then + log "Colima already running" +else + log "Starting Colima..." + + # Clean stale state from hard shutdown + colima stop --force 2>/dev/null || true + colima delete --force 2>/dev/null || true sleep 2 - WAITED=$((WAITED + 2)) - if [ $WAITED -ge $MAX_WAIT ]; then - log "ERROR: Docker not available after ${MAX_WAIT}s" + + colima start \ + --cpu 8 \ + --memory 12 \ + --disk 200 \ + --vm-type vz \ + --vz-rosetta \ + --mount-type virtiofs \ + --mount /Volumes/ManaData:w \ + 2>&1 | tee -a "$LOG_FILE" + + if ! colima status 2>/dev/null | grep -q "running"; then + log "ERROR: Colima failed to start" exit 1 fi -done -log "Docker is ready (waited ${WAITED}s)" + log "Colima started successfully" +fi -# Check if env file exists +# ─── Verify Docker CLI works ─── +if ! docker info >/dev/null 2>&1; then + log "ERROR: Docker CLI not connected to Colima" + exit 1 +fi +log "Docker CLI connected" + +# ─── Restore named volumes if missing ─── +BACKUP_DIR="/Volumes/ManaData/backups/docker-migration-20260328" +for vol in mana-redis-data mana-victoria-data mana-alertmanager-data mana-grafana-data mana-analytics-data mana-loki-data mana-matrix-bots-data; do + if ! docker volume inspect "$vol" >/dev/null 2>&1; then + BACKUP_FILE="$BACKUP_DIR/${vol}.tar.gz" + if [ -f "$BACKUP_FILE" ]; then + log "Restoring volume: $vol" + docker volume create "$vol" >/dev/null + docker run --rm -v "$vol":/target -v "$BACKUP_DIR":/backup:ro \ + alpine sh -c "tar xzf /backup/${vol}.tar.gz -C /target 2>/dev/null" + fi + fi +done + +# ─── Check prerequisites ─── +if [ ! -f "$COMPOSE_FILE" ]; then + log "ERROR: $COMPOSE_FILE not found" + exit 1 +fi if [ ! -f "$ENV_FILE" ]; then - log "ERROR: Environment file not found: $ENV_FILE" + log "ERROR: $ENV_FILE not found" exit 1 fi -# Pull latest images (optional, comment out for faster startup) -# log "Pulling latest images..." -# docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" pull - -# Start containers +# ─── Start containers ─── log "Starting Docker containers..." cd "$PROJECT_ROOT" -docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d +docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d --no-build 2>&1 | tee -a "$LOG_FILE" -# Wait for services to initialize +# ─── Wait and verify ─── log "Waiting 45s for services to initialize..." sleep 45 -# Create databases if they don't exist +RUNNING=$(docker ps -q | wc -l | tr -d ' ') +log "Containers running: $RUNNING" + +# ─── Create missing databases ─── log "Ensuring databases exist..." -docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U postgres -c "CREATE DATABASE manacore_auth;" 2>/dev/null || true -docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U postgres -c "CREATE DATABASE chat;" 2>/dev/null || true -docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U postgres -c "CREATE DATABASE todo;" 2>/dev/null || true -docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U postgres -c "CREATE DATABASE calendar;" 2>/dev/null || true -docker compose -f "$COMPOSE_FILE" exec -T postgres psql -U postgres -c "CREATE DATABASE clock;" 2>/dev/null || true +for db in mana_auth mana_credits chat todo calendar clock contacts storage; do + docker exec mana-infra-postgres psql -U postgres -c "CREATE DATABASE $db;" 2>/dev/null || true +done -# Run health checks -log "Running health checks..." -"$SCRIPT_DIR/health-check.sh" >> "$LOG_FILE" 2>&1 - -log "=== Startup Complete ===" +log "=== Startup Complete ($RUNNING containers running) ==="