-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
240 lines (206 loc) · 6.89 KB
/
Makefile
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Concise introduction to GNU Make:
# https://swcarpentry.github.io/make-novice/reference.html
include .env
docker_compose = \
docker compose \
--file ./docker-compose.yml
# Taken from https://www.client9.com/self-documenting-makefiles/
help : ## Print this help
@awk -F ':|##' '/^[^\t].+?:.*?##/ {\
printf "\033[36m%-30s\033[0m %s\n", $$1, $$NF \
}' $(MAKEFILE_LIST)
.PHONY : help
.DEFAULT_GOAL := help
scan : ## Scan for additional hard disks without restarting the virtual machine
sudo rescan-scsi-bus.sh
.PHONY : scan
user : ## Add user `${USER}` (he/she will have access to restricted areas like staging with the correct password), for example, `make USER=jdoe user`
sudo htpasswd ./nginx/.htpasswd ${USER}
.PHONY : user
setup : ## Setup machine
LC_ALL=C.UTF-8 \
NON_WWW_PRODUCTION_HOST=${NON_WWW_PRODUCTION_HOST} \
EMAIL_ADDRESS=${EMAIL_ADDRESS} \
SMTP_HOST=${SMTP_HOST} \
SMTP_PORT=${SMTP_PORT} \
ansible-playbook ./local.yml
.PHONY : setup
pull : ## Pull images
COMPOSE_DOCKER_CLI_BUILD=1 \
DOCKER_BUILDKIT=1 \
${docker_compose} pull
.PHONY : pull
# To debug errors during build add `--progress plain \` to get additional
# output.
build : pull ## Build images
COMPOSE_DOCKER_CLI_BUILD=1 \
DOCKER_BUILDKIT=1 \
${docker_compose} build \
--pull \
--no-cache
.PHONY : build
up : ## (Re)create and (re)start services
${docker_compose} up \
--force-recreate \
--renew-anon-volumes \
--remove-orphans \
--detach \
reverse_proxy
.PHONY : up
deploy : setup build up ## Deploy services, that is, setup machine, pull and build images, and (re)create and (re)start services
.PHONY : deploy
logs : ## Follow logs
${docker_compose} logs \
--follow
.PHONY : logs
down : ## Stop containers and remove containers, networks, volumes, and images created by `deploy`
${docker_compose} down \
--remove-orphans
.PHONY : down
list : ## List all containers with health status
${docker_compose} ps \
--no-trunc \
--all
.PHONY : list
list-services : ## List all services specified in the docker-compose file (used by Monit)
${docker_compose} config \
--services
.PHONY : list-services
# See https://docs.docker.com/config/daemon/#view-stack-traces
daemon-logs : ## Follow Docker daemon logs
sudo journalctl --follow --unit docker.service
.PHONY : daemon-logs
reload-daemon : ## Reload Docker daemon
sudo systemctl reload docker
.PHONY : reload-daemon
# See https://docs.docker.com/config/containers/runmetrics/
docker-stats : ## Show Docker run-time metrics
docker stats
.PHONY : docker-stats
crontab : ## List user's and root's contab
crontab -l
sudo crontab -u root -l
.PHONY : crontab
cron-logs : ## Follow cron logs
sudo journalctl --follow --unit cron.service
.PHONY : cron-logs
monit-logs : ## Follow monit logs
sudo tail --follow /var/log/monit.log
.PHONY : monit-logs
smtp-logs : ## Follow monit logs
sudo tail --follow \
/var/log/msmtp \
~/.msmtp.log
.PHONY : smtp-logs
vacuum-journald : ## Vaccum journald logs keeping seven days worth of logs
journalctl --rotate
journalctl --vacuum-time=7d
.PHONY : vacuum-journald
renew-tls : renew-certificates deploy ## Renew Transport Layer Security (TLS) certificates needed for the `S` in `HTTPS`
.PHONY : renew-tls
backup-database : ## Backup production database and prunce backups
mkdir --parents /app/data/backups
make \
--directory=/app/production \
--file /app/production/Makefile.production \
--keep-going \
BACKUP_DIRECTORY=/app/data/backups/$(shell date +"\%Y-\%m-\%d_\%H_\%M_\%S") \
begin-maintenance \
backup \
end-maintenance \
prune-backups
.PHONY : backup-database
prune-docker : ## Prune docker
docker system prune \
--force \
--filter "until=24h"
.PHONY : prune-docker
dummy-certificates : ## Create dummy certificates for `${OUT_PATH}`
${docker_compose} run \
--rm \
--entrypoint " \
openssl req \
-x509 \
-nodes \
-newkey rsa:4096 \
-days 1 \
-keyout '${OUT_PATH}/privkey.pem' \
-out '${OUT_PATH}/fullchain.pem' \
-subj '/CN=localhost' \
" \
certbot
.PHONY : dummy-certificates
delete-dummy-certificates : ## Delete dummy certificates for `${DOMAINS}`
${docker_compose} run \
--rm \
--entrypoint " \
rm -R -f \
/etc/letsencrypt/live/${DOMAINS} \
/etc/letsencrypt/archive/${DOMAINS} \
/etc/letsencrypt/renewal/${DOMAINS}.conf \
" \
certbot
.PHONY : delete-dummy-certificates
request-certificates : ## Request certificates
${docker_compose} run \
--rm \
--entrypoint " \
certbot certonly \
--webroot \
-w /var/www/certbot \
${STAGING_ARG} \
${DOMAIN_ARGS} \
--email ${EMAIL} \
--key-type ecdsa \
--elliptic-curve secp256r1 \
--must-staple \
--agree-tos \
--force-renewal \
" \
certbot
.PHONY : request-certificates
renew-certificates : ## Renew certificates
${docker_compose} run \
--rm \
--entrypoint " \
certbot renew \
" \
certbot
.PHONY: renew-certificates
begin-maintenance : ## Begin maintenance
for environment in staging production ; do \
make --directory=/app/$${environment} --file=Makefile.production begin-maintenance ; \
done
.PHONY : begin-maintenance
end-maintenance : ## End maintenance
if [ -f /var/run/reboot-required ] ; then \
echo 'Reboot and run `make end-maintenance`' ; \
else \
for environment in staging production ; do \
make --directory=/app/$${environment} --file=Makefile.production end-maintenance ; \
done ; \
fi
.PHONY : end-maintenance
upgrade-system : ## Upgrade system (Is used to install the newest versions of all packages currently installed on the system from the sources enumerated in /etc/apt/sources.list. Packages currently installed with new versions available are retrieved and upgraded. Under no circumstances are currently installed packages removed, or packages not already installed retrieved and installed. New versions of currently installed packages that cannot be upgraded without changing the install status of another package will be left at their current version.)
make begin-maintenance
sudo apt-get --assume-yes update
sudo apt-get --assume-yes upgrade
sudo apt-get --assume-yes auto-remove
sudo apt-get --assume-yes clean
sudo apt-get --assume-yes auto-clean
make end-maintenance
.PHONY : upgrade-system
dist-upgrade-system : ## Upgrade system (In addition to performing the function of `upgrade-system`, also intelligently handles changing dependencies with new versions of packages. It will attempt to upgrade the most important packages at the expense of less important ones if necessary. It may therefore remove some packages.)
make begin-maintenance
sudo apt-get --assume-yes update
sudo apt-get --assume-yes dist-upgrade
sudo apt-get --assume-yes auto-remove
sudo apt-get --assume-yes clean
sudo apt-get --assume-yes auto-clean
make end-maintenance
.PHONY : dist-upgrade-system
dry-run-unattended-upgrades : ## Dry-run unattended upgrades for testing purposes
sudo unattended-upgrades \
--dry-run \
--debug
.PHONY : dry-run-unattended-upgrades