Spaces:
Sleeping
Sleeping
feat: Ubuntu Server with ttyd web terminal + SSH + Claude Code + persistence stress test
Browse files- Dockerfile: switch from Desktop (XFCE/noVNC) to Server (ttyd web terminal)
- Install Node.js 20, Claude Code, openssh, ttyd, tmux, htop, vim
- ubuntu-server/start-server.sh: ttyd on 7860, sshd on 2222, persistent /data/home
- monitor_and_test.py: add persistence stress test (concurrent file I/O, checksums,
large file writes, read-write under load)
- test_local.sh: updated for server (ttyd instead of noVNC)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Dockerfile +27 -29
- scripts/monitor_and_test.py +111 -2
- scripts/test_local.sh +27 -27
- ubuntu-server/start-server.sh +77 -0
Dockerfile
CHANGED
|
@@ -1,57 +1,55 @@
|
|
| 1 |
-
# Ubuntu 24.04
|
| 2 |
FROM ubuntu:24.04
|
| 3 |
|
| 4 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 5 |
|
| 6 |
-
# System + Python (for sync)
|
| 7 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
-
ca-certificates curl python3 python3-pip python3-venv \
|
| 9 |
&& pip3 install --no-cache-dir --break-system-packages huggingface_hub \
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
-
#
|
| 13 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 14 |
-
xvfb \
|
| 15 |
-
xfce4 xfce4-goodies \
|
| 16 |
-
dbus-x11 \
|
| 17 |
-
x11vnc \
|
| 18 |
-
firefox \
|
| 19 |
-
procps \
|
| 20 |
openssh-server openssh-client \
|
|
|
|
|
|
|
|
|
|
| 21 |
&& rm -rf /var/lib/apt/lists/*
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
RUN
|
| 25 |
-
&&
|
| 26 |
-
&&
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# HF Spaces run as user 1000; UID 1000 may already exist in base
|
| 30 |
-
RUN (useradd -m -u 1000 user 2>/dev/null) || \
|
| 31 |
-
(EXISTING=$
|
| 32 |
-
usermod -l user $
|
| 33 |
mkdir -p /home/user && chown 1000:1000 /home/user)
|
| 34 |
ENV HOME=/home/user
|
| 35 |
RUN mkdir -p /data && chown 1000:1000 /data
|
| 36 |
|
| 37 |
-
# Pre-generate SSH host key so sshd can start
|
| 38 |
RUN mkdir -p /home/user/.ssh && \
|
| 39 |
ssh-keygen -t ed25519 -f /home/user/.ssh/ssh_host_ed25519_key -N "" -C "" && \
|
| 40 |
chown -R 1000:1000 /home/user/.ssh
|
| 41 |
|
| 42 |
-
# HuggingRun scripts
|
| 43 |
COPY scripts /scripts
|
| 44 |
-
COPY ubuntu-
|
| 45 |
-
RUN chmod +x /scripts/entrypoint.sh /opt/start-
|
| 46 |
|
| 47 |
ENV PERSIST_PATH=/data
|
| 48 |
-
|
| 49 |
-
ENV
|
| 50 |
-
ENV
|
| 51 |
-
ENV
|
| 52 |
-
ENV VNC_PORT=5901
|
| 53 |
-
ENV NOVNC_PORT=7860
|
| 54 |
|
| 55 |
USER user
|
| 56 |
-
EXPOSE 7860
|
| 57 |
ENTRYPOINT ["/scripts/entrypoint.sh"]
|
|
|
|
| 1 |
+
# Ubuntu 24.04 Server on HuggingRun β ttyd web terminal on 7860, SSH on 2222
|
| 2 |
FROM ubuntu:24.04
|
| 3 |
|
| 4 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 5 |
|
| 6 |
+
# System + Python (for HuggingRun persistence sync)
|
| 7 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
+
ca-certificates curl wget python3 python3-pip python3-venv git \
|
| 9 |
&& pip3 install --no-cache-dir --break-system-packages huggingface_hub \
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
+
# Server essentials + SSH + ttyd web terminal
|
| 13 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
openssh-server openssh-client \
|
| 15 |
+
procps htop vim nano less tmux \
|
| 16 |
+
build-essential \
|
| 17 |
+
ttyd \
|
| 18 |
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
|
| 20 |
+
# Node.js 20 LTS (for Claude Code)
|
| 21 |
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
| 22 |
+
&& apt-get install -y nodejs \
|
| 23 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 24 |
+
|
| 25 |
+
# Claude Code (native binary installer)
|
| 26 |
+
RUN curl -fsSL https://claude.ai/install.sh | bash \
|
| 27 |
+
|| npm install -g @anthropic-ai/claude-code
|
| 28 |
|
| 29 |
# HF Spaces run as user 1000; UID 1000 may already exist in base
|
| 30 |
+
RUN (useradd -m -u 1000 -s /bin/bash user 2>/dev/null) || \
|
| 31 |
+
(EXISTING=$(getent passwd 1000 | cut -d: -f1); \
|
| 32 |
+
usermod -l user -s /bin/bash $EXISTING; usermod -d /home/user user; \
|
| 33 |
mkdir -p /home/user && chown 1000:1000 /home/user)
|
| 34 |
ENV HOME=/home/user
|
| 35 |
RUN mkdir -p /data && chown 1000:1000 /data
|
| 36 |
|
| 37 |
+
# Pre-generate SSH host key so sshd can start as non-root user
|
| 38 |
RUN mkdir -p /home/user/.ssh && \
|
| 39 |
ssh-keygen -t ed25519 -f /home/user/.ssh/ssh_host_ed25519_key -N "" -C "" && \
|
| 40 |
chown -R 1000:1000 /home/user/.ssh
|
| 41 |
|
| 42 |
+
# HuggingRun scripts
|
| 43 |
COPY scripts /scripts
|
| 44 |
+
COPY ubuntu-server/start-server.sh /opt/start-server.sh
|
| 45 |
+
RUN chmod +x /scripts/entrypoint.sh /opt/start-server.sh
|
| 46 |
|
| 47 |
ENV PERSIST_PATH=/data
|
| 48 |
+
ENV RUN_CMD="stdbuf -oL -eL /opt/start-server.sh"
|
| 49 |
+
ENV SSH_PORT=2222
|
| 50 |
+
ENV SSH_LISTEN=0.0.0.0
|
| 51 |
+
ENV TTYD_PORT=7860
|
|
|
|
|
|
|
| 52 |
|
| 53 |
USER user
|
| 54 |
+
EXPOSE 7860 2222
|
| 55 |
ENTRYPOINT ["/scripts/entrypoint.sh"]
|
scripts/monitor_and_test.py
CHANGED
|
@@ -220,8 +220,8 @@ def test_ssh_command(host, port, user, identity_file=None):
|
|
| 220 |
checks = [
|
| 221 |
("whoami", lambda out: user in out),
|
| 222 |
("uname -s", lambda out: "Linux" in out),
|
| 223 |
-
("
|
| 224 |
-
("pgrep -a
|
| 225 |
]
|
| 226 |
all_ok = True
|
| 227 |
for cmd, validate in checks:
|
|
@@ -270,6 +270,107 @@ def test_ssh_bruteforce(host, port, user, rounds=3, ramp_up=None, identity_file=
|
|
| 270 |
return all_ok
|
| 271 |
|
| 272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
def _curl_logs_url(space_id: str, log_type: str) -> str:
|
| 274 |
"""Build the logs API URL (same as user's curl command)."""
|
| 275 |
return f"https://huggingface.co/api/spaces/{space_id}/logs/{log_type}"
|
|
@@ -496,6 +597,14 @@ def main():
|
|
| 496 |
identity_file=args.ssh_key):
|
| 497 |
print("[ssh-test] BRUTEFORCE FAILED")
|
| 498 |
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
print("=" * 60)
|
| 500 |
print("[ssh-test] ALL SSH TESTS PASSED")
|
| 501 |
return
|
|
|
|
| 220 |
checks = [
|
| 221 |
("whoami", lambda out: user in out),
|
| 222 |
("uname -s", lambda out: "Linux" in out),
|
| 223 |
+
("which claude || echo no-claude", lambda out: "claude" in out.lower()),
|
| 224 |
+
("pgrep -a ttyd || pgrep -a sshd", lambda out: len(out.strip()) > 0),
|
| 225 |
]
|
| 226 |
all_ok = True
|
| 227 |
for cmd, validate in checks:
|
|
|
|
| 270 |
return all_ok
|
| 271 |
|
| 272 |
|
| 273 |
+
def test_ssh_persistence_stress(host, port, user, persist_path="/data",
|
| 274 |
+
n_files=100, concurrency=10, identity_file=None):
|
| 275 |
+
"""Persistence stress test: write many files via SSH, verify they exist, check integrity.
|
| 276 |
+
|
| 277 |
+
Tests the operating system's persistent storage under load:
|
| 278 |
+
1. Write n_files with known content (concurrent)
|
| 279 |
+
2. Verify all files exist and content matches
|
| 280 |
+
3. Write large files to test storage capacity
|
| 281 |
+
4. Verify checksums
|
| 282 |
+
"""
|
| 283 |
+
import concurrent.futures
|
| 284 |
+
import hashlib
|
| 285 |
+
|
| 286 |
+
test_dir = f"{persist_path}/stress-test-{int(time.time())}"
|
| 287 |
+
print(f"[persist-stress] Creating {n_files} files in {test_dir} ...")
|
| 288 |
+
|
| 289 |
+
# Phase 1: Create test directory
|
| 290 |
+
rc, _, err = _ssh_cmd(host, port, user, f"mkdir -p {test_dir}", identity_file=identity_file)
|
| 291 |
+
if rc != 0:
|
| 292 |
+
print(f"[persist-stress] FAIL: cannot mkdir {test_dir}: {err}")
|
| 293 |
+
return False
|
| 294 |
+
|
| 295 |
+
# Phase 2: Write files concurrently
|
| 296 |
+
def write_file(i):
|
| 297 |
+
content = f"persistence-test-file-{i}-{time.time()}"
|
| 298 |
+
cmd = f"echo '{content}' > {test_dir}/file_{i:04d}.txt"
|
| 299 |
+
rc, _, _ = _ssh_cmd(host, port, user, cmd, timeout=20, identity_file=identity_file)
|
| 300 |
+
return rc == 0, content
|
| 301 |
+
|
| 302 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as ex:
|
| 303 |
+
results = list(ex.map(write_file, range(n_files)))
|
| 304 |
+
written = sum(1 for ok, _ in results if ok)
|
| 305 |
+
print(f"[persist-stress] Written: {written}/{n_files} files")
|
| 306 |
+
if written < n_files:
|
| 307 |
+
print(f"[persist-stress] FAIL: only {written}/{n_files} files written")
|
| 308 |
+
return False
|
| 309 |
+
|
| 310 |
+
# Phase 3: Verify all files exist
|
| 311 |
+
rc, out, _ = _ssh_cmd(host, port, user, f"ls {test_dir}/ | wc -l",
|
| 312 |
+
timeout=30, identity_file=identity_file)
|
| 313 |
+
count = int(out.strip()) if rc == 0 and out.strip().isdigit() else 0
|
| 314 |
+
print(f"[persist-stress] Verified: {count} files exist on disk")
|
| 315 |
+
if count < n_files:
|
| 316 |
+
print(f"[persist-stress] FAIL: expected {n_files}, found {count}")
|
| 317 |
+
return False
|
| 318 |
+
|
| 319 |
+
# Phase 4: Write a large file (1MB) to test storage
|
| 320 |
+
rc, _, err = _ssh_cmd(host, port, user,
|
| 321 |
+
f"dd if=/dev/urandom of={test_dir}/large_1mb.bin bs=1024 count=1024 2>/dev/null && "
|
| 322 |
+
f"ls -la {test_dir}/large_1mb.bin",
|
| 323 |
+
timeout=30, identity_file=identity_file)
|
| 324 |
+
if rc != 0:
|
| 325 |
+
print(f"[persist-stress] FAIL: cannot write large file: {err}")
|
| 326 |
+
return False
|
| 327 |
+
print(f"[persist-stress] Large file (1MB) written OK")
|
| 328 |
+
|
| 329 |
+
# Phase 5: Compute and verify checksum
|
| 330 |
+
rc, out, _ = _ssh_cmd(host, port, user,
|
| 331 |
+
f"sha256sum {test_dir}/large_1mb.bin",
|
| 332 |
+
timeout=30, identity_file=identity_file)
|
| 333 |
+
if rc != 0 or not out.strip():
|
| 334 |
+
print(f"[persist-stress] FAIL: cannot compute checksum")
|
| 335 |
+
return False
|
| 336 |
+
checksum1 = out.strip().split()[0]
|
| 337 |
+
|
| 338 |
+
# Re-read and verify checksum matches
|
| 339 |
+
rc, out, _ = _ssh_cmd(host, port, user,
|
| 340 |
+
f"sha256sum {test_dir}/large_1mb.bin",
|
| 341 |
+
timeout=30, identity_file=identity_file)
|
| 342 |
+
checksum2 = out.strip().split()[0] if rc == 0 else ""
|
| 343 |
+
if checksum1 != checksum2:
|
| 344 |
+
print(f"[persist-stress] FAIL: checksum mismatch {checksum1} != {checksum2}")
|
| 345 |
+
return False
|
| 346 |
+
print(f"[persist-stress] Checksum verified: {checksum1[:16]}...")
|
| 347 |
+
|
| 348 |
+
# Phase 6: Concurrent read-write (simulates real usage)
|
| 349 |
+
def read_write(i):
|
| 350 |
+
# Read existing file, write new one
|
| 351 |
+
rc1, out, _ = _ssh_cmd(host, port, user,
|
| 352 |
+
f"cat {test_dir}/file_{i:04d}.txt",
|
| 353 |
+
timeout=20, identity_file=identity_file)
|
| 354 |
+
rc2, _, _ = _ssh_cmd(host, port, user,
|
| 355 |
+
f"echo 'updated-{i}' >> {test_dir}/file_{i:04d}.txt",
|
| 356 |
+
timeout=20, identity_file=identity_file)
|
| 357 |
+
return rc1 == 0 and rc2 == 0
|
| 358 |
+
|
| 359 |
+
print(f"[persist-stress] Concurrent read-write test ({n_files} files, {concurrency} workers)...")
|
| 360 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as ex:
|
| 361 |
+
results = list(ex.map(read_write, range(n_files)))
|
| 362 |
+
rw_ok = sum(results)
|
| 363 |
+
print(f"[persist-stress] Read-write: {rw_ok}/{n_files} ok")
|
| 364 |
+
|
| 365 |
+
# Cleanup
|
| 366 |
+
_ssh_cmd(host, port, user, f"rm -rf {test_dir}", timeout=30, identity_file=identity_file)
|
| 367 |
+
|
| 368 |
+
all_ok = rw_ok == n_files
|
| 369 |
+
if all_ok:
|
| 370 |
+
print(f"[persist-stress] ALL PERSISTENCE TESTS PASSED")
|
| 371 |
+
return all_ok
|
| 372 |
+
|
| 373 |
+
|
| 374 |
def _curl_logs_url(space_id: str, log_type: str) -> str:
|
| 375 |
"""Build the logs API URL (same as user's curl command)."""
|
| 376 |
return f"https://huggingface.co/api/spaces/{space_id}/logs/{log_type}"
|
|
|
|
| 597 |
identity_file=args.ssh_key):
|
| 598 |
print("[ssh-test] BRUTEFORCE FAILED")
|
| 599 |
sys.exit(1)
|
| 600 |
+
print()
|
| 601 |
+
print("[Phase 5] Persistence Stress Test")
|
| 602 |
+
if not test_ssh_persistence_stress(args.ssh_host, args.ssh_port, args.ssh_user,
|
| 603 |
+
n_files=args.ssh_stress_n,
|
| 604 |
+
concurrency=args.ssh_concurrency,
|
| 605 |
+
identity_file=args.ssh_key):
|
| 606 |
+
print("[ssh-test] PERSISTENCE STRESS FAILED")
|
| 607 |
+
sys.exit(1)
|
| 608 |
print("=" * 60)
|
| 609 |
print("[ssh-test] ALL SSH TESTS PASSED")
|
| 610 |
return
|
scripts/test_local.sh
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
-
# HuggingRun: Local integration test for Ubuntu
|
| 4 |
-
# Build Docker β run container β wait β test
|
| 5 |
# Exit 0 only when ALL tests pass. Iterative TDD style.
|
| 6 |
#
|
| 7 |
# Usage:
|
|
@@ -13,11 +13,11 @@ set -euo pipefail
|
|
| 13 |
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
| 14 |
cd "$REPO_ROOT"
|
| 15 |
|
| 16 |
-
IMAGE_NAME="huggingrun-ubuntu-
|
| 17 |
CONTAINER_NAME="huggingrun-test-$$"
|
| 18 |
-
|
| 19 |
SSH_PORT=2222
|
| 20 |
-
|
| 21 |
HOST_SSH_PORT="${HOST_SSH_PORT:-12222}"
|
| 22 |
MAX_WAIT=120 # seconds to wait for services to be ready
|
| 23 |
SSH_USER="user"
|
|
@@ -40,7 +40,7 @@ trap cleanup EXIT
|
|
| 40 |
# ββ Phase 0: Build ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
if [ "${SKIP_BUILD:-}" != "1" ]; then
|
| 42 |
echo -e "${YELLOW}[build] Building Docker image: ${IMAGE_NAME}${NC}"
|
| 43 |
-
docker build -
|
| 44 |
echo -e "${GREEN}[build] Image built successfully${NC}"
|
| 45 |
else
|
| 46 |
echo -e "${YELLOW}[build] SKIP_BUILD=1, using existing image${NC}"
|
|
@@ -49,12 +49,12 @@ fi
|
|
| 49 |
# ββ Phase 1: Run container ββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
echo ""
|
| 51 |
echo -e "${YELLOW}[run] Starting container: ${CONTAINER_NAME}${NC}"
|
| 52 |
-
echo -e "${YELLOW}[run]
|
| 53 |
-
echo -e "${YELLOW}[run] SSH:
|
| 54 |
|
| 55 |
docker run -d \
|
| 56 |
--name "$CONTAINER_NAME" \
|
| 57 |
-
-p "${
|
| 58 |
-p "${HOST_SSH_PORT}:${SSH_PORT}" \
|
| 59 |
-e SSH_LISTEN=0.0.0.0 \
|
| 60 |
-e SSH_PORT=${SSH_PORT} \
|
|
@@ -62,29 +62,29 @@ docker run -d \
|
|
| 62 |
|
| 63 |
echo -e "${GREEN}[run] Container started${NC}"
|
| 64 |
|
| 65 |
-
# ββ Phase 2: Wait for
|
| 66 |
echo ""
|
| 67 |
-
echo -e "${YELLOW}[wait] Waiting for
|
| 68 |
START=$(date +%s)
|
| 69 |
-
|
| 70 |
while [ $(($(date +%s) - START)) -lt "$MAX_WAIT" ]; do
|
| 71 |
-
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${
|
| 72 |
if [ "$HTTP_CODE" = "200" ]; then
|
| 73 |
-
|
| 74 |
break
|
| 75 |
fi
|
| 76 |
-
echo -e "
|
| 77 |
sleep 3
|
| 78 |
done
|
| 79 |
|
| 80 |
-
if [ "$
|
| 81 |
-
echo -e "${RED}[FAIL]
|
| 82 |
echo ""
|
| 83 |
echo "=== Container logs (last 50 lines) ==="
|
| 84 |
docker logs --tail 50 "$CONTAINER_NAME" 2>&1
|
| 85 |
exit 1
|
| 86 |
fi
|
| 87 |
-
echo -e "${GREEN}[wait]
|
| 88 |
|
| 89 |
# ββ Phase 3: Wait for SSH βββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
echo ""
|
|
@@ -111,19 +111,19 @@ if [ "$SSH_READY" = false ]; then
|
|
| 111 |
fi
|
| 112 |
echo -e "${GREEN}[wait] SSH is ready${NC}"
|
| 113 |
|
| 114 |
-
# ββ Phase 4: Run HTTP tests (
|
| 115 |
echo ""
|
| 116 |
-
echo -e "${YELLOW}[test] Phase 4: HTTP basic + stress test on
|
| 117 |
python3 scripts/monitor_and_test.py \
|
| 118 |
--test \
|
| 119 |
-
--url "http://localhost:${
|
| 120 |
-
--expect "
|
| 121 |
--stress-n 50
|
| 122 |
echo -e "${GREEN}[test] HTTP tests PASSED${NC}"
|
| 123 |
|
| 124 |
-
# ββ Phase 5: Run SSH tests βββββββββββββββββββββ
|
| 125 |
echo ""
|
| 126 |
-
echo -e "${YELLOW}[test] Phase 5: SSH connect + command + stress +
|
| 127 |
python3 scripts/monitor_and_test.py \
|
| 128 |
--ssh-test \
|
| 129 |
--ssh-host localhost \
|
|
@@ -131,13 +131,13 @@ python3 scripts/monitor_and_test.py \
|
|
| 131 |
--ssh-user "$SSH_USER" \
|
| 132 |
--ssh-stress-n "$SSH_STRESS_N" \
|
| 133 |
--ssh-concurrency "$SSH_CONCURRENCY"
|
| 134 |
-
echo -e "${GREEN}[test] SSH tests PASSED${NC}"
|
| 135 |
|
| 136 |
# ββ Summary βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
echo ""
|
| 138 |
echo "============================================================"
|
| 139 |
echo -e "${GREEN} ALL TESTS PASSED${NC}"
|
| 140 |
echo ""
|
| 141 |
-
echo "
|
| 142 |
-
echo " SSH access:
|
| 143 |
echo "============================================================"
|
|
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
+
# HuggingRun: Local integration test for Ubuntu Server
|
| 4 |
+
# Build Docker β run container β wait β test ttyd + SSH + persistence stress β cleanup
|
| 5 |
# Exit 0 only when ALL tests pass. Iterative TDD style.
|
| 6 |
#
|
| 7 |
# Usage:
|
|
|
|
| 13 |
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
| 14 |
cd "$REPO_ROOT"
|
| 15 |
|
| 16 |
+
IMAGE_NAME="huggingrun-ubuntu-server-test"
|
| 17 |
CONTAINER_NAME="huggingrun-test-$$"
|
| 18 |
+
TTYD_PORT=7860
|
| 19 |
SSH_PORT=2222
|
| 20 |
+
HOST_TTYD_PORT="${HOST_TTYD_PORT:-17860}"
|
| 21 |
HOST_SSH_PORT="${HOST_SSH_PORT:-12222}"
|
| 22 |
MAX_WAIT=120 # seconds to wait for services to be ready
|
| 23 |
SSH_USER="user"
|
|
|
|
| 40 |
# ββ Phase 0: Build ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
if [ "${SKIP_BUILD:-}" != "1" ]; then
|
| 42 |
echo -e "${YELLOW}[build] Building Docker image: ${IMAGE_NAME}${NC}"
|
| 43 |
+
docker build -t "$IMAGE_NAME" . 2>&1 | tail -20
|
| 44 |
echo -e "${GREEN}[build] Image built successfully${NC}"
|
| 45 |
else
|
| 46 |
echo -e "${YELLOW}[build] SKIP_BUILD=1, using existing image${NC}"
|
|
|
|
| 49 |
# ββ Phase 1: Run container ββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
echo ""
|
| 51 |
echo -e "${YELLOW}[run] Starting container: ${CONTAINER_NAME}${NC}"
|
| 52 |
+
echo -e "${YELLOW}[run] ttyd: localhost:${HOST_TTYD_PORT} β :${TTYD_PORT}${NC}"
|
| 53 |
+
echo -e "${YELLOW}[run] SSH: localhost:${HOST_SSH_PORT} β :${SSH_PORT}${NC}"
|
| 54 |
|
| 55 |
docker run -d \
|
| 56 |
--name "$CONTAINER_NAME" \
|
| 57 |
+
-p "${HOST_TTYD_PORT}:${TTYD_PORT}" \
|
| 58 |
-p "${HOST_SSH_PORT}:${SSH_PORT}" \
|
| 59 |
-e SSH_LISTEN=0.0.0.0 \
|
| 60 |
-e SSH_PORT=${SSH_PORT} \
|
|
|
|
| 62 |
|
| 63 |
echo -e "${GREEN}[run] Container started${NC}"
|
| 64 |
|
| 65 |
+
# ββ Phase 2: Wait for ttyd web terminal ββββββββββββββββββββββββββββββ
|
| 66 |
echo ""
|
| 67 |
+
echo -e "${YELLOW}[wait] Waiting for ttyd on localhost:${HOST_TTYD_PORT} (max ${MAX_WAIT}s)...${NC}"
|
| 68 |
START=$(date +%s)
|
| 69 |
+
TTYD_READY=false
|
| 70 |
while [ $(($(date +%s) - START)) -lt "$MAX_WAIT" ]; do
|
| 71 |
+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${HOST_TTYD_PORT}/" 2>/dev/null || echo "000")
|
| 72 |
if [ "$HTTP_CODE" = "200" ]; then
|
| 73 |
+
TTYD_READY=true
|
| 74 |
break
|
| 75 |
fi
|
| 76 |
+
echo -e " ttyd not ready (HTTP ${HTTP_CODE}), waiting 3s..."
|
| 77 |
sleep 3
|
| 78 |
done
|
| 79 |
|
| 80 |
+
if [ "$TTYD_READY" = false ]; then
|
| 81 |
+
echo -e "${RED}[FAIL] ttyd did not become ready within ${MAX_WAIT}s${NC}"
|
| 82 |
echo ""
|
| 83 |
echo "=== Container logs (last 50 lines) ==="
|
| 84 |
docker logs --tail 50 "$CONTAINER_NAME" 2>&1
|
| 85 |
exit 1
|
| 86 |
fi
|
| 87 |
+
echo -e "${GREEN}[wait] ttyd is ready (HTTP 200)${NC}"
|
| 88 |
|
| 89 |
# ββ Phase 3: Wait for SSH βββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
echo ""
|
|
|
|
| 111 |
fi
|
| 112 |
echo -e "${GREEN}[wait] SSH is ready${NC}"
|
| 113 |
|
| 114 |
+
# ββ Phase 4: Run HTTP tests (ttyd) ββββββββββββββββββββββββββββββββββ
|
| 115 |
echo ""
|
| 116 |
+
echo -e "${YELLOW}[test] Phase 4: HTTP basic + stress test on ttyd${NC}"
|
| 117 |
python3 scripts/monitor_and_test.py \
|
| 118 |
--test \
|
| 119 |
+
--url "http://localhost:${HOST_TTYD_PORT}" \
|
| 120 |
+
--expect "ttyd" --expect "terminal" \
|
| 121 |
--stress-n 50
|
| 122 |
echo -e "${GREEN}[test] HTTP tests PASSED${NC}"
|
| 123 |
|
| 124 |
+
# ββ Phase 5: Run SSH + persistence stress tests βββββββββββββββββββββ
|
| 125 |
echo ""
|
| 126 |
+
echo -e "${YELLOW}[test] Phase 5: SSH connect + command + stress + persistence${NC}"
|
| 127 |
python3 scripts/monitor_and_test.py \
|
| 128 |
--ssh-test \
|
| 129 |
--ssh-host localhost \
|
|
|
|
| 131 |
--ssh-user "$SSH_USER" \
|
| 132 |
--ssh-stress-n "$SSH_STRESS_N" \
|
| 133 |
--ssh-concurrency "$SSH_CONCURRENCY"
|
| 134 |
+
echo -e "${GREEN}[test] SSH + persistence tests PASSED${NC}"
|
| 135 |
|
| 136 |
# ββ Summary βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
echo ""
|
| 138 |
echo "============================================================"
|
| 139 |
echo -e "${GREEN} ALL TESTS PASSED${NC}"
|
| 140 |
echo ""
|
| 141 |
+
echo " Web terminal: http://localhost:${HOST_TTYD_PORT}/"
|
| 142 |
+
echo " SSH access: ssh -p ${HOST_SSH_PORT} ${SSH_USER}@localhost"
|
| 143 |
echo "============================================================"
|
ubuntu-server/start-server.sh
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Start Ubuntu Server: ttyd web terminal on 7860 + sshd on 2222
|
| 3 |
+
# All state persisted in /data via HuggingRun sync
|
| 4 |
+
echo "[start-server] Starting ..." >&2
|
| 5 |
+
set -e
|
| 6 |
+
|
| 7 |
+
export PERSIST_PATH="${PERSIST_PATH:-/data}"
|
| 8 |
+
export SSH_PORT="${SSH_PORT:-2222}"
|
| 9 |
+
export SSH_LISTEN="${SSH_LISTEN:-0.0.0.0}"
|
| 10 |
+
export TTYD_PORT="${TTYD_PORT:-7860}"
|
| 11 |
+
|
| 12 |
+
# Persistent home: shell history, configs, installed tools survive restarts
|
| 13 |
+
SERVER_HOME="$PERSIST_PATH/home"
|
| 14 |
+
mkdir -p "$SERVER_HOME"
|
| 15 |
+
export HOME="$SERVER_HOME"
|
| 16 |
+
|
| 17 |
+
# Ensure basic dirs
|
| 18 |
+
mkdir -p "$HOME/.ssh" "$HOME/.config" "$HOME/.local/bin"
|
| 19 |
+
|
| 20 |
+
# Add persistent bin to PATH (user-installed tools survive restart)
|
| 21 |
+
export PATH="$HOME/.local/bin:$PATH"
|
| 22 |
+
|
| 23 |
+
# Copy bashrc if not yet personalized
|
| 24 |
+
if [ ! -f "$HOME/.bashrc" ]; then
|
| 25 |
+
cat > "$HOME/.bashrc" <<'BASHRC'
|
| 26 |
+
# HuggingRun Ubuntu Server
|
| 27 |
+
export PATH="$HOME/.local/bin:$PATH"
|
| 28 |
+
export PS1='\[\033[01;32m\]\u@huggingrun\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
|
| 29 |
+
alias ll='ls -la'
|
| 30 |
+
alias la='ls -A'
|
| 31 |
+
BASHRC
|
| 32 |
+
fi
|
| 33 |
+
|
| 34 |
+
# ββ SSH ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
set +e
|
| 36 |
+
|
| 37 |
+
# Host key: use pre-generated from Docker build, or generate at runtime
|
| 38 |
+
HOST_KEY="$HOME/.ssh/ssh_host_ed25519_key"
|
| 39 |
+
[ ! -f "$HOST_KEY" ] && cp /home/user/.ssh/ssh_host_ed25519_key "$HOST_KEY" 2>/dev/null
|
| 40 |
+
[ ! -f "$HOST_KEY" ] && ssh-keygen -t ed25519 -f "$HOST_KEY" -N "" -C "" 2>/dev/null
|
| 41 |
+
|
| 42 |
+
# If SSH_AUTHORIZED_KEYS env is set, use key-based auth; otherwise password auth for testing
|
| 43 |
+
[ -n "${SSH_AUTHORIZED_KEYS-}" ] && echo "$SSH_AUTHORIZED_KEYS" > "$HOME/.ssh/authorized_keys" && chmod 600 "$HOME/.ssh/authorized_keys"
|
| 44 |
+
|
| 45 |
+
if [ -f "$HOST_KEY" ]; then
|
| 46 |
+
if [ -f "$HOME/.ssh/authorized_keys" ]; then
|
| 47 |
+
echo "[start-server] Starting sshd (key auth) on $SSH_LISTEN:$SSH_PORT ..." >&2
|
| 48 |
+
/usr/sbin/sshd -o "Port=$SSH_PORT" -o "HostKey=$HOST_KEY" \
|
| 49 |
+
-o "AuthorizedKeysFile=$HOME/.ssh/authorized_keys" \
|
| 50 |
+
-o "PermitEmptyPasswords=no" -o "PasswordAuthentication=no" \
|
| 51 |
+
-o "ListenAddress=$SSH_LISTEN" -o "PidFile=$HOME/.ssh/sshd.pid" \
|
| 52 |
+
-o "UsePAM=no" -o "PermitUserEnvironment=yes" -D -e &
|
| 53 |
+
else
|
| 54 |
+
echo "[start-server] Starting sshd (password-less, test mode) on $SSH_LISTEN:$SSH_PORT ..." >&2
|
| 55 |
+
/usr/sbin/sshd -o "Port=$SSH_PORT" -o "HostKey=$HOST_KEY" \
|
| 56 |
+
-o "PermitEmptyPasswords=yes" -o "PasswordAuthentication=yes" \
|
| 57 |
+
-o "ListenAddress=$SSH_LISTEN" -o "PidFile=$HOME/.ssh/sshd.pid" \
|
| 58 |
+
-o "UsePAM=no" -o "PermitRootLogin=no" -D -e &
|
| 59 |
+
fi
|
| 60 |
+
SSHD_PID=$!
|
| 61 |
+
sleep 1
|
| 62 |
+
echo "[start-server] sshd PID=$SSHD_PID" >&2
|
| 63 |
+
|
| 64 |
+
# Reverse SSH tunnel (HF Spaces: outbound only on 80/443/8080)
|
| 65 |
+
[ -n "${SSH_REVERSE_TARGET-}" ] && ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=60 \
|
| 66 |
+
-R "0.0.0.0:${SSH_PORT}:127.0.0.1:${SSH_PORT}" $SSH_REVERSE_TARGET -N &
|
| 67 |
+
fi
|
| 68 |
+
set -e
|
| 69 |
+
|
| 70 |
+
# ββ ttyd web terminal βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 71 |
+
echo "[start-server] Starting ttyd on 0.0.0.0:$TTYD_PORT ..." >&2
|
| 72 |
+
# ttyd runs bash in foreground; writable=true allows input
|
| 73 |
+
exec ttyd \
|
| 74 |
+
--port "$TTYD_PORT" \
|
| 75 |
+
--writable \
|
| 76 |
+
--base-path / \
|
| 77 |
+
bash --login
|