Asking for a cluster and building it were two clicks on two pages, with the record sitting at "requested" in between. Nobody wants the record; they want the cluster. Creating it now starts the build and opens the log. A portal with no provisioner still just records the request - that is all it can do - and lands on the list instead, and a build that fails to start does the same rather than showing an error page about a cluster that is nonetheless there. The log used to be a text endpoint you reloaded, which is the same information by a worse route: the interesting moment is usually the one you reloaded past. /clusters/watch follows it as it is written and stops on its own when the cluster stops moving. It polls rather than streams. A build is one writer appending to one file, and asking for the bytes after the ones you already have is the whole of what server-sent events would give here, without a long-lived connection to keep alive through Caddy and the proxy. ?from=N is what makes that cheap: a Terraform log reaches a few hundred kilobytes, and re-sending all of it every two seconds to append one line is most of a megabyte a minute for nothing. An offset past the end means a rebuild truncated the file, so the reader starts again rather than waiting for bytes that will never come. X-Cluster-State is read from the record, not guessed from the log's last line: a build that was killed writes no last line at all. The script backs off to five seconds while nothing is happening, and keeps the view pinned to the bottom only while the reader is already there - scrolling up to read something is deliberate, and yanking the view back down two seconds later undoes it. The script is a file rather than an inline block because the control plane's Content-Security-Policy is script-src 'self', and loosening that for a log viewer would be the wrong trade. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
// Follows a build log by asking for the bytes it does not have yet.
|
|
//
|
|
// The page carries the cluster id and whether the build was still moving
|
|
// when it was rendered; everything after that comes from the endpoint,
|
|
// including when to stop.
|
|
(function () {
|
|
"use strict";
|
|
var out = document.getElementById("log");
|
|
var status = document.getElementById("status");
|
|
var box = document.getElementById("logbox");
|
|
var id = out.dataset.cluster;
|
|
var at = 0;
|
|
var idle = 0;
|
|
|
|
// Stay pinned to the bottom only while the reader is already there.
|
|
// Scrolling up to read something is a deliberate act, and yanking the
|
|
// view back down two seconds later undoes it.
|
|
function atBottom() {
|
|
return box.scrollHeight - box.scrollTop - box.clientHeight < 40;
|
|
}
|
|
|
|
function setStatus(text, cls) {
|
|
status.textContent = text;
|
|
status.className = "pill " + cls;
|
|
}
|
|
|
|
function poll() {
|
|
var stick = atBottom();
|
|
fetch("/clusters/log?cluster=" + encodeURIComponent(id) + "&from=" + at, {
|
|
credentials: "same-origin",
|
|
})
|
|
.then(function (r) {
|
|
if (!r.ok) throw new Error("HTTP " + r.status);
|
|
var state = r.headers.get("X-Cluster-State") || "";
|
|
var size = parseInt(r.headers.get("X-Log-Size") || "0", 10);
|
|
return r.text().then(function (body) {
|
|
return { state: state, size: size, body: body };
|
|
});
|
|
})
|
|
.then(function (d) {
|
|
if (d.body) {
|
|
out.appendChild(document.createTextNode(d.body));
|
|
if (stick) box.scrollTop = box.scrollHeight;
|
|
idle = 0;
|
|
} else {
|
|
idle++;
|
|
}
|
|
at = d.size;
|
|
|
|
if (d.state === "provisioning") {
|
|
setStatus("building", "warn");
|
|
} else if (d.state === "deleting") {
|
|
setStatus("releasing", "warn");
|
|
} else if (d.state === "ready") {
|
|
setStatus("ready", "ok");
|
|
return; // stop: nothing more will be written
|
|
} else if (d.state === "failed") {
|
|
setStatus("failed", "crit");
|
|
return;
|
|
} else {
|
|
setStatus(d.state || "unknown", "muted");
|
|
return;
|
|
}
|
|
// Back off while nothing is happening, so a build that spends
|
|
// four minutes inside one Terraform step is not asked 120 times.
|
|
setTimeout(poll, idle > 5 ? 5000 : 2000);
|
|
})
|
|
.catch(function (e) {
|
|
setStatus("lost: " + e.message, "crit");
|
|
setTimeout(poll, 5000);
|
|
});
|
|
}
|
|
|
|
poll();
|
|
})();
|