This commit is contained in:
Mikan
2026-10-08 23:40:36 +04:00
parent b5060ddb8b
commit 4c5d35ebd4
4 changed files with 290 additions and 39 deletions

View File

@@ -1,3 +1,6 @@
TAILSCALE_HOSTNAME=gpu-node TAILSCALE_HOSTNAME=gpu-node
TS_AUTHKEY= TS_AUTHKEY=
LLAMA_MODELS_PATH=./gpu/llama_models LLAMA_MODELS_PATH=./gpu/llama_models
WEBDAV_USER=operator
WEBDAV_PASS=
HOST_PROJECT_PATH=

View File

@@ -3,8 +3,8 @@ name: gpu-node
services: services:
tailscale: tailscale:
image: tailscale/tailscale:latest image: tailscale/tailscale:latest
container_name: gpu-node-tailscale
container_name: gpu-node-tailscale
hostname: ${TAILSCALE_HOSTNAME:-gpu-node} hostname: ${TAILSCALE_HOSTNAME:-gpu-node}
environment: environment:
@@ -21,37 +21,46 @@ services:
restart: always restart: always
ssh: ssh:
build: build:
context: ./ssh context: ./ssh
container_name: gpu-node-ssh container_name: gpu-node-ssh-main
hostname: ssh-operator hostname: ssh-main
environment:
HOST_PROJECT_PATH: ${HOST_PROJECT_PATH}
SSH_ROLE: main
SSH_PORT: "22"
volumes: volumes:
- ${SSH_KEYS_HOST:-./ssh/authorized_keys}:/mounted-authorized-keys:ro - ${SSH_KEYS_HOST:-./ssh/authorized_keys}:/mounted-authorized-keys:ro
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
- ./:/project - ${HOST_PROJECT_PATH:-./}:/project
networks: networks:
- gpu-node - gpu-node
restart: always restart: always
ssh-companion: ssh-companion:
build: build:
context: ./ssh context: ./ssh
container_name: gpu-node-ssh-companion container_name: gpu-node-ssh-companion
hostname: ssh-operator-companion hostname: ssh-companion
environment: environment:
HOST_PROJECT_PATH: ${HOST_PROJECT_PATH}
SSH_ROLE: companion
SSH_PORT: "2222" SSH_PORT: "2222"
volumes: volumes:
- ${SSH_KEYS_HOST:-./ssh/authorized_keys}:/mounted-authorized-keys:ro - ${SSH_KEYS_HOST:-./ssh/authorized_keys}:/mounted-authorized-keys:ro
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
- ./:/project - ${HOST_PROJECT_PATH:-./}:/project
networks: networks:
- gpu-node - gpu-node
@@ -77,7 +86,7 @@ services:
- ${WEBDAV_PASS} - ${WEBDAV_PASS}
volumes: volumes:
- ${HOST_PROJECT_DIR:-./}:/data - ${HOST_PROJECT_PATH:-./}:/data
networks: networks:
- gpu-node - gpu-node
@@ -101,6 +110,7 @@ services:
restart: "no" restart: "no"
llama: llama:
profiles: profiles:
- llama - llama
@@ -125,6 +135,7 @@ services:
restart: "no" restart: "no"
comfyui: comfyui:
profiles: profiles:
- comfy - comfy
@@ -138,8 +149,8 @@ services:
gpus: all gpus: all
volumes: volumes:
- ./comfyui/run:/comfy/mnt - ${COMFY_RUN_HOST:-./comfyui/run}:/comfy/mnt
- ./gpu/comfy:/basedir - ${COMFY_BASE_HOST:-./gpu/comfy}:/basedir
environment: environment:
USE_UV: "true" USE_UV: "true"
@@ -154,6 +165,7 @@ services:
restart: "no" restart: "no"
networks: networks:
gpu-node: gpu-node:
name: gpu-node name: gpu-node

View File

@@ -1,5 +1,11 @@
#!/bin/sh #!/bin/sh
set -e
set -eu
# ------------------------------------------------------------
# SSH authorized keys
# ------------------------------------------------------------
cp /mounted-authorized-keys /home/operator/.ssh/authorized_keys cp /mounted-authorized-keys /home/operator/.ssh/authorized_keys
@@ -7,6 +13,76 @@ chown -R operator:operator /home/operator/.ssh
chmod 700 /home/operator/.ssh chmod 700 /home/operator/.ssh
chmod 600 /home/operator/.ssh/authorized_keys chmod 600 /home/operator/.ssh/authorized_keys
# ------------------------------------------------------------
# Configuration
# ------------------------------------------------------------
HOST_PROJECT_PATH="${HOST_PROJECT_PATH:-}"
SSH_ROLE="${SSH_ROLE:-}"
SSH_PORT="${SSH_PORT:-22}" SSH_PORT="${SSH_PORT:-22}"
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
# ------------------------------------------------------------
# Interactive shell environment
# ------------------------------------------------------------
cat > /etc/profile.d/gpu-node.sh <<EOF
export HOST_PROJECT_PATH='$HOST_PROJECT_PATH'
export SSH_ROLE='$SSH_ROLE'
export SSH_PORT='$SSH_PORT'
EOF
chmod 644 /etc/profile.d/gpu-node.sh
# ------------------------------------------------------------
# Export for processes started by sshd
# ------------------------------------------------------------
export HOST_PROJECT_PATH
export SSH_ROLE
export SSH_PORT
echo "SSH role: $SSH_ROLE"
echo "Host project path: $HOST_PROJECT_PATH"
echo "SSH port: $SSH_PORT"
# ------------------------------------------------------------
# Start SSH
# ------------------------------------------------------------
exec /usr/sbin/sshd -D -e -p "$SSH_PORT" exec /usr/sbin/sshd -D -e -p "$SSH_PORT"

View File

@@ -2,12 +2,29 @@
set -eu set -eu
PROJECT_DIR="/project" PROJECT_DIR="/project"
COMPOSE_FILE="$PROJECT_DIR/compose.yaml"
get_host_project_dir() { # ------------------------------------------------------------
docker inspect gpu-node-ssh \ # Helpers
--format '{{range .Mounts}}{{if eq .Destination "/project"}}{{.Source}}{{end}}{{end}}' # ------------------------------------------------------------
require_host_project_path() {
if [ -z "${HOST_PROJECT_PATH:-}" ]; then
echo "ERROR: HOST_PROJECT_PATH is not set." >&2
echo "The SSH container environment should initialize it." >&2
exit 1
fi
}
require_ssh_role() {
if [ -z "${SSH_ROLE:-}" ]; then
echo "ERROR: SSH_ROLE is not set." >&2
exit 1
fi
} }
@@ -18,84 +35,148 @@ show_status() {
} }
# ------------------------------------------------------------
# Start llama.cpp
# ------------------------------------------------------------
start_llama() { start_llama() {
MODEL="${1:-model.gguf}" MODEL="${1:-model.gguf}"
shift || true shift || true
cd "$PROJECT_DIR" cd "$PROJECT_DIR"
HOST_PROJECT_DIR="$(get_host_project_dir)" require_host_project_path
if [ -z "$HOST_PROJECT_DIR" ]; then
echo "ERROR: cannot determine host project directory"
exit 1
fi
EXTRA_ARGS="$*" EXTRA_ARGS="$*"
echo "Host project: $HOST_PROJECT_PATH"
echo "Starting llama..." echo "Starting llama..."
echo "Model: $MODEL" echo "Model: $MODEL"
echo "Models: $HOST_PROJECT_DIR/gpu/llama_models" echo "Models: $HOST_PROJECT_PATH/gpu/llama_models"
if [ -n "$EXTRA_ARGS" ]; then if [ -n "$EXTRA_ARGS" ]; then
echo "Extra args: $EXTRA_ARGS" echo "Extra args: $EXTRA_ARGS"
fi fi
LLAMA_MODELS_HOST="$HOST_PROJECT_DIR/gpu/llama_models" \ 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_MODEL="$MODEL" \
LLAMA_EXTRA_ARGS="$EXTRA_ARGS" \ LLAMA_EXTRA_ARGS="$EXTRA_ARGS" \
docker compose \ docker compose \
-f "$COMPOSE_FILE" \
--profile llama \ --profile llama \
up -d \ up -d \
--force-recreate \ --force-recreate \
llama llama
echo echo
docker ps \ docker ps \
--filter "name=gpu-node-llama" \ --filter "name=gpu-node-llama" \
--format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
} }
# ------------------------------------------------------------
# Start ComfyUI
# ------------------------------------------------------------
start_comfy() { start_comfy() {
cd "$PROJECT_DIR" cd "$PROJECT_DIR"
HOST_PROJECT_DIR="$(get_host_project_dir)" require_host_project_path
if [ -z "$HOST_PROJECT_DIR" ]; then
echo "ERROR: cannot determine host project directory"
exit 1
fi
EXTRA_ARGS="$*" EXTRA_ARGS="$*"
echo "Host project: $HOST_PROJECT_PATH"
echo "Starting ComfyUI..." echo "Starting ComfyUI..."
echo "Directory: $HOST_PROJECT_DIR/gpu/comfy" echo "Base directory: $HOST_PROJECT_PATH/gpu/comfy"
echo "Run directory: $HOST_PROJECT_PATH/comfyui/run"
if [ -n "$EXTRA_ARGS" ]; then if [ -n "$EXTRA_ARGS" ]; then
echo "Extra args: $EXTRA_ARGS" echo "Extra args: $EXTRA_ARGS"
fi fi
COMFY_BASE_HOST="$HOST_PROJECT_DIR/gpu/comfy" \ HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \
COMFY_RUN_HOST="$HOST_PROJECT_DIR/gpu/comfy/run" \ PROJECT_HOST_DIR="$HOST_PROJECT_PATH" \
COMFY_BASE_HOST="$HOST_PROJECT_PATH/gpu/comfy" \
COMFY_RUN_HOST="$HOST_PROJECT_PATH/comfyui/run" \
COMFY_EXTRA_ARGS="$EXTRA_ARGS" \ COMFY_EXTRA_ARGS="$EXTRA_ARGS" \
docker compose \ docker compose \
-f "$COMPOSE_FILE" \
--profile comfy \ --profile comfy \
up -d \ up -d \
--force-recreate \ --force-recreate \
comfyui comfyui
echo echo
docker ps \ docker ps \
--filter "name=gpu-node-comfyui" \ --filter "name=gpu-node-comfyui" \
--format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
} }
# ------------------------------------------------------------
# Update SSH container
# ------------------------------------------------------------
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_SERVICE="ssh-companion"
;;
companion)
TARGET_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_SERVICE"
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_SERVICE"
}
# ------------------------------------------------------------
# Stop service
# ------------------------------------------------------------
stop_service() { stop_service() {
SERVICE="$1" SERVICE="$1"
case "$SERVICE" in case "$SERVICE" in
llama) llama)
docker stop gpu-node-llama 2>/dev/null || true docker stop gpu-node-llama 2>/dev/null || true
;; ;;
@@ -109,21 +190,39 @@ stop_service() {
;; ;;
ssh) ssh)
docker stop gpu-node-ssh 2>/dev/null || true docker stop gpu-node-ssh-main 2>/dev/null || true
;;
ssh-main)
docker stop gpu-node-ssh-main 2>/dev/null || true
;;
ssh-companion)
docker stop gpu-node-ssh-companion 2>/dev/null || true
;;
webdav)
docker stop gpu-node-webdav 2>/dev/null || true
;; ;;
*) *)
echo "Unknown service: $SERVICE" echo "Unknown service: $SERVICE"
exit 1 exit 1
;; ;;
esac esac
} }
# ------------------------------------------------------------
# Restart service
# ------------------------------------------------------------
restart_service() { restart_service() {
SERVICE="$1" SERVICE="$1"
case "$SERVICE" in case "$SERVICE" in
llama) llama)
docker restart gpu-node-llama docker restart gpu-node-llama
;; ;;
@@ -137,21 +236,39 @@ restart_service() {
;; ;;
ssh) ssh)
docker restart gpu-node-ssh docker restart gpu-node-ssh-main
;;
ssh-main)
docker restart gpu-node-ssh-main
;;
ssh-companion)
docker restart gpu-node-ssh-companion
;;
webdav)
docker restart gpu-node-webdav
;; ;;
*) *)
echo "Unknown service: $SERVICE" echo "Unknown service: $SERVICE"
exit 1 exit 1
;; ;;
esac esac
} }
# ------------------------------------------------------------
# Logs
# ------------------------------------------------------------
logs_service() { logs_service() {
SERVICE="$1" SERVICE="$1"
case "$SERVICE" in case "$SERVICE" in
llama) llama)
docker logs -f gpu-node-llama docker logs -f gpu-node-llama
;; ;;
@@ -165,24 +282,43 @@ logs_service() {
;; ;;
ssh) ssh)
docker logs -f gpu-node-ssh docker logs -f gpu-node-ssh-main
;;
ssh-main)
docker logs -f gpu-node-ssh-main
;;
ssh-companion)
docker logs -f gpu-node-ssh-companion
;;
webdav)
docker logs -f gpu-node-webdav
;; ;;
*) *)
echo "Unknown service: $SERVICE" echo "Unknown service: $SERVICE"
exit 1 exit 1
;; ;;
esac esac
} }
# ------------------------------------------------------------
# CLI
# ------------------------------------------------------------
case "${1:-}" in case "${1:-}" in
status) status)
show_status show_status
;; ;;
start) start)
case "${2:-}" in case "${2:-}" in
llama) llama)
@@ -201,41 +337,65 @@ case "${1:-}" in
echo " gpuctl start comfy [extra args...]" echo " gpuctl start comfy [extra args...]"
exit 1 exit 1
;; ;;
esac esac
;; ;;
update)
if [ -n "${2:-}" ]; then
echo "Usage:"
echo " gpuctl update"
exit 1
fi
update_ssh
;;
stop) stop)
if [ -z "${2:-}" ]; then if [ -z "${2:-}" ]; then
echo "Usage: gpuctl stop <service>" echo "Usage:"
echo " gpuctl stop <service>"
exit 1 exit 1
fi fi
stop_service "$2" stop_service "$2"
;; ;;
restart) restart)
if [ -z "${2:-}" ]; then if [ -z "${2:-}" ]; then
echo "Usage: gpuctl restart <service>" echo "Usage:"
echo " gpuctl restart <service>"
exit 1 exit 1
fi fi
restart_service "$2" restart_service "$2"
;; ;;
logs) logs)
if [ -z "${2:-}" ]; then if [ -z "${2:-}" ]; then
echo "Usage: gpuctl logs <service>" echo "Usage:"
echo " gpuctl logs <service>"
exit 1 exit 1
fi fi
logs_service "$2" logs_service "$2"
;; ;;
*) *)
echo "GPU node controller"
echo
echo "Usage:" echo "Usage:"
echo
echo " gpuctl status" echo " gpuctl status"
echo " gpuctl update"
echo " gpuctl start llama [model.gguf] [extra args...]" echo " gpuctl start llama [model.gguf] [extra args...]"
echo " gpuctl start comfy [extra args...]" echo " gpuctl start comfy [extra args...]"
echo " gpuctl stop <service>" echo " gpuctl stop <service>"