From 7d8dc55dbeb08804345f4fb0917cd0b33a35d2ff Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sun, 27 Oct 2024 11:47:30 +0800 Subject: [PATCH 1/3] Fix: the rootless user put in the wrong place (#5257) --- docker/dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/dockerfile b/docker/dockerfile index 572c732e0..d55f94f61 100644 --- a/docker/dockerfile +++ b/docker/dockerfile @@ -27,7 +27,6 @@ RUN mkdir ./data # ⭐ Main Image ############################################ FROM $BASE_IMAGE AS release -USER node WORKDIR /app LABEL org.opencontainers.image.source="https://github.com/louislam/uptime-kuma" @@ -46,6 +45,7 @@ CMD ["node", "server/server.js"] # Rootless Image ############################################ FROM release AS rootless +USER node ############################################ # Mark as Nightly From 46d8744fa40cf6a5773578252af655058437b1e6 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sun, 27 Oct 2024 13:22:23 +0800 Subject: [PATCH 2/3] Fix: Docker Healthcheck is not happy during migration (#5258) --- server/database.js | 34 ++++++++-- server/server.js | 2 +- server/utils/simple-migration-server.js | 84 +++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 server/utils/simple-migration-server.js diff --git a/server/database.js b/server/database.js index 714c51ba3..75f5f25cf 100644 --- a/server/database.js +++ b/server/database.js @@ -9,6 +9,7 @@ const mysql = require("mysql2/promise"); const { Settings } = require("./settings"); const { UptimeCalculator } = require("./uptime-calculator"); const dayjs = require("dayjs"); +const { SimpleMigrationServer } = require("./utils/simple-migration-server"); /** * Database & App Data Folder @@ -382,9 +383,11 @@ class Database { /** * Patch the database + * @param {number} port Start the migration server for aggregate tables on this port if provided + * @param {string} hostname Start the migration server for aggregate tables on this hostname if provided * @returns {Promise} */ - static async patch() { + static async patch(port = undefined, hostname = undefined) { // Still need to keep this for old versions of Uptime Kuma if (Database.dbConfig.type === "sqlite") { await this.patchSqlite(); @@ -409,7 +412,7 @@ class Database { await R.exec("PRAGMA foreign_keys = ON"); } - await this.migrateAggregateTable(); + await this.migrateAggregateTable(port, hostname); } catch (e) { // Allow missing patch files for downgrade or testing pr. @@ -735,9 +738,11 @@ class Database { * Normally, it should be in transaction, but UptimeCalculator wasn't designed to be in transaction before that. * I don't want to heavily modify the UptimeCalculator, so it is not in transaction. * Run `npm run reset-migrate-aggregate-table-state` to reset, in case the migration is interrupted. + * @param {number} port Start the migration server on this port if provided + * @param {string} hostname Start the migration server on this hostname if provided * @returns {Promise} */ - static async migrateAggregateTable() { + static async migrateAggregateTable(port, hostname = undefined) { log.debug("db", "Enter Migrate Aggregate Table function"); // Add a setting for 2.0.0-dev users to skip this migration @@ -758,6 +763,18 @@ class Database { throw new Error("Aggregate table migration is already in progress"); } + /** + * Start migration server for displaying the migration status + * @type {SimpleMigrationServer} + */ + let migrationServer; + let msg; + + if (port) { + migrationServer = new SimpleMigrationServer(); + await migrationServer.start(port, hostname); + } + await Settings.set("migrateAggregateTableState", "migrating"); log.info("db", "Migrating Aggregate Table"); @@ -777,6 +794,7 @@ class Database { let count = countResult.count; if (count > 0) { log.warn("db", `Aggregate table ${table} is not empty, migration will not be started (Maybe you were using 2.0.0-dev?)`); + await migrationServer?.stop(); return; } } @@ -811,7 +829,9 @@ class Database { `, [ monitor.monitor_id, date.date ]); if (heartbeats.length > 0) { - log.info("db", `[DON'T STOP] Migrating monitor data ${monitor.monitor_id} - ${date.date} [${progressPercent.toFixed(2)}%][${i}/${monitors.length}]`); + msg = `[DON'T STOP] Migrating monitor data ${monitor.monitor_id} - ${date.date} [${progressPercent.toFixed(2)}%][${i}/${monitors.length}]`; + log.info("db", msg); + migrationServer?.update(msg); } for (let heartbeat of heartbeats) { @@ -829,9 +849,13 @@ class Database { i++; } - await Database.clearHeartbeatData(true); + msg = "Clearing non-important heartbeats"; + log.info("db", msg); + migrationServer?.update(msg); + await Database.clearHeartbeatData(true); await Settings.set("migrateAggregateTableState", "migrated"); + await migrationServer?.stop(); if (monitors.length > 0) { log.info("db", "Aggregate Table Migration Completed"); diff --git a/server/server.js b/server/server.js index 7c46fa894..ec5ad49f6 100644 --- a/server/server.js +++ b/server/server.js @@ -1716,7 +1716,7 @@ async function initDatabase(testMode = false) { log.info("server", "Connected to the database"); // Patch the database - await Database.patch(); + await Database.patch(port, hostname); let jwtSecretBean = await R.findOne("setting", " `key` = ? ", [ "jwtSecret", diff --git a/server/utils/simple-migration-server.js b/server/utils/simple-migration-server.js new file mode 100644 index 000000000..680f8df24 --- /dev/null +++ b/server/utils/simple-migration-server.js @@ -0,0 +1,84 @@ +const express = require("express"); +const http = require("node:http"); +const { log } = require("../../src/util"); + +/** + * SimpleMigrationServer + * For displaying the migration status of the server + * Also, it is used to let Docker healthcheck know the status of the server, as the main server is not started yet, healthcheck will think the server is down incorrectly. + */ +class SimpleMigrationServer { + /** + * Express app instance + * @type {?Express} + */ + app; + + /** + * Server instance + * @type {?Server} + */ + server; + + /** + * Response object + * @type {?Response} + */ + response; + + /** + * Start the server + * @param {number} port Port + * @param {string} hostname Hostname + * @returns {Promise} + */ + start(port, hostname) { + this.app = express(); + this.server = http.createServer(this.app); + + this.app.get("/", (req, res) => { + res.set("Content-Type", "text/plain"); + res.write("Migration is in progress, listening message...\n"); + if (this.response) { + this.response.write("Disconnected\n"); + this.response.end(); + } + this.response = res; + // never ending response + }); + + return new Promise((resolve) => { + this.server.listen(port, hostname, () => { + if (hostname) { + log.info("migration", `Migration server is running on http://${hostname}:${port}`); + } else { + log.info("migration", `Migration server is running on http://localhost:${port}`); + } + resolve(); + }); + }); + } + + /** + * Update the message + * @param {string} msg Message to update + * @returns {void} + */ + update(msg) { + this.response?.write(msg + "\n"); + } + + /** + * Stop the server + * @returns {Promise} + */ + async stop() { + this.response?.write("Finished, please refresh this page.\n"); + this.response?.end(); + await this.server?.close(); + } +} + +module.exports = { + SimpleMigrationServer, +}; From 277d6fe0cee9306d4e298d8ed53f5474c3682928 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sun, 27 Oct 2024 20:30:44 +0800 Subject: [PATCH 3/3] Fix #5087 and fix migration state (#5260) --- .../2024-11-27-1927-fix-info-json-data-type.js | 13 +++++++++++++ server/database.js | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 db/knex_migrations/2024-11-27-1927-fix-info-json-data-type.js diff --git a/db/knex_migrations/2024-11-27-1927-fix-info-json-data-type.js b/db/knex_migrations/2024-11-27-1927-fix-info-json-data-type.js new file mode 100644 index 000000000..0f3f75e5b --- /dev/null +++ b/db/knex_migrations/2024-11-27-1927-fix-info-json-data-type.js @@ -0,0 +1,13 @@ +// Update info_json column to LONGTEXT mainly for MariaDB +exports.up = function (knex) { + return knex.schema + .alterTable("monitor_tls_info", function (table) { + table.text("info_json", "longtext").alter(); + }); +}; + +exports.down = function (knex) { + return knex.schema.alterTable("monitor_tls_info", function (table) { + table.text("info_json", "text").alter(); + }); +}; diff --git a/server/database.js b/server/database.js index 75f5f25cf..eb4594356 100644 --- a/server/database.js +++ b/server/database.js @@ -775,8 +775,6 @@ class Database { await migrationServer.start(port, hostname); } - await Settings.set("migrateAggregateTableState", "migrating"); - log.info("db", "Migrating Aggregate Table"); log.info("db", "Getting list of unique monitors"); @@ -799,6 +797,8 @@ class Database { } } + await Settings.set("migrateAggregateTableState", "migrating"); + let progressPercent = 0; let part = 100 / monitors.length; let i = 1;