Skip to content

Commit

Permalink
🧱(infra) select databases to run sites
Browse files Browse the repository at this point in the history
Running the activate script it also provides the possibility of select for what of the supported databases it should build the app
  • Loading branch information
Tiago-Salles committed Jan 13, 2025
1 parent 657d0da commit 5dfc6d5
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# -- Load .env file
ifneq (,$(wildcard .env))
include .env
export $(shell sed 's/=.*//' .env)
endif

# -- Terminal colors
COLOR_INFO = \033[0;36m
COLOR_RESET = \033[0m
Expand All @@ -13,15 +19,13 @@ COMPOSE = \
NGINX_IMAGE_TAG="$(NGINX_IMAGE_TAG)" \
DOCKER_USER="$(DOCKER_UID):$(DOCKER_GID)" \
docker compose
COMPOSE_UP_WAIT = $(COMPOSE) up --wait --wait-timeout 60
COMPOSE_RUN = $(COMPOSE) run --rm
COMPOSE_RUN_APP = $(COMPOSE_RUN) app-dev
COMPOSE_EXEC = $(COMPOSE) exec
COMPOSE_EXEC_APP = $(COMPOSE_EXEC) app-dev
COMPOSE_TEST_RUN = $(COMPOSE) run --rm -e DJANGO_CONFIGURATION=Test
COMPOSE_TEST_RUN_APP = $(COMPOSE_TEST_RUN) app-dev
WAIT_DB = $(COMPOSE_RUN) dockerize -wait tcp://db:5432 -timeout 60s
WAIT_ES = $(COMPOSE_RUN) dockerize -wait tcp://elasticsearch:9200 -timeout 60s
WAIT_SENTINEL = $(COMPOSE_RUN) dockerize -wait tcp://redis-sentinel:26379 -wait tcp://redis-primary:6379 -timeout 20s

# -- Node

Expand All @@ -44,7 +48,7 @@ bootstrap: \
env.d/aws \
env.d/development \
data/media/$(RICHIE_SITE)/.keep \
data/db/$(RICHIE_SITE) \
data/${ACTIVATED_DB}/$(RICHIE_SITE) \
stop \
build-front \
build \
Expand Down Expand Up @@ -75,79 +79,77 @@ add-site: generate-site ## add a new site to the site factory
@bin/activate

# == Docker
build: .env ## build all containers
build: ## build all containers
$(COMPOSE) build app
$(COMPOSE) build nginx
$(COMPOSE) build app-dev
.PHONY: build

reset: .env ## Remove database and local files
reset: ## Remove database and local files
$(COMPOSE) stop
rm -Ir data/* || exit 0
$(COMPOSE) rm db
$(COMPOSE) rm ${ACTIVATED_DB}
.PHONY: reset

down: .env ## stop & remove containers
down: ## stop & remove containers
@$(COMPOSE) down
.PHONY: down

logs: .env ## display app logs (follow mode)
logs: ## display app logs (follow mode)
@$(COMPOSE) logs -f app-dev
.PHONY: logs

run: .env ## start the wsgi (production) or development server
@$(COMPOSE) up -V -d redis-sentinel
@$(WAIT_SENTINEL)
@$(COMPOSE) up -d nginx
@$(COMPOSE) up -d app-dev
@$(WAIT_DB)
run: ## start the wsgi (production) or development server
@$(COMPOSE_UP_WAIT) -V -d redis-sentinel
@$(COMPOSE_UP_WAIT) -d nginx
@$(COMPOSE_UP_WAIT) -d app-dev
.PHONY: run

stop: .env ## stop the development server
stop: ## stop the development server
@$(COMPOSE) stop
.PHONY: stop

info: .env ## get activated site info
info: ## get activated site info
@cat .env
.PHONY: info

# == Frontend
build-front: install-front build-ts build-sass ## build front-end application
.PHONY: build-front

build-sass: .env ## build Sass files to css
build-sass: ## build Sass files to css
@$(YARN) build-sass
.PHONY: build-sass

build-sass-production: .env ## build Sass files to css (production mode)
build-sass-production: ## build Sass files to css (production mode)
@$(YARN) build-sass-production
.PHONY: build-sass-production

build-ts: .env ## build ts(x) files to js
build-ts: ## build ts(x) files to js
@$(YARN) build-ts
.PHONY: build-ts

build-ts-production: .env ## build ts(x) files to js (production mode)
build-ts-production: ## build ts(x) files to js (production mode)
@$(YARN) build-ts-production
.PHONY: build-ts-production

install-front: .env ## install front-end dependencies
install-front: ## install front-end dependencies
@$(YARN) install
.PHONY: install-front

install-front-production: .env ## install front-end dependencies (production mode)
install-front-production: ## install front-end dependencies (production mode)
@$(YARN) install --frozen-lockfile
.PHONY: install-front-production

lint-front-prettier: .env ## run prettier linter over ts(x) & scss files
lint-front-prettier: ## run prettier linter over ts(x) & scss files
@$(YARN) prettier
.PHONY: lint-front-prettier

lint-front-prettier-write: .env ## run prettier over ts(x) & scss files -- beware! overwrites files
lint-front-prettier-write: ## run prettier over ts(x) & scss files -- beware! overwrites files
@$(YARN) prettier-write
.PHONY: lint-front-prettier-write

lint-front-eslint: .env ## run eslint over ts files
lint-front-eslint: ## run eslint over ts files
@$(YARN) lint
.PHONY: lint-front-eslint

Expand All @@ -157,15 +159,15 @@ lint-front: \
lint-front-eslint
.PHONY: lint-front

test-back: .env ## run back-end tests
test-back: ## run back-end tests
bin/pytest
.PHONY: test-back

watch-sass: .env ## watch changes in Sass files
watch-sass: ## watch changes in Sass files
@$(YARN) watch-sass
.PHONY: watch-sass

watch-ts: .env ## watch changes in js files
watch-ts: ## watch changes in js files
@$(YARN) watch-ts
.PHONY: watch-ts

Expand All @@ -174,19 +176,18 @@ env.d/aws:
cp env.d/aws.dist env.d/aws

# == Django
check: .env ## perform django checks
check: ## perform django checks
@$(MANAGE) check
.PHONY: check

demo-site: .env ## create a demo site
@$(COMPOSE) up -d db
@$(WAIT_DB)
demo-site: ## create a demo site
@$(COMPOSE) up -d ${ACTIVATED_DB}
@$(MANAGE) flush
@$(MANAGE) create_demo_site
@${MAKE} search-index
.PHONY: demo-site

init: .env ## create base site structure
init: ## create base site structure
@$(MANAGE) richie_init
@${MAKE} search-index
.PHONY: init
Expand All @@ -202,37 +203,37 @@ lint-back: \
lint-back-raincoat
.PHONY: lint-back

lint-back-black: .env ## lint back-end python sources with black
lint-back-black: ## lint back-end python sources with black
@echo 'lint:black started…'
@$(COMPOSE_TEST_RUN_APP) black .
.PHONY: lint-back-black

lint-back-flake8: .env ## lint back-end python sources with flake8
lint-back-flake8: ## lint back-end python sources with flake8
@echo 'lint:flake8 started…'
@$(COMPOSE_TEST_RUN_APP) flake8
.PHONY: lint-back-flake8

lint-back-isort: .env ## automatically re-arrange python imports in back-end code base
lint-back-isort: ## automatically re-arrange python imports in back-end code base
@echo 'lint:isort started…'
@$(COMPOSE_TEST_RUN_APP) isort --atomic .
.PHONY: lint-back-isort

lint-back-pylint: .env ## lint back-end python sources with pylint
lint-back-pylint: ## lint back-end python sources with pylint
@echo 'lint:pylint started…'
@$(COMPOSE_TEST_RUN_APP) pylint .
.PHONY: lint-back-pylint

lint-back-raincoat: .env ## lint back-end python sources with raincoat
lint-back-raincoat: ## lint back-end python sources with raincoat
@echo 'lint:raincoat started…'
@$(COMPOSE_TEST_RUN_APP) raincoat
.PHONY: lint-back-raincoat

lint-back-bandit: .env ## lint back-end python sources with bandit
lint-back-bandit: ## lint back-end python sources with bandit
@echo 'lint:bandit started…'
@$(COMPOSE_TEST_RUN_APP) bandit -qr .
.PHONY: lint-back-bandit

import-fixtures: .env ## import fixtures
import-fixtures: ## import fixtures
@$(MANAGE) import_fixtures -v3
.PHONY: import-fixtures

Expand All @@ -242,48 +243,47 @@ i18n: \
i18n-front
.PHONY: i18n

i18n-back: .env ## create/update .po files and compile .mo files used for i18n
i18n-back: ## create/update .po files and compile .mo files used for i18n
@$(MANAGE) makemessages --keep-pot --all
@echo 'Reactivating obsolete strings (allow overriding strings defined in dependencies)'
@$(COMPOSE_RUN_APP) find ./ -type f -name django.po -exec sed -i 's/#~ //g' {} \;
@$(MANAGE) compilemessages
.PHONY: i18n-back

i18n-front: .env ## Extract and compile translation files used for react-intl
i18n-front: ## Extract and compile translation files used for react-intl
@$(YARN) extract-translations
@$(YARN) compile-translations
.PHONY: i18n-front

migrate: .env ## perform database migrations
@$(COMPOSE) up -d db
@$(WAIT_DB)
migrate: ## perform database migrations
@echo "Start and wait for ${ACTIVATED_DB} to be up..."
@$(COMPOSE_UP_WAIT) -d ${ACTIVATED_DB}
@$(MANAGE) migrate
.PHONY: migrate

search-index: .env ## (re)generate the Elasticsearch index
@$(COMPOSE) up -d elasticsearch
@$(WAIT_ES)
search-index: ## (re)generate the Elasticsearch index
@echo "Start and wait for ${ACTIVATED_DB} & elasticsearch to be up..."
@$(COMPOSE_UP_WAIT) -d ${ACTIVATED_DB} elasticsearch
@$(MANAGE) bootstrap_elasticsearch
.PHONY: search-index

superuser: .env ## create a DjangoCMS superuser
@$(COMPOSE) up -d db
@$(WAIT_DB)
superuser: ## Create an admin user with password "admin"
@echo "Start and wait for ${ACTIVATED_DB} to be up..."
@$(COMPOSE_UP_WAIT) -d ${ACTIVATED_DB}
@$(MANAGE) createsuperuser
.PHONY: superuser

# == CI
ci-check: .env ## run django check management command on productin image
ci-check: ## run django check management command on productin image
$(COMPOSE_RUN) app python manage.py check
.PHONY: ci-check

ci-migrate: .env ## run django migrate command on production image
@$(COMPOSE) up -d db
@$(WAIT_DB)
ci-migrate: ## run django migrate command on production image
@$(COMPOSE) up -d ${ACTIVATED_DB}
$(COMPOSE_RUN) app python manage.py migrate
.PHONY: ci-migrate

ci-run: .env ## start the wsgi server (and linked services)
ci-run: ## start the wsgi server (and linked services)
@$(COMPOSE) up -d app
# As we use a remote docker environment, we should explicitly use the same
# network to check containers status
Expand All @@ -292,7 +292,7 @@ ci-run: .env ## start the wsgi server (and linked services)
docker run --network container:fun_elasticsearch_1 --rm jwilder/dockerize -wait tcp://localhost:9200 -timeout 60s
.PHONY: ci-run

ci-version: .env ## check version file bundled in the docker image
ci-version: ## check version file bundled in the docker image
$(COMPOSE_RUN) --no-deps app cat version.json
.PHONY: ci-version

Expand All @@ -301,14 +301,14 @@ clean: ## restore repository state as it was freshly cloned
git clean -idx
.PHONY: clean

data/media/$(RICHIE_SITE)/.keep: .env
data/media/$(RICHIE_SITE)/.keep:
@echo 'Preparing media volume...'
@mkdir -p data/media/$(RICHIE_SITE)
@touch data/media/$(RICHIE_SITE)/.keep

data/db/$(RICHIE_SITE): .env
@echo 'Preparing db volume...'
@mkdir -p data/db/$(RICHIE_SITE)
data/${ACTIVATED_DB}/$(RICHIE_SITE):
@echo 'Preparing ${ACTIVATED_DB} volume...'
@mkdir -p data/${ACTIVATED_DB}/$(RICHIE_SITE)

env.d/development:
cp env.d/development.dist env.d/development
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ declare -a sites
declare -i default=1
declare -i choice
declare -i n_sites
declare -a databases=("mysql" "postgresql" "sqlite")
declare -i database_choice
declare -i n_databases

# List sites by looking in the "sites" directory
# and store them in an array
Expand All @@ -17,6 +20,7 @@ read -r -a sites <<< "$(
xargs
)"
n_sites=${#sites[@]}
n_databases=${#databases[@]}

if [[ n_sites -eq 0 ]]; then
(>&2 echo "You should first add a site to the project by running: make add-site")
Expand Down Expand Up @@ -49,5 +53,27 @@ if [[ ${choice} -le 0 ]]; then
choice=${default}
fi

prompt="Select an available database to activate:\\n"
for (( i=0; i<n_databases; i++ )); do
prompt+="[$((i+1))] ${databases[$i]}"
if [[ $((i+1)) -eq ${default} ]]; then
prompt+=" (default)"
fi
prompt+="\\n"
done
prompt+="Your choice: "
read -r -p "$(echo -e "${prompt}")" database_choice

if [[ ${choice} -le 0 ]]; then
database_choice=${default}
fi

# Activate the chosen site
echo "RICHIE_SITE=${sites[$((choice-1))]}" > .env
echo "ACTIVATED_DB=${databases[$((database_choice-1))]}" >> .env

if [[ ${database_choice} == 1 ]]; then
echo "DB_PORT=3306" >> .env
else [[ ${database_choice} == 2 ]];
echo "DB_PORT=5472" >> .env
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
mysql:
image: mysql:8.0
ports:
- "3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_ROOT_PASSWORD=
- MYSQL_DATABASE=richie_${RICHIE_SITE:?}
command: --default-authentication-plugin=caching_sha2_password
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 1s
timeout: 2s
retries: 300
volumes:
- ./data/db/${RICHIE_SITE:?}:/var/lib/mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
postgresql:
image: postgres:16.4
ports:
- "5472:5432"
env_file:
- env.d/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 1s
timeout: 2s
retries: 300
volumes:
- ./data/db/${RICHIE_SITE:?}:/var/lib/postgresql/data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file is empty on purpose!
Loading

0 comments on commit 5dfc6d5

Please sign in to comment.