add basic update functionality

If enabled, jfa-go pings buildrone (hosted at builds.hrfee.pw) every 30
min for new updates. If there is one, it gets information (and if
applicable, a binary) from the appropriate source (buildrone, github, or
dockerhub) and displays it on the admin page. You can switch update
channels between stable and unstable. For binary releases, updates are
downloaded automatically and installed when the user presses update.

Since this obviously introduces some "phone-home" functionality into
jfa-go, I just want to say IPs are not and will not be logged by
buildrone, although I may later introduce functionality to give a rough
idea of the number of users (again, no IPs stored). The whole thing can
also be turned off in settings.
This commit is contained in:
Harvey Tindall
2021-03-07 15:23:44 +00:00
parent 9787fce275
commit 92332206f0
26 changed files with 858 additions and 50 deletions
+5 -1
View File
@@ -7,6 +7,7 @@ import { accountsList } from "./modules/accounts.js";
import { settingsList } from "./modules/settings.js";
import { ProfileEditor } from "./modules/profiles.js";
import { _get, _post, notificationBox, whichAnimationEvent, toggleLoader } from "./modules/common.js";
import { Updater } from "./modules/update.js";
loadTheme();
(document.getElementById('button-theme') as HTMLSpanElement).onclick = toggleTheme;
@@ -59,9 +60,12 @@ window.availableProfiles = window.availableProfiles || [];
window.modals.customizeEmails = new Modal(document.getElementById("modal-customize"));
window.modals.extendExpiry = new Modal(document.getElementById("modal-extend-expiry"));
window.modals.updateInfo = new Modal(document.getElementById("modal-update"));
})();
var inviteCreator = new createInvite();
var accounts = new accountsList();
window.invites = new inviteList();
@@ -154,6 +158,7 @@ function login(username: string, password: string, run?: (state?: number) => voi
} else {
const data = this.response;
window.token = data["token"];
window.updater = new Updater(); // mmm, a race condition
window.modals.login.close();
setInterval(() => { window.invites.reload(); accounts.reload(); }, 30*1000);
const currentTab = window.tabs.current;
@@ -198,4 +203,3 @@ login("", "");
return false;
}
});
+8
View File
@@ -598,6 +598,14 @@ export class settingsList {
`;
(editButton.querySelector("span.button") as HTMLSpanElement).onclick = this._emailEditor.showList;
this.addSection(name, settings.sections[name], editButton);
} else if (name == "updates") {
const icon = document.createElement("span") as HTMLSpanElement;
if (window.updater.updateAvailable) {
icon.classList.add("button", "~urge");
icon.innerHTML = `<i class="ri-download-line" title="${window.lang.strings("update")}"></i>`;
icon.onclick = () => window.updater.checkForUpdates(window.modals.updateInfo.show);
}
this.addSection(name, settings.sections[name], icon);
} else {
this.addSection(name, settings.sections[name]);
}
+124
View File
@@ -0,0 +1,124 @@
import { _get, _post, toggleLoader } from "../modules/common.js";
import { Marked, Renderer } from "@ts-stack/markdown";
interface updateDTO {
new: boolean;
update: Update;
}
export class Updater implements updater {
private _update: Update;
private _date: Date;
updateAvailable = false;
checkForUpdates = (run?: (req: XMLHttpRequest) => void) => _get("/config/update", null, (req: XMLHttpRequest) => {
if (req.readyState == 4) {
if (req.status != 200) {
window.notifications.customError("errorCheckUpdate", window.lang.notif("errorCheckUpdate"));
return
}
let resp = req.response as updateDTO;
if (resp.new) {
this.update = resp.update;
if (run) { run(req); }
// } else {
// window.notifications.customPositive("noUpdatesAvailable", "", window.lang.notif("noUpdatesAvailable"));
}
}
});
get date(): number { return Math.floor(this._date.getTime() / 1000); }
set date(unix: number) {
this._date = new Date(unix * 1000);
document.getElementById("update-date").textContent = this._date.toDateString() + " " + this._date.toLocaleTimeString();
}
get description(): string { return this._update.description; }
set description(description: string) {
this._update.description = description;
const el = document.getElementById("update-description") as HTMLParagraphElement;
el.textContent = description;
if (this.version == "git") {
el.classList.add("monospace");
} else {
el.classList.remove("monospace");
}
}
get changelog(): string { return this._update.changelog; }
set changelog(changelog: string) {
this._update.changelog = changelog;
document.getElementById("update-changelog").innerHTML = Marked.parse(changelog);
}
get version(): string { return this._update.version; }
set version(version: string) {
this._update.version = version;
document.getElementById("update-version").textContent = version;
}
get commit(): string { return this._update.commit; }
set commit(commit: string) {
this._update.commit = commit;
document.getElementById("update-commit").textContent = commit.slice(0, 7);
}
get link(): string { return this._update.link; }
set link(link: string) {
this._update.link = link;
(document.getElementById("update-version") as HTMLAnchorElement).href = link;
}
get download_link(): string { return this._update.download_link; }
set download_link(link: string) { this._update.download_link = link; }
get can_update(): boolean { return this._update.can_update; }
set can_update(can: boolean) {
this._update.can_update = can;
const download = document.getElementById("update-download") as HTMLSpanElement;
const update = document.getElementById("update-update") as HTMLSpanElement;
if (can) {
download.classList.add("unfocused");
update.classList.remove("unfocused");
} else {
download.onclick = () => window.open(this._update.download_link || this._update.link);
download.classList.remove("unfocused");
update.classList.add("unfocused");
}
}
get update(): Update { return this._update; }
set update(update: Update) {
this._update = update;
this.version = update.version;
this.commit = update.commit;
this.date = update.date;
this.description = update.description;
this.changelog = update.changelog;
this.link = update.link;
this.download_link = update.download_link;
this.can_update = update.can_update;
}
constructor() {
const update = document.getElementById("update-update") as HTMLSpanElement;
update.onclick = () => {
toggleLoader(update);
_post("/config/update", null, (req: XMLHttpRequest) => {
if (req.readyState == 4) {
toggleLoader(update);
if (req.status != 200) {
window.notifications.customError("applyUpdateError", window.lang.notif("errorApplyUpdate"));
} else {
window.notifications.customSuccess("applyUpdate", window.lang.notif("updateApplied"));
}
window.modals.updateInfo.close();
}
});
};
this.checkForUpdates(() => {
this.updateAvailable = true;
window.notifications.customPositive("updateAvailable", "", window.lang.notif("updateAvailable"));
});
}
}
+19
View File
@@ -30,6 +30,24 @@ declare interface Window {
language: string;
lang: Lang;
langFile: {};
updater: updater;
}
declare interface Update {
version: string;
commit: string;
date: number;
description: string;
changelog: string;
link: string;
download_link?: string;
can_update: boolean;
}
declare interface updater extends Update {
checkForUpdates: (run?: (req: XMLHttpRequest) => void) => void;
updateAvailable: boolean;
update: Update;
}
declare interface Lang {
@@ -78,6 +96,7 @@ declare interface Modals {
editor: Modal;
customizeEmails: Modal;
extendExpiry: Modal;
updateInfo: Modal;
}
interface Invite {