#!/usr/bin/env python3 """Merge CRON_SECRET into client-portal Dokploy env, then Deploy.""" from __future__ import annotations import json import re import sys import time from pathlib import Path from playwright.sync_api import sync_playwright ROOT = Path("/Users/4ventures/4ventures-client-portal") BPS = Path("/Users/4ventures/Projects/bps-facturacion") BASE = "https://dokploy.4ventures.es" APP_ID = "NZS797t9Rnq9Pj4mHiii3" APP = ( f"{BASE}/dashboard/project/Z_4M-uDOH1_--itGSTh4l" f"/environment/bGja70l_a7HQ1dxyXqSuf/services/application/{APP_ID}" ) HEALTH = "https://app.4ventures.es/api/health" def cred(key: str) -> str: import os v = os.environ.get(key, "").strip() if v: return v for p in (BPS / ".env", ROOT / ".env.local"): if not p.is_file(): continue for line in p.read_text(encoding="utf-8").splitlines(): if line.startswith(f"{key}="): return line.split("=", 1)[1].strip().strip('"').strip("'") return "" def main() -> int: cron = cred("CRON_SECRET") if not cron: print("missing CRON_SECRET locally", flush=True) return 2 email = cred("DOKPLOY_EMAIL") or "marketing@4ventures.es" password = cred("DOKPLOY_PASSWORD") if not password: print("missing DOKPLOY_PASSWORD", flush=True) return 2 with sync_playwright() as p: browser = p.chromium.launch(headless=True) page = browser.new_page(viewport={"width": 1400, "height": 1000}) page.goto(BASE + "/", wait_until="domcontentloaded", timeout=90000) page.wait_for_timeout(600) if "/dashboard" not in page.url: page.locator('input[type="email"], input[name="email"]').first.fill(email) page.locator('input[type="password"]').first.fill(password) page.locator('button[type="submit"]').first.click() page.wait_for_url(re.compile(r"/dashboard"), timeout=90000) print("login ok", flush=True) page.goto(APP, wait_until="networkidle", timeout=120000) page.wait_for_timeout(800) result = page.evaluate( """async ({ appId, cronSecret }) => { const oneUrl = '/api/trpc/application.one?batch=1&input=' + encodeURIComponent(JSON.stringify({ '0': { json: { applicationId: appId } } })); const one = await fetch(oneUrl, { credentials: 'include' }); const oneText = await one.text(); if (!one.ok) return { ok: false, step: 'one', status: one.status, body: oneText.slice(0, 400) }; let envText = ''; try { const j = JSON.parse(oneText); const data = j[0]?.result?.data?.json || j[0]?.result?.data || j; envText = data?.env || data?.environment || ''; if (typeof envText !== 'string') envText = ''; } catch (e) { return { ok: false, step: 'parse', error: String(e), body: oneText.slice(0, 400) }; } const lines = envText.split('\\n').filter(Boolean); const map = {}; for (const line of lines) { const i = line.indexOf('='); if (i < 0) continue; map[line.slice(0, i)] = line.slice(i + 1); } map['CRON_SECRET'] = cronSecret; const merged = Object.keys(map).map(k => k + '=' + map[k]).join('\\n') + '\\n'; const keys = Object.keys(map); const tries = []; const payloads = [ { path: '/api/application.update', body: { applicationId: appId, env: merged } }, { path: '/api/trpc/application.update?batch=1', body: { '0': { json: { applicationId: appId, env: merged } } } }, { path: '/api/trpc/application.saveEnvironment?batch=1', body: { '0': { json: { applicationId: appId, env: merged } } } }, ]; for (const t of payloads) { const r = await fetch(t.path, { method: 'POST', credentials: 'include', headers: { 'content-type': 'application/json' }, body: JSON.stringify(t.body), }); const text = await r.text(); tries.push({ path: t.path, status: r.status, body: text.slice(0, 180) }); if (r.ok) return { ok: true, keys, tries, hadCron: keys.includes('CRON_SECRET') }; } return { ok: false, keys, tries, envLen: envText.length }; }""", {"appId": APP_ID, "cronSecret": cron}, ) print( json.dumps( { "ok": result.get("ok"), "hadCron": result.get("hadCron"), "keyCount": len(result.get("keys") or []), "tries": result.get("tries"), "step": result.get("step"), "status": result.get("status"), }, indent=2, )[:2000], flush=True, ) if not result.get("ok"): browser.close() return 6 page.goto(APP, wait_until="domcontentloaded", timeout=90000) page.wait_for_timeout(700) page.get_by_role("tab", name=re.compile(r"^General$", re.I)).first.click() page.wait_for_timeout(500) page.get_by_role("button", name=re.compile(r"^Deploy$", re.I)).first.click() page.wait_for_timeout(600) conf = page.get_by_role("button", name=re.compile(r"^Confirm$", re.I)) if conf.count(): conf.first.click() print("DEPLOY_CLICKED", flush=True) for i in range(30): time.sleep(10) try: import urllib.request code = urllib.request.urlopen(HEALTH, timeout=12).getcode() except Exception as e: code = str(e) print(f"health_try{i}", code, flush=True) if code == 200 and i >= 6: # give container time to pick new env after first healthy break browser.close() return 0 if __name__ == "__main__": raise SystemExit(main())