#!/bin/bash
# Update script for MCP ComfyUI Flux - Optimized Production Setup
set -euo pipefail
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
PROJECT_NAME="${PROJECT_NAME:-mcp-comfyui-flux}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $*"
}
error() {
echo -e "${RED}[ERROR]${NC} $*" >&2
exit 1
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $*"
}
info() {
echo -e "${BLUE}[INFO]${NC} $*"
}
# Check if Docker Compose is available
if docker compose version >/dev/null 2>&1; then
COMPOSE=(docker compose)
elif command -v docker-compose >/dev/null 2>&1; then
COMPOSE=(docker-compose)
else
error "Neither 'docker compose' nor 'docker-compose' found"
fi
# Function to check if containers are running
containers_running() {
"${COMPOSE[@]}" -p "${PROJECT_NAME}" ps --quiet 2>/dev/null | grep -q .
}
# Function to confirm action
confirm() {
local prompt="${1:-Continue?}"
read -rp "$prompt (y/N): " ans
[[ "$ans" =~ ^[Yy]$ ]]
}
# Main update process
main() {
echo -e "${BLUE}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ MCP ComfyUI Flux - Update Script ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════╝${NC}"
echo
# Change to project root
cd "$PROJECT_ROOT"
# Check git status
log "Checking repository status..."
if [[ -n "$(git status --porcelain)" ]]; then
warn "You have uncommitted changes:"
git status --short
if ! confirm "Continue with update?"; then
info "Update cancelled"
exit 0
fi
fi
# Backup current state
log "Creating backup..."
if [[ -x "$SCRIPT_DIR/backup.sh" ]]; then
"$SCRIPT_DIR/backup.sh"
else
warn "Backup script not found, skipping backup"
if ! confirm "Continue without backup?"; then
exit 1
fi
fi
# Check if containers are running
if containers_running; then
log "Stopping running containers..."
"${COMPOSE[@]}" -p "${PROJECT_NAME}" down
fi
# Fetch latest changes
log "Fetching latest changes from repository..."
git fetch
# Check if there are updates
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse @{u})
if [[ "$LOCAL" = "$REMOTE" ]]; then
info "Already up to date"
else
log "Updates available, pulling changes..."
# Show what will be updated
info "Changes to be applied:"
git log --oneline HEAD..@{u}
if confirm "Apply these updates?"; then
git pull
log "Repository updated successfully"
else
info "Update cancelled"
exit 0
fi
fi
# Check for dependency updates
if [[ -f "$PROJECT_ROOT/requirements.txt" ]]; then
log "Checking Python dependencies..."
# Note: Just informational, actual update happens during rebuild
info "Requirements will be updated during container rebuild"
fi
# Rebuild containers with BuildKit optimizations
log "Rebuilding containers with optimizations..."
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
if [[ -x "$PROJECT_ROOT/build.sh" ]]; then
log "Using build.sh script..."
"$PROJECT_ROOT/build.sh" --start
else
log "Building with docker-compose..."
"${COMPOSE[@]}" -p "${PROJECT_NAME}" build
# Start containers
log "Starting updated containers..."
"${COMPOSE[@]}" -p "${PROJECT_NAME}" up -d
fi
# Wait for services to be healthy
log "Waiting for services to become healthy..."
local max_wait=60
local elapsed=0
while (( elapsed < max_wait )); do
if curl -fsS --max-time 5 "http://localhost:8188/system_stats" >/dev/null 2>&1; then
log "ComfyUI is ready!"
break
fi
printf '.'
sleep 2
elapsed=$((elapsed + 2))
done
echo
if (( elapsed >= max_wait )); then
warn "Services did not become healthy within ${max_wait}s"
info "Check logs with: ${COMPOSE[*]} -p ${PROJECT_NAME} logs"
fi
# Show status
echo
echo -e "${GREEN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Update Complete! 🎉 ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════╝${NC}"
echo
# Show current version
if command -v git >/dev/null 2>&1; then
CURRENT_COMMIT=$(git rev-parse --short HEAD)
CURRENT_BRANCH=$(git branch --show-current)
info "Current version: ${CURRENT_BRANCH}@${CURRENT_COMMIT}"
fi
# Show container status
echo
log "Container Status:"
"${COMPOSE[@]}" -p "${PROJECT_NAME}" ps
echo
info "Access Points:"
echo " • ComfyUI: http://localhost:8188"
echo " • Logs: ${COMPOSE[*]} -p ${PROJECT_NAME} logs -f"
# Check PyTorch version
if docker exec "${PROJECT_NAME}-comfyui-1" python3.11 -c "import torch; print(f'PyTorch {torch.__version__}')" 2>/dev/null; then
:
fi
}
# Handle script arguments
case "${1:-}" in
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Update MCP ComfyUI Flux to the latest version"
echo ""
echo "Options:"
echo " --no-backup Skip backup before update"
echo " --no-start Don't start containers after update"
echo " --help, -h Show this help message"
exit 0
;;
--no-backup)
SKIP_BACKUP=1
;;
--no-start)
NO_START=1
;;
esac
# Run main update process
main