Show progress for uploads and updates, and reload when they are done
An uploaded bundle gets a progress bar over the page; a running update an overlay that follows its steps. A page that finished working, or that was sent to the login page by a restart, is loaded whole. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
This commit is contained in:
@@ -14,12 +14,17 @@
|
||||
setTimeout(function () {
|
||||
fetch(location.pathname + location.search,{ credentials: "same-origin", headers: { "X-Live": "1" } })
|
||||
.then(function (r) {
|
||||
if (r.redirected || !r.ok) throw new Error("not the page");
|
||||
// Sent elsewhere: the web UI restarted and forgot the session. Show where it leads.
|
||||
if (r.redirected) { location.reload(); return null; }
|
||||
if (!r.ok) throw new Error("not the page");
|
||||
return r.text();
|
||||
})
|
||||
.then(function (html) {
|
||||
if (html === null) return;
|
||||
var next = new DOMParser().parseFromString(html, "text/html").getElementById("live");
|
||||
if (!next) throw new Error("no content");
|
||||
// Done: load the page whole, as it is when nothing is going on.
|
||||
if (!next.getAttribute("data-live")) { location.reload(); return; }
|
||||
var old = live.querySelector("pre.log");
|
||||
var atEnd = !old || old.scrollTop + old.clientHeight >= old.scrollHeight - 8;
|
||||
var kept = old ? old.scrollTop : 0;
|
||||
|
||||
@@ -58,6 +58,11 @@ h1 { font-size: 1.45rem; font-weight: 600; letter-spacing: -.01em; margin: .25re
|
||||
h2 { font-size: .95rem; font-weight: 600; margin: 0 0 .6rem; }
|
||||
section { margin: 0 0 1.5rem; }
|
||||
section.busy, section.pending { padding: .75rem 1rem; border-radius: var(--radius); background: var(--warn); }
|
||||
.upload-overlay { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, .55); padding: 1rem; }
|
||||
.upload-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 32px rgba(0, 0, 0, .35); padding: 1.25rem 1.5rem; width: min(30rem, 100%); }
|
||||
.upload-card h2 { margin: 0 0 .75rem; }
|
||||
.upload-card progress { width: 100%; height: 1rem; accent-color: var(--accent); }
|
||||
.upload-card p { margin: .6rem 0 0; font-size: .9rem; }
|
||||
.flash, .error { padding: .6rem .9rem; border-radius: var(--radius); margin: 0 0 1rem; }
|
||||
.flash { background: var(--ok); }
|
||||
.error { background: color-mix(in srgb, var(--bad) 12%, transparent); }
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// A form marked data-upload is sent with a progress bar over the page: a
|
||||
// bundle is hundreds of megabytes. The server answers with a redirect once it
|
||||
// has taken the file; the browser follows it and we go to where it ended up.
|
||||
(function () {
|
||||
function size(n) {
|
||||
return n >= 1 << 30 ? (n / (1 << 30)).toFixed(2) + " GiB" : (n / (1 << 20)).toFixed(1) + " MiB";
|
||||
}
|
||||
|
||||
function overlay() {
|
||||
var box = document.createElement("div");
|
||||
box.className = "upload-overlay";
|
||||
box.setAttribute("role", "dialog");
|
||||
box.setAttribute("aria-modal", "true");
|
||||
var card = document.createElement("div");
|
||||
card.className = "upload-card";
|
||||
var title = document.createElement("h2");
|
||||
var bar = document.createElement("progress");
|
||||
bar.max = 100;
|
||||
var text = document.createElement("p");
|
||||
text.className = "muted";
|
||||
card.append(title, bar, text);
|
||||
box.append(card);
|
||||
document.body.append(box);
|
||||
return { box: box, title: title, bar: bar, text: text };
|
||||
}
|
||||
|
||||
document.addEventListener("submit", function (e) {
|
||||
var form = e.target;
|
||||
if (!form.hasAttribute("data-upload") || e.defaultPrevented || !window.XMLHttpRequest || !window.FormData) return;
|
||||
e.preventDefault();
|
||||
var ui = overlay();
|
||||
var started = Date.now();
|
||||
ui.title.textContent = "Uploading…";
|
||||
ui.bar.value = 0;
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", form.action);
|
||||
xhr.upload.onprogress = function (ev) {
|
||||
if (!ev.lengthComputable) {
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = size(ev.loaded) + " sent";
|
||||
return;
|
||||
}
|
||||
var secs = Math.max((Date.now() - started) / 1000, 0.5);
|
||||
var rate = ev.loaded / secs;
|
||||
ui.bar.value = (ev.loaded / ev.total) * 100;
|
||||
ui.text.textContent = size(ev.loaded) + " of " + size(ev.total) + " (" + Math.floor((ev.loaded / ev.total) * 100) + " %), " +
|
||||
size(rate) + "/s";
|
||||
};
|
||||
xhr.upload.onload = function () {
|
||||
// All sent; the router checks and unpacks it before it answers.
|
||||
ui.title.textContent = "Verifying…";
|
||||
ui.bar.removeAttribute("value");
|
||||
ui.text.textContent = "The router is checking the signature and unpacking the bundle.";
|
||||
};
|
||||
function failed(msg) {
|
||||
ui.box.remove();
|
||||
alert(msg);
|
||||
}
|
||||
xhr.onerror = function () { failed("The upload failed: the router could not be reached."); };
|
||||
xhr.onabort = function () { failed("The upload was cancelled."); };
|
||||
xhr.onload = function () {
|
||||
// The answer is the page the server redirected to (with its message).
|
||||
if (xhr.responseURL && xhr.responseURL !== location.href) location.href = xhr.responseURL;
|
||||
else location.reload();
|
||||
};
|
||||
xhr.send(new FormData(form));
|
||||
});
|
||||
})();
|
||||
@@ -44,5 +44,6 @@
|
||||
</main>
|
||||
<script src="/static/menu.js"></script>
|
||||
<script src="/static/live.js"></script>
|
||||
<script src="/static/upload.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -10,8 +10,15 @@
|
||||
</section>
|
||||
|
||||
{{if $d.Status.Running}}
|
||||
<section class="busy"><h2>Updating…</h2><p>Verifying, importing and building the new version. This page refreshes on its own;
|
||||
the web UI restarts when the new version is switched on.</p></section>
|
||||
<div class="upload-overlay" role="dialog" aria-modal="true">
|
||||
<div class="upload-card">
|
||||
<h2>Updating…</h2>
|
||||
<progress max="100" value="{{$d.Progress}}"></progress>
|
||||
<p>{{$d.Step}}</p>
|
||||
<p class="muted">This takes a few minutes. The page reloads when it is done; the web UI restarts when the new
|
||||
version is switched on, and you log in again.</p>
|
||||
</div>
|
||||
</div>
|
||||
{{else if $d.Pending}}
|
||||
<section class="pending"><h2>Waiting for confirmation</h2>
|
||||
<p>A new configuration is active and not confirmed yet. Confirm it on the <a href="/">Overview</a>, or the router
|
||||
@@ -23,7 +30,7 @@
|
||||
<p>A <code>.bundle</code> file made with <code>nix run .#release</code>. It is installed only if it is signed with
|
||||
this router's update key. The settings are kept (and migrated if the new version needs it); the new version
|
||||
must be confirmed afterwards, or the router goes back.</p>
|
||||
<form method="post" action="/update/upload?csrf={{.CSRF}}" enctype="multipart/form-data">
|
||||
<form method="post" action="/update/upload?csrf={{.CSRF}}" enctype="multipart/form-data" data-upload>
|
||||
<p><label><input type="checkbox" name="force"> also install a version that is not newer</label></p>
|
||||
<p><input type="file" name="bundle" required> <button>Upload and install</button></p>
|
||||
</form>
|
||||
|
||||
@@ -242,8 +242,34 @@ func unitRun(unit string) UpdateStatus {
|
||||
|
||||
func (s System) UpdateStatus() UpdateStatus { return unitRun("router-update.service") }
|
||||
|
||||
// updateProgress is how far router-update has come, from the last step its log
|
||||
// names: a share of the whole (the build at the end takes the longest, so the
|
||||
// bar is only a guide) and what it is doing.
|
||||
func updateProgress(log string) (int, string) {
|
||||
steps := []struct {
|
||||
marker string
|
||||
pct int
|
||||
what string
|
||||
}{
|
||||
{"router-update: verifying", 10, "Verifying the signature"},
|
||||
{"router-update: importing", 30, "Importing the release"},
|
||||
{"router-update: installed", 50, "Checking the version"},
|
||||
{"router-update: building and activating", 70, "Building and activating the new version"},
|
||||
}
|
||||
pct, what := 5, "Starting"
|
||||
best := -1
|
||||
for _, s := range steps {
|
||||
if i := strings.LastIndex(log, s.marker); i > best {
|
||||
best, pct, what = i, s.pct, s.what
|
||||
}
|
||||
}
|
||||
return pct, what
|
||||
}
|
||||
|
||||
type updateData struct {
|
||||
Installed Release
|
||||
Progress int // percent, while updating
|
||||
Step string // what it does now
|
||||
Status UpdateStatus
|
||||
URL string
|
||||
Offer *Available
|
||||
@@ -253,12 +279,14 @@ type updateData struct {
|
||||
|
||||
func (a *app) updateData() updateData {
|
||||
st := a.sys.Status()
|
||||
return updateData{
|
||||
d := updateData{
|
||||
Installed: a.sys.installedRelease(),
|
||||
Status: a.sys.UpdateStatus(),
|
||||
URL: a.sys.updateURL(),
|
||||
Pending: st.RollbackIn > 0,
|
||||
}
|
||||
d.Progress, d.Step = updateProgress(d.Status.Log)
|
||||
return d
|
||||
}
|
||||
|
||||
func (a *app) updatePage(w http.ResponseWriter, r *http.Request, sess session) {
|
||||
|
||||
@@ -244,3 +244,22 @@ func TestUpdateUploadAndInstall(t *testing.T) {
|
||||
t.Fatalf("the page lacks the saved server:\n%s", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProgress(t *testing.T) {
|
||||
log := ""
|
||||
last := 0
|
||||
for _, step := range []struct{ line, what string }{
|
||||
{"", "Starting"},
|
||||
{"router-update: verifying /nix/store/x-release\n", "Verifying the signature"},
|
||||
{"router-update: importing it\n", "Importing the release"},
|
||||
{"router-update: installed 0.1.0, update 0.2.0\n", "Checking the version"},
|
||||
{"router-update: building and activating 0.2.0\nbuilding the system\n", "Building and activating the new version"},
|
||||
} {
|
||||
log += step.line
|
||||
pct, what := updateProgress(log)
|
||||
if what != step.what || pct <= last {
|
||||
t.Fatalf("after %q: %d%% %q (before: %d%%)", step.line, pct, what, last)
|
||||
}
|
||||
last = pct
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user