#!/bin/sh show_status() { docker ps \ --filter "name=gpu-node-" \ --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' } update_ssh() { cd "$PROJECT_DIR" require_host_project_path require_ssh_role SSH_KEYS_HOST="$HOST_PROJECT_PATH/ssh/authorized_keys" case "$SSH_ROLE" in main) TARGET_SSH_SERVICE="ssh-companion" ;; companion) TARGET_SSH_SERVICE="ssh" ;; *) echo "ERROR: unknown SSH role: $SSH_ROLE" >&2 exit 1 ;; esac echo "Current SSH role: $SSH_ROLE" echo "Host project: $HOST_PROJECT_PATH" echo "SSH keys: $SSH_KEYS_HOST" echo "Updating: $TARGET_SSH_SERVICE + model-worker" HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \ SSH_KEYS_HOST="$SSH_KEYS_HOST" \ docker compose \ -f "$COMPOSE_FILE" \ up -d \ --build \ --force-recreate \ "$TARGET_SSH_SERVICE" \ model-worker } get_service_container() { case "$1" in llama) echo "gpu-node-llama" ;; comfy) echo "gpu-node-comfyui" ;; tailscale) echo "gpu-node-tailscale" ;; ssh|ssh-main) echo "gpu-node-ssh-main" ;; ssh-companion) echo "gpu-node-ssh-companion" ;; webdav) echo "gpu-node-webdav" ;; model-worker) echo "gpu-node-model-worker" ;; *) return 1 ;; esac } stop_service() { CONTAINER="$(get_service_container "$1")" || { echo "Unknown service: $1" >&2 return 1 } docker stop "$CONTAINER" 2>/dev/null || true } restart_service() { CONTAINER="$(get_service_container "$1")" || { echo "Unknown service: $1" >&2 return 1 } docker restart "$CONTAINER" } logs_service() { CONTAINER="$(get_service_container "$1")" || { echo "Unknown service: $1" >&2 return 1 } docker logs -f "$CONTAINER" } container_is_running() { [ "$(docker inspect \ --format '{{.State.Running}}' "$1" 2>/dev/null || true)" = "true" ] } sleep_mode() { cd "$PROJECT_DIR" require_host_project_path require_ssh_role MAIN_CONTAINER="gpu-node-ssh-main" COMPANION_CONTAINER="gpu-node-ssh-companion" # Prefer ssh-main if it is running. if container_is_running "$MAIN_CONTAINER"; then KEEP_SSH="ssh" REMOVE_SSH="ssh-companion" elif container_is_running "$COMPANION_CONTAINER"; then KEEP_SSH="ssh-companion" REMOVE_SSH="ssh" else echo "No SSH container is running. Starting ssh-main..." HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \ SSH_KEYS_HOST="$HOST_PROJECT_PATH/ssh/authorized_keys" \ docker compose \ -f "$COMPOSE_FILE" \ up -d ssh KEEP_SSH="ssh" REMOVE_SSH="ssh-companion" fi echo "Entering sleep mode." echo "Keeping: tailscale + $KEEP_SSH" # Remove optional containers, including stopped ones. docker rm -f \ gpu-node-llama \ gpu-node-comfyui \ gpu-node-gpu-test \ gpu-node-model-worker \ gpu-node-webdav \ 2>/dev/null || true # Remove the other SSH container. if [ "$REMOVE_SSH" = "ssh" ]; then docker rm -f "$MAIN_CONTAINER" 2>/dev/null || true else docker rm -f "$COMPANION_CONTAINER" 2>/dev/null || true fi echo echo "Sleep mode ready." docker ps \ --filter "name=gpu-node-" \ --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' } wake_mode() { cd "$PROJECT_DIR" require_host_project_path require_ssh_role case "$SSH_ROLE" in main) TARGET_SSH_SERVICE="ssh-companion" ;; companion) TARGET_SSH_SERVICE="ssh" ;; *) echo "ERROR: unknown SSH role: $SSH_ROLE" >&2 return 1 ;; esac echo "Entering wake mode." echo "Current SSH role: $SSH_ROLE" echo "Host project: $HOST_PROJECT_PATH" echo "Starting: $TARGET_SSH_SERVICE + model-worker + webdav" HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \ SSH_KEYS_HOST="$HOST_PROJECT_PATH/ssh/authorized_keys" \ docker compose \ -f "$COMPOSE_FILE" \ up -d \ "$TARGET_SSH_SERVICE" \ model-worker \ webdav echo echo "Containers:" show_status echo show_gpu }