Skip to content

Commit

Permalink
Update makefile and docs for dev setup
Browse files Browse the repository at this point in the history
* Align Makefile syntax with ScanCode.io
* Update postgresdb make target to work on Linux reusing ScanCode.io
  config for sudo
* Add new postgresdb_clean make target to clear the DB and users
* Add new envfile_dev target to also create a DB_PASSWORD in .env file
* Add extra ABOUT file checks to check target
* Add new upgrade target
* Update documentation accordingly

Signed-off-by: Philippe Ombredanne <[email protected]>
  • Loading branch information
pombredanne committed Dec 7, 2024
1 parent 9ed6314 commit 8312190
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
53 changes: 41 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ PYTHON_EXE=python3.12
VENV_LOCATION=.venv
ACTIVATE?=. ${VENV_LOCATION}/bin/activate;
MANAGE=${VENV_LOCATION}/bin/python manage.py
# Do not depend on Python to generate the SECRET_KEY
GET_SECRET_KEY=`head -c50 /dev/urandom | base64 | head -c50`
PIP_ARGS=--find-links=./thirdparty/dist/ --no-index --no-cache-dir
GET_SECRET_KEY=`cat /dev/urandom | head -c 50 | base64`
# Customize with `$ make envfile ENV_FILE=/etc/dejacode/.env`
ENV_FILE=.env
DOCS_LOCATION=./docs
DOCKER_COMPOSE=docker compose -f docker-compose.yml
DOCKER_EXEC=${DOCKER_COMPOSE} exec
DOCKER_EXEC=${DOCKER_COMPOSE} eUSERNAMExec
DB_NAME=dejacode_db
DB_USERNAME=dejacode
DB_PASSWORD=dejacode
DB_CONTAINER_NAME=db
DB_INIT_FILE=./data/postgresql/initdb.sql.gz
POSTGRES_INITDB_ARGS=--encoding=UTF-8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8
TIMESTAMP=$(shell date +"%Y-%m-%d_%H%M")

# Use sudo for postgres, only on Linux
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
SUDO_POSTGRES=sudo -u postgres
else
SUDO_POSTGRES=
endif

virtualenv:
@echo "-> Bootstrap the virtualenv with PYTHON_EXE=${PYTHON_EXE}"
${PYTHON_EXE} -m venv ${VENV_LOCATION}
Expand All @@ -42,12 +52,17 @@ envfile:
@echo "-> Create the .env file and generate a secret key"
@if test -f ${ENV_FILE}; then echo "${ENV_FILE} file exists already"; exit 1; fi
@mkdir -p $(shell dirname ${ENV_FILE}) && touch ${ENV_FILE}
@echo "SECRET_KEY=${GET_SECRET_KEY}" > ${ENV_FILE}
@echo SECRET_KEY=\"${GET_SECRET_KEY}\" > ${ENV_FILE}

envfile_dev: envfile
@echo "-> Update the .env file for development"
@echo DATABASE_PASSWORD=\"dejacode\" >> ${ENV_FILE}

doc8:
@echo "-> Run doc8 validation"
@${ACTIVATE} doc8 --max-line-length 100 --ignore-path docs/_build/ \
--ignore-path docs/installation_and_sysadmin/ --quiet docs/
--ignore-path docs/installation_and_sysadmin/ \
--quiet docs/

valid:
@echo "-> Run Ruff format"
Expand All @@ -62,14 +77,17 @@ check:
@${ACTIVATE} ruff format --check
@echo "-> Running ABOUT files validation"
@${ACTIVATE} about check ./thirdparty/
@${ACTIVATE} about check ./data/
@${ACTIVATE} about check ./dje/
@${ACTIVATE} about check ./dejacode_toolkit/
@$(MAKE) doc8

check-deploy:
@echo "-> Check Django deployment settings"
${MANAGE} check --deploy

clean:
@echo "-> Cleaning the Python env"
@echo "-> Clean the Python env"
rm -rf .venv/ .*_cache/ *.egg-info/ build/ dist/
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete

Expand All @@ -94,17 +112,28 @@ migrate:
@echo "-> Apply database migrations"
${MANAGE} migrate

# make postgresdb DB_PASSWORD=YOUR_PASSWORD
upgrade:
@echo "-> Upgrade local git checkout"
@git pull
@$(MAKE) migrate

postgresdb:
@echo "-> Configure PostgreSQL database"
@echo "-> Create database user ${DB_NAME}"
@createuser --no-createrole --no-superuser --login --inherit --createdb '${DB_USERNAME}' || true
@psql -c "alter user ${DB_USERNAME} with encrypted password '${DB_PASSWORD}';" || true
@${SUDO_POSTGRES} createuser --no-createrole --no-superuser --login --inherit --createdb '${DB_USERNAME}' || true
@${SUDO_POSTGRES} psql -c "alter user ${DB_USERNAME} with encrypted password '${DB_PASSWORD}';" || true
@echo "-> Drop ${DB_NAME} database if exists"
@dropdb ${DB_NAME} || true
@echo "-> Create ${DB_NAME} database"
@createdb --owner=${DB_USERNAME} ${POSTGRES_INITDB_ARGS} ${DB_NAME}
@${SUDO_POSTGRES} dropdb ${DB_NAME} || true
@echo "-> Create ${DB_NAME} database: createdb --owner=${DB_USERNAME} ${POSTGRES_INITDB_ARGS} ${DB_NAME}"
@${SUDO_POSTGRES} createdb --owner=${DB_USERNAME} ${POSTGRES_INITDB_ARGS} ${DB_NAME}
@gunzip < ${DB_INIT_FILE} | psql --username=${DB_USERNAME} ${DB_NAME}
@echo "-> Apply database migrations"
${MANAGE} migrate

postgresdb_clean:
@echo "-> Drop PostgreSQL user and database"
@${SUDO_POSTGRES} dropdb ${DB_NAME} || true
@${SUDO_POSTGRES} dropuser '${DB_USERNAME}' || true

run:
${MANAGE} runserver 8000 --insecure
Expand Down Expand Up @@ -143,4 +172,4 @@ log:
createsuperuser:
${DOCKER_EXEC} web ./manage.py createsuperuser

.PHONY: virtualenv conf dev envfile check doc8 valid check-deploy clean initdb postgresdb migrate run test docs build psql bash shell log createsuperuser
.PHONY: virtualenv conf dev envfile envfile_dev check doc8 valid check-deploy clean initdb postgresdb postgresdb_clean migrate upgrade run test docs build psql bash shell log createsuperuser
7 changes: 6 additions & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Clone and Configure

#. Create an environment file::

make envfile
make envfile_dev

Database
--------
Expand All @@ -213,6 +213,11 @@ To set up the database user, database, and table, run::

make postgresdb

To entirely delete the database user, database, and tables, run::

make postgresdb_clean


Tests
-----

Expand Down

2 comments on commit 8312190

@th
Copy link

@th th commented on 8312190 Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pombredanne
I think there’s a copy/paste mistake with username in the middle of exec:
DOCKER_EXEC=${DOCKER_COMPOSE} eUSERNAMExec

@pombredanne
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pombredanne I think there’s a copy/paste mistake with username in the middle of exec: DOCKER_EXEC=${DOCKER_COMPOSE} eUSERNAMExec

@th yes, Good catch. @tdruez fixed it with https://github.com/aboutcode-org/dejacode/blame/1a4f7039d823fdddc05e22f0f61406f1f22f9077/Makefile#L20

Please sign in to comment.