-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
366 lines (274 loc) · 11.1 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
# People dev Makefile
# `make help` to get the list of available rules
SHELL=/bin/bash
-include .env
KEYBASE ?= /keybase
KBPATH ?= $(KEYBASE)/team/epfl_people.prod
-include $(KBPATH)/$(SECRETS)
COMPOSE_FILE ?= docker-compose.yml
SSH_AUTH_SOCK_FILE ?= $(SSH_AUTH_SOCK)
SSH_AUTH_SOCK_DIR = $(dir $(SSH_AUTH_SOCK_FILE))
ELE_SRCDIR ?= ../elements
ELE_DSTDIR = ./app/assets/stylesheets/elements
ELE_FILES = $(addprefix $(ELE_DSTDIR)/,elements.css vendors.css bootstrap-variables.scss)
REBUNDLE ?= $(shell if [ -f Gemfile.lock.docker ] ; then echo "no" ; else echo "yes" ; fi)
# NOCIMAGE ?= nicolaka/netshoot
NOCIMAGE ?= jonlabelle/network-tools
# Figure out the ip address of the host machine so that we can use "public"
# dns names served by traefik from within the containers when the name is
# resolved as 127.0.0.1 like for all Giovanni's domains with glob ssl certs.
DOCKER_IP ?= $(shell docker run -it --rm $(NOCIMAGE) dig +short host.docker.internal)
KCDUMPFILE ?= tmp/dbdumps/keycloak.sql
export
SQL=docker compose exec -T mariadb mariadb -u root --password=mariadb
SQLDUMP=docker compose exec -T mariadb mariadb-dump --password=mariadb
# ----------------------------------------------------------- Run/stop local app
.PHONY: css dev up reload kc down fulldown tunnel_up tunnel_down
## start the dev env with sass builder and app server (try to emulate ./bin/dev)
dev: up
./bin/dev -f Procfile.docker
make down
css:
bin/rails dartsass:watch
## start the dev tunnel and start all the servers
up: tunnel_up dcup
## restart the webapp container
reload: envcheck
docker compose stop webapp
KILLPID=1 docker compose up -d
## restart puma withoud taking down the running container (in case of changes in gem's code within the container)
pumareload:
docker compose exec webapp kill -SIGUSR2 1
## stop the basic servers (all except test)
down: tunnel_down
docker compose down
## stop everything including keycloak and the test server
fulldown:
docker compose --profile test down
docker compose --profile kc down
docker compose down
tunnel_up:
./bin/tunneld.sh -m local start
tunnel_down:
./bin/tunneld.sh -m local stop
dcup: envcheck Gemfile.lock $(ELE_FILES)
docker compose up --no-recreate -d
Gemfile.lock: Gemfile.lock.docker
cp $< $@
# --------------------------------------------------- Interaction with local app
.PHONY: logs ps top console shell dbconsole debug redis dbstatus
## tail -f the logs
logs:
docker compose logs -f
llogs:
docker compose logs --since 10s -f
## show the status of running containers
ps:
docker compose ps
## show memory and cpu usage off all containers
top:
docker stats
## start a rails console on the webapp container
console: dcup
docker compose exec webapp ./bin/rails console
## start a shell on the webapp container
shell: dcup
docker compose exec webapp /bin/bash
## start an sql console con the database container
dbconsole: dcup
docker compose exec mariadb mariadb -u root --password=mariadb
# docker compose exec webapp ./bin/rails dbconsole
## attach the console of the rails app for debugging
debug:
docker-compose attach webapp
## start console for interacting with redis db
redis:
docker compose exec cache valkey-cli
## toggle Rails caching in dev (will persist reloads as it is just a file in tmp)
devcache:
docker compose exec webapp bin/rails dev:cache
## start a shell within a container including all usefull network tools
noc:
docker compose --profile noc run --rm noc
## show mariadb/INNODB status
dbstatus:
echo $$(echo "SHOW ENGINE INNODB STATUS" | $(SQL))
## show code stats and versions
about:
./bin/rails about && ./bin/rails stats
# -------------------------------------------------------------- Container image
.PHONY: build rebuild
## build the web app and atela container
build: envcheck $(ELE_FILES) #codecheck
if [ "$(REBUNDLE)" == "yes" ] ; then rm -f Gemfile.lock ; else cp Gemfile.lock.docker Gemfile.lock ; fi
docker compose build
if [ "$(REBUNDLE)" == "yes" ] ; then docker compose run webapp cat /rails/Gemfile.lock > Gemfile.lock.docker ; fi
## build image discarding all cached layers
rebuild: envcheck
docker compose build --no-cache
envcheck: .env .git/hooks/pre-commit
# ------------------------------------------ Source code and dev env maintenance
.PHONY: erd codecheck cop docop dodocop minor patch
## generate an entity relation diagram with mermaid_erd
erd:
docker compose exec webapp ./bin/rails mermaid_erd
## check the code with linter and run all automated tests (TODO)
codecheck: cop
# TODO: automated tests too...
# bundle-audit returns 1 if there are vulnerabilities => prevents build
# nicer gui available at https://audit.fastruby.io
# bundle exec bundle-audit check --update
# bundle exec brakeman
## run rubocop linter to check code copliance with style and syntax rules
cop:
bundle exec rubocop --extra-details 2>/dev/null
## run rubocop linter in autocorrect mode
docop:
./bin/bundle exec rubocop --autocorrect
## run rubocop linter in autocorrect-all mode
dodocop:
./bin/bundle exec rubocop --autocorrect-all
## increase minor version
minor:
./bin/rails version:minor
## increase patch version
patch:
./bin/rails version:patch
.git/hooks/pre-commit:
if [ ! -l .git/hooks ] ; then mv .git/hooks .git/hooks.trashme && ln -s ../.git_hooks .git/hooks ; fi
.env:
@echo ".env file not present. Please copy .env.sample and edit to fit your setup"
exit 1
$(ELE_DSTDIR)/bootstrap-variables.scss: $(ELE_SRCDIR)/assets/config/bootstrap-variables.scss
grep -E -v "^@include" $< > $@
# grep -E -v "^@include" $< | \
# gsed 's/theme-color(/map.get($$theme-colors, /g' | \
# gsed '/^@use /a @use "sass:map";' > $@
$(ELE_DSTDIR)/%.css: $(ELE_SRCDIR)/dist/css/%.css
cp $< $@
$(ELE_SRCDIR)/dist/css/*.css:
cd $(ELEMENTS_DIR) && yarn build
# ---------------------------------------------------------------------- Testing
.PHONY: test testup test-system
## run automated tests
test:
docker compose exec -e RAILS_ENV=test webapp ./bin/rails test
## prepare and run the test server
testup:
docker compose --profile test up --no-recreate -d
# TODO: find a way to fix this by selecting the good deps (stopped working
# after an upgrade don't know if due to ruby or packages version.
docker compose exec webapp sed -i '0,/end/{s/initialize(\*)/initialize(*args)/}' /usr/local/bundle/gems/capybara-3.39.0/lib/capybara/selenium/logger_suppressor.rb
docker compose exec webapp sed -i '0,/end/{s/super/super args/}' /usr/local/bundle/gems/capybara-3.39.0/lib/capybara/selenium/logger_suppressor.rb
# testprepare:
# docker compose exec webapp ./bin/rails db:test:prepare
# docker compose exec -e RAILS_ENV=test webapp ./bin/rails db:migrate
test-system: testup
docker compose exec webapp ./bin/rails test:system
test-models:
docker compose exec webapp ./bin/rails test:models
# -------------------------------------------------- Cache and off-line webmocks
## flush cache from redis db
flush:
docker compose exec cache redis-cli FLUSHALL
## copy webmocks from keybase. This will enable the off-line use (set ENABLE_WEBMOCK=true in .env)
webmocks:
rsync -av --delete $(KBPATH)/webmocks/ test/fixtures/webmocks/
## generate and restore webmocks (can only be used from computer with access to remote servers)
refresh_webmocks:
@ENABLE_WEBMOCK=false WEBMOCKS=$(KBPATH)/webmocks URLS=$(KBPATH)/webmock_urls.txt APIPASS=$(EPFLAPI_PASSWORD) RAILS_ENV=development ./bin/rails data:webmocks
rsync -av --delete $(KBPATH)/webmocks/ test/fixtures/webmocks/
# ------------------------------------------------ migration data/db maintenance
.PHONY: migrate seed reseed nukedb restore_webmocks webmocks
## run rails migration
migrate: dcup
docker compose exec webapp ./bin/rails db:migrate
## run rails migration and seed with initial data
seed: migrate webmocks
docker compose exec webapp bin/rails db:seed
docker compose exec webapp bin/rails legacy:import
docker compose exec webapp bin/rails data:courses
## reload the list of all courses from ISA
courses: dcup
docker compose exec webapp bin/rails data:courses
## seed the data for Work::Sciper
scipers: dcup
docker compose exec webapp bin/rails legacy:seed_scipers
legaimport: dcup
docker compose exec webapp bin/rails legacy:import
nukestorage:
docker compose exec webapp /bin/bash -c "rm -rf storage/[0-9a-zA-Z][0-9a-zA-Z]"
## restart with a fresh new dev database for the webapp
reseed:
make nukedb
make nukestorage
sleep 2
make seed
fastreseed:
make nukedb
sleep 2
docker compose exec webapp bin/rails db:seed
# ## load changed schema and seed
# reschema:
# docker compose exec webapp bin/rails db:schema:load
# docker compose exec webapp bin/rails db:seed
# make courses
## delete the people database
nukedb:
echo "DROP DATABASE people" | $(SQL)
echo "CREATE DATABASE people;" | $(SQL)
## delete keycloak database and recreate it
rekc:
docker compose stop keycloak
echo "DROP DATABASE IF EXISTS keycloak;" | $(SQL)
echo "CREATE DATABASE keycloak;" | $(SQL)
# cat keycloak/initdb.d/keycloak-database-and-user.sql | $(SQL)
echo "GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'%';" | $(SQL)
@echo "Keycloak db reset."
kconfig: up
@/bin/bash -c 'while curl -I https://keycloak.dev.jkldsa.com/admin/master/console 2>/dev/null | grep -q "HTTP/2 502" ; do echo "waiting for kc to be alive (interrupt if it continues for more than ~40 secs)"; sleep 5; done'
cd ops && ./possible.sh --dev -t keycloak.config
# ---------------------------------------------------------- Legacy DB from prod
# since we moved this to the external script we keep them just as a reminder
.PHONY: restore restore_cv restore_cadi restore_dinfo restore_accred restore_bottin
## restore the legacy databases (copy from the on-line DB server to local ones)
restore:
./bin/restoredb.sh all
## restore the legacy `accred` database only
restore_accred:
./bin/restoredb.sh accred
## restore the legacy `bottin` database only
restore_bottin:
./bin/restoredb.sh bottin
## restore the legacy `cadi` database only
restore_cadi:
./bin/restoredb.sh cadi
## restore the legacy `cv` database only
restore_cv:
./bin/restoredb.sh cv
## restore the legacy `dinfo` database only
restore_dinfo:
./bin/restoredb.sh dinfo
# --------------------------------------------------- Test (dev-like) deployment
.PHONY: nata_patch nata_reseed
## patch the source code of the app mounted on the test server for Natalie
nata_patch:
cd ops && ./possible.sh --test -t people.src.patch
nata_reinit: dcup
ssh peonext 'rm -rf data/people/storage/*'
docker compose exec webapp tar cvf - storage | ssh peonext "tar -xvf - -C data/people"
$(SQLDUMP) people | ssh peonext "./bin/peopledb"
make nata_patch
## reseed the database on the test server for Natalie
nata_reseed: test_patch
cd ops && ./possible.sh --test -t people.db.reseed
# ------------------------------------------------------------------------------
.PHONY: clean
clean:
rm -f api_examples.txt
rm -rf test/fixtures/webmocks
rm -f $(ELE_FILES)
# ------------------------------------------------------------------------------
.PHONY: help
help:
@cat Makefile | gawk 'BEGIN{print "Available rules:";} /^# ---+ /{gsub(/ -+/,"", $$0); printf("\n%s:\n", $$0);} /^##/{gsub("^##", "", $$0); i=$$0; getline; gsub(/:.*$$/, "", $$0); printf("%-16s %s\n", $$0, i);}'