-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakefile
More file actions
192 lines (166 loc) · 5.51 KB
/
Copy pathMakefile
File metadata and controls
192 lines (166 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Makefile for Docker Compose commands
# Load .env so we can switch database without editing this file
include .env
# Default Docker image name for direct builds
IMAGE_NAME ?= wagtail-starter-kit
# Determine which compose override to use based on DATABASE env var
# Allowed values: sqlite (default), postgres, mysql
ifeq ($(DATABASE),postgres)
COMPOSE_DB_FILE := compose.postgresql.override.yaml
else ifeq ($(DATABASE),mysql)
COMPOSE_DB_FILE := compose.mysql.override.yaml
else
COMPOSE_DB_FILE := compose.sqlite3.override.yaml
endif
DC = docker compose -f compose.yaml -f $(COMPOSE_DB_FILE)
.PHONY: help
help:
@echo "Usage: make [target]"
@echo ""
@echo "Docker Compose commands (DATABASE=$(DATABASE) -> $(COMPOSE_DB_FILE))"
@echo " build Build the Docker containers"
@echo " up Start the Docker containers"
@echo " down Stop and remove the Docker containers"
@echo " destroy Stop and remove the Docker containers, networks, and volumes"
@echo " run Run the Django development server"
@echo ""
@echo "Container commands"
@echo " migrate Run Django migrations"
@echo " superuser Create a superuser"
@echo " restoredb Restore the database from a backup"
@echo " sh Execute a command in a running container"
@echo " restart Restart the containers"
@echo ""
@echo "Miscellaneous"
@echo " check-env Validate required .env variables for selected database"
@echo " quickstart Build, start, and run the containers (npm & docker)"
@echo " docker-size Show the size of the Docker image $(IMAGE_NAME)"
@echo " docker-build-size Build the Docker image and show its size"
@echo " docker-prune Remove unused Docker data (images, containers, networks)"
@echo " Add WITH_VOLUMES=1 to include volumes"
@echo " docker-prune-all Remove unused Docker data including volumes"
@echo " clean Clean up generated files and folders (node_modules, static, media, etc.)"
@echo " frontend Build the frontend (npm)"
@echo " start Build the front end and start local development server (npm)"
@echo ""
# Required env variables
REQUIRED_COMMON := DJANGO_ALLOWED_HOSTS DJANGO_SECRET_KEY WAGTAIL_SITE_NAME WAGTAILADMIN_BASE_URL
ifeq ($(DATABASE),postgres)
REQUIRED_DB := POSTGRES_HOST POSTGRES_PORT POSTGRES_DB POSTGRES_USER POSTGRES_PASSWORD
else ifeq ($(DATABASE),mysql)
REQUIRED_DB := MYSQL_HOST MYSQL_PORT MYSQL_DATABASE MYSQL_USER MYSQL_PASSWORD MYSQL_ROOT_PASSWORD MYSQL_ROOT_HOST
else
REQUIRED_DB :=
endif
.PHONY: check-env
check-env:
@if [ ! -f .env ]; then \
echo "Missing .env. Copy .env.example to .env and set required values."; \
exit 1; \
fi; \
set -a; . ./.env; set +a; \
missing=0; \
for v in $(REQUIRED_COMMON) $(REQUIRED_DB); do \
val=$$(printenv $$v); \
if [ -z "$$val" ]; then \
echo "Missing required env var: $$v"; \
missing=1; \
fi; \
done; \
if [ $$missing -ne 0 ]; then \
echo "\nSet the missing variables in .env (DATABASE=$(DATABASE))."; \
exit 1; \
else \
echo "Environment OK for DATABASE=$(DATABASE)"; \
fi
# Build the containers
.PHONY: build
build: check-env
$(DC) build
# Start the containers
.PHONY: up
up: check-env
$(DC) up -d
# Stop and remove containers, networks, and volumes
.PHONY: down
down:
$(DC) down
# Restart the containers
.PHONY: restart
restart:
$(DC) restart
# Execute a command in a running container
.PHONY: sh
sh:
$(DC) exec app bash
# Run the Django development server
.PHONY: run
run:
$(DC) exec app python manage.py runserver 0.0.0.0:8000
# Stop and remove the Docker containers, networks, and volumes
.PHONY: destroy
destroy:
$(DC) down -v
# Run migrations
.PHONY: migrate
migrate:
$(DC) exec app python manage.py migrate
# Create a superuser
.PHONY: superuser
superuser:
$(DC) exec app python manage.py createsuperuser
# Collect static files
.PHONY: collectstatic
collectstatic:
$(DC) exec app python manage.py collectstatic --noinput
# Run tests, you will need to have run `make collectstatic` first
.PHONY: test
test:
$(DC) exec app python manage.py test
# Quickstart
.PHONY: quickstart
quickstart: frontend build up migrate collectstatic test run
# Build the fontend
.PHONY: frontend
frontend:
npm install
npm run build
# Start the frontend and run the local development server
.PHONY: start
start:
npm install
npm run build
npm run start
# Show the size of the built Docker image
.PHONY: docker-size
docker-size:
@docker image ls $(IMAGE_NAME) --format '{{.Repository}}:{{.Tag}} {{.Size}}' | sed -n '1p' || (echo "Image not found: $(IMAGE_NAME)" && exit 1)
# Build the Docker image directly and print its size
.PHONY: docker-build-size
docker-build-size:
docker build -t $(IMAGE_NAME) .
@docker image ls $(IMAGE_NAME) --format '{{.Repository}}:{{.Tag}} {{.Size}}' | sed -n '1p'
# Prune unused Docker resources
.PHONY: docker-prune
docker-prune:
@if [ "$(WITH_VOLUMES)" = "1" ]; then \
echo "Pruning unused Docker data (including volumes)"; \
docker system prune -f --volumes; \
else \
echo "Pruning unused Docker data (images, containers, networks)"; \
docker system prune -f; \
fi
# Always prune including volumes
.PHONY: docker-prune-all
docker-prune-all:
@echo "Pruning unused Docker data (including volumes)"
docker system prune -f --volumes
# Clean up
.PHONY: clean
clean:
@echo "This will remove all generated files and folders (node_modules, static, media + db.sqlite3)"
@read -p "Are you sure? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
rm -rf ./node_modules ./static ./static_compiled ./media db.sqlite3; \
fi