1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-26 09:37:28 +00:00

Add updater page, disabled by default

This commit is contained in:
Cadence Fish 2020-02-22 23:44:17 +13:00
parent aac358cd65
commit d6ee6c0411
No known key found for this signature in database
GPG Key ID: 81015DF9AA8607E1
4 changed files with 123 additions and 1 deletions

View File

@ -25,13 +25,15 @@ let constants = {
settings: { settings: {
rss_enabled: true, rss_enabled: true,
display_feed_validation_buttons: false display_feed_validation_buttons: false,
enable_updater_page: false
}, },
caching: { caching: {
image_cache_control: `public, max-age=${7*24*60*60}`, image_cache_control: `public, max-age=${7*24*60*60}`,
resource_cache_time: 30*60*1000, resource_cache_time: 30*60*1000,
instance_list_cache_time: 3*60*1000, instance_list_cache_time: 3*60*1000,
updater_cache_time: 2*60*1000,
db_user_id: true, db_user_id: true,
db_post_n3: true db_post_n3: true
}, },

62
src/site/api/admin.js Normal file
View File

@ -0,0 +1,62 @@
const child_process = require("child_process")
const constants = require("../../lib/constants")
const {render} = require("pinski/plugins")
function run(command) {
return new Promise((resolve, reject) => {
child_process.exec(command, {encoding: "utf8"}, (error, stdout, stderr) => {
if (error) reject(error)
else resolve({stdout, stderr})
})
})
}
let lastFetchRunAt = 0
async function fetch() {
if (lastFetchRunAt + constants.caching.updater_cache_time < Date.now()) {
await run("git fetch origin")
lastFetchRunAt = Date.now()
}
}
module.exports = [
{route: "/admin/updater", methods: ["GET"], code: async () => {
if (constants.settings.enable_updater_page) {
let {stdout: current} = await run("git rev-parse --abbrev-ref HEAD")
current = current.trim()
let {stdout: upstream} = await run("git rev-parse --abbrev-ref '@{u}'")
upstream = upstream.trim()
if (current !== "master" || upstream !== "origin/master") {
return render(200, "pug/admin_update.pug", {error: `Refusing to automatically update: current is ${current}, upstream is ${upstream}, need master and origin/master`})
} else {
await fetch()
let {stdout: log} = await run("git log --oneline origin/master ^master")
log = log.trim()
const numberOfCommits = log === "" ? 0 : log.split("\n").length
const {stdout: changedFiles} = await run("git diff master origin/master --name-only")
const changedFilesList = changedFiles.trim().split("\n")
let requiresRestart = false
let requiresDeps = false
if (changedFilesList.some(c => c.startsWith("src/lib") || c === "server.js")) {
requiresRestart = true
}
if (changedFilesList.some(c => c === "package.json" || c === "package-lock.json")) {
requiresDeps = true
requiresRestart = true
}
const formattedLog = log.split("\n").map(line => {
if (!line) return line // skip for empty string
const [hash, message] = line.match(/(\w+) (.*)$/).slice(1)
return {hash, message}
})
return render(200, "pug/admin_update.pug", {formattedLog, numberOfCommits, requiresDeps, requiresRestart})
}
} else {
return render(403, "pug/friendlyerror.pug", {
statusCode: 403,
title: "Updater page disabled",
message: "Updater page disabled"
})
}
}}
]

View File

@ -0,0 +1,32 @@
//- Needs {error} or {formattedLog, numberOfCommits, requiresDeps, requiresRestart}
doctype html
html
head
title Updater | Bibliogram
include includes/head
body.updater-page
if error
h1 Aw man.
div= error
else
h1 Bibliogram updater
if numberOfCommits > 0
div You are #{numberOfCommits} commits behind (most recent first):
div.commits
each line in formattedLog
div.commit
code.hash= line.hash
span.message= line.message
div
if requiresDeps
span.attention You must run #[code npm install] after pulling.
else
span No changes to dependencies.
div
if requiresRestart
span.attention You must restart Bibliogram after pulling.
else
span You do not need to restart Bibliogram after pulling.
else
div You are running the latest version. Hurrah!

View File

@ -518,3 +518,29 @@ body
border: 2px solid #aaa border: 2px solid #aaa
font-weight: bold font-weight: bold
background-color: #282828 background-color: #282828
.updater-page
font-size: 20px
max-width: 800px
margin: 0 auto
color: #222
code
font-size: 0.9em
background: #ccc
color: #000
padding: 0px 4px
border-radius: 2px
.commits
border-left: 2px solid #555
margin: 10px 0px
padding-left: 10px
.hash
margin-right: 6px
.attention
border-left: 2px solid
padding-left: 8px
color: #700