File size: 9,220 Bytes
ecb5fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
"""Run the wiwi gateway on a Runtime Cloud sandbox.

This is the deployment harness that moves the project's real work onto Runtime
(https://withruntime.com). It:

  1. Creates a Runtime sandbox (2 vCPU / 4 GiB / 8 GiB disk) on free usage.
  2. Uploads the source tree (secrets, venvs, node_modules and runtime state
     excluded) plus a self-contained config, so the sandbox needs no keys.
  3. Installs the project (pip install -e '.[dev,redis,otel]').
  4. Runs the project's own gate: `ruff check` and the pytest suite.
  5. Starts the real job -- the wiwi gateway server -- in the background via
     `spawn`, then proves it is live: /health returns 200 and /v1/models
     answers with the configured providers.
  6. Deletes the sandbox so nothing is left billed.

Every step is fatal: a red gate exits non-zero instead of being printed and
skipped, so this can be wired into CI. Exit codes are 1 (gate red), 2 (server
never became healthy) and 3 (server did not answer /v1/models).

Usage:
    RUNTIME_API_KEY=... python3 deploy/runtime_deploy.py
    # or reuse the machine's saved Runtime connection (no key needed)

Requirements: Python 3.11+ with `withruntime` installed (`pip install withruntime`).
"""

from __future__ import annotations

import io
import re
import sys
import tarfile
import time
from pathlib import Path

from withruntime import Sandbox

REPO = Path(__file__).resolve().parent.parent
SANDBOX_NAME = "wiwi-runtime-move"
LABELS = {"app": "wiwi", "job": "runtime-move"}
REMOTE = "/workspace/wiwi"
MASTER_KEY = "sk-test-master"

# Never ship: secrets, caches, nested repos, runtime state.
EXCLUDE_TOP = {
    ".git", "node_modules", ".venv", "__pycache__", ".pytest_cache",
    ".hypothesis", ".mimocode", ".playwright-mcp", ".agents", ".perch",
    ".freebuff", ".claude", ".ruff_cache", ".wiwi", ".verify", ".env",
    "deepseek-web2api-free", "wiwi.db", "web/node_modules",
}
# ``EXCLUDE_RE`` matches a *directory component* anywhere in the path; the
# compiled / object files are matched as suffixes. (The old pattern anchored on
# ``$``, so a file inside ``web/node_modules/pkg/index.js`` was never excluded
# — the whole tree shipped.)
EXCLUDE_RE = re.compile(r"(^|/)(__pycache__|\.pyc$|\.git|node_modules|\.venv)(/|$)")

# The harness must not depend on the operator's wiwi.yaml (gitignored, carries
# live keys) nor on .env (which it deliberately does not upload). This config
# binds one placeholder credential, so the provider and its ``smoke`` group
# exist and /health reports ``ok`` instead of ``degraded`` regardless of what
# the box has configured. ``database_url`` is left unset on purpose:
# DATABASE_URL is not forwarded, so the server takes its SQLite default.
#
# Shape matters: ``model_list`` entries nest their fields under
# ``wiwi_params`` (``provider`` + ``model``). A flat entry is dropped silently
# by ``WiwiConfig._validate``, leaving a gateway with no routes that still
# answers /health with 200 — which is why the health gate checks the body.
CONFIG_YAML = """\
general_settings:
  master_key: os.environ/WIWI_MASTER_KEY

providers:
  - name: local-ollama
    provider: openai-compatible
    base_url: http://localhost:11434/v1
    keys:
      - {label: local, key: "ollama"}

model_list:
  - model_name: smoke
    wiwi_params:
      provider: local-ollama
      model: smoke

wiwi_settings:
  port: 4000
"""


def make_tar(root: Path) -> bytes:
    buf = io.BytesIO()
    with tarfile.open(fileobj=buf, mode="w:gz") as tf:
        for path in root.rglob("*"):
            rel = path.relative_to(root)
            if rel.parts and rel.parts[0] in EXCLUDE_TOP:
                continue
            if EXCLUDE_RE.search(str(rel)):
                continue
            if path.is_file() and path.exists():
                tf.add(path, arcname=f"wiwi-source/{rel}")
    return buf.getvalue()


def main() -> int:
    print(":: building source archive", flush=True)
    tarball = make_tar(REPO)
    print(f":: archive ~{len(tarball) / 1e6:.1f} MB", flush=True)

    print(f":: creating sandbox '{SANDBOX_NAME}'", flush=True)
    sbx = Sandbox.create(
        name=SANDBOX_NAME,
        labels=LABELS,
        vcpu=2,
        memory_mib=4096,
        disk_mib=8192,
        timeout_seconds=3600,
        on_lease_end="stop",
    )
    print(f":: sandbox {sbx.id} ready", flush=True)
    try:
        print(":: uploading source", flush=True)
        sbx.files.write("/workspace/wiwi-source.tar.gz", tarball)

        def run(cmd: str, timeout: int = 900) -> str:
            """Run ``cmd`` in the sandbox, echo its output, return stdout+stderr.

            ``cmd | tail`` exits with ``tail``'s status, not the command's, so
            callers check the text (a marker line) instead of an exit code.
            The exit status is also folded into the returned text as a
            ``[exit N]`` line, which is what :func:`run` shares with callers.
            """
            print(f"\n$ {cmd}\n" + "=" * 60, flush=True)
            r = sbx.exec(cmd, timeout_ms=timeout * 1000,
                         on_stdout=sys.stdout.write, on_stderr=sys.stderr.write)
            marker = f"\n[exit {r.exit_code}]"
            print(marker, flush=True)
            return (r.stdout or "") + (r.stderr or "") + marker

        run("mkdir -p /workspace/wiwi && "
            "tar -xzf /workspace/wiwi-source.tar.gz -C /workspace/wiwi "
            "--strip-components=1")

        sbx.files.write(f"{REMOTE}/wiwi.yaml", CONFIG_YAML)

        # Unpiped: the install's real exit code is the gate.
        if "[exit 0]" not in run("cd /workspace/wiwi && "
                                  "python3 -m pip install -q -e '.[dev,redis,otel]'",
                                  timeout=1500):
            print(":: pip install failed", flush=True)
            return 1

        print(":: running the project's lint gate", flush=True)
        if "All checks passed" not in run("cd /workspace/wiwi && "
                                          "python3 -m ruff check wiwi/ tests/ "
                                          "2>&1 | tail -25", timeout=600):
            print(":: ruff gate is red", flush=True)
            return 1

        print(":: running the project's pytest suite (full). This may take several minutes.",
              flush=True)
        if not re.search(r"\d+ passed", run(
                "cd /workspace/wiwi && python3 -m pytest tests/ -q "
                "--no-header -p no:cacheprovider 2>&1 | tail -25", timeout=2400)):
            print(":: pytest gate is red", flush=True)
            return 1

        print(":: starting the real job: wiwi gateway server", flush=True)
        prof = sbx.spawn(
            f"cd {REMOTE} && "
            f"WIWI_MASTER_KEY={MASTER_KEY} python3 -m wiwi.main --config wiwi.yaml"
        )
        print(f":: server process {prof.id} spawned", flush=True)

        def http_status(url: str, extra: str = "") -> str:
            try:
                r = sbx.exec(f"curl -s -o /tmp/body.json -w '%{{http_code}}' {extra} {url}",
                             timeout_ms=20_000)
                return r.stdout.strip()
            except Exception as e:  # noqa: BLE001 - remote sandbox call can raise many SDK errors
                print(f":: probe error: {e}", file=sys.stderr, flush=True)
                return ""

        deadline = time.time() + 180
        ok = False
        while time.time() < deadline and not ok:
            # A 200 alone is not proof: /health answers 200 while the gateway
            # has no deployments and reports ``degraded`` (measured — a
            # model_list that fails the ``wiwi_params`` shape check is dropped
            # silently). Require a healthy body AND a resolvable route.
            ok = (http_status("http://127.0.0.1:4000/health") == "200"
                  and '"status":"ok"' in sbx.files.read_text("/tmp/body.json")
                  and http_status("http://127.0.0.1:4000/public/models") == "200"
                  and "smoke" in sbx.files.read_text("/tmp/body.json"))
            if not ok:
                time.sleep(3)
        if not ok:
            print(":: gateway never became healthy with the 'smoke' group", flush=True)
            return 2
        print(":: /health healthy, /public/models serves 'smoke'", flush=True)

        code = http_status("http://127.0.0.1:4000/v1/models",
                           f"-H 'Authorization: Bearer {MASTER_KEY}'")
        print(f":: /v1/models HTTP {code}", flush=True)
        body = sbx.files.read_text("/tmp/body.json")[:500]
        print(f":: /v1/models body: {body}", flush=True)
        # Clean up in ``finally`` too: a failed probe used to leave the server
        # running until the sandbox was deleted.
        prof.kill("SIGTERM")
        if code != "200" or "smoke" not in body:
            print(":: /v1/models did not serve the configured model", flush=True)
            return 3

        print(":: DONE — wiwi gateway verified running on Runtime", flush=True)
        return 0
    finally:
        print(f":: deleting sandbox {sbx.id}", flush=True)
        sbx.delete()
        print(":: sandbox deleted", flush=True)


if __name__ == "__main__":
    sys.exit(main())