From dd259fa6a44ec92405887d035bd5a61db15b2e48 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Wed, 8 Feb 2023 23:43:02 +0000 Subject: [PATCH] feat: bootstrap containerisation insane laughter ensues --- .gitignore | 3 ++- Dockerfile.bootstrap | 15 +++++++++++++ _scripts/bootstrap.sh | 36 ++++++++++++++++++++++++++++--- docker-compose-dev.yml | 17 +++++++++++++++ server/package.json | 3 ++- server/src/scripts/set-indexes.ts | 7 +++++- 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 Dockerfile.bootstrap diff --git a/.gitignore b/.gitignore index 4029f14a7..d8654947f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ node_modules # PNPM noise .pnpm-debug.log +.pnpm-store # Compiled output js @@ -39,4 +40,4 @@ failed-tests.log .tsbuildinfo # bootstrap flag -_scripts/I_HAVE_BOOTSTRAPPED_OK \ No newline at end of file +I_HAVE_BOOTSTRAPPED_OK \ No newline at end of file diff --git a/Dockerfile.bootstrap b/Dockerfile.bootstrap new file mode 100644 index 000000000..fef9016dc --- /dev/null +++ b/Dockerfile.bootstrap @@ -0,0 +1,15 @@ +# Yes, I've made an entire docker container just to run a bash script +# instead of rewriting the script in a ~~real~~ cross-platform scripting language. +# This docker container **depends** on an external volume being mounted +# (see how it's used in docker-compose-dev.yml) +# and *intentionally* mutates state outside of its container. + +# I really do not care. +# Want it better? Do it yourself. +FROM node:16 as base + +RUN npm install --silent -g pnpm + +WORKDIR /app + +CMD ["bash", "_scripts/bootstrap.sh"] \ No newline at end of file diff --git a/_scripts/bootstrap.sh b/_scripts/bootstrap.sh index a705d1373..1f1408863 100755 --- a/_scripts/bootstrap.sh +++ b/_scripts/bootstrap.sh @@ -2,6 +2,7 @@ set -eo pipefail + # https://stackoverflow.com/questions/59895/how-can-i-get-the-directory-where-a-bash-script-is-located-from-within-the-scrip # if you actually think bash is a good programming language you are # *straight up delusional* @@ -10,6 +11,11 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) cd "$SCRIPT_DIR"; cd ..; +if [ -e I_HAVE_BOOTSTRAPPED_OK ]; then + echo "Already bootstrapped." + exit 0 +fi + function mvExampleFiles { echo "Moving example config files into usable places..." @@ -54,12 +60,26 @@ function pnpmInstall { echo "Installed dependencies." } +function setIndexes { + echo "Setting indexes..." + + ( + cd server + pnpm run set-indexes + ) + + echo "Set." + +} + function syncDatabaseWithSeeds { echo "Syncing database with seeds..." - cd server + ( + cd server - pnpm run sync-database-local + pnpm run sync-database-local + ) echo "Synced." } @@ -67,6 +87,16 @@ function syncDatabaseWithSeeds { mvExampleFiles selfSignHTTPS pnpmInstall +setIndexes syncDatabaseWithSeeds -touch I_HAVE_BOOTSTRAPPED_OK +cat << EOF > I_HAVE_BOOTSTRAPPED_OK +Tachi was bootstrapped here on $(date). + +The existence of this file stops Tachi from bootstrapping again. +There's nothing harmful about this -- you can bootstrap as much as you want! +We just don't want to necessarily bootstrap *each* time we boot Tachi. + +To bootstrap again (incase you think something has gone wrong) +Delete this file and re-run npm start. +EOF diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index eb3fa54f1..7dbe60c44 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -51,6 +51,23 @@ services: dockerfile: Dockerfile.seeds volumes: - ./:/app + bootstrap: + container_name: tachi-bootstrap + build: + dockerfile: Dockerfile.bootstrap + volumes: + - ./:/app + depends_on: + - redis + - mongo + links: + - redis + - mongo + environment: + REDIS_URL: redis + MONGO_URL: mongo + PORT: 8080 + NODE_ENV: "dev" volumes: tachi: driver: local \ No newline at end of file diff --git a/server/package.json b/server/package.json index c3ebd8b2c..862a887a9 100644 --- a/server/package.json +++ b/server/package.json @@ -22,7 +22,8 @@ "recalc-everything": "ts-node src/scripts/state-sync/sync-state.ts", "make-user-admin": "ts-node src/scripts/make-user-admin.ts $@", "validate-database": "ts-node src/scripts/validate-database", - "dev": "NODE_PATH=js/ nodemon" + "dev": "NODE_PATH=js/ nodemon", + "set-indexes": "ts-node src/scripts/set-indexes" }, "author": "zkldi", "license": "AGPL3", diff --git a/server/src/scripts/set-indexes.ts b/server/src/scripts/set-indexes.ts index c8a5d0d10..fec182bd7 100644 --- a/server/src/scripts/set-indexes.ts +++ b/server/src/scripts/set-indexes.ts @@ -2,13 +2,14 @@ import { SetIndexes } from "../external/mongo/indexes"; import { Command } from "commander"; import CreateLogCtx from "lib/logger/logger"; +import { ServerConfig } from "lib/setup/config"; import { WrapScriptPromise } from "utils/misc"; const program = new Command(); const logger = CreateLogCtx(__filename); -program.requiredOption("-d, --db ", "The database to index."); +program.option("-d, --db ", "The database to index."); program.option( "-r, --reset", "Whether to reset all indexes on this database before indexing or not." @@ -17,6 +18,10 @@ program.option( program.parse(process.argv); const options: { db: string; reset?: boolean } = program.opts(); +if (!options.db) { + options.db = ServerConfig.MONGO_DATABASE_NAME; +} + if (require.main === module) { WrapScriptPromise(SetIndexes(options.db, options.reset === true), logger); }