forked from traefikturkey/onramp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
406 lines (305 loc) · 13.3 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# include .env variable in the current environment
ifneq (,$(wildcard ./.env))
include .env
export
endif
export DOCKER_COMPOSE_FILES := $(wildcard services-enabled/*.yml) $(wildcard overrides-enabled/*.yml) $(wildcard docker-compose.*.yml)
export DOCKER_COMPOSE_FLAGS := -f docker-compose.yml $(foreach file, $(DOCKER_COMPOSE_FILES), -f $(file))
# look for the second target word passed to make
export SERVICE_PASSED_DNCASED := $(strip $(word 2,$(MAKECMDGOALS)))
export SERVICE_PASSED_UPCASED := $(strip $(subst -,_,$(shell echo $(SERVICE_PASSED_DNCASED) | tr a-z A-Z )))
# get the boxes ip address and the current users id and group id
export HOSTIP := $(shell ip route get 1.1.1.1 | grep -oP 'src \K\S+')
export PUID := $(shell id -u)
export PGID := $(shell id -g)
export HOST_NAME := $(or $(HOST_NAME), $(shell hostname))
# check if we should use docker-compose or docker compose
ifeq (, $(shell which docker-compose))
DOCKER_COMPOSE := docker compose
else
DOCKER_COMPOSE := docker-compose
endif
# setup PLEX_ALLOWED_NETWORKS defaults if they are not already in the .env file
ifndef PLEX_ALLOWED_NETWORKS
export PLEX_ALLOWED_NETWORKS := $(HOSTIP/24)
endif
# check what editor is available
ifdef VSCODE_IPC_HOOK_CLI
EDITOR := code
else
EDITOR := nano
endif
# used to look for the file in the services-enabled folder when [start|stop|pull]-service is used
SERVICE_FILES := $(wildcard services-enabled/$(SERVICE_PASSED_DNCASED).yml) $(wildcard overrides-enabled/$(SERVICE_PASSED_DNCASED)-*.yml)
SERVICE_FLAGS := --project-directory ./ $(foreach file, $(SERVICE_FILES), -f $(file))
# use the rest as arguments as empty targets aka: MAGIC
EMPTY_TARGETS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(EMPTY_TARGETS):;@:)
# this is the default target run if no other targets are passed to make
# i.e. if you just type: make
start: build
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) up -d
remove-orphans: build
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) up -d --remove-orphans
up: build
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) up --force-recreate --remove-orphans --abort-on-container-exit
down:
-$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) down --remove-orphans
-docker volume ls --quiet --filter "label=remove_volume_on=down" | xargs -r docker volume rm
pull:
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) pull
logs:
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) logs -f $(SERVICE_PASSED_DNCASED)
restart: down start
update: down pull start
bash-run:
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) run -it --rm $(SERVICE_PASSED_DNCASED) sh
bash-exec:
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) exec $(SERVICE_PASSED_DNCASED) sh
#########################################################
#
# service commands
#
#########################################################
start-service: COMPOSE_IGNORE_ORPHANS = true
start-service: build enable-service
$(DOCKER_COMPOSE) $(SERVICE_FLAGS) up -d --force-recreate $(SERVICE_PASSED_DNCASED)
down-service: stop-service
stop-service:
-$(DOCKER_COMPOSE) $(SERVICE_FLAGS) stop $(SERVICE_PASSED_DNCASED)
restart-service: down-service start-service
update-service: down-service pull-service start-service
pull-service:
$(DOCKER_COMPOSE) $(SERVICE_FLAGS) pull $(SERVICE_PASSED_DNCASED)
enable-game: etc/$(SERVICE_PASSED_DNCASED)
@ln -s ../services-available/games/$(SERVICE_PASSED_DNCASED).yml ./services-enabled/$(SERVICE_PASSED_DNCASED).yml || true
enable-service: etc/$(SERVICE_PASSED_DNCASED) services-enabled/$(SERVICE_PASSED_DNCASED).yml
etc/$(SERVICE_PASSED_DNCASED):
@mkdir -p ./etc/$(SERVICE_PASSED_DNCASED)
services-enabled/$(SERVICE_PASSED_DNCASED).yml:
@ln -s ../services-available/$(SERVICE_PASSED_DNCASED).yml ./services-enabled/$(SERVICE_PASSED_DNCASED).yml || true
remove-game: disable-service
disable-game: disable-service
remove-service: disable-service
disable-service: stop-service
rm ./services-enabled/$(SERVICE_PASSED_DNCASED).yml
rm ./overrides-enabled/$(SERVICE_PASSED_DNCASED)-*.yml 2> /dev/null || true
create-service:
envsubst '$${SERVICE_PASSED_DNCASED},$${SERVICE_PASSED_UPCASED}' < ./.templates/service.template > ./services-available/$(SERVICE_PASSED_DNCASED).yml
$(EDITOR) ./services-available/$(SERVICE_PASSED_DNCASED).yml
create-game:
envsubst '$${SERVICE_PASSED_DNCASED},$${SERVICE_PASSED_UPCASED}' < ./.templates/service.template > ./services-available/games/$(SERVICE_PASSED_DNCASED).yml
$(EDITOR) ./services-available/games/$(SERVICE_PASSED_DNCASED).yml
#########################################################
#
# compose commands
#
#########################################################
start-compose: COMPOSE_IGNORE_ORPHANS = true
start-compose: build
$(DOCKER_COMPOSE) $(SERVICE_FLAGS) up -d --force-recreate
down-compose: stop-compose
stop-compose:
-$(DOCKER_COMPOSE) $(SERVICE_FLAGS) stop
update-compose: down-compose pull-compose start-compose
pull-compose:
$(DOCKER_COMPOSE) $(SERVICE_FLAGS) pull
#########################################################
#
# staging commands
#
#########################################################
start-staging: build
ACME_CASERVER=https://acme-staging-v02.api.letsencrypt.org/directory $(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) up -d --force-recreate
@echo "waiting 30 seconds for cert DNS propogation..."
@sleep 30
@echo "open https://$(HOST_NAME).$(HOST_DOMAIN)/traefik in a browser"
@echo "and check that you have a staging cert from LetsEncrypt!"
@echo ""
@echo "if you don't get a LetsEncrypt staging cert run the following command and look for error messages:"
@echo "$(DOCKER_COMPOSE) logs | grep acme"
@echo ""
@echo "otherwise run the following command if you successfully got a staging certificate:"
@echo "make down-staging"
down-staging:
$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FLAGS) down
$(MAKE) clean-acme
#########################################################
#
# list commands
#
#########################################################
list-games:
@ls -1 ./services-available/games | sed -n 's/\.yml$ //p'
list-services:
@ls -1 ./services-available/ | sed -e 's/\.yml$ //'
list-overrides:
@ls -1 ./overrides-available/ | sed -e 's/\.yml$ //'
list-external:
@ls -1 ./etc/traefik/available/ | sed -e 's/\.yml$ //'
#########################################################
#
# build related commands
#
#########################################################
build: .env etc/authelia/configuration.yml etc/dashy/dashy-config.yml etc/prometheus/conf etc/adguard/conf/AdGuardHome.yaml
.env:
cp .templates/env.template .env
$(EDITOR) .env
etc/authelia/configuration.yml:
envsubst '$${HOST_DOMAIN}' < ./etc/authelia/configuration.template > ./etc/authelia/configuration.yml
etc/adguard/conf/AdGuardHome.yaml:
envsubst '$${ADGUARD_PASSWORD}, $${ADGUARD_USER}, $${HOST_DOMAIN}' < ./etc/adguard/conf/AdGuardHome.template > ./etc/adguard/conf/AdGuardHome.yaml
etc/pihole/dnsmasq/03-custom-dns-names.conf:
envsubst '$${HOST_DOMAIN}, $${HOSTIP} ' < ./etc/pihole/dns.template > ./etc/pihole/dnsmasq/03-custom-dns-names.conf
etc/dashy/dashy-config.yml:
mkdir -p ./etc/dashy
touch ./etc/dashy/dashy-config.yml
etc/prometheus/conf:
mkdir -p etc/prometheus/conf
cp --no-clobber --recursive etc/prometheus/conf-originals/* etc/prometheus/conf
#########################################################
#
# override commands
#
#########################################################
enable-override: overrides-enabled/$(SERVICE_PASSED_DNCASED).yml
overrides-enabled/$(SERVICE_PASSED_DNCASED).yml:
@ln -s ../overrides-available/$(SERVICE_PASSED_DNCASED).yml ./overrides-enabled/$(SERVICE_PASSED_DNCASED).yml || true
remove-override: disable-override
disable-override:
rm ./overrides-enabled/$(SERVICE_PASSED_DNCASED).yml
#########################################################
#
# external commands
#
#########################################################
disable-external:
rm ./etc/traefik/enabled/$(SERVICE_PASSED_DNCASED).yml
enable-external:
@cp ./etc/traefik/available/$(SERVICE_PASSED_DNCASED).yml ./etc/traefik/enabled/$(SERVICE_PASSED_DNCASED).yml || true
create-external:
envsubst '$${SERVICE_PASSED_DNCASED},$${SERVICE_PASSED_UPCASED}' < ./.templates/external.template > ./etc/traefik/available/$(SERVICE_PASSED_DNCASED).yml
$(EDITOR) ./etc/traefik/available/$(SERVICE_PASSED_DNCASED).yml
#########################################################
#
# helper commands
#
#########################################################
edit-env:
$(EDITOR) .env
install-node-exporter:
curl -s https://gist.githubusercontent.com/ilude/2cf7a3b7712378c6b9bcf1e1585bf70f/raw/setup_node_exporter.sh?$(date +%s) | /bin/bash -s | tee build.log
#########################################################
#
# backup and restore up commands
#
#########################################################
export-backup: create-backup
@echo "export-backup is depercated and will be removed in the future, please use make create-backup"
import-backup: restore-backup
@echo "import-backup is depercated and will be removed in the future, please use make restore-backup"
create-backup: backups
sudo tar --exclude=.keep -czf ./backups/onramp-config-backup-$(HOST_NAME)-$(shell date +'%y-%m-%d-%H%M').tar.gz ./etc ./services-enabled ./overrides-enabled .env || true
create-nfs-backup: create-backup
sudo mount -t nfs $(NFS_SERVER):$(NFS_BACKUP_PATH) $(NFS_BACKUP_TMP_DIR)
sudo mv ./backups/onramp-config-backup* $NFS_BACKUP_TEMP_DIR &
sudo umount $(NFS_BACKUP_TMP_DIR)
backups:
mkdir -p ./backups/
restore-backup:
sudo tar -xvf ./backups/traefik-config-backup.tar.gz
restore-nfs-backup:
sudo mount -t nfs $(NFS_SERVER):$(NFS_BACKUP_PATH) $(NFS_BACKUP_TMP_DIR)
# look for backups folder, if not there, create it. Works when it's a brand new installation
ifeq (! -d ./backups )
mkdir ./backups
endif
# clean out old backups if exist
rm -rf ./backups/*
# find latest backup file on NFS share
find $(NFS_BACKUP_TMP_DIR) -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1 | cp "{}" ./backups
# expand archive to /apps/onramp and wait until finished
sudo tar -xvf ./backups/* -C /apps/onramp &
wait
# cleanup
sudo umount $(NFS_BACKUP_TMP_DIR)
echo -n "Please run 'make restart' to apply restored backup"
#########################################################
#
# clean up commands
#
#########################################################
clean-acme:
@echo "removing acme certificate file"
sudo rm etc/traefik/letsencrypt/acme.json
remove-etc:
rm -rf ./etc/$(or $(SERVICE_PASSED_DNCASED),no_service_passed)/
reset-database-folder:
rm -rf ./media/databases/$(or $(SERVICE_PASSED_DNCASED),no_service_passed)/
git checkout ./media/databases/$(or $(SERVICE_PASSED_DNCASED),no_service_passed)/.keep
reset-etc: remove-etc
git checkout ./etc/$(or $(SERVICE_PASSED_DNCASED),no_service_passed)/
stop-reset-etc: stop-service reset-etc
reset-database: remove-etc reset-database-folder
#########################################################
#
# cloudflare tunnel commands
#
#########################################################
cloudflare-login:
$(DOCKER_COMPOSE) run --rm cloudflared login
create-tunnel:
$(DOCKER_COMPOSE) run --rm cloudflared tunnel create $(CLOUDFLARE_TUNNEL_NAME)
$(DOCKER_COMPOSE) run --rm cloudflared tunnel route dns $(CLOUDFLARE_TUNNEL_NAME) $(CLOUDFLARE_TUNNEL_HOSTNAME)
delete-tunnel:
$(DOCKER_COMPOSE) run --rm cloudflared tunnel cleanup $(CLOUDFLARE_TUNNEL_NAME)
$(DOCKER_COMPOSE) run --rm cloudflared tunnel delete $(CLOUDFLARE_TUNNEL_NAME)
show-tunnel:
$(DOCKER_COMPOSE) run --rm cloudflared tunnel info $(CLOUDFLARE_TUNNEL_NAME)
#########################################################
#
# mariadb commands
#
#########################################################
ifndef MARIADB_CONTAINER_NAME
MARIADB_CONTAINER_NAME=mariadb
endif
# enable this to be asked for password to when you connect to the database
#mysql-connect = @docker exec -it $(MARIADB_CONTAINER_NAME) mysql -p
# enable this to not be asked for password to when you connect to the database
mysql-connect = @docker exec -it $(MARIADB_CONTAINER_NAME) mysql -p$(MARIADB_ROOT_PASSWORD)
first_arg = $(shell echo $(EMPTY_TARGETS)| cut -d ' ' -f 1)
second_arg = $(shell echo $(EMPTY_TARGETS)| cut -d ' ' -f 2)
password := $(shell openssl rand -hex 16)
mariadb-console:
$(mysql-connect)
create-database:
$(mysql-connect) -e 'CREATE DATABASE IF NOT EXISTS $(first_arg);'
show-databases:
$(mysql-connect) -e 'show databases;'
create-db-user:
$(mysql-connect) -e 'CREATE USER $(first_arg) IDENTIFIED BY "'$(second_arg)'";'
create-db-user-pw:
@echo Here is your password : $(password) : Please put it in the .env file under the service name
$(mysql-connect) -e 'CREATE USER IF NOT EXISTS $(first_arg) IDENTIFIED BY "'$(password)'";'
grant-db-perms:
$(mysql-connect) -e 'GRANT ALL PRIVILEGES ON '$(first_arg)'.* TO $(first_arg);'
remove-db-user:
$(mysql-connect) -e 'DROP USER $(first_arg);'
create-user-with-db: create-db-user-pw create-database grant-db-perms
#########################################################
#
# test and debugging commands
#
#########################################################
excuse:
@curl -s programmingexcuses.com | egrep -o "<a[^<>]+>[^<>]+</a>" | egrep -o "[^<>]+" | sed -n 2p
test-smtp:
envsubst .templates/smtp.template | nc localhost 25
# https://stackoverflow.com/questions/7117978/gnu-make-list-the-values-of-all-variables-or-macros-in-a-particular-run
echo:
@$(MAKE) -pn | grep -A1 "^# makefile"| grep -v "^#\|^--" | grep -e "^[A-Z]+*" | sort
env:
@env | sort
include env_ifs.mk