Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A migrator for upgrading v1 to v2 #3853

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/dockerfile → docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG BASE_IMAGE=louislam/uptime-kuma:base2
############################################
# Build in Golang
# Run npm run build-healthcheck-armv7 in the host first, otherwise it will be super slow where it is building the armv7 healthcheck
# Check file: builder-go.dockerfile
# Check file: builder-go.Dockerfile
############################################
FROM louislam/uptime-kuma:builder-go AS build_healthcheck

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions docker/migrator.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM louislam/uptime-kuma:base2-slim
WORKDIR /app
ENV UPTIME_KUMA_IS_CONTAINER=1
COPY ./extra/migrator.js ./extra/migrator.js
RUN npm install args-parser
CMD ["node", "extra/migrator.js"]
71 changes: 71 additions & 0 deletions extra/migrator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Migrator for upgrading from v1 to v2
// Mainly for Docker users

if (!process.env.UPTIME_KUMA_IS_CONTAINER) {
console.error("Migrator: Not running in a container");
process.exit(1);
}

const fs = require("fs");
const path = require("path");
const args = require("args-parser")(process.argv);
const childProcess = require("child_process");

const dryRun = args["dry-run"] || false;

// Change the data directory's (Usually /app/data) owner to `node` user
const dataDir = process.env.DATA_DIR || args["data-dir"] || "./data/";

// Convert the `node` user to uid using `id -u node`
const nodeUID = Number(childProcess.execSync("id -u node").toString().trim());
const nodeGID = Number(childProcess.execSync("id -g node").toString().trim());

if (dryRun) {
console.log("Dry-run enabled, no changes will be made");
}

console.log("Data directory:", dataDir);
console.log("node's uid:", nodeUID);
console.log("node's gid:", nodeGID);

chownDir(dataDir);

console.log("Done");

/**
* Chown to node user recursively
* Only if the data directory is not owned by node user
* @param {string} dir Directory path
* @returns {void}
*/
function chownDir(dir) {
chown(dir);

fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.statSync(filePath);

if (fileStat.isDirectory()) {
chownDir(filePath);
} else {
chown(filePath);
}
});
}

/**
* @param {string} path File path
* @returns {void}
*/
function chown(path) {
const dataDirStat = fs.statSync(dataDir);

if (dataDirStat.uid !== nodeUID || dataDirStat.gid !== nodeGID) {
console.log(path, "is owned by", dataDirStat.uid + ":" + dataDirStat.gid);
console.log(path, ": Changing owner to", nodeUID + ":" + nodeGID);

if (!dryRun) {
fs.chownSync(path, nodeUID, nodeGID);
}
}
}
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@
"tsc": "tsc",
"vite-preview-dist": "vite preview --host --config ./config/vite.config.js",
"build-docker": "npm run build && npm run build-docker-full && npm run build-docker-slim",
"build-docker-base": "docker buildx build -f docker/debian-base.dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:base2 --target base2 . --push",
"build-docker-base-slim": "docker buildx build -f docker/debian-base.dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:base2-slim --target base2-slim . --push",
"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": "node ./extra/test-docker.js && 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-test2 --target pr-test2 . --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",
"build-docker-base": "docker buildx build -f docker/base.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:base2 --target base2 . --push",
"build-docker-base-slim": "docker buildx build -f docker/base.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:base2-slim --target base2-slim . --push",
"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": "node ./extra/test-docker.js && 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-test2 --target pr-test2 . --push",
"build-docker-migrator": "docker buildx build -f docker/migrator.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:migrator . --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.23.2 && npm ci --production && npm run download-dist",
"download-dist": "node extra/download-dist.js",
"mark-as-nightly": "node extra/mark-as-nightly.js",
Expand All @@ -60,7 +61,7 @@
"simple-mongo": "docker run --rm -p 27017:27017 mongo",
"update-language-files": "cd extra/update-language-files && node index.js && cross-env-shell eslint ../../src/languages/$npm_config_language.js --fix",
"release-final": "node ./extra/test-docker.js && node extra/update-version.js && npm run build-docker && node ./extra/press-any-key.js && npm run upload-artifacts && node ./extra/update-wiki-version.js",
"release-beta": "node ./extra/test-docker.js && node extra/beta/update-version.js && npm run build && node ./extra/env2arg.js docker buildx build -f docker/dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:$VERSION -t louislam/uptime-kuma:beta . --target release --push && node ./extra/press-any-key.js && npm run upload-artifacts",
"release-beta": "node ./extra/test-docker.js && node extra/beta/update-version.js && npm run build && node ./extra/env2arg.js docker buildx build -f docker/Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:$VERSION -t louislam/uptime-kuma:beta . --target release --push && node ./extra/press-any-key.js && npm run upload-artifacts",
"git-remove-tag": "git tag -d",
"build-dist-and-restart": "npm run build && npm run start-server-dev",
"start-pr-test": "node extra/checkout-pr.js && npm install && npm run dev",
Expand Down