From 84005ef12123d510563aefaa6e887f99db881a56 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 | 2 +- docker-compose.yaml | 2 - makefile | 122 +++++++++++++++++------------ 3 files changed, 71 insertions(+), 55 deletions(-) diff --git a/.github/workflows/ci_check_pr.yaml b/.github/workflows/ci_check_pr.yaml index a8b155b3..feaecb1e 100644 --- a/.github/workflows/ci_check_pr.yaml +++ b/.github/workflows/ci_check_pr.yaml @@ -126,6 +126,6 @@ jobs: - name: run tests shell: bash run: | - make ci-test + make test CI=1 env: APP_ENV: "TEST" 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/makefile b/makefile index 921d655d..c0ad8477 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,19 @@ 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 $$?) +CARGO_FLAGS := --color always --no-default-features + +ifeq ($(HAS_DOCKER),0) + DOCKER := docker +else ifeq ($(HAS_PODMAN),0) + DOCKER := podman +else + $(error "Neither docker nor podman found, please install one of them.") +endif + +COMPOSE := $(DOCKER) compose .PHONY: db-init @@ -14,22 +27,36 @@ FEATURES ?= ssr validate-psql-connection cac +db: +ifndef CI + $(COMPOSE) up -d postgres +else + @echo "Skipping postgres container-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 container-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 +71,22 @@ 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) +ifeq ($(shell test -f .env; echo $$?),1) + @echo ".env not present, copying .env.example as .env" + @cp .env.example .env +endif + npm ci kill: -pkill -f target/debug/superposition & @@ -78,18 +94,24 @@ kill: get-password: export DB_PASSWORD=`./docker-compose/localstack/get_db_password.sh` && echo $$DB_PASSWORD +superposition: CARGO_FLAGS += --features=$(FEATURES) superposition: - cargo run --color always --bin superposition --no-default-features --features=$(FEATURES) + cargo build $(CARGO_FLAGS) --bin superposition superposition-example: cargo run --bin cac-demo-app +superposition_legacy: CARGO_FLAGS += --features='ssr superposition_types/disable_db_data_validation +superposition_legacy: CARGO_FLAGS += superposition_types/disable_db_data_validation +superposition_legacy: CARGO_FLAGS += context_aware_config/disable_db_data_validation +superposition_legacy: CARGO_FLAGS += experimentation_platform/disable_db_data_validation' superposition_legacy: - cargo run --color always --bin superposition --no-default-features --features='ssr superposition_types/disable_db_data_validation context_aware_config/disable_db_data_validation experimentation_platform/disable_db_data_validation' + cargo run $(CARGO_FLAGS) --bin superposition +superposition_dev: CARGO_FLAGS += --features=$(FEATURES) superposition_dev: # export DB_PASSWORD=`./docker-compose/localstack/get_db_password.sh` - cargo watch -x 'run --color always --bin superposition --no-default-features --features=$(FEATURES)' + cargo watch -x 'run $(CARGO_FLAGS) --bin superposition' frontend: @@ -110,22 +132,18 @@ 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 + @./target/debug/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: setup frontend superposition cargo test + @echo "Running superposition" + @./target/debug/superposition & + @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: