This commit is contained in:
PierreDubrulle 2024-01-23 12:10:26 +01:00
parent 0f43088dab
commit 2f9ea5683f
4 changed files with 107 additions and 17 deletions

View file

@ -1,4 +1,5 @@
SHELL := /bin/bash SHELL := /bin/bash
ENV_FILE := .env
# PYTHON # PYTHON
VENV_NAME := .venv VENV_NAME := .venv
@ -10,29 +11,75 @@ REQUIREMENTS := requirements-dev.txt
DOCKER_COMPOSE := docker-compose DOCKER_COMPOSE := docker-compose
DOCKER_COMPOSE_FILE := docker-compose.yml DOCKER_COMPOSE_FILE := docker-compose.yml
DOCKER_DIRECTORY := dev_env DOCKER_DIRECTORY := dev_env
POSTGRES_CONTAINER := postgres_chariotte
.PHONY: all venv install activate clean dev_env_start dev_env_stop MAX_TRIES := 10
all: venv install activate .PHONY: python venv_python install_python clean_python db_start db_stop front_install help
venv: .DEFAULT_GOAL := help
help:
@echo "Available targets:"
@echo " python : Create and activate a Python virtual environment. Install dependencies."
@echo " install_python: Install Python dependencies from requirements file."
@echo " clean_python : Remove the Python virtual environment."
@echo " db_start : Start Docker containers and run database migrations."
@echo " db_stop : Stop Docker containers and remove volumes."
@echo " front_install : Install front-end dependencies using Yarn or NPM."
@echo " run_server : Run the Django development server."
@echo " help : Display this help message."
python: venv_python install_python
venv_python:
python3 -m venv $(VENV_NAME) python3 -m venv $(VENV_NAME)
@echo "Activating virtual environment with 'source $(VENV_NAME)/bin/activate'. Run 'deactivate' to exit." @echo "Activating virtual environment with 'source $(VENV_NAME)/bin/activate'. Run 'deactivate' to exit."
install: venv install_python: venv_python
@source $(VENV_NAME)/bin/activate && $(PIP) install -r $(REQUIREMENTS) @$(VENV_NAME)/bin/pip install --upgrade pip
@$(VENV_NAME)/bin/pip install -r $(REQUIREMENTS)
clean: clean_python:
rm -rf $(VENV_NAME) rm -rf $(VENV_NAME)
dev_env_start: db_start:
if [ ! $(DOCKER_COMPOSE)]; then \ @if [ ! -x $$(command -v $(DOCKER_COMPOSE)) ]; then \
echo "Docker Compose not installed. Please install Docker Compose."; \ echo "Docker Compose not installed. Please install Docker Compose."; \
exit 1; \ exit 1; \
fi fi
$(DOCKER_COMPOSE) -f $(DOCKER_DIRECTORY)/$(DOCKER_COMPOSE_FILE) up -d @$(DOCKER_COMPOSE) -f $(DOCKER_DIRECTORY)/$(DOCKER_COMPOSE_FILE) --env-file ${ENV_FILE} up -d
@for ((tries=0; tries<$(MAX_TRIES); tries++)); do \
if docker inspect --format '{{json .State.Health.Status }}' $(POSTGRES_CONTAINER) | grep -q 'healthy'; then \
break; \
fi; \
echo "Waiting for the container to be healthy..."; \
sleep 1; \
done; \
if [ $$tries -ge $(MAX_TRIES) ]; then \
echo "Timed out waiting for the container to be healthy."; \
exit 1; \
fi
@echo "Running migrations..." @echo "Running migrations..."
$(PYTHON) manage.py migrate @$(PYTHON) manage.py migrate
dev_env_stop: db_stop:
$(DOCKER_COMPOSE) -f $(DOCKER_DIRECTORY)/$(DOCKER_COMPOSE_FILE) down -v @$(DOCKER_COMPOSE) -f $(DOCKER_DIRECTORY)/$(DOCKER_COMPOSE_FILE) down -v
front_install:
@if command -v yarn > /dev/null; then \
echo "Installing dependencies with Yarn ..."; \
yarn install; \
elif command -v npm > /dev/null; then \
echo "Installing dependencies with NPM ..."; \
npm install; \
else \
echo "Neither Yarn nor NPM found. Please install one of them."; \
exit 1; \
fi
run_server:
@$(PYTHON) manage.py runserver
superuser:
@$(PYTHON) manage.py createsuperuser

View file

@ -3,11 +3,21 @@ version: '3'
services: services:
db: db:
image: postgres:13 image: postgres:13
container_name: postgres_chariotte
restart: always restart: always
env_file: healthcheck:
- ../.env test: /usr/bin/pg_isready
interval: 3s
timeout: 30s
retries: 5
start_period: 5s
environment:
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports: ports:
- 5432:5432 - ${POSTGRES_PORT}:5432
volumes: volumes:
- ./postgres-data:/var/lib/postgresql/data - ./postgres-data:/var/lib/postgresql/data

View file

@ -4,6 +4,39 @@ First, clone the project
git clone git@gitlab.com:la-chariotte/la_chariotte.git git clone git@gitlab.com:la-chariotte/la_chariotte.git
``` ```
## Setting up the Development Environment
Welcome to our development environment! To make it easy to set up your local environment, we use a Makefile that automates several development-related tasks, such as creating a Python virtual environment, installing dependencies, starting Docker services, and more.
### Prerequisites
Make sure you have the following installed on your system:
- [`Docker`](https://www.docker.com/get-started/)
- Docker Compose](https://docs.docker.com/compose/install/linux/)
- `Make`
### Configuration
Create the .env file: Create an .env file at the root of the project and [configure the necessary environment variables](../.env.example).
### Using the Makefile
The Makefile offers several commands to simplify management of the development environment.
make python: Creates a virtual Python environment, activates it and installs dependencies.
make db_start: Starts the Postgres database in a Docker container and launches migrations.
make db_stop: Stops the container and associated volume.
make front_install: Installs front-end dependencies using Yarn or NPM, depending on their availability on your system.
make run_server: Launches the Django application.
make help: Displays a summary of available commands and their descriptions.
### Run
Follow these steps to set up your development environment:
1. Run make python to create and activate the virtual environment and install dependencies.
2. Use make db_start to start Docker services. This command waits until the PostgreSQL container is ready before proceeding.
3. To install the front-end dependencies, use make front_install.
**Congratulations! Your development environment is now ready. You can use make db_stop to stop Docker services when you're done.**
Explore and develop your application with complete peace of mind!
## Virtual environment ## Virtual environment
To prevent the project's necessary libraries from conflicting with those on your system, it may be beneficial to install a virtual environment: To prevent the project's necessary libraries from conflicting with those on your system, it may be beneficial to install a virtual environment:

View file

@ -96,8 +96,8 @@ if os.getenv("POSTGRES_PASSWORD"):
DATABASES["default"]["PASSWORD"] = os.getenv("POSTGRES_PASSWORD") DATABASES["default"]["PASSWORD"] = os.getenv("POSTGRES_PASSWORD")
if os.getenv("POSTGRES_HOST"): if os.getenv("POSTGRES_HOST"):
DATABASES["default"]["HOST"] = os.getenv("POSTGRES_HOST") DATABASES["default"]["HOST"] = os.getenv("POSTGRES_HOST")
if os.getenv("DB_PORT"): if os.getenv("POSTGRES_PORT"):
DATABASES["default"]["PORT"] = os.getenv("DB_PORT") DATABASES["default"]["PORT"] = os.getenv("POSTGRES_PORT")
# Password validation # Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators