Files
gitea-k3s-deployer/internal/server/web/index.html
T
2026-11-17 21:11:30 +00:00

266 lines
11 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gitea -> K3s Deployer</title>
<style>
:root {
--bg: #0f1419;
--surface: #161d24;
--border: #28333e;
--text: #e6edf3;
--muted: #8b98a5;
--accent: #3b82f6;
--green: #22c55e;
--red: #ef4444;
--amber: #f59e0b;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.45;
}
header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 16px 24px;
background: var(--surface);
border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 1.1rem; }
.pill {
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 600;
border: 1px solid var(--border);
background: rgba(59, 130, 246, 0.12);
color: #93c5fd;
}
.pill.ok { background: rgba(34, 197, 94, 0.14); color: #86efac; border-color: rgba(34,197,94,0.3); }
.pill.err { background: rgba(239, 68, 68, 0.14); color: #fca5a5; border-color: rgba(239,68,68,0.3); }
.pill.warn { background: rgba(245, 158, 11, 0.14); color: #fcd34d; border-color: rgba(245,158,11,0.3); }
main { max-width: 1200px; margin: 0 auto; padding: 20px; display: grid; gap: 20px; }
section {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 16px 20px;
overflow-x: auto;
}
h2 { margin: 0 0 12px; font-size: 1rem; display: flex; align-items: center; justify-content: space-between; }
form.deploy-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
input, button {
font: inherit;
padding: 8px 12px;
border-radius: 6px;
border: 1px solid var(--border);
background: var(--bg);
color: var(--text);
}
input { min-width: 180px; flex: 1; }
button {
background: var(--accent);
border-color: var(--accent);
color: #fff;
font-weight: 600;
cursor: pointer;
}
button.secondary { background: transparent; border-color: var(--border); color: var(--text); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.82rem; color: #cbd5e1; }
a { color: #60a5fa; text-decoration: none; }
a:hover { text-decoration: underline; }
.logs {
margin-top: 8px;
padding: 10px;
background: #0b0f14;
border: 1px solid var(--border);
border-radius: 6px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.78rem;
white-space: pre-wrap;
color: #cbd5e1;
max-height: 220px;
overflow-y: auto;
}
.muted { color: var(--muted); font-size: 0.82rem; }
#toast { font-size: 0.85rem; margin-top: 8px; }
</style>
</head>
<body>
<header>
<div class="brand">
<span>Gitea &rarr; K3s Deployer</span>
<span id="kubeMode" class="pill">loading...</span>
</div>
<div class="muted">
<span id="giteaUrl"></span> &middot;
<button class="secondary" onclick="refreshAll()">Refresh</button>
</div>
</header>
<main>
<section>
<h2>Manual Deploy</h2>
<form class="deploy-form" onsubmit="triggerDeploy(event)">
<input id="repoInput" placeholder="owner/repo (e.g. experiments/my-app)" required>
<input id="refInput" placeholder="branch or tag (default: main)" value="main">
<input id="tagInput" placeholder="image tag override (optional)">
<button id="deployBtn" type="submit">Deploy to K3s</button>
</form>
<div id="toast" class="muted"></div>
</section>
<section>
<h2>Apps <span id="appsCount" class="pill">0</span></h2>
<table>
<thead>
<tr><th>App</th><th>Repo</th><th>Namespace</th><th>Host</th><th>Last Ref</th><th>Status</th><th>Action</th></tr>
</thead>
<tbody id="appsBody"><tr><td colspan="7" class="muted">No apps deployed yet.</td></tr></tbody>
</table>
</section>
<section>
<h2>Recent Deployments <span id="depsCount" class="pill">0</span></h2>
<table>
<thead>
<tr><th>ID</th><th>App / Repo</th><th>Ref / Tag</th><th>Trigger</th><th>Status</th><th>Time</th><th>Actions</th></tr>
</thead>
<tbody id="depsBody"><tr><td colspan="7" class="muted">No deployments yet.</td></tr></tbody>
</table>
</section>
<section>
<h2>Recent Gitea Webhooks <span id="hooksCount" class="pill">0</span></h2>
<table>
<thead>
<tr><th>Event</th><th>Repo</th><th>Ref</th><th>Sig</th><th>Accepted</th><th>Message</th><th>Time</th></tr>
</thead>
<tbody id="hooksBody"><tr><td colspan="7" class="muted">No webhooks received yet.</td></tr></tbody>
</table>
</section>
</main>
<script>
function statusPill(s) {
if (s === 'succeeded' || s === true) return `<span class="pill ok">${s}</span>`;
if (s === 'failed' || s === false) return `<span class="pill err">${s}</span>`;
return `<span class="pill warn">${s || 'unknown'}</span>`;
}
function esc(str) {
return String(str ?? '').replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
}
async function refreshAll() {
const [statusRes, appsRes, depsRes, hooksRes] = await Promise.all([
fetch('/healthz').then(r => r.json()),
fetch('/api/apps').then(r => r.json()),
fetch('/api/deployments?limit=25').then(r => r.json()),
fetch('/api/webhooks?limit=25').then(r => r.json()),
]);
document.getElementById('kubeMode').textContent = 'kube: ' + (statusRes.kube_mode || 'unknown');
document.getElementById('giteaUrl').textContent = statusRes.gitea_url || '';
const apps = appsRes.apps || [];
document.getElementById('appsCount').textContent = apps.length;
document.getElementById('appsBody').innerHTML = apps.length ? apps.map(a => `
<tr>
<td><strong>${esc(a.name)}</strong></td>
<td><code>${esc(a.repo)}</code></td>
<td><code>${esc(a.namespace)}</code></td>
<td>${a.host ? `<a href="https://${esc(a.host)}" target="_blank">${esc(a.host)}</a>` : '<span class="muted">-</span>'}</td>
<td><code>${esc(a.last_ref || a.branch)}</code></td>
<td>${statusPill(a.last_status)}</td>
<td><button class="secondary" onclick="quickDeploy('${esc(a.repo)}','${esc(a.last_ref || a.branch || 'main')}')">Redeploy</button></td>
</tr>`).join('') : '<tr><td colspan="7" class="muted">No apps deployed yet.</td></tr>';
const deps = depsRes.deployments || [];
document.getElementById('depsCount').textContent = deps.length;
document.getElementById('depsBody').innerHTML = deps.length ? deps.map(d => `
<tr>
<td><code>${esc(d.id)}</code></td>
<td><strong>${esc(d.app_name)}</strong><br><span class="muted">${esc(d.repo)}</span></td>
<td><code>${esc(d.ref)}</code>${d.image_tag ? ` <span class="muted">(${esc(d.image_tag)})</span>` : ''}</td>
<td><code>${esc(d.trigger)}</code></td>
<td>${statusPill(d.status)}</td>
<td class="muted">${esc(new Date(d.started_at).toLocaleString())}</td>
<td>
<button class="secondary" onclick="toggleLogs('log-${esc(d.id)}')">Logs</button>
<button class="secondary" onclick="rollback('${esc(d.id)}')">Rollback</button>
<div id="log-${esc(d.id)}" class="logs" style="display:none">${esc((d.logs || []).join('\n'))}</div>
</td>
</tr>`).join('') : '<tr><td colspan="7" class="muted">No deployments yet.</td></tr>';
const hooks = hooksRes.webhooks || [];
document.getElementById('hooksCount').textContent = hooks.length;
document.getElementById('hooksBody').innerHTML = hooks.length ? hooks.map(h => `
<tr>
<td><code>${esc(h.event)}</code></td>
<td><code>${esc(h.repo)}</code></td>
<td><code>${esc(h.ref)}</code></td>
<td>${statusPill(h.signature_ok)}</td>
<td>${statusPill(h.accepted)}</td>
<td>${esc(h.message)}</td>
<td class="muted">${esc(new Date(h.received_at).toLocaleString())}</td>
</tr>`).join('') : '<tr><td colspan="7" class="muted">No webhooks received yet.</td></tr>';
}
function toggleLogs(id) {
const el = document.getElementById(id);
if (el) el.style.display = el.style.display === 'none' ? 'block' : 'none';
}
async function triggerDeploy(e) {
e.preventDefault();
const repo = document.getElementById('repoInput').value.trim();
const ref = document.getElementById('refInput').value.trim() || 'main';
const image_tag = document.getElementById('tagInput').value.trim();
await runDeploy({ repo, ref, image_tag });
}
async function quickDeploy(repo, ref) {
await runDeploy({ repo, ref });
}
async function runDeploy(payload) {
const toast = document.getElementById('toast');
toast.textContent = `Deploying ${payload.repo}@${payload.ref}...`;
const res = await fetch('/api/deploy', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await res.json();
toast.textContent = res.ok ? `Deployment ${data.id}: ${data.status}` : `Error: ${data.error || res.status}`;
await refreshAll();
}
async function rollback(id) {
const toast = document.getElementById('toast');
toast.textContent = `Rolling back to ${id}...`;
const res = await fetch(`/api/deployments/${encodeURIComponent(id)}/rollback`, { method: 'POST' });
const data = await res.json();
toast.textContent = res.ok ? `Rollback ${data.id}: ${data.status}` : `Error: ${data.error || res.status}`;
await refreshAll();
}
refreshAll();
setInterval(refreshAll, 10000);
</script>
</body>
</html>