From 6ef770ba06bdab63b146c0eb2737e8fa933fe929 Mon Sep 17 00:00:00 2001 From: James Monger Date: Sat, 26 Aug 2023 07:45:10 +0000 Subject: [PATCH] build: use makefile for local setup, run migrations from container --- Makefile | 41 +++++++++++++++++++++++++++++----- README.md | 12 +++++----- apps/nodejs-builder.Dockerfile | 18 ++++++++++++--- docker-compose.db.yml | 9 ++++++++ docker-compose.yml | 13 +++++++++++ package.json | 4 ---- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 5556ba2d..11201d7c 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,45 @@ +# ===== + +all: install build db server +rebuild: build server + +# ===== + install: @echo "Setting up the project..." yarn +build: + @echo "Building the docker images..." + docker compose -f docker-compose.yml build + db: @echo "Setting up the database and running migrations..." - yarn dockerup-db - - @read -p "Enter the DATABASE_URL for seeding: " DATABASE_URL; \ - DATABASE_URL=$$DATABASE_URL yarn workspace @creature-chess/data prisma-migrate deploy + docker compose -f docker-compose.db.yml up -d postgres + docker compose run -e DATABASE_URL nodejs-builder yarn workspace @creature-chess/data prisma migrate deploy server: @echo "Running the game..." - yarn dockerup + docker compose -f docker-compose.yml up -d + +down: + @echo "Stopping the game..." + docker compose -f docker-compose.yml down + @echo "Stopping the database..." + docker compose -f docker-compose.db.yml down + +# ===== + +# This step is used to create a new migration. +# It creates a new nodejs-builder container, runs the migration command inside, +# then copies the migration files to the local machine. +add-migration: + @echo "Creating new migration" + read -p "Enter the migration name: " MIGRATION_NAME; \ + docker compose -f docker-compose.db.yml up -d postgres; \ + docker compose run -e DATABASE_URL nodejs-builder yarn workspace @creature-chess/data prisma migrate dev --name $$MIGRATION_NAME; \ + CONTAINER_ID=$$(docker ps -aqf "ancestor=nodejs-builder" --latest); \ + docker cp $$CONTAINER_ID:/code/modules/@creature-chess/data/prisma/migrations ./modules/@creature-chess/data/prisma/; \ + docker stop $$CONTAINER_ID; \ + docker rm $$CONTAINER_ID; \ + yarn workspace @creature-chess/data prisma generate diff --git a/README.md b/README.md index 6f83fcdb..818dee0c 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ See "Environment variables" above for info on how to store them. ### Setup -You can use the following `make` commands to set up the project: +To build and run the project, you can use `make`: -- `make install` - install dependencies -- `make db` - seed the database - - take the `DATABASE_URL` from `.env` and input it when asked. - - you may need to configure the connection string to use `localhost` instead of `postgres` as the host, depending on your setup. -- `make server` - build and run the docker containers +```shell +make +``` + +If you change the server, you can run `make rebuild` to rebuild and restart the server. ### Dev toolkit diff --git a/apps/nodejs-builder.Dockerfile b/apps/nodejs-builder.Dockerfile index b879ba69..f4de467a 100644 --- a/apps/nodejs-builder.Dockerfile +++ b/apps/nodejs-builder.Dockerfile @@ -46,7 +46,6 @@ ADD modules/@shoki/networking/package.json ./modules/@shoki/networking/ ADD modules/@tools/battle-tester/package.json ./modules/@tools/battle-tester/ # Run the install now that we have the package.json and yarn.lock files - RUN yarn install --frozen-lockfile --network-timeout 1000000 # Finally, copy the rest of the source code into the image @@ -54,7 +53,20 @@ RUN yarn install --frozen-lockfile --network-timeout 1000000 # install every time one of these steps has a different result ADD tsconfig.json ./ -ADD modules/ ./modules/ +# Copy and build the `@shoki` packages (excluding board-react) +ADD modules/@shoki/ ./modules/@shoki/ +RUN yarn workspaces foreach --include "@shoki/*" --exclude "@shoki/board-react" run build + +# Copy and build the @creature-chess/models +ADD modules/@creature-chess/models/ ./modules/@creature-chess/models/ +RUN yarn workspace @creature-chess/models run build + +# Copy and build the @creature-chess/data +ADD modules/@creature-chess/data/ ./modules/@creature-chess/data/ RUN yarn workspace @creature-chess/data prisma-generate -RUN yarn workspaces foreach --exclude "@creature-chess-app/*" run build +RUN yarn workspace @creature-chess/data run build + +# Copy and build the remaining `@creature-chess` packages +ADD modules/@creature-chess/ ./modules/@creature-chess/ +RUN yarn workspaces foreach --include "@creature-chess/*" --exclude "@creature-chess/models" --exclude "@creature-chess/data" run build diff --git a/docker-compose.db.yml b/docker-compose.db.yml index f67f30d9..b5726ffe 100644 --- a/docker-compose.db.yml +++ b/docker-compose.db.yml @@ -1,5 +1,10 @@ version: "3.8" +networks: + creature-chess: + name: creature-chess + driver: bridge + volumes: pgdata: pgadmin: @@ -22,6 +27,8 @@ services: timeout: 5s start_period: 10s retries: 20 + networks: + - creature-chess # pgadmin, a local web UI for managing the DB pgadmin: image: dpage/pgadmin4 @@ -33,6 +40,8 @@ services: - POSTGRES_PASSWORD ports: - 5050:80 + networks: + - creature-chess volumes: - pgadmin:/root/.pgadmin depends_on: diff --git a/docker-compose.yml b/docker-compose.yml index ddc81d99..c356febb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,10 @@ version: "3.8" +networks: + creature-chess: + name: creature-chess + external: true + services: # this is the base image for all nodejs-based apps nodejs-builder: @@ -7,6 +12,8 @@ services: context: . dockerfile: apps/nodejs-builder.Dockerfile image: nodejs-builder + networks: + - creature-chess # this image builds the web-game app # TODO (James) we could extend this in local dev to also watch for changes web-game-builder: @@ -67,6 +74,8 @@ services: ports: - 80:80 - 443:443 + networks: + - creature-chess depends_on: web-game-builder: condition: service_completed_successfully @@ -95,6 +104,8 @@ services: - APP_DIR=server-game ports: - "3000" + networks: + - creature-chess restart: always depends_on: nodejs-builder: @@ -116,6 +127,8 @@ services: - APP_DIR=server-info ports: - "3000" + networks: + - creature-chess restart: always depends_on: nodejs-builder: diff --git a/package.json b/package.json index cfa2db00..d7e95634 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,6 @@ "start-server-info": "ts-node-dev apps/server-info/index.ts", "start-web-menu": "yarn workspace @creature-chess-app/web-menu dev", "test": "yarn workspaces foreach run test", - "dockerup-db": "docker-compose --env-file ./.env -f docker-compose.db.yml up -d", - "dockerup-db-live": "docker-compose --env-file ./.env.live -f docker-compose.db.yml up -d", - "dockerup": "docker-compose --env-file ./.env -f docker-compose.yml -f docker-compose.db.yml build && docker-compose --env-file ./.env -f docker-compose.yml -f docker-compose.db.yml up", - "dockerup-live": "docker-compose --env-file ./.env.live build && docker-compose --env-file ./.env.live up -d", "storybook": "start-storybook -p 6006", "build-storybook": "build-storybook -o ./output", "lint": "eslint \"apps/**/*.ts\" \"modules/**/*.ts\" \"apps/**/*.tsx\" \"modules/**/*.tsx\"",