From 119886582f86e9cc950584d2f112409bacb83efe Mon Sep 17 00:00:00 2001 From: Ben <109954107+builditben@users.noreply.github.com> Date: Mon, 6 Feb 2023 18:05:44 +0000 Subject: [PATCH] chore: Split up docker files and upgrade docker compose (#1333) * Split docker compose into environments * Upgrade docker-compose * Update all workflow OS versions to Ubuntu 22.04 (LTS) --- .github/workflows/pizza-teardown-manual.yml | 2 +- .github/workflows/pizza-teardown.yml | 2 +- .github/workflows/pull-request.yml | 43 ++++++++++----------- .github/workflows/push-main.yml | 6 +-- .github/workflows/push-production.yml | 6 +-- README.md | 12 +++--- docker-compose.local.yml | 20 ++++++++++ docker-compose.pizza.yml | 11 ++++-- docker-compose.yml | 19 +++------ e2e/install-dependencies.sh | 2 +- package.json | 7 ++++ scripts/README.md | 4 +- scripts/pullrequest/create.sh | 42 ++++++++++++-------- scripts/pullrequest/update.sh | 16 ++++++-- scripts/start-containers-for-tests.sh | 33 ++++++++-------- 15 files changed, 133 insertions(+), 92 deletions(-) create mode 100644 docker-compose.local.yml create mode 100644 package.json diff --git a/.github/workflows/pizza-teardown-manual.yml b/.github/workflows/pizza-teardown-manual.yml index 68a59596f8..e04eebe93c 100644 --- a/.github/workflows/pizza-teardown-manual.yml +++ b/.github/workflows/pizza-teardown-manual.yml @@ -12,7 +12,7 @@ env: jobs: teardown_pizza: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Remove vultr resources uses: theopensystemslab/vultr-action@v1.15 diff --git a/.github/workflows/pizza-teardown.yml b/.github/workflows/pizza-teardown.yml index 48d7209360..6e708cbc34 100644 --- a/.github/workflows/pizza-teardown.yml +++ b/.github/workflows/pizza-teardown.yml @@ -9,7 +9,7 @@ env: jobs: teardown_pizza: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Remove vultr resources uses: theopensystemslab/vultr-action@v1.15 diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 6dffea30e0..ccfcd57feb 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -18,7 +18,7 @@ env: jobs: changes: name: Check file changes - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 permissions: pull-requests: read outputs: @@ -56,7 +56,7 @@ jobs: integration_tests: name: Run Integration tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes] if: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.e2e == 'true' || needs.changes.outputs.editor == 'true' || needs.changes.outputs.sharedb == 'true' || needs.changes.outputs.hasura == 'true' }} steps: @@ -96,7 +96,7 @@ jobs: api_tests: name: Run API Tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 timeout-minutes: 10 needs: [changes] if: ${{ needs.changes.outputs.api == 'true' }} @@ -135,7 +135,7 @@ jobs: test_react: name: Run React Tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes] if: ${{ needs.changes.outputs.editor == 'true' }} steps: @@ -171,7 +171,7 @@ jobs: build_react_app: name: Build React App - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes] if: "${{ !contains(needs.changes.outputs.commit, '[skip pizza]') }}" steps: @@ -214,7 +214,7 @@ jobs: build_storybook: name: Build Storybook - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes] if: "${{ !contains(needs.changes.outputs.commit, '[skip pizza]') }}" steps: @@ -256,7 +256,7 @@ jobs: pulumi_preview: name: Run Pulumi Preview - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes, build_react_app] if: ${{ needs.changes.outputs.infrastructure == 'true' }} steps: @@ -294,7 +294,7 @@ jobs: hasura-change-summary: name: Generate Hasura Change Summary - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 needs: [changes] if: ${{ needs.changes.outputs.hasura == 'true' }} steps: @@ -321,7 +321,7 @@ jobs: create_or_update_vultr_instance: name: Upsert Vultr Instance needs: [changes, build_react_app, build_storybook] - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 if: "${{ success() && !contains(needs.changes.outputs.commit, '[skip pizza]') }}" steps: - name: Create Pizza (if it doesn't exist) @@ -331,7 +331,7 @@ jobs: action: create api_key: ${{ secrets.VULTR_API_KEY }} domain: ${{ env.DOMAIN }} - os_id: 387 + os_id: 1743 plan: vc2-1c-1gb pullrequest_id: ${{ env.PULLREQUEST_ID }} region: lhr @@ -359,12 +359,10 @@ jobs: ./scripts/pull-secrets.sh - echo " - ROOT_DOMAIN=${{ env.FULL_DOMAIN }} - " > .env.temp - cat .env .env.temp .env.staging > .env.prod + echo -e "\nROOT_DOMAIN=${{ env.FULL_DOMAIN }}\n" > .env.temp + cat .env .env.temp .env.staging > .env.pizza - SSH_PASSWORD=${{ secrets.SSH_PASSWORD }} sh scripts/pullrequest/create.sh + SSH_PASSWORD=${{ secrets.SSH_PASSWORD }} ./scripts/pullrequest/create.sh # UPDATE STEPS @@ -389,12 +387,10 @@ jobs: ./scripts/pull-secrets.sh - echo " - ROOT_DOMAIN=${{ env.FULL_DOMAIN }} - " > .env.temp - cat .env .env.temp .env.staging > .env.prod + echo -e "\nROOT_DOMAIN=${{ env.FULL_DOMAIN }}\n" > .env.temp + cat .env .env.temp .env.staging > .env.pizza - sh scripts/pullrequest/update.sh + ./scripts/pullrequest/update.sh # CREATE & UPDATE STEPS @@ -440,22 +436,25 @@ jobs: healthcheck: name: Run Healthcheck on Pizza Services needs: [create_or_update_vultr_instance] - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: API healthcheck run: | timeout 150s bash -c "until curl --fail https://api.${{ env.FULL_DOMAIN }}; do sleep 1; done" + - name: Hasura healthcheck run: | timeout 150s bash -c "until curl --fail https://hasura.${{ env.FULL_DOMAIN }}/healthz; do sleep 1; done" + - name: Editor healthcheck run: | timeout 150s bash -c "until curl --fail https://${{ env.FULL_DOMAIN }}; do sleep 1; done" + lighthouse: name: Lighthouse needs: [create_or_update_vultr_instance] - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 - name: Audit URLs using Lighthouse diff --git a/.github/workflows/push-main.yml b/.github/workflows/push-main.yml index 2b022a8bca..69df2a44a9 100644 --- a/.github/workflows/push-main.yml +++ b/.github/workflows/push-main.yml @@ -16,7 +16,7 @@ env: jobs: build_react: name: Build React - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 @@ -71,7 +71,7 @@ jobs: preview: name: Pulumi Up needs: build_react - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 @@ -105,7 +105,7 @@ jobs: notifications: name: Notifications needs: preview - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 # Airbrake notification - https://airbrake.io/docs/features/deploy-tracking diff --git a/.github/workflows/push-production.yml b/.github/workflows/push-production.yml index 4ad67758fc..98ed66c7a0 100644 --- a/.github/workflows/push-production.yml +++ b/.github/workflows/push-production.yml @@ -16,7 +16,7 @@ env: jobs: build_react: name: Test and Build - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 @@ -71,7 +71,7 @@ jobs: preview: name: Pulumi Up needs: build_react - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 with: @@ -107,7 +107,7 @@ jobs: notifications: name: Notifications needs: preview - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 # Airbrake notification - https://airbrake.io/docs/features/deploy-tracking diff --git a/README.md b/README.md index 2b659b6b50..def48abc21 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ planx-new is a monorepo containing our full application stack. Here's a quick su - `sharedb.planx.uk` is our implementation of [ShareDB](https://github.com/share/sharedb), a library for realtime document collaboration based on JSON Operational Transformation (OT) used in our "editor" environment - `infrastructure` is [Pulumi](https://www.pulumi.com/) infrastructure-as-code for configuring and managing our AWS environments + ## Running Locally 1. Download and install the following dependencies if you don't have them already: @@ -26,15 +27,15 @@ planx-new is a monorepo containing our full application stack. Here's a quick su 1. Clone this repository. You'll need to get some additional environment variable files that aren't published here. Find instructions for copying these in 1password under the AWS Staging IAM user role entry -1. Run the following command to get everything (postgres, sharedb, api and hasura server processes) up and running `docker-compose up --build -d` +1. Run `pnpm docker:up` from the project root to get everything (postgres, sharedb, api and hasura server processes) up and running and `pnpm docker:down` to stop all services. `pnpm docker:down-hard` will remove volumes (i.e. database data) and can be a useful hard reset when necessary. 1. Move into the hasura directory `cd ../hasura.planx.uk` & install dependencies `pnpm i`. -1. Open [Hasura's](https://hasura.io/) web console `pnpm start` & check that your Google email address is in the `users` table, if not then add it +1. Open [Hasura's](https://hasura.io/) web console (`cd hasura.planx.uk` then `pnpm start`) & check that your Google email address is in the `users` table, if not then add it 1. Move into the editor directory `cd ../editor.planx.uk` & install dependencies `pnpm i`. -1. Start the dev server! `pnpm start` & open http://localhost:3000 & login with your GMail/Google email address +1. Start the editor dev server! `pnpm start` & open `http://localhost:3000` & login with your GMail/Google email address ### Troubleshooting @@ -42,15 +43,14 @@ planx-new is a monorepo containing our full application stack. Here's a quick su If you run into trouble, you may want to try the following: * Ensure you have a local `.env` file with up-to-date keys etc (see relevant `.env.example` files for reference and 1Password for an initial `.env` file). -* The Hasura CLI may require a postinstall step which can be run manually with `cd node_modules/hasura-cli && pnpm postinstall`. ### Analytics -Running `docker-compose up` won't spin up [Metabase](https://www.metabase.com/). +Running `docker compose up` won't spin up [Metabase](https://www.metabase.com/). To spin it up, run: - `docker-compose --profile analytics up` + `docker compose --profile analytics up` ### Documentation diff --git a/docker-compose.local.yml b/docker-compose.local.yml new file mode 100644 index 0000000000..7d0fd4200a --- /dev/null +++ b/docker-compose.local.yml @@ -0,0 +1,20 @@ +version: "3.8" + +services: + postgres: + volumes: + - postgres_data:/var/lib/postgresql/data + + api: + build: + target: development + volumes: + - "./api.planx.uk:/api" + + sharedb: + volumes: + - "./sharedb.planx.uk:/sharedb" + - "/sharedb/node_modules" + +volumes: + postgres_data: diff --git a/docker-compose.pizza.yml b/docker-compose.pizza.yml index 38d1af5db2..bb3fa2795f 100644 --- a/docker-compose.pizza.yml +++ b/docker-compose.pizza.yml @@ -1,7 +1,12 @@ version: "3.8" +# If you change anything here (e.g. environment variables) +# remember to cascade the change to `infrastructure/` + services: postgres: + volumes: + - postgres_data:/var/lib/postgresql/data labels: virtual.host: postgres.${ROOT_DOMAIN} virtual.port: 5432 @@ -19,6 +24,8 @@ services: virtual.tls: ${TLS_EMAIL} api: + build: + target: production volumes: - "/api/node_modules" - "/api/dist" @@ -35,9 +42,6 @@ services: virtual.port: 8000 virtual.tls: ${TLS_EMAIL} - minio: - entrypoint: ["echo", "minio disabled"] - editor: image: pierrezemb/gostatic volumes: @@ -59,5 +63,6 @@ services: - 443:443 volumes: + postgres_data: caddy_data: caddy_config: diff --git a/docker-compose.yml b/docker-compose.yml index 0cd4f758e4..cd4deda6e8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,11 @@ version: "3.8" -# If you change anything here (e.g. environment variables) remember to cascade the change to `infrastructure/` - services: postgres: image: postgis/postgis:12-3.0-alpine restart: unless-stopped ports: - ${PG_PORT}:5432 - volumes: - - data:/var/lib/postgresql/data command: "postgres -c listen_addresses='*'" healthcheck: test: pg_isready --username=${PG_USERNAME} --dbname=${PG_DATABASE} --quiet @@ -51,7 +47,6 @@ services: hasura: build: context: ./hasura.planx.uk - dockerfile: Dockerfile depends_on: postgres: condition: service_healthy @@ -81,10 +76,10 @@ services: HASURA_GRAPHQL_UNAUTHORIZED_ROLE: "public" HASURA_PLANX_API_KEY: ${HASURA_PLANX_API_KEY} HASURA_PLANX_API_URL: ${HASURA_PLANX_API_URL} + hasura-proxy: build: context: ./hasura.planx.uk/proxy - dockerfile: Dockerfile depends_on: hasura: condition: service_healthy @@ -99,10 +94,10 @@ services: interval: 15s timeout: 3s retries: 3 + seed-database: build: context: ./scripts/seed-database - dockerfile: Dockerfile volumes: - "./hasura.planx.uk/:/hasura" restart: "no" @@ -114,6 +109,7 @@ services: HASURA_GRAPHQL_URL: http://hasura-proxy:7000/v1/graphql HASURA_GRAPHQL_ENDPOINT: http://hasura-proxy:7000/ PRODUCTION_GRAPHQL_URL: 'https://hasura.editor.planx.uk/v1/graphql' + api: restart: unless-stopped build: @@ -122,10 +118,6 @@ services: depends_on: hasura-proxy: condition: service_healthy - volumes: - - "./api.planx.uk:/api" - - "/api/node_modules" - - "/api/dist" ports: - ${API_PORT}:${API_PORT} healthcheck: @@ -193,8 +185,10 @@ services: JWT_SECRET: ${JWT_SECRET} PG_URL: postgres://${PG_USERNAME}:${PG_PASSWORD}@postgres/${PG_DATABASE} + # used as an S3 service mock minio: image: minio/minio:RELEASE.2021-08-31T05-46-54Z + profiles: ["mock-services"] ports: - ${MINIO_PORT}:9000 - ${MINIO_ADMIN_PORT}:9001 @@ -214,6 +208,3 @@ services: interval: 30s timeout: 20s retries: 3 - -volumes: - data: diff --git a/e2e/install-dependencies.sh b/e2e/install-dependencies.sh index ff1689f609..73eb8d15b5 100755 --- a/e2e/install-dependencies.sh +++ b/e2e/install-dependencies.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash SCRIPT_DIR=$(dirname "$0") . "$SCRIPT_DIR/../.env" diff --git a/package.json b/package.json new file mode 100644 index 0000000000..27999b89fd --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "scripts": { + "docker:up": "docker compose -f ./docker-compose.yml -f ./docker-compose.local.yml up -d", + "docker:down": "docker compose -f ./docker-compose.yml -f ./docker-compose.local.yml down", + "docker:down-hard": "docker compose -f ./docker-compose.yml -f ./docker-compose.local.yml down --remove-orphans -v" + } +} diff --git a/scripts/README.md b/scripts/README.md index 309abc39f2..97b621fa0d 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -6,7 +6,7 @@ This is useful to debug scenarios where bugs depend on content (i.e. on flow dat It upserts teams and flows if the flows table is empty. Beware that all teams will be deleted and replaced by the teams on production. -To run it, either run `pnpm upsert-flows` or `docker-compose up` from the root folder. +To run it, either run `pnpm upsert-flows` or `docker compose up` from the root folder. ## `pull-secrets` This script is used to pull secrets required for local development of PlanX to a local machine. @@ -23,4 +23,4 @@ It uses the AWS CLI to copy files to S3. In order to use this you will require - - The AWS CLI installed locally - AWS credentials stored in the `~/.aws` directory of you machine. These should be generated and shared as part of developer onboarding. -You will need to run this script if you have added a new secret, or rotated API keys for local or Pizza environments. \ No newline at end of file +You will need to run this script if you have added a new secret, or rotated API keys for local or Pizza environments. diff --git a/scripts/pullrequest/create.sh b/scripts/pullrequest/create.sh index 1f20771e8a..6fb425eb87 100755 --- a/scripts/pullrequest/create.sh +++ b/scripts/pullrequest/create.sh @@ -1,16 +1,16 @@ -#!/bin/bash +#!/usr/bin/env bash +set -o errexit -o pipefail + +# run from project root +cd "$(dirname $0)/../.." echo "root:$SSH_PASSWORD" | chpasswd apt-get update -y -# create swap -# https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04 -fallocate -l 2G /swapfile -chmod 600 /swapfile -mkswap /swapfile -swapon /swapfile -echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab +# check if swap space is available - see link for more on updating swap: +# https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04 +swapon --show # install docker apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y @@ -19,15 +19,23 @@ echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null apt-get update -y -apt-get install docker-ce docker-ce-cli containerd.io -y - -# install docker-compose -curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose -chmod +x /usr/local/bin/docker-compose - -docker-compose --env-file .env.prod -f docker-compose.yml -f docker-compose.pizza.yml up --build -d +apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y # install hasura cli curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash -cd hasura.planx.uk || exit 1 -hasura seed apply --envfile ./../.env + +# set env for this shell +set -o allexport +source .env.pizza +DOCKER_BUILDKIT=1 +set +o allexport + +# start services +docker compose \ + -f docker-compose.yml \ + -f docker-compose.pizza.yml \ + up --build --wait + +# insert hasura seeds +cd hasura.planx.uk +hasura seed apply --envfile .env diff --git a/scripts/pullrequest/update.sh b/scripts/pullrequest/update.sh index d02fcc352c..b44c9dbddb 100755 --- a/scripts/pullrequest/update.sh +++ b/scripts/pullrequest/update.sh @@ -1,6 +1,16 @@ -#!/bin/bash +#!/usr/bin/env bash +set -o errexit -o pipefail -DOCKER_BUILDKIT=1 docker-compose --env-file .env.prod \ +# run from project root +cd "$(dirname $0)/../.." + +# set env for this shell +set -o allexport +source .env.pizza +DOCKER_BUILDKIT=1 +set +o allexport + +docker compose \ -f docker-compose.yml \ -f docker-compose.pizza.yml \ - up --build --renew-anon-volumes --force-recreate --remove-orphans -d + up --build --renew-anon-volumes --force-recreate --remove-orphans --wait diff --git a/scripts/start-containers-for-tests.sh b/scripts/start-containers-for-tests.sh index 185d1146f6..a8159dadf6 100755 --- a/scripts/start-containers-for-tests.sh +++ b/scripts/start-containers-for-tests.sh @@ -1,22 +1,23 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash +set -o errexit -o errtrace -set -e +# run from project root +cd "$(dirname $0)/.." -SCRIPT_DIR=$(dirname "$0") -ROOT_DIR="${SCRIPT_DIR}/.." +trap 'echo "Cleaning up…" ; docker compose logs api; docker compose down --volumes --remove-orphans' ERR -echo "SCRIPT_DIR=${SCRIPT_DIR}" -echo "ROOT_DIR=${ROOT_DIR}" +function setupContainers(){ + # Destroy all previous containers and data (just in case) + docker compose down --volumes --remove-orphans -# Go to root directory -cd "$ROOT_DIR" || exit + echo "Starting docker…" + DOCKER_BUILDKIT=1 docker compose \ + -f docker-compose.yml \ + -f docker-compose.e2e.yml \ + --profile mock-services \ + up --build --wait test-ready -# Destroy all previous containers and data (just in case) -docker-compose down --volumes --remove-orphans + echo "All containers ready." +} -trap 'echo "Cleaning up…" ; docker-compose down --volumes --remove-orphans' TERM INT - -echo "Starting docker…" -DOCKER_BUILDKIT=1 docker-compose -f docker-compose.yml -f docker-compose.e2e.yml up --build -d test-ready - -echo "All containers ready." +setupContainers