From d286c534bd0d3cf07135d82b227e223ccc777eea Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 30 Jun 2023 22:17:07 +0800 Subject: [PATCH] Improve the setup database for embedded MariaDB --- docker/debian-base.dockerfile | 3 +- docker/docker-compose-dev.yml | 2 +- package.json | 6 +- server/server.js | 12 ++- server/setup-database.js | 38 ++++++- src/pages/Setup.vue | 2 + src/pages/SetupDatabase.vue | 188 +++++++++++++++++++--------------- 7 files changed, 160 insertions(+), 91 deletions(-) diff --git a/docker/debian-base.dockerfile b/docker/debian-base.dockerfile index cde5cd2fe..f84aa32a6 100644 --- a/docker/debian-base.dockerfile +++ b/docker/debian-base.dockerfile @@ -23,6 +23,7 @@ RUN curl https://pkg.cloudflare.com/cloudflare-main.gpg --output /usr/share/keyr # Not working for armv7, so use the older version (10.5) of MariaDB from the debian repo # curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | bash -s -- --mariadb-server-version="mariadb-11.1" && \ FROM base2-slim AS base2 +ENV UPTIME_KUMA_ENABLE_EMBEDDED_MARIADB=1 RUN apt update && \ apt --yes --no-install-recommends install chromium fonts-indic fonts-noto fonts-noto-cjk mariadb-server && \ apt --yes remove curl && \ @@ -30,4 +31,4 @@ RUN apt update && \ apt --yes autoremove && \ chown -R node:node /var/lib/mysql -ENV UPTIME_KUMA_ENABLE_EMBEDDED_MARIADB=1 + diff --git a/docker/docker-compose-dev.yml b/docker/docker-compose-dev.yml index 5510b0d80..6eeb91e65 100644 --- a/docker/docker-compose-dev.yml +++ b/docker/docker-compose-dev.yml @@ -5,7 +5,7 @@ services: container_name: uptime-kuma-dev image: louislam/uptime-kuma:nightly2 volumes: - - ./data:/app/data + #- ./data:/app/data - ../server:/app/server ports: - "3001:3001" # : diff --git a/package.json b/package.json index 3ebb62500..9913a480c 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "build-docker-builder-go": "docker buildx build -f docker/builder-go.dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:builder-go . --push", "build-docker-slim": "node ./extra/env2arg.js docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:2-slim -t louislam/uptime-kuma:$VERSION-slim --target release --build-arg BASE_IMAGE=louislam/uptime-kuma:base2-slim . --push", "build-docker-full": "node ./extra/env2arg.js docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:2 -t louislam/uptime-kuma:$VERSION --target release . --push", - "build-docker-nightly": "npm run build && docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:nightly2 --target nightly --build-arg . --push", - "build-docker-nightly-local": "docker build -f docker/dockerfile -t louislam/uptime-kuma:nightly2 --target nightly .", + "build-docker-nightly": "npm run build && docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:nightly2 --target nightly . --push", + "build-docker-nightly-local": "npm run build && docker build -f docker/dockerfile -t louislam/uptime-kuma:nightly2 --target nightly .", "build-docker-pr-test": "docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64 -t louislam/uptime-kuma:pr-test --target pr-test . --push", "upload-artifacts": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:upload-artifact --build-arg VERSION --build-arg GITHUB_TOKEN --target upload-artifact . --progress plain", "setup": "git checkout 1.22.0 && npm ci --production && npm run download-dist", @@ -63,7 +63,7 @@ "deploy-demo-server": "node extra/deploy-demo-server.js", "sort-contributors": "node extra/sort-contributors.js", "quick-run-nightly": "docker run --rm --env NODE_ENV=development -p 3001:3001 louislam/uptime-kuma:nightly2", - "start-dev-container": "cd docker && docker-compose -f docker-compose-dev.yml up" + "start-dev-container": "cd docker && docker-compose -f docker-compose-dev.yml up --force-recreate" }, "dependencies": { "@grpc/grpc-js": "~1.7.3", diff --git a/server/server.js b/server/server.js index b9ecdd21d..26dfe1c13 100644 --- a/server/server.js +++ b/server/server.js @@ -76,7 +76,9 @@ log.info("server", "Importing this project modules"); log.debug("server", "Importing Monitor"); const Monitor = require("./model/monitor"); log.debug("server", "Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, doubleCheckPassword, startE2eTests } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest, FBSD, doubleCheckPassword, startE2eTests, + allowDevAllOrigin +} = require("./util-server"); log.debug("server", "Importing Notification"); const { Notification } = require("./notification"); @@ -228,6 +230,14 @@ let needSetup = false; } }); + app.get("/setup-database-info", (request, response) => { + allowDevAllOrigin(response); + response.json({ + runningSetup: false, + needSetup: false, + }); + }); + if (isDev) { app.post("/test-webhook", async (request, response) => { log.debug("test", request.headers); diff --git a/server/setup-database.js b/server/setup-database.js index e40649fa4..26a55ab34 100644 --- a/server/setup-database.js +++ b/server/setup-database.js @@ -18,6 +18,7 @@ class SetupDatabase { * @type {boolean} */ needSetup = true; + runningSetup = false; server; @@ -80,6 +81,12 @@ class SetupDatabase { let tempServer; app.use(express.json()); + // Disable Keep Alive, otherwise the server will not shutdown, as the client will keep the connection alive + app.use(function (req, res, next) { + res.setHeader("Connection", "close"); + next(); + }); + app.get("/", async (request, response) => { response.redirect("/setup-database"); }); @@ -91,9 +98,12 @@ class SetupDatabase { }); }); - app.get("/info", (request, response) => { + app.get("/setup-database-info", (request, response) => { allowDevAllOrigin(response); + console.log("Request /setup-database-info"); response.json({ + runningSetup: this.runningSetup, + needSetup: this.needSetup, isEnabledEmbeddedMariaDB: this.isEnabledEmbeddedMariaDB(), }); }); @@ -101,7 +111,12 @@ class SetupDatabase { app.post("/setup-database", async (request, response) => { allowDevAllOrigin(response); - console.log(request); + if (this.runningSetup) { + response.status(400).json("Setup is already running"); + return; + } + + this.runningSetup = true; let dbConfig = request.body.dbConfig; @@ -114,42 +129,50 @@ class SetupDatabase { // Validate input if (typeof dbConfig !== "object") { response.status(400).json("Invalid dbConfig"); + this.runningSetup = false; return; } if (!dbConfig.type) { response.status(400).json("Database Type is required"); + this.runningSetup = false; return; } if (!supportedDBTypes.includes(dbConfig.type)) { response.status(400).json("Unsupported Database Type"); + this.runningSetup = false; return; } if (dbConfig.type === "mariadb") { if (!dbConfig.hostname) { response.status(400).json("Hostname is required"); + this.runningSetup = false; return; } if (!dbConfig.port) { response.status(400).json("Port is required"); + this.runningSetup = false; return; } if (!dbConfig.dbName) { response.status(400).json("Database name is required"); + this.runningSetup = false; return; } if (!dbConfig.username) { response.status(400).json("Username is required"); + this.runningSetup = false; return; } if (!dbConfig.password) { response.status(400).json("Password is required"); + this.runningSetup = false; return; } } @@ -162,11 +185,16 @@ class SetupDatabase { }); // Shutdown down this express and start the main server - log.info("setup-database", "Database is configured, close setup-database server and start the main server now."); + log.info("setup-database", "Database is configured, close the setup-database server and start the main server now."); if (tempServer) { - tempServer.close(); + tempServer.close(() => { + log.info("setup-database", "The setup-database server is closed"); + resolve(); + }); + } else { + resolve(); } - resolve(); + }); app.use("/", expressStaticGzip("dist", { diff --git a/src/pages/Setup.vue b/src/pages/Setup.vue index cd2d149cd..b5eafcc64 100644 --- a/src/pages/Setup.vue +++ b/src/pages/Setup.vue @@ -62,6 +62,8 @@ export default { }, mounted() { + // TODO: Check if it is a database setup + this.$root.getSocket().emit("needSetup", (needSetup) => { if (! needSetup) { this.$router.push("/"); diff --git a/src/pages/SetupDatabase.vue b/src/pages/SetupDatabase.vue index 122b548d1..74cf88cfe 100644 --- a/src/pages/SetupDatabase.vue +++ b/src/pages/SetupDatabase.vue @@ -1,5 +1,5 @@