Compare commits
9 commits
feat/tts-g
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6092c432fe | |||
| dedde311f1 | |||
| 0d9a43c8f6 | |||
| cb54a7977a | |||
| 64e4a1fb5b | |||
| 0e9a42bb4f | |||
| 723b3e5a61 | |||
| 9fc08a3145 | |||
| 0782404723 |
1 changed files with 15 additions and 119 deletions
134
app.py
134
app.py
|
|
@ -1165,117 +1165,6 @@ async def vps_speedtest_deploy(req: SpeedtestDeployRequest, _=Depends(_verify)):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
BW_MANAGER_REPO_FILES = (
|
|
||||||
".env.example", ".gitignore", "README.md", "compose.yaml",
|
|
||||||
"src/.dockerignore", "src/Dockerfile", "src/app.py",
|
|
||||||
"src/remote_policy.py", "src/requirements.txt",
|
|
||||||
"src/templates/base.html", "src/templates/history.html",
|
|
||||||
"src/templates/index.html", "src/templates/users.html",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _fetch_bw_manager_text(path: str) -> str:
|
|
||||||
if path not in BW_MANAGER_REPO_FILES:
|
|
||||||
raise ValueError("unsupported BW Manager file")
|
|
||||||
cfg = SERVICES.get("forgejo", {})
|
|
||||||
base_url, token = cfg.get("url"), _get_key(cfg)
|
|
||||||
if not base_url or not token:
|
|
||||||
raise RuntimeError("Forgejo service configuration is unavailable")
|
|
||||||
url = f"{base_url}/api/v1/repos/sascha/bw-manager/contents/{path}"
|
|
||||||
async with httpx.AsyncClient(timeout=30) as client:
|
|
||||||
response = await client.get(
|
|
||||||
url, params={"ref": "main"},
|
|
||||||
headers={"Authorization": f"token {token}"},
|
|
||||||
)
|
|
||||||
response.raise_for_status()
|
|
||||||
return base64.b64decode(response.json()["content"]).decode()
|
|
||||||
|
|
||||||
|
|
||||||
def _deploy_bw_manager_compose(files: dict[str, str]) -> dict:
|
|
||||||
if set(files) != set(BW_MANAGER_REPO_FILES):
|
|
||||||
raise ValueError("BW Manager source bundle is incomplete")
|
|
||||||
if "build: ./src" not in files["compose.yaml"]:
|
|
||||||
raise ValueError("BW Manager compose contract is invalid")
|
|
||||||
if "build_gated_targets" not in files["src/app.py"]:
|
|
||||||
raise ValueError("BW Manager candidate lacks the user/network AND gate")
|
|
||||||
rc, working_dir, err = _ssh(
|
|
||||||
VPS_SSH,
|
|
||||||
"docker inspect -f '{{ index .Config.Labels \"com.docker.compose.project.working_dir\" }}' bw-manager",
|
|
||||||
timeout=30,
|
|
||||||
)
|
|
||||||
working_dir = working_dir.strip()
|
|
||||||
if rc != 0 or not re.fullmatch(r"/app-config/[A-Za-z0-9_./-]+", working_dir):
|
|
||||||
raise RuntimeError(f"cannot determine safe BW Manager working directory: {(err or working_dir)[-300:]}")
|
|
||||||
timestamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
|
||||||
candidate = f"/app-config/deployment-candidates/bw-manager-{timestamp}"
|
|
||||||
backup = f"/app-config/deployment-backups/bw-manager-{timestamp}"
|
|
||||||
image = "bw-manager-bw-manager"
|
|
||||||
rollback_image = f"{image}:rollback-{timestamp}"
|
|
||||||
init_script = f"""from pathlib import Path
|
|
||||||
import shutil
|
|
||||||
candidate = Path({candidate!r})
|
|
||||||
if candidate.exists(): shutil.rmtree(candidate)
|
|
||||||
candidate.mkdir(parents=True)
|
|
||||||
live_env = Path({working_dir!r}) / '.env'
|
|
||||||
if live_env.exists(): shutil.copy2(live_env, candidate / '.env')
|
|
||||||
"""
|
|
||||||
rc, _out, err = _remote_python(init_script)
|
|
||||||
if rc != 0:
|
|
||||||
raise RuntimeError(f"BW Manager candidate initialization failed: {err[-300:]}")
|
|
||||||
# Stage one file per SSH call. Sending the complete repository in one
|
|
||||||
# command exceeds Linux's argv limit once app.py and templates are encoded.
|
|
||||||
for relative, content in files.items():
|
|
||||||
file_script = f"""from pathlib import Path
|
|
||||||
target = Path({candidate!r}) / {relative!r}
|
|
||||||
target.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
target.write_text({content!r})
|
|
||||||
"""
|
|
||||||
rc, _out, err = _remote_python(file_script)
|
|
||||||
if rc != 0:
|
|
||||||
raise RuntimeError(f"BW Manager staging failed for {relative}: {err[-300:]}")
|
|
||||||
rc, _out, err = _ssh(VPS_SSH, f"cd {candidate} && docker compose config -q && docker compose build --pull", timeout=600)
|
|
||||||
if rc != 0:
|
|
||||||
raise RuntimeError(f"BW Manager candidate build failed: {err[-500:]}")
|
|
||||||
deploy_script = f"""from pathlib import Path
|
|
||||||
import shutil
|
|
||||||
live, backup, candidate = Path({working_dir!r}), Path({backup!r}), Path({candidate!r})
|
|
||||||
backup.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
if backup.exists(): shutil.rmtree(backup)
|
|
||||||
shutil.copytree(live, backup)
|
|
||||||
for relative in {BW_MANAGER_REPO_FILES!r}:
|
|
||||||
source, target = candidate / relative, live / relative
|
|
||||||
target.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
shutil.copy2(source, target)
|
|
||||||
"""
|
|
||||||
rc, _out, err = _remote_python(deploy_script)
|
|
||||||
if rc != 0:
|
|
||||||
raise RuntimeError(f"BW Manager live file switch failed: {err[-300:]}")
|
|
||||||
_ssh(VPS_SSH, f"docker image tag {image} {rollback_image}", timeout=60)
|
|
||||||
rollback = (
|
|
||||||
f"rm -rf {working_dir} && cp -a {backup} {working_dir} && "
|
|
||||||
f"docker image tag {rollback_image} {image} && cd {working_dir} && "
|
|
||||||
"docker compose up -d --no-build"
|
|
||||||
)
|
|
||||||
rc, out, err = _ssh(VPS_SSH, f"cd {working_dir} && docker compose up -d --build --remove-orphans", timeout=600)
|
|
||||||
if rc != 0:
|
|
||||||
_ssh(VPS_SSH, rollback, timeout=180)
|
|
||||||
raise RuntimeError(f"BW Manager deployment failed: {(err or out)[-500:]}")
|
|
||||||
health = "for i in $(seq 1 45); do curl -fsS --max-time 3 http://127.0.0.1:8870/api/status >/dev/null && exit 0; sleep 2; done; exit 1"
|
|
||||||
rc, _out, err = _ssh(VPS_SSH, health, timeout=105)
|
|
||||||
if rc != 0:
|
|
||||||
_ssh(VPS_SSH, rollback, timeout=180)
|
|
||||||
raise RuntimeError(f"BW Manager health failed; rollback attempted: {err[-300:]}")
|
|
||||||
return {"status": "deployed", "health": "ok", "working_dir": working_dir, "backup": backup}
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/vps/bw-manager/deploy")
|
|
||||||
async def vps_bw_manager_deploy(_=Depends(_verify)):
|
|
||||||
contents = await asyncio.gather(*(_fetch_bw_manager_text(path) for path in BW_MANAGER_REPO_FILES))
|
|
||||||
result = await asyncio.to_thread(_deploy_bw_manager_compose, dict(zip(BW_MANAGER_REPO_FILES, contents)))
|
|
||||||
_audit("/vps/bw-manager/deploy", "POST", 200, "Git-managed BW Manager deployment")
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
# --- VM Lifecycle Endpoints ---
|
# --- VM Lifecycle Endpoints ---
|
||||||
import subprocess as _sp
|
import subprocess as _sp
|
||||||
|
|
||||||
|
|
@ -1905,14 +1794,21 @@ async def tts_bridge_deploy(req: TTSBridgeDeployRequest, _=Depends(_verify)):
|
||||||
"/app-config/tts-bridge/butler-token": BUTLER_TOKEN,
|
"/app-config/tts-bridge/butler-token": BUTLER_TOKEN,
|
||||||
"/app-config/tts-bridge/client-token": client_token,
|
"/app-config/tts-bridge/client-token": client_token,
|
||||||
}
|
}
|
||||||
encoded = base64.b64encode(json.dumps(files).encode()).decode()
|
installer = """import json, os, pathlib
|
||||||
script = (
|
files = json.loads({files_json!r})
|
||||||
"import base64,json,os,pathlib;"
|
base = pathlib.Path('/app-config/tts-bridge')
|
||||||
f"files=json.loads(base64.b64decode('{encoded}'));"
|
base.mkdir(parents=True, exist_ok=True)
|
||||||
"pathlib.Path('/app-config/tts-bridge').mkdir(parents=True,exist_ok=True);"
|
for filename, value in files.items():
|
||||||
"[(pathlib.Path(p).write_text(v),os.chmod(p,0o600)) for p,v in files.items()]"
|
path = pathlib.Path(filename)
|
||||||
)
|
if path.is_dir():
|
||||||
rc, _out, err = _ssh("sascha@10.5.85.5", f"python3 -c {__import__('shlex').quote(script)}", timeout=30)
|
path.rmdir()
|
||||||
|
path.write_text(value)
|
||||||
|
os.chown(path, 10001, 10001)
|
||||||
|
os.chmod(path, 0o400)
|
||||||
|
""".format(files_json=json.dumps(files))
|
||||||
|
encoded = base64.b64encode(installer.encode()).decode()
|
||||||
|
command = f"sudo python3 -c {__import__('shlex').quote(f'import base64;exec(base64.b64decode({encoded!r}))')}"
|
||||||
|
rc, _out, err = _ssh("sascha@10.5.85.5", command, timeout=30)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
_audit("/tts/bridge/deploy", "POST", 500, "secret installation failed")
|
_audit("/tts/bridge/deploy", "POST", 500, "secret installation failed")
|
||||||
raise HTTPException(status_code=500, detail="Could not install bridge secrets")
|
raise HTTPException(status_code=500, detail="Could not install bridge secrets")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue