From aa6be804f7b50406e106c1dabd8fe369c2694c1b Mon Sep 17 00:00:00 2001 From: Shrey Bana Date: Sat, 11 Jan 2025 19:10:03 +0530 Subject: [PATCH] refactor: Refactored make & related files. - Removed nested/recursive make calls in targets. - Made docker conditonal, w/ podman as an alternative tool. --- .github/workflows/ci_check_pr.yaml | 8 +- .github/workflows/ci_merge_main.yaml | 2 +- Dockerfile | 2 +- docker-compose.yaml | 2 - example.Dockerfile | 2 +- makefile | 105 ++++++++++++++------------- 6 files changed, 64 insertions(+), 57 deletions(-) diff --git a/.github/workflows/ci_check_pr.yaml b/.github/workflows/ci_check_pr.yaml index a8b155b3..f9c0de18 100644 --- a/.github/workflows/ci_check_pr.yaml +++ b/.github/workflows/ci_check_pr.yaml @@ -57,7 +57,7 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@master with: - toolchain: 1.76.0 + toolchain: 1.78.0 targets: wasm32-unknown-unknown components: rustfmt, clippy @@ -110,7 +110,7 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@master with: - toolchain: 1.76.0 + toolchain: 1.78.0 targets: wasm32-unknown-unknown components: rustfmt, clippy @@ -126,6 +126,8 @@ jobs: - name: run tests shell: bash run: | - make ci-test + make setup CI=1 + make run & + make test env: APP_ENV: "TEST" diff --git a/.github/workflows/ci_merge_main.yaml b/.github/workflows/ci_merge_main.yaml index 0622d976..a9fd63fb 100644 --- a/.github/workflows/ci_merge_main.yaml +++ b/.github/workflows/ci_merge_main.yaml @@ -22,7 +22,7 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@master with: - toolchain: 1.76.0 + toolchain: 1.78.0 targets: wasm32-unknown-unknown components: rustfmt, clippy diff --git a/Dockerfile b/Dockerfile index 3f623724..fd69deb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.76.0 as builder +FROM rust:1.78.0 as builder WORKDIR /build diff --git a/docker-compose.yaml b/docker-compose.yaml index 0e775938..46a09a1b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,3 @@ -version: "3.4" - services: postgres: build: ./docker-compose/postgres/ diff --git a/example.Dockerfile b/example.Dockerfile index 4e644e50..003d2450 100644 --- a/example.Dockerfile +++ b/example.Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.76.0 as builder +FROM rust:1.78.0 as builder WORKDIR /build diff --git a/makefile b/makefile index 921d655d..33449ff8 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,22 @@ DOCKER_DNS ?= localhost TENANT ?= dev SHELL := /usr/bin/env bash FEATURES ?= ssr +HAS_DOCKER := $(shell command -v docker > /dev/null; echo $$?) +HAS_PODMAN := $(shell command -v podman > /dev/null; echo $$?) + +ifeq ($(shell test -f .env; echo $$?),1) +$(shell cp .env.example .env) +endif + +ifeq ($(HAS_DOCKER),0) + DOCKER := docker +else ifeq ($(HAS_PODMAN),0) + DOCKER := podman +else + $(error "No docker or podman found, please install one of them.") +endif + +COMPOSE := $(DOCKER) compose .PHONY: db-init @@ -14,22 +30,36 @@ FEATURES ?= ssr validate-psql-connection cac +db: +ifndef CI + $(COMPOSE) up -d postgres +else + @echo "Skipping postgres setup in CI." +endif + @echo "Verifying postgres readiness..." + @while ! pg_isready -h $(DOCKER_DNS) -p 5432; do sleep 0.5; done + +localstack: +ifndef CI + $(COMPOSE) up -d localstack +else + @echo "Skipping localstack setup in CI." +endif + @echo "Verifying localstack readiness..." + @while ! aws --no-cli-pager --endpoint-url=http://$(DOCKER_DNS):4566 --region=ap-south-1 sts get-caller-identity; do \ + sleep 0.5; \ + done + db-init: diesel migration run --locked-schema --config-file=crates/superposition_types/src/database/diesel.toml cleanup: - -docker rm -f $$(docker container ls --filter name=^context-aware-config -a -q) - -docker rmi -f $$(docker images | grep context-aware-config-postgres | cut -f 10 -d " ") - -migration: cleanup - docker-compose up -d postgres - cp .env.example .env - while ! make validate-psql-connection; \ - do echo "waiting for postgres bootup"; \ - sleep 0.5; \ - done + -$(DOCKER) rm -f $$($(DOCKER) container ls --filter name=^context-aware-config -a -q) + -$(DOCKER) rmi -f $$($(DOCKER) images | grep context-aware-config-postgres | cut -f 10 -d " ") + +migration: cleanup db diesel migration run --locked-schema --config-file=crates/superposition_types/src/database/diesel.toml - docker-compose down + $(COMPOSE) down legacy_db_setup: grep 'DATABASE_URL=' .env | sed -e 's/DATABASE_URL=//' | xargs ./scripts/legacy-db-setup.sh @@ -44,33 +74,18 @@ validate-psql-connection: pg_isready -h $(DOCKER_DNS) -p 5432 -env-setup: - npm ci - -docker-compose up -d postgres localstack - cp .env.example .env - while ! make validate-psql-connection validate-aws-connection; \ - do echo "waiting for postgres, localstack bootup"; \ - sleep 0.5; \ - done - -test-tenant: - make tenant TENANT='test' - -dev-tenant: - make tenant TENANT='dev' +test-tenant: TENANT = 'test' +test-tenant: tenant -ci-setup: env-setup test-tenant - npm ci --loglevel=error - make run -e DOCKER_DNS=$(DOCKER_DNS) 2>&1 | tee test_logs & - while ! grep -q "starting in Actix" test_logs; \ - do echo "ci-test: waiting for bootup..." && sleep 4; \ - done - # NOTE: `make db-init` finally starts a postgres container and runs all the migrations with locked-schema option - # to prevent update of schema.rs for both cac and experimentation. - # NOTE: The container spinned-up here is the actual container being used in development - echo setup completed successfully!!! +dev-tenant: TENANT = 'dev' +dev-tenant: tenant -setup: env-setup +SETUP_DEPS = db localstack +ifdef CI + SETUP_DEPS += test-tenant +endif +setup: $(SETUP_DEPS) + npm ci kill: -pkill -f target/debug/superposition & @@ -110,22 +125,14 @@ backend: build: frontend backend -run: kill frontend - while ! make validate-psql-connection validate-aws-connection; \ - do echo "waiting for postgres, localstack bootup"; \ - sleep 0.5; \ - done - make superposition -e DOCKER_DNS=$(DOCKER_DNS) +run: kill db localstack frontend superposition -run_legacy: kill build - while ! make validate-psql-connection validate-aws-connection; \ - do echo "waiting for postgres, localstack bootup"; \ - sleep 0.5; \ - done - make superposition_legacy -e DOCKER_DNS=$(DOCKER_DNS) +run_legacy: kill build db localstack superposition_legacy -ci-test: ci-setup +test: cargo test + @echo "Awaiting superposition boot..." + @timeout 200s bash -c "while ! curl --silent 'http://localhost:8080/health' 2>&1 > /dev/null; do sleep 0.5; done" npm run test tailwind: