File size: 3,013 Bytes
39cd153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541eaba
 
39cd153
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
FROM node:22-slim

ENV NODE_ENV=production \
    PORT=7860 \
    HOSTNAME=0.0.0.0 \
    NEXT_TELEMETRY_DISABLED=1

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 python3-pip curl procps build-essential ca-certificates \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --break-system-packages --no-cache-dir huggingface_hub

# Install OmniRoute & runtime dependencies
RUN npm install -g omniroute@3.8.0 --prefer-online --no-audit --no-fund \
    && mkdir -p /root/.omniroute/runtime \
    && cd /root/.omniroute/runtime \
    && npm install sql.js@1.14.1 better-sqlite3@12.10.0 --no-audit --no-fund --prefer-online

WORKDIR /app

# Script Sync
RUN cat > /app/sync.py <<'PY'
import os
import tarfile
import time
import sys
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download

REPO_ID = os.environ.get("DATASET_REPO")
TOKEN = os.environ.get("HF_TOKEN")
BACKUP_FILE = "/tmp/omniroute_backup.tar.gz"
DATA_DIR = Path("/root/.omniroute")

def safe_extract(tar, path):
    base = Path(path).resolve()
    for member in tar.getmembers():
        target = (base / member.name).resolve()
        if not str(target).startswith(str(base)):
            raise RuntimeError(f"unsafe tar member: {member.name}")
    tar.extractall(path)

def upload():
    if not REPO_ID or not TOKEN: return
    if not DATA_DIR.exists(): return
    try:
        with tarfile.open(BACKUP_FILE, "w:gz") as tar:
            tar.add(str(DATA_DIR), arcname=".omniroute")
        HfApi(token=TOKEN).upload_file(
            path_or_fileobj=BACKUP_FILE,
            path_in_repo="omniroute_backup.tar.gz",
            repo_id=REPO_ID,
            repo_type="dataset",
        )
        print(">>> [Sync] Backup sukses: " + time.strftime("%Y-%m-%d %H:%M:%S"), flush=True)
    except Exception as e:
        print(">>> [Sync] Backup gagal: " + str(e), flush=True)

def download():
    if not REPO_ID or not TOKEN: return
    try:
        print(">>> [Sync] Restore dari Dataset...", flush=True)
        path = hf_hub_download(repo_id=REPO_ID, filename="omniroute_backup.tar.gz", repo_type="dataset", token=TOKEN)
        with tarfile.open(path, "r:gz") as tar:
            safe_extract(tar, "/root")
        print(">>> [Sync] Restore sukses", flush=True)
    except Exception as e:
        print(">>> [Sync] Skip restore: " + str(e), flush=True)

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "upload": upload()
    elif len(sys.argv) > 1 and sys.argv[1] == "download": download()
PY

# Entrypoint script
RUN cat > /app/run.sh <<'SH'
#!/bin/bash
set -euo pipefail

mkdir -p /root/.omniroute/runtime

echo '=== 1. Restore Data ==='
python3 /app/sync.py download

echo '=== 2. Cron Backup (10m) ==='
(while true; do sleep 600; python3 /app/sync.py upload; done) &

echo '=== 3. Start OmniRoute ==='
export PORT=7860
# OmniRoute 3.8.0 CLI flags fix:
exec omniroute serve --port 7860 --no-open --log
SH

RUN chmod +x /app/run.sh

EXPOSE 7860
CMD ["/bin/bash", "/app/run.sh"]