mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-26 13:35:56 +00:00
Compare commits
10 commits
16739d3e7f
...
8ead23252c
Author | SHA1 | Date | |
---|---|---|---|
|
8ead23252c | ||
|
46d8744fa4 | ||
|
7d8dc55dbe | ||
|
64f528456d | ||
|
cd69146f33 | ||
|
ecac2917a0 | ||
|
8b82520d3d | ||
|
af230ba5fb | ||
|
c28710cdc6 | ||
|
0d328e7c8e |
7 changed files with 187 additions and 60 deletions
|
@ -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
|
||||
|
|
|
@ -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<void>}
|
||||
*/
|
||||
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<void>}
|
||||
*/
|
||||
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");
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const NotificationProvider = require("./notification-provider");
|
||||
const axios = require("axios");
|
||||
const { DOWN, UP } = require("../../src/util");
|
||||
const { Settings } = require("../settings");
|
||||
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
|
||||
|
||||
class Discord extends NotificationProvider {
|
||||
name = "discord";
|
||||
|
@ -19,8 +20,13 @@ class Discord extends NotificationProvider {
|
|||
}
|
||||
|
||||
// If heartbeatJSON is null, assume we're testing.
|
||||
|
||||
const baseURL = await Settings.get("primaryBaseURL");
|
||||
const address = this.extractAddress(monitorJSON);
|
||||
const hasAddress = address !== "" && address !== monitorJSON.hostname;
|
||||
|
||||
if (heartbeatJSON == null) {
|
||||
let discordtestdata = {
|
||||
const discordtestdata = {
|
||||
username: discordDisplayName,
|
||||
content: msg,
|
||||
};
|
||||
|
@ -33,84 +39,89 @@ class Discord extends NotificationProvider {
|
|||
return okMsg;
|
||||
}
|
||||
|
||||
const embedFields = [
|
||||
{
|
||||
name: "Service Name",
|
||||
value: monitorJSON.name,
|
||||
},
|
||||
...(hasAddress ? [{
|
||||
name: "Service URL",
|
||||
value: address
|
||||
}] : []),
|
||||
{
|
||||
name: `Time (${heartbeatJSON.timezone})`,
|
||||
value: heartbeatJSON.localDateTime,
|
||||
},
|
||||
{
|
||||
name: "Error",
|
||||
value: msg,
|
||||
},
|
||||
];
|
||||
|
||||
const components = [
|
||||
{
|
||||
type: 1, // Action Row
|
||||
components: [
|
||||
baseURL && {
|
||||
type: 2, // Button
|
||||
style: 5, // Link Button,
|
||||
label: "Visit Uptime Kuma",
|
||||
url: baseURL + getMonitorRelativeURL(monitorJSON.id)
|
||||
},
|
||||
hasAddress && {
|
||||
type: 2, // Button
|
||||
style: 5, // Link Button,
|
||||
label: "Visit Service URL",
|
||||
url: address
|
||||
}
|
||||
].filter(Boolean) // remove invalid data
|
||||
}
|
||||
];
|
||||
|
||||
// If heartbeatJSON is not null, we go into the normal alerting loop.
|
||||
if (heartbeatJSON["status"] === DOWN) {
|
||||
let discorddowndata = {
|
||||
if (heartbeatJSON.status === DOWN) {
|
||||
const discorddowndata = {
|
||||
username: discordDisplayName,
|
||||
content: notification.discordPrefixMessage || "",
|
||||
embeds: [{
|
||||
title: "❌ Your service " + monitorJSON["name"] + " went down. ❌",
|
||||
title: `❌ Your service ${monitorJSON.name} went down. ❌`,
|
||||
color: 16711680,
|
||||
timestamp: heartbeatJSON["time"],
|
||||
fields: [
|
||||
{
|
||||
name: "Service Name",
|
||||
value: monitorJSON["name"],
|
||||
},
|
||||
{
|
||||
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
|
||||
value: this.extractAddress(monitorJSON),
|
||||
},
|
||||
{
|
||||
name: `Time (${heartbeatJSON["timezone"]})`,
|
||||
value: heartbeatJSON["localDateTime"],
|
||||
},
|
||||
{
|
||||
name: "Error",
|
||||
value: heartbeatJSON["msg"] == null ? "N/A" : heartbeatJSON["msg"],
|
||||
},
|
||||
],
|
||||
timestamp: heartbeatJSON.time,
|
||||
fields: embedFields,
|
||||
}],
|
||||
components: components,
|
||||
};
|
||||
|
||||
if (notification.discordChannelType === "createNewForumPost") {
|
||||
discorddowndata.thread_name = notification.postName;
|
||||
}
|
||||
if (notification.discordPrefixMessage) {
|
||||
discorddowndata.content = notification.discordPrefixMessage;
|
||||
}
|
||||
|
||||
await axios.post(webhookUrl.toString(), discorddowndata);
|
||||
return okMsg;
|
||||
}
|
||||
|
||||
} else if (heartbeatJSON["status"] === UP) {
|
||||
let discordupdata = {
|
||||
if (heartbeatJSON.status === UP) {
|
||||
const discordupdata = {
|
||||
username: discordDisplayName,
|
||||
content: notification.discordPrefixMessage || "",
|
||||
embeds: [{
|
||||
title: "✅ Your service " + monitorJSON["name"] + " is up! ✅",
|
||||
title: `✅ Your service ${monitorJSON.name} is up! ✅`,
|
||||
color: 65280,
|
||||
timestamp: heartbeatJSON["time"],
|
||||
fields: [
|
||||
{
|
||||
name: "Service Name",
|
||||
value: monitorJSON["name"],
|
||||
},
|
||||
{
|
||||
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
|
||||
value: this.extractAddress(monitorJSON),
|
||||
},
|
||||
{
|
||||
name: `Time (${heartbeatJSON["timezone"]})`,
|
||||
value: heartbeatJSON["localDateTime"],
|
||||
},
|
||||
{
|
||||
name: "Ping",
|
||||
value: heartbeatJSON["ping"] == null ? "N/A" : heartbeatJSON["ping"] + " ms",
|
||||
},
|
||||
],
|
||||
timestamp: heartbeatJSON.time,
|
||||
fields: embedFields,
|
||||
}],
|
||||
components: components,
|
||||
};
|
||||
|
||||
if (notification.discordChannelType === "createNewForumPost") {
|
||||
discordupdata.thread_name = notification.postName;
|
||||
}
|
||||
|
||||
if (notification.discordPrefixMessage) {
|
||||
discordupdata.content = notification.discordPrefixMessage;
|
||||
}
|
||||
|
||||
await axios.post(webhookUrl.toString(), discordupdata);
|
||||
return okMsg;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
this.throwGeneralAxiosError(error);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
84
server/utils/simple-migration-server.js
Normal file
84
server/utils/simple-migration-server.js
Normal file
|
@ -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<void>}
|
||||
*/
|
||||
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<void>}
|
||||
*/
|
||||
async stop() {
|
||||
this.response?.write("Finished, please refresh this page.\n");
|
||||
this.response?.end();
|
||||
await this.server?.close();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SimpleMigrationServer,
|
||||
};
|
|
@ -4,6 +4,12 @@
|
|||
<input id="discord-webhook-url" v-model="$parent.notification.discordWebhookUrl" type="text" class="form-control" required autocomplete="false">
|
||||
<div class="form-text">
|
||||
{{ $t("wayToGetDiscordURL") }}
|
||||
<i18n-t keypath="wayToGetDiscordURLEnhanced">
|
||||
<a
|
||||
href="https://discord.com/developers/docs/resources/webhook#create-webhook"
|
||||
target="_blank"
|
||||
>{{ $t("withADiscordBot") }}</a>
|
||||
</i18n-t>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -541,6 +541,8 @@
|
|||
"smtpBCC": "BCC",
|
||||
"Discord Webhook URL": "Discord Webhook URL",
|
||||
"wayToGetDiscordURL": "You can get this by going to Server Settings -> Integrations -> View Webhooks -> New Webhook",
|
||||
"wayToGetDiscordURLEnhanced": "Using the Discord API, you may also create the webhook {0} for button support.",
|
||||
"withADiscordBot": "with a bot",
|
||||
"Bot Display Name": "Bot Display Name",
|
||||
"Prefix Custom Message": "Prefix Custom Message",
|
||||
"Hello @everyone is...": "Hello {'@'}everyone is…",
|
||||
|
|
Loading…
Add table
Reference in a new issue