diff --git a/ssh/Dockerfile b/ssh/Dockerfile index 9b7eddc..62614d1 100644 --- a/ssh/Dockerfile +++ b/ssh/Dockerfile @@ -36,10 +36,18 @@ RUN printf '%s\n' \ >> /etc/ssh/sshd_config COPY entrypoint.sh /entrypoint.sh -COPY gpuctl /usr/local/bin/gpuctl -RUN sed -i 's/\r$//' /entrypoint.sh /usr/local/bin/gpuctl \ - && chmod +x /entrypoint.sh /usr/local/bin/gpuctl +COPY gpuctl /usr/local/bin/gpuctl +COPY scripts/ /usr/local/lib/scripts/ + +RUN sed -i 's/\r$//' \ + /entrypoint.sh \ + /usr/local/bin/gpuctl \ + /usr/local/lib/scripts/*.sh \ + && chmod +x \ + /entrypoint.sh \ + /usr/local/bin/gpuctl \ + /usr/local/lib/scripts/*.sh EXPOSE 22 diff --git a/ssh/gpuctl b/ssh/gpuctl index 6c3e449..caa3378 100644 --- a/ssh/gpuctl +++ b/ssh/gpuctl @@ -2,323 +2,55 @@ set -eu +GPuctl_LIB="/usr/local/lib/scripts" -PROJECT_DIR="/project" -COMPOSE_FILE="$PROJECT_DIR/compose.yaml" +. "$GPuctl_LIB/common.sh" +. "$GPuctl_LIB/status.sh" +. "$GPuctl_LIB/gpu.sh" +. "$GPuctl_LIB/llama.sh" +. "$GPuctl_LIB/comfy.sh" -# ------------------------------------------------------------ -# Helpers -# ------------------------------------------------------------ +show_usage() { + cat <<'EOF' +Usage: -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 + gpuctl status + gpuctl gpu + gpuctl update + + gpuctl start llama [model.gguf] [extra args...] + gpuctl start comfy [extra args...] + + gpuctl comfy models list [type] + + gpuctl stop + gpuctl restart + gpuctl logs +EOF } -require_ssh_role() { - if [ -z "${SSH_ROLE:-}" ]; then - echo "ERROR: SSH_ROLE is not set." >&2 - exit 1 - fi -} - - -show_status() { - docker ps \ - --filter "name=gpu-node-" \ - --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' -} - - -# ------------------------------------------------------------ -# Start llama.cpp -# ------------------------------------------------------------ - -start_llama() { - MODEL="${1:-model.gguf}" - - shift || true - - cd "$PROJECT_DIR" - - require_host_project_path - - EXTRA_ARGS="$*" - - echo "Host project: $HOST_PROJECT_PATH" - echo "Starting llama..." - echo "Model: $MODEL" - echo "Models: $HOST_PROJECT_PATH/gpu/llama_models" - - if [ -n "$EXTRA_ARGS" ]; then - echo "Extra args: $EXTRA_ARGS" - 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" \ - docker compose \ - -f "$COMPOSE_FILE" \ - --profile llama \ - up -d \ - --force-recreate \ - llama - - echo - - docker ps \ - --filter "name=gpu-node-llama" \ - --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' -} - - -# ------------------------------------------------------------ -# Start ComfyUI -# ------------------------------------------------------------ - -start_comfy() { - cd "$PROJECT_DIR" - - require_host_project_path - - EXTRA_ARGS="$*" - - echo "Host project: $HOST_PROJECT_PATH" - echo "Starting ComfyUI..." - echo "Base directory: $HOST_PROJECT_PATH/gpu/comfy" - echo "Run directory: $HOST_PROJECT_PATH/comfyui/run" - - if [ -n "$EXTRA_ARGS" ]; then - echo "Extra args: $EXTRA_ARGS" - fi - - HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ - 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" \ - docker compose \ - -f "$COMPOSE_FILE" \ - --profile comfy \ - up -d \ - --force-recreate \ - comfyui - - echo - - docker ps \ - --filter "name=gpu-node-comfyui" \ - --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() { - SERVICE="$1" - - case "$SERVICE" in - - llama) - docker stop gpu-node-llama 2>/dev/null || true - ;; - - comfy) - docker stop gpu-node-comfyui 2>/dev/null || true - ;; - - tailscale) - docker stop gpu-node-tailscale 2>/dev/null || true - ;; - - ssh) - 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" - exit 1 - ;; - - esac -} - - -# ------------------------------------------------------------ -# Restart service -# ------------------------------------------------------------ - -restart_service() { - SERVICE="$1" - - case "$SERVICE" in - - llama) - docker restart gpu-node-llama - ;; - - comfy) - docker restart gpu-node-comfyui - ;; - - tailscale) - docker restart gpu-node-tailscale - ;; - - 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" - exit 1 - ;; - - esac -} - - -# ------------------------------------------------------------ -# Logs -# ------------------------------------------------------------ - -logs_service() { - SERVICE="$1" - - case "$SERVICE" in - - llama) - docker logs -f gpu-node-llama - ;; - - comfy) - docker logs -f gpu-node-comfyui - ;; - - tailscale) - docker logs -f gpu-node-tailscale - ;; - - 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" - exit 1 - ;; - - esac -} - - -# ------------------------------------------------------------ -# CLI -# ------------------------------------------------------------ - case "${1:-}" in status) show_status ;; + gpu) + show_gpu + ;; + + update) + if [ "$#" -ne 1 ]; then + echo "Usage: gpuctl update" >&2 + exit 1 + fi + + update_ssh + ;; start) - case "${2:-}" in llama) @@ -332,75 +64,51 @@ case "${1:-}" in ;; *) - echo "Usage:" - echo " gpuctl start llama [model.gguf] [extra args...]" - echo " gpuctl start comfy [extra args...]" + show_usage exit 1 ;; - esac ;; - - update) - - if [ -n "${2:-}" ]; then - echo "Usage:" - echo " gpuctl update" - exit 1 - fi - - update_ssh + comfy) + shift + comfy_command "$@" ;; + llama) + shift + llama_command "$@" + ;; stop) - - if [ -z "${2:-}" ]; then - echo "Usage:" - echo " gpuctl stop " + if [ "$#" -ne 2 ]; then + echo "Usage: gpuctl stop " >&2 exit 1 fi stop_service "$2" ;; - restart) - - if [ -z "${2:-}" ]; then - echo "Usage:" - echo " gpuctl restart " + if [ "$#" -ne 2 ]; then + echo "Usage: gpuctl restart " >&2 exit 1 fi restart_service "$2" ;; - logs) - - if [ -z "${2:-}" ]; then - echo "Usage:" - echo " gpuctl logs " + if [ "$#" -ne 2 ]; then + echo "Usage: gpuctl logs " >&2 exit 1 fi logs_service "$2" ;; - *) - - echo "Usage:" - echo - echo " gpuctl status" - echo " gpuctl update" - echo " gpuctl start llama [model.gguf] [extra args...]" - echo " gpuctl start comfy [extra args...]" - echo " gpuctl stop " - echo " gpuctl restart " - echo " gpuctl logs " + show_usage exit 1 ;; diff --git a/ssh/scripts/comfy.sh b/ssh/scripts/comfy.sh new file mode 100644 index 0000000..1484e30 --- /dev/null +++ b/ssh/scripts/comfy.sh @@ -0,0 +1,241 @@ +#!/bin/sh + +start_comfy() { + cd "$PROJECT_DIR" + + require_host_project_path + + EXTRA_ARGS="$*" + + echo "Host project: $HOST_PROJECT_PATH" + echo "Starting ComfyUI..." + echo "Base directory: $HOST_PROJECT_PATH/gpu/comfy" + echo "Run directory: $HOST_PROJECT_PATH/comfyui/run" + + if [ -n "$EXTRA_ARGS" ]; then + echo "Extra args: $EXTRA_ARGS" + fi + + HOST_PROJECT_PATH="$HOST_PROJECT_PATH" \ + 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" \ + docker compose \ + -f "$COMPOSE_FILE" \ + --profile comfy \ + up -d \ + --force-recreate \ + comfyui + + echo + + docker ps \ + --filter "name=gpu-node-comfyui" \ + --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' +} + + +list_comfy_models() { + MODELS_DIR="$PROJECT_DIR/gpu/comfy/models" + + if [ ! -d "$MODELS_DIR" ]; then + echo "ERROR: Models directory not found: $MODELS_DIR" >&2 + return 1 + fi + + python3 - "$MODELS_DIR" "${1:-}" <<'PY' +import os +import sys + +root = os.path.abspath(sys.argv[1]) +selected_type = sys.argv[2] if len(sys.argv) > 2 else "" + +IGNORED_NAMES = {".gitkeep"} +IGNORED_EXTENSIONS = {".txt"} + + +def is_model_file(name): + if name in IGNORED_NAMES: + return False + + return os.path.splitext(name)[1].lower() not in IGNORED_EXTENSIONS + + +def human_size(size): + units = ["B", "KB", "MB", "GB", "TB"] + value = float(size) + + for unit in units: + if value < 1024 or unit == units[-1]: + return f"{value:.2f} {unit}" + + value /= 1024 + + +def collect_files(directory): + result = [] + + for current, dirs, files in os.walk(directory): + dirs[:] = sorted( + name + for name in dirs + if not os.path.islink(os.path.join(current, name)) + ) + + for name in sorted(files): + path = os.path.join(current, name) + + if not is_model_file(name): + continue + + if os.path.islink(path): + continue + + try: + if os.path.isfile(path): + result.append( + (path, os.path.getsize(path)) + ) + except OSError as exc: + print( + f"WARNING: Cannot read {path}: {exc}", + file=sys.stderr, + ) + + return result + + +groups = [] + +for name in sorted(os.listdir(root)): + directory = os.path.join(root, name) + + if not os.path.isdir(directory): + continue + + if os.path.islink(directory): + continue + + files = collect_files(directory) + + if not files: + continue + + size = sum( + file_size + for _, file_size in files + ) + + groups.append( + (name, files, size) + ) + + +if selected_type: + groups = [ + group + for group in groups + if group[0] == selected_type + ] + + if not groups: + print( + f"ERROR: No models found for type: " + f"{selected_type}" + ) + + available = [] + + for name in sorted(os.listdir(root)): + path = os.path.join(root, name) + + if not os.path.isdir(path): + continue + + if os.path.islink(path): + continue + + if collect_files(path): + available.append(name) + + print( + "Available types: " + + (", ".join(available) or "(none)") + ) + + sys.exit(1) + + +total_size = 0 +total_files = 0 + +for name, files, size in groups: + print( + f"\n{name}/ — " + f"{len(files)} files — " + f"{human_size(size)}" + ) + + for path, file_size in files: + relative = os.path.relpath( + path, + root, + ) + + print( + f" {relative} — " + f"{human_size(file_size)}" + ) + + total_size += size + total_files += len(files) + + +print( + f"\nTotal: " + f"{human_size(total_size)} " + f"({total_files} files)" +) +PY +} + + +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 "${1:-}" + ;; + + *) + echo "Usage:" + echo " gpuctl comfy models list [type]" + return 1 + ;; + esac +} + + +comfy_command() { + case "${1:-}" in + + models) + shift + comfy_models_command "$@" + ;; + + *) + echo "Usage:" + echo " gpuctl comfy models list [type]" + return 1 + ;; + esac +} \ No newline at end of file diff --git a/ssh/scripts/common.sh b/ssh/scripts/common.sh new file mode 100644 index 0000000..f98aed2 --- /dev/null +++ b/ssh/scripts/common.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +PROJECT_DIR="${PROJECT_DIR:-/project}" +COMPOSE_FILE="$PROJECT_DIR/compose.yaml" + +require_host_project_path() { + if [ -z "${HOST_PROJECT_PATH:-}" ]; then + echo "ERROR: HOST_PROJECT_PATH is not set." >&2 + exit 1 + fi +} + +require_ssh_role() { + if [ -z "${SSH_ROLE:-}" ]; then + echo "ERROR: SSH_ROLE 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 diff --git a/ssh/scripts/gpu.sh b/ssh/scripts/gpu.sh new file mode 100644 index 0000000..f886daa --- /dev/null +++ b/ssh/scripts/gpu.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +show_gpu() { + cd "$PROJECT_DIR" + + echo "GPU status:" + echo + + docker compose \ + -f "$COMPOSE_FILE" \ + --profile test \ + run \ + --rm \ + gpu-test \ + nvidia-smi \ + --query-gpu=index,name,utilization.gpu,memory.used,memory.total,temperature.gpu,power.draw \ + --format=csv +} \ No newline at end of file diff --git a/ssh/scripts/llama.sh b/ssh/scripts/llama.sh new file mode 100644 index 0000000..7dec2b8 --- /dev/null +++ b/ssh/scripts/llama.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +start_llama() { + MODEL="${1:-model.gguf}" + + shift || true + + cd "$PROJECT_DIR" + + require_host_project_path + + EXTRA_ARGS="$*" + + echo "Host project: $HOST_PROJECT_PATH" + echo "Starting llama..." + echo "Model: $MODEL" + echo "Models: $HOST_PROJECT_PATH/gpu/llama_models" + + if [ -n "$EXTRA_ARGS" ]; then + echo "Extra args: $EXTRA_ARGS" + 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" \ + docker compose \ + -f "$COMPOSE_FILE" \ + --profile llama \ + up -d \ + --force-recreate \ + llama + + echo + + docker ps \ + --filter "name=gpu-node-llama" \ + --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' +} + + +llama_command() { + case "${1:-}" in + *) + echo "Usage:" + echo " gpuctl llama" + ;; + esac +} \ No newline at end of file diff --git a/ssh/scripts/status.sh b/ssh/scripts/status.sh new file mode 100644 index 0000000..63aa166 --- /dev/null +++ b/ssh/scripts/status.sh @@ -0,0 +1,102 @@ +#!/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_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" +} + + +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" + ;; + *) + 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" +} \ No newline at end of file