diff --git a/compose.yaml b/compose.yaml index ec237fd..b23e18d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,3 +1,4 @@ + name: gpu-node services: @@ -46,6 +47,9 @@ services: ssh-companion: + profiles: + - wake + build: context: ./ssh @@ -69,6 +73,9 @@ services: webdav: + profiles: + - wake + image: rclone/rclone:latest container_name: gpu-node-webdav @@ -112,6 +119,9 @@ services: model-worker: + profiles: + - wake + build: context: ./model-worker @@ -126,6 +136,7 @@ services: restart: always + llama: profiles: - llama diff --git a/ssh/gpuctl b/ssh/gpuctl index f9b2afd..63842a8 100644 --- a/ssh/gpuctl +++ b/ssh/gpuctl @@ -1,3 +1,4 @@ + #!/bin/sh set -eu @@ -18,6 +19,8 @@ Usage: gpuctl status gpuctl gpu gpuctl update + gpuctl sleep + gpuctl wake gpuctl start llama [model.gguf] [extra args...] gpuctl start comfy [extra args...] @@ -53,6 +56,24 @@ case "${1:-}" in update_ssh ;; + sleep) + if [ "$#" -ne 1 ]; then + echo "Usage: gpuctl sleep" >&2 + exit 1 + fi + + sleep_mode + ;; + + wake) + if [ "$#" -ne 1 ]; then + echo "Usage: gpuctl wake" >&2 + exit 1 + fi + + wake_mode + ;; + start) case "${2:-}" in diff --git a/ssh/scripts/status.sh b/ssh/scripts/status.sh index 76c0802..d51e39a 100644 --- a/ssh/scripts/status.sh +++ b/ssh/scripts/status.sh @@ -1,3 +1,4 @@ + #!/bin/sh show_status() { @@ -103,4 +104,113 @@ logs_service() { } 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 } \ No newline at end of file