From fbff0edf77aac736e47cd10d72137c6c670f0d08 Mon Sep 17 00:00:00 2001 From: Mikan <72257910+Mikan-DS@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:36:58 +0400 Subject: [PATCH] labels --- compose.yaml | 84 ++++++++++------------------- ssh/entrypoint.sh | 18 ++----- ssh/scripts/comfy.sh | 30 ++++------- ssh/scripts/common.sh | 31 ++++++++++- ssh/scripts/llama.sh | 7 ++- ssh/scripts/status.sh | 122 +++++++++++++----------------------------- 6 files changed, 112 insertions(+), 180 deletions(-) diff --git a/compose.yaml b/compose.yaml index 9960910..f1ee7ec 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,84 +1,72 @@ - name: ${TAILSCALE_HOSTNAME:-gpu-node} services: tailscale: image: tailscale/tailscale:latest - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-tailscale hostname: ${TAILSCALE_HOSTNAME:-gpu-node} - environment: TS_AUTHKEY: ${TS_AUTHKEY} TS_STATE_DIR: /var/lib/tailscale TS_SERVE_CONFIG: /config/serve.json - volumes: - ${HOST_PROJECT_PATH:-.}/tailscale/state:/var/lib/tailscale - ${HOST_PROJECT_PATH:-.}/tailscale/config:/config:ro - networks: - gpu-node - restart: always - + labels: + - "node.role=tailscale" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" ssh: build: context: ./ssh - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-ssh-main hostname: ssh-main - environment: HOST_PROJECT_PATH: ${HOST_PROJECT_PATH} SSH_ROLE: main SSH_PORT: "22" - + NODE_INSTANCE: ${TAILSCALE_HOSTNAME:-gpu-node} volumes: - /var/run/docker.sock:/var/run/docker.sock - ${HOST_PROJECT_PATH:-./}:/project - networks: - gpu-node - restart: always - + labels: + - "node.role=ssh-main" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" ssh-companion: profiles: - wake - build: context: ./ssh - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-ssh-companion hostname: ssh-companion - environment: HOST_PROJECT_PATH: ${HOST_PROJECT_PATH} SSH_ROLE: companion SSH_PORT: "2222" - + NODE_INSTANCE: ${TAILSCALE_HOSTNAME:-gpu-node} volumes: - /var/run/docker.sock:/var/run/docker.sock - ${HOST_PROJECT_PATH:-./}:/project - networks: - gpu-node - restart: always - + labels: + - "node.role=ssh-companion" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" webdav: profiles: - wake - image: rclone/rclone:latest - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-webdav hostname: webdav - command: - serve - webdav @@ -89,93 +77,75 @@ services: - ${WEBDAV_USER:-operator} - --pass - ${WEBDAV_PASS} - volumes: - ${HOST_PROJECT_PATH:-./}:/data - networks: - gpu-node - restart: always - + labels: + - "node.role=webdav" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" gpu-test: profiles: - test - build: context: ./gpu - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-gpu-test - gpus: all - networks: - gpu-node - restart: "no" - + labels: + - "node.role=gpu-test" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" model-worker: profiles: - wake - build: context: ./model-worker - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-model-worker - volumes: - ${HOST_PROJECT_PATH:-./}/gpu/comfy/models:/models - ${HOST_PROJECT_PATH:-./}/gpu/model-worker:/state - networks: - gpu-node - restart: always - + labels: + - "node.role=model-worker" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" llama: profiles: - llama - image: ghcr.io/ggml-org/llama.cpp:server-cuda - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-llama - gpus: all - volumes: - ${LLAMA_MODELS_HOST:-./gpu/llama_models}:/models - command: > --host 0.0.0.0 --port 8080 --model /models/${LLAMA_MODEL:-model.gguf} ${LLAMA_EXTRA_ARGS:-} - networks: - gpu-node - restart: "no" - + labels: + - "node.role=llama" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" comfyui: profiles: - comfy - image: mmartial/comfyui-nvidia-docker:latest - container_name: ${TAILSCALE_HOSTNAME:-gpu-node}-comfyui - init: true - gpus: all - volumes: - ${COMFY_RUN_HOST:-./comfyui/run}:/comfy/mnt - ${COMFY_BASE_HOST:-./gpu/comfy}:/basedir - environment: USE_UV: "true" WANTED_UID: "1000" @@ -183,13 +153,13 @@ services: BASE_DIRECTORY: "/basedir" SECURITY_LEVEL: "normal" COMFY_CMDLINE_EXTRA: ${COMFY_EXTRA_ARGS:-} - networks: - gpu-node - restart: "no" - + labels: + - "node.role=comfyui" + - "node.instance=${TAILSCALE_HOSTNAME:-gpu-node}" networks: gpu-node: - name: ${TAILSCALE_HOSTNAME:-gpu-node} + name: ${TAILSCALE_HOSTNAME:-gpu-node} \ No newline at end of file diff --git a/ssh/entrypoint.sh b/ssh/entrypoint.sh index 7af14b6..180b70a 100644 --- a/ssh/entrypoint.sh +++ b/ssh/entrypoint.sh @@ -2,8 +2,6 @@ set -eu - - # ------------------------------------------------------------ # Configuration # ------------------------------------------------------------ @@ -11,36 +9,30 @@ set -eu HOST_PROJECT_PATH="${HOST_PROJECT_PATH:-}" SSH_ROLE="${SSH_ROLE:-}" SSH_PORT="${SSH_PORT:-22}" - +NODE_INSTANCE="${NODE_INSTANCE:-gpu-node}" if [ -z "$HOST_PROJECT_PATH" ]; then echo "ERROR: HOST_PROJECT_PATH is not set." >&2 exit 1 fi - if [ -z "$SSH_ROLE" ]; then echo "ERROR: SSH_ROLE is not set." >&2 exit 1 fi - case "$SSH_ROLE" in - main) EXPECTED_PORT="22" ;; - companion) EXPECTED_PORT="2222" ;; - *) echo "ERROR: unknown SSH_ROLE: $SSH_ROLE" >&2 echo "Expected: main or companion" >&2 exit 1 ;; - esac # ------------------------------------------------------------ @@ -61,11 +53,11 @@ cat > /etc/profile.d/gpu-node.sh <&2 + return 1 + } + docker exec \ - gpu-node-model-worker \ + "$MW_CONTAINER" \ python3 \ /app/worker.py \ "$@" } - list_comfy_models() { model_worker_exec list "$@" } - queue_comfy_models() { model_worker_exec queue "$@" } - add_comfy_model() { if [ "$#" -ne 2 ]; then echo "Usage: gpuctl comfy models add " >&2 @@ -63,7 +65,6 @@ add_comfy_model() { model_worker_exec add "$1" "$2" } - remove_comfy_model() { if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then echo "Usage: gpuctl comfy models rm [type]" >&2 @@ -73,42 +74,32 @@ remove_comfy_model() { model_worker_exec rm "$@" } - comfy_models_command() { case "${1:-}" in - list) shift - if [ "$#" -gt 1 ]; then echo "Usage: gpuctl comfy models list [type]" >&2 return 1 fi - list_comfy_models "$@" ;; - queue) shift - if [ "$#" -ne 0 ]; then echo "Usage: gpuctl comfy models queue" >&2 return 1 fi - queue_comfy_models ;; - add) shift add_comfy_model "$@" ;; - rm) shift remove_comfy_model "$@" ;; - *) echo "Usage:" echo " gpuctl comfy models list [type]" @@ -120,15 +111,12 @@ comfy_models_command() { esac } - comfy_command() { case "${1:-}" in - models) shift comfy_models_command "$@" ;; - *) echo "Usage:" echo " gpuctl comfy models list [type]" @@ -138,4 +126,4 @@ comfy_command() { return 1 ;; esac -} \ No newline at end of file +} diff --git a/ssh/scripts/common.sh b/ssh/scripts/common.sh index f98aed2..8384bf3 100644 --- a/ssh/scripts/common.sh +++ b/ssh/scripts/common.sh @@ -17,9 +17,38 @@ require_ssh_role() { fi } +require_node_instance() { + if [ -z "${NODE_INSTANCE:-}" ]; then + echo "ERROR: NODE_INSTANCE is not set." >&2 + exit 1 + fi +} + require_project_dir() { if [ ! -d "$PROJECT_DIR" ]; then echo "ERROR: Project directory not found: $PROJECT_DIR" >&2 exit 1 fi -} \ No newline at end of file +} + + +get_service_container() { + ROLE="$1" + + case "$ROLE" in + ssh|ssh-main) ROLE="ssh-main" ;; + comfy) ROLE="comfyui" ;; + esac + + CONTAINER=$(docker ps -a \ + --filter "label=node.role=$ROLE" \ + --filter "label=node.instance=${NODE_INSTANCE}" \ + --format '{{.Names}}' \ + | head -n 1) + + if [ -z "$CONTAINER" ]; then + return 1 + fi + + echo "$CONTAINER" +} diff --git a/ssh/scripts/llama.sh b/ssh/scripts/llama.sh index 7dec2b8..27c4678 100644 --- a/ssh/scripts/llama.sh +++ b/ssh/scripts/llama.sh @@ -21,7 +21,6 @@ start_llama() { fi HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ - PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \ LLAMA_MODELS_HOST="$HOST_PROJECT_PATH/gpu/llama_models" \ LLAMA_MODEL="$MODEL" \ LLAMA_EXTRA_ARGS="$EXTRA_ARGS" \ @@ -35,11 +34,11 @@ start_llama() { echo docker ps \ - --filter "name=gpu-node-llama" \ + --filter "label=node.role=llama" \ + --filter "label=node.instance=${NODE_INSTANCE}" \ --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' } - llama_command() { case "${1:-}" in *) @@ -47,4 +46,4 @@ llama_command() { echo " gpuctl llama" ;; esac -} \ No newline at end of file +} diff --git a/ssh/scripts/status.sh b/ssh/scripts/status.sh index d8f9a61..d70a36a 100644 --- a/ssh/scripts/status.sh +++ b/ssh/scripts/status.sh @@ -1,21 +1,17 @@ - #!/bin/sh show_status() { docker ps \ - --filter "name=gpu-node-" \ + --filter "label=node.instance=${NODE_INSTANCE}" \ --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" @@ -30,11 +26,11 @@ update_ssh() { esac echo "Current SSH role: $SSH_ROLE" + echo "Node instance: $NODE_INSTANCE" echo "Host project: $HOST_PROJECT_PATH" echo "Updating: $TARGET_SSH_SERVICE + model-worker" HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ - PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \ docker compose \ -f "$COMPOSE_FILE" \ up -d \ @@ -44,37 +40,6 @@ update_ssh() { 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 @@ -84,7 +49,6 @@ stop_service() { docker stop "$CONTAINER" 2>/dev/null || true } - restart_service() { CONTAINER="$(get_service_container "$1")" || { echo "Unknown service: $1" >&2 @@ -94,7 +58,6 @@ restart_service() { docker restart "$CONTAINER" } - logs_service() { CONTAINER="$(get_service_container "$1")" || { echo "Unknown service: $1" >&2 @@ -104,78 +67,71 @@ logs_service() { docker logs -f "$CONTAINER" } - container_is_running() { - [ "$(docker inspect \ - --format '{{.State.Running}}' "$1" 2>/dev/null || true)" = "true" ] -} + TARGET="$1" + # Если передали роль — резолвим в имя контейнера + RESOLVED=$(get_service_container "$TARGET" 2>/dev/null) || true + if [ -n "$RESOLVED" ]; then + TARGET="$RESOLVED" + fi + + [ "$(docker inspect \ + --format '{{.State.Running}}' "$TARGET" 2>/dev/null || true)" = "true" ] +} sleep_mode() { cd "$PROJECT_DIR" require_host_project_path require_ssh_role + require_node_instance - 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" + MAIN_CONTAINER=$(get_service_container "ssh-main") || MAIN_CONTAINER="" + COMPANION_CONTAINER=$(get_service_container "ssh-companion") || COMPANION_CONTAINER="" + if [ -n "$MAIN_CONTAINER" ] && container_is_running "$MAIN_CONTAINER"; then + KEEP_SSH_ROLE="ssh-main" + REMOVE_SSH_ROLE="ssh-companion" + elif [ -n "$COMPANION_CONTAINER" ] && container_is_running "$COMPANION_CONTAINER"; then + KEEP_SSH_ROLE="ssh-companion" + REMOVE_SSH_ROLE="ssh-main" else - echo "No SSH container is running. Starting ssh-main..." + echo "No SSH container running for instance '$NODE_INSTANCE'. 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" + KEEP_SSH_ROLE="ssh-main" + REMOVE_SSH_ROLE="ssh-companion" fi - echo "Entering sleep mode." - echo "Keeping: tailscale + $KEEP_SSH" + echo "Entering sleep mode for instance: $NODE_INSTANCE" + echo "Keeping: tailscale + $KEEP_SSH_ROLE" - # 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 + # Удаляем опциональные контейнеры ТОЛЬКО своего инстанса + for ROLE in llama comfyui gpu-test model-worker webdav; do + CTR=$(get_service_container "$ROLE" 2>/dev/null) || true + [ -n "$CTR" ] && docker rm -f "$CTR" 2>/dev/null || true + done - # 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 + # Удаляем ненужный SSH-контейнер + REMOVE_CTR=$(get_service_container "$REMOVE_SSH_ROLE" 2>/dev/null) || true + [ -n "$REMOVE_CTR" ] && docker rm -f "$REMOVE_CTR" 2>/dev/null || true echo echo "Sleep mode ready." - - docker ps \ - --filter "name=gpu-node-" \ - --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' + show_status } - wake_mode() { cd "$PROJECT_DIR" require_host_project_path require_ssh_role + require_node_instance case "$SSH_ROLE" in main) @@ -190,14 +146,12 @@ wake_mode() { ;; esac - echo "Entering wake mode." + echo "Entering wake mode for instance: $NODE_INSTANCE" 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 \ @@ -211,4 +165,4 @@ wake_mode() { echo show_gpu -} \ No newline at end of file +}