-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
1221 lines (1012 loc) · 33.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# .ONESHELL only works for GNU Make 3.82+
.ONESHELL:
SHELL = bash
TIME=gtime -f '%es'
RED='\e[0;31m'
NC='\033[0m' # No Color
ERROR="${RED}ERROR${NC}"
YELLOW='\e[38;5;82m'
DEBUG="${YELLOW}DEBUG${NC}"
default: pre-commit
local-test:
@require=$${DATA_DIR_ROOT:?envar is missing}
feed_id=$${FEED_ID:-testblog}
# This is necessary when sending through prod smtp-in
export DOMAIN_NAME=feedsubscription.com
rm -v $$DATA_DIR_ROOT/accounts/da772874f7963b4612ec6c59005c7fbe1b0264302a501568cfed1e5035080ef5/feeds/$${feed_id}/lastPostMetadata.json
node_modules/.bin/ts-node src/app/cron-cli.ts rss-checking $${feed_id}
node_modules/.bin/ts-node src/app/cron-cli.ts email-sending $${feed_id}
email-sending:
node_modules/.bin/ts-node src/app/cron-cli.ts email-sending testblog
rss-checking:
@require=$${DATA_DIR_ROOT:?envar is missing}
rm -v $$DATA_DIR_ROOT/accounts/da772874f7963b4612ec6c59005c7fbe1b0264302a501568cfed1e5035080ef5/feeds/{gurdiga,testblog,blogger}/lastPostMetadata.json
node_modules/.bin/ts-node src/app/cron-cli.ts rss-checking testblog
node_modules/.bin/ts-node src/app/cron-cli.ts rss-checking gurdiga
node_modules/.bin/ts-node src/app/cron-cli.ts rss-checking blogger
test:
@export TS_NODE_TRANSPILE_ONLY=true
node_modules/.bin/mocha \
--require ts-node/register \
--reporter dot \
'src/**/*.spec.ts'
test-quiet:
@printf "Test... "
$(TIME) $(MAKE) test > /dev/null
t: test
tw:
@export TS_NODE_TRANSPILE_ONLY=true
node_modules/.bin/mocha \
--require ts-node/register \
--reporter dot \
--watch \
--watch-files src \
'src/**/*.spec.ts'
edit:
code -n .
e: edit
compile:
node_modules/.bin/tsc --project tsconfig.json && \
node_modules/.bin/tsc --project src/web-ui/tsconfig.json
compile-quiet:
@printf "Compile... "
$(TIME) $(MAKE) compile check-no-node-in-web-ui > /dev/null
check-no-node-in-web-ui:
@grep --line-number --color=always -RF 'node:' dist/api/web-ui-scripts/ |
tee /dev/stderr | ifne false
c: compile
cw:
node_modules/.bin/tsc --watch --project tsconfig.json &
node_modules/.bin/tsc --watch --project src/web-ui/tsconfig.json
pre-commit: lint-quiet compile-quiet test-quiet format-check-quiet
pc: pre-commit
lint: lint-mocha-only lint-require-strict-interpolation lint-docker-compose lint-dockerfile lint-shell-scripts lint-nginx-config lint-dnsmasq-conf
l: lint
lint-quiet:
@printf "Lint... "
$(TIME) $(MAKE) lint > /dev/null
# docker cp website:/etc/nginx/nginx.conf website/nginx/ # plus, comment out irrelevant pieces
# sudo cp -r ./.tmp/certbot/conf/live/feedsubscription.com /etc/letsencrypt/live/
lint-nginx-config:
@nginx_image=`yq -r .services.website.image docker-compose.yml`
docker run --rm \
-v $$PWD/website/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $$PWD/website/nginx/conf.d/website.conf:/etc/nginx/conf.d/website.conf:ro \
-v $$PWD/.tmp/certbot/conf/live/feedsubscription.com/fullchain.pem:/etc/letsencrypt/live/feedsubscription.com/fullchain.pem:ro \
-v $$PWD/.tmp/certbot/conf/live/feedsubscription.com/privkey.pem:/etc/letsencrypt/live/feedsubscription.com/privkey.pem:ro \
-e NGINX_ENTRYPOINT_QUIET_LOGS=1 \
"$$nginx_image" \
nginx -q -t -c /etc/nginx/nginx.conf
lint-docker-compose:
docker compose --file docker-compose.yml config
lint-dnsmasq-conf:
@command="dnsmasq --test -C docker-services/resolver/etc/dnsmasq.conf"
if ! $$command &> /dev/null; then
$$command
fi
lint-dockerfile:
find . -name Dockerfile |
# tee /dev/stderr | # DEBUG
xargs hadolint
lint-shell-scripts:
@find . \
-not -path './node_modules/*' \
-name '*.sh' \
| xargs shellcheck
lsh: lint-shell-scripts
lint-mocha-only:
@function changed_files {
# List staged files if any, or changed if not
git diff-index --cached --name-only HEAD | ifne -n \
git diff-index --name-only HEAD |
while read file; do if [ -f "$$file" ]; then echo "$$file"; fi; done
}
changed_files | grep -E '.spec.ts$$' | xargs grep --color=always -H --line-number -F ".only" | tee /dev/stderr | ifne false
lint-require-strict-interpolation:
find src -name '*.ts' |
grep -v src/web-ui/subscription-form.ts | # exceptions
xargs grep --line-number --color=always -P '(?<!si)`[^;\]), ]' |
tee /dev/stderr | ifne false
smtp-test:
node_modules/.bin/ts-node src/app/email-sending/email-delivery.slow-test.ts
app:
@$(call include_log_to)
set -euo pipefail
docker build \
--progress=plain \
--tag app \
-f docker-services/app/Dockerfile \
. |&
log_to .tmp/logs/feedsubscription/docker-build-app.log
delmon: app
docker build \
--progress=plain \
--tag delmon \
docker-services/delmon
# This is targeted after the log rotation in logger, which happens around 01:02, but sometimes later
delmon-restart:
@docker restart delmon |
cat <(
echo "Subject: RES delmon-restart"
echo "From: RES <[email protected]>"
echo ""
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
logger:
docker build \
--progress=plain \
--tag logger \
docker-services/logger
resolver:
docker build \
--progress=plain \
--tag resolver \
docker-services/resolver
resolver-check:
@set -euo pipefail
docker exec -it resolver dig @localhost feedsubscription.com
docker exec -it resolver dig @localhost feedsubscription.com |
grep -E "^;; Query time: [0-4] msec"
# cron @reboot
watch-resolver:
@tail -n0 --follow=name --retry .tmp/logs/feedsubscription/resolver.log |
grep --line-buffered -E \
-e ' (exiting|started,|using|read) ' \
-e ' is NODATA-' \
|
while read -r timestamp _skip_namespace _skip_container_name_and_id _skip_process_name record; do
(
date_and_hour=`echo $$timestamp | cut -d: -f1`
echo "From: RES <[email protected]>"
echo "Subject: RES resolver $$date_and_hour"
echo ""
echo "$$timestamp $$record"
) |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi
done \
& disown
logger-list-packages:
docker exec -it logger apk list -a |
grep -P "^(syslog-ng|logrotate|tini)-\d"
smtp-out:
docker build \
--progress=plain \
--tag smtp-out \
docker-services/smtp-out
smtp-in:
source .env
docker build \
--no-cache \
--build-arg SASL_PASSWORD="$${SMTP_IN_SASL_PASSWORD:?envar is missing}"\
--progress=plain \
--tag smtp-in \
docker-services/smtp-in
# cron @reboot
start:
docker compose --project-name res up --remove-orphans --detach
start-resolver:
docker compose --project-name res up --remove-orphans --detach \
-- resolver
start-app: app
docker compose --project-name res up --remove-orphans --detach \
-- app
start-delmon: delmon
docker compose --project-name res up --remove-orphans --detach \
-- delmon
start-logger: logger
docker compose --project-name res up --remove-orphans --detach \
-- logger
start-api: compile test build-website website app
export NODE_ENV="production"
docker compose --project-name res up --remove-orphans --force-recreate \
-- resolver logger website api > /dev/null &
sleep 1
docker compose --project-name res logs --follow --since 10s --timestamps &
wait
start-website: build-website start-api
start-testblog:
@echo 'Run this:'
echo ' cd ~/tmp/testblog'
echo ' make start'
stop:
docker compose --project-name res down
restart: stop start
hashing-salt:
tr -dc A-Za-z0-9 </dev/urandom | head -c 16 ; echo ''
reload-app:
docker kill --signal=SIGHUP app
# cron @weekly
reload-website:
@docker kill --signal=SIGHUP website | \
cat <( \
echo "Subject: RES reload-website"; \
echo "From: RES <[email protected]>"; \
echo; \
) - \
| if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
purge-smtp-queue:
docker exec -it smtp postsuper -d ALL
certbot:
@$(call include_log_to)
docker build \
--progress=plain \
--tag certbot \
-f docker-services/certbot/Dockerfile \
. |&
log_to .tmp/logs/feedsubscription/docker-build-certbot.log
start-certbot: certbot
docker compose --project-name res up --remove-orphans --detach \
-- certbot
# NOTE: When changing certificate domains, rm -rf ll ./.tmp/certbot/conf/ first.
ssl:
docker compose --project-name res run --rm --entrypoint "\
certbot certonly \
--webroot --webroot-path /var/www/certbot \
--domains feedsubscription.com \
--domains www.feedsubscription.com \
--domains localhost.feedsubscription.com \
--expand \
--rsa-key-size 4096 \
--agree-tos \
--non-interactive \
--email [email protected]" certbot
docker kill --signal=SIGHUP website
# NOTE: Eleventy manages the website’s /dist/, so I’m puting the compiled
# scrips in its src/web-ui-scripts/ and let it take it from there. If I put
# them directly in /dist/, Eleventy removes them as extraneous.
WEB_UI_DEST_DIR=$(DOCUMENT_ROOT)/../src/web-ui-scripts/
web-ui-systemjs:
mkdir -p $(WEB_UI_DEST_DIR)
cp --target-directory=$(WEB_UI_DEST_DIR) \
./node_modules/systemjs/dist/system.min.js*
cat ./src/web-ui/systemjs-resolve-patch.js >> $(WEB_UI_DEST_DIR)/system.min.js
web-ui:
node_modules/.bin/tsc --project src/web-ui/tsconfig.json
web-ui-watch: web-ui-systemjs
node_modules/.bin/tsc --watch --project src/web-ui/tsconfig.json --outDir $(WEB_UI_DEST_DIR) &
start-dev: web-ui-watch
node_modules/.bin/nodemon src/api/server.ts
api-test:
@export TS_NODE_TRANSPILE_ONLY=true
node_modules/.bin/mocha \
--require ts-node/register \
--reporter dot \
--bail -R dot api-test.spec.ts
snyk:
snyk test
# cron @reboot
watch-app:
@function handle_ENOTFOUND {
while read -r _getaddrinfo _ENOTFOUND server_name; do
echo "DEBUG: Trying to resolve $$server_name..."
echo "DEBUG: nslookup from inside container"
docker exec app nslookup $$server_name
echo "DEBUG: nslookup from outside container"
nslookup $$server_name
done
}
tail -n0 --follow=name --retry .tmp/logs/feedsubscription/{app,api}.log |
grep --line-buffered -E \
-e '"severity":"(error|warning)"' \
-e '"message":"Starting (cron|API server)' \
-e '"message":"Sending report"' \
-e '"message":"sendSampleEmail"' \
-e '"message":"Blog RSS check"' \
-e '"message":"heartbeat"' \
-e '"message":"(New unconfirmed subscriber|Subscriber confirmed email)"' \
-e '"message":"(New feed added|Feed updated|Feed deleted)"' \
-e '"message":"(Unsubscribed)"' \
-e '"message":"Deleted confirmation secrets' \
-e '"message":"(User registered|User confirmed registration|User (requested|confirmed) password change|User logged in|Account deleted)"' \
|
while read -r _skip_timestamp _skip_namespace container_name_and_id json; do
(
container_name=$$(grep -Po '^[^[]+' <<<"$$container_name_and_id")
severity=$$(jq -r .severity <<<"$$json")
message=$$(jq -r .message <<<"$$json")
reason=$$(jq -r .data.reason <<<"$$json")
echo "Subject: RES $$container_name $$severity: $$message"
echo "From: RES <[email protected]>"
echo
jq . <<<"$$json"
if [[ $$reason == "getaddrinfo ENOTFOUND"* ]]; then handle_ENOTFOUND <<<"$$reason"; fi
if [[ $$message == "Feed updated" ]]; then
echo "DIFF:"
jq -r .data.diff <<<"$$json"
fi
) |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi;
done \
& disown
# cron @reboot
watch-website:
@function url_decode {
while read line; do
line=$${line//%/\\x}
line=$${line//+/ }
echo -e $$line
done
}
tail -n0 --follow=name --retry .tmp/logs/feedsubscription/website.log |
grep -v -E '(bingbot|Googlebot)' |
grep --line-buffered -F \
-e 'GET /error?stack=' \
|
grep -v 'chrome-extension' |
while read -r timestamp _2 _3 client_ip _5 _6 _7 _8 _9 url _11 _12 _13 referrer rest; do
(
echo "Subject: RES website error-log"
echo "From: [email protected]"; `# needs FromLineOverride=YES in /etc/ssmtp/ssmtp.conf`
echo ""
echo "User-Agent: $$rest"
echo "Client IP: $$client_ip"
echo "Referrer: $$referrer"
echo "Timestamp: $$timestamp"
echo "URL: $$url" | url_decode
echo "Whois:"
whois $$client_ip | grep -iE '^(Address|StateProv|PostalCode|Country):' | sort -u | head -10 | sed 's/^/ /'
echo "https://whois.com/whois/$$client_ip"
) |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi;
done \
& disown
# cron @reboot
watch-smtp-out:
@tail -n0 --follow=name --retry .tmp/logs/feedsubscription/smtp-out.log |
grep --line-buffered -E \
-e '(warning|error|fatal|panic|reject):' \
-e ' POSTFIX STARTING UP ' \
-e ' INFO (stopped|spawned): ' \
-e ' (WARN|CRIT|ERR) ' \
|
while read -r timestamp rest; do
(
echo "Subject: RES smtp-out $$timestamp"
echo "From: RES <[email protected]>"
echo ""
echo "$$rest"
no_mx_regex="^.*no\ MX\ host\ for\ ([-a-z.]+).*$$"
ns_error_regex="^.*Name\ service\ error\ for\ name=([-a-z.]+)\.*$$"
if [[ $$rest =~ $$no_mx_regex ]] || [[ $$rest =~ $$ns_error_regex ]] ; then
domain=$${BASH_REMATCH[1]}
echo "--- DEBUG: A record from inside container"
docker exec smtp-out nslookup "$$domain"
echo "--- DEBUG: MX record from inside container"
docker exec smtp-out nslookup -query=mx "$$domain"
echo "--- DEBUG: A record from host"
nslookup "$$domain"
echo "--- DEBUG: MX record from host"
nslookup -query=mx "$$domain"
fi
) |
if [ -t 1 ]; then cat; else sleep 1m; ifne ssmtp [email protected]; fi
done \
& disown
# cron @weekly
certbot-report:
@(
echo "Subject: RES weekly-certbot"
echo "From: RES <[email protected]>"
echo
ls -1t .tmp/logs/feedsubscription/certbot.log{,-*.gz} |
head -2 |
sort -r |
xargs zcat -f |
tail -12
) 2>&1 |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi
# cron @reboot
watch-delmon:
@tail -n0 --follow=name --retry .tmp/logs/feedsubscription/delmon.log |
while read -r _skip_timestamp _skip_namespace _skip_container_name_and_id json; do
(
echo "From: RES <[email protected]>"
if jq --exit-status <<<"$$json" &> /dev/null; then
severity=$$(jq -r .severity <<<"$$json")
message=$$(jq -r .message <<<"$$json")
echo "Subject: RES delmon $$severity: $$message"
echo
jq . <<<"$$json"
else
echo "Subject: RES delmon non-JSON log record"
echo
wc --bytes <<<"$$json" | ts "bytes:"
echo "$$json"
fi
) |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi
done \
& disown
# This is to be run when delmon container died for some reason, and
# there are unprocessed qid-index entries.
delmon-catch-up:
@source .env
require=$${DATA_DIR_ROOT:?envar is missing}
ls -1 $$DATA_DIR_ROOT/qid-index/ |
while read qid; do
grep -P "INFO postfix/smtp.+ $$qid: .+ status=" .tmp/logs/feedsubscription/smtp-out.log
done |
tee /dev/stderr | # so that I can see that something is happening
docker exec --interactive delmon node dist/app/delivery-monitoring
# cron @daily
list-qid-index:
@source .env
require=$${DATA_DIR_ROOT:?envar is missing}
ls -1 $$DATA_DIR_ROOT/qid-index |
grep -v "^total " |
ifne -n echo '(empty)' |
cat <(
echo "Subject: RES list-qid-index"
echo "From: RES <[email protected]>"
echo ""
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
delmon-dev:
@DATA_DIR_ROOT=.tmp/docker-data ts-node ./src/app/delivery-monitoring
# cron 59 23 * * *
delivery-report:
@function send_report() {
(
echo "Subject: RES delivery report"
echo "From: RES <[email protected]>"
echo ""
cat
) \
| if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
}
export -f send_report
( \
grep -P "^`date +%F`" .tmp/logs/feedsubscription/smtp-out.log \
| ( tee /dev/stderr 2> >(grep -P "status=(deferred|bounced)" > /dev/stderr) ) \
| grep -Po '(?<= status=)\S+' \
| sort | uniq -c \
) 2>&1 |
ifne -n echo '(empty)' |
send_report
# cron 59 23 * * *
mailq-report:
@function send_report() {
(
echo "Subject: RES mailq report"
echo "From: RES <[email protected]>"
echo ""
cat
) \
| if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
}
export -f send_report
docker exec smtp-out mailq |
ifne -n echo '(empty)' |
send_report
.PHONY: website
website:
@$(call include_log_to)
docker build \
--output type=docker \
--progress=plain \
--tag website \
-f docker-services/website/Dockerfile \
. |&
log_to .tmp/logs/feedsubscription/docker-build-website.log
build-website:
( cd ../feedsubscription.com && source ~/.nvm/nvm.sh && nvm use && make build-prod )
rsync -avz --delete-after ../feedsubscription.com/dist/ website/html/
RCLONE_BINARY=$(shell which rclone || echo RCLONE_BINARY_NOT_FOUND)
RCLONE_CONFIG=~/.config/rclone/rclone.conf
# cron 50 23 * * * cd
backup: ${RCLONE_BINARY} ${RCLONE_CONFIG}
@REMOTE="gdrive-res:/RES-backups"
DATA_DESTINATION="$$REMOTE/`date +%F-%H-%M-%S`"
DATA_ARCHIVE="./data.tgz"
DATA_DIR=".tmp/docker-data"
LOGS_DIR=".tmp/logs/feedsubscription"
LOGS_DESTINATION="$$REMOTE/logs"
function upload_data() {
echo ""
echo "--- upload_data ---"
echo ""
tar -czf $$DATA_ARCHIVE $$DATA_DIR
du -sh $$DATA_DIR $$DATA_ARCHIVE
rclone \
--stats=0 \
--verbose \
copy $$DATA_ARCHIVE $$DATA_DESTINATION \
--exclude=postfix/** \
--exclude=sessions/**
}
function upload_logs() {
echo ""
echo "--- upload_logs ---"
echo ""
du -sh $$LOGS_DIR
rclone \
--stats=0 \
--verbose \
copy --no-traverse $$LOGS_DIR $$LOGS_DESTINATION
}
{
time upload_data
time upload_logs
} 2>&1 |
cat <(
echo "Subject: RES backup"
echo "From: RES <[email protected]>"
echo ""
echo "$$DATA_DESTINATION"
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
rm $$DATA_ARCHIVE
backup-purge:
@rclone lsf gdrive-res:RES-backups |
sort |
head --lines=-31 | # exlude last 31
xargs --no-run-if-empty -I {} sh -c "echo {}; rclone purge gdrive-res:RES-backups/{} 2>&1" |
cat <(
echo "Subject: RES backup-purge"
echo "From: RES <[email protected]>"
echo ""
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
${RCLONE_BINARY}:
curl https://rclone.org/install.sh | sudo bash
${RCLONE_CONFIG}:
rclone config
npm-update:
@npm outdated && echo -e "✅ Everything is up-to-date.\n"
format-check:
prettier --check 'src/**/*.ts'
format-check-quiet:
@printf "Format check... "
$(TIME) $(MAKE) format-check > /dev/null
clean:
rm -rf website/html/ src/api/web-ui-scripts/ dist/
clean-docker: stop
docker image rm --force app delmon smtp-out smtp-in website certbot logger
init-data-dir:
@require=$${DATA_DIR_ROOT:?envar is missing}
set -e
mkdir $$DATA_DIR_ROOT/feeds
mkdir $$DATA_DIR_ROOT/accounts
echo "Initialized data dir: $$DATA_DIR_ROOT"
rsync-logs:
rsync -avz [email protected]:src/rss-email-subscription/.tmp/logs/ .tmp/logs/
sent-count: rsync-logs
@$(MAKE) --no-print-directory sent-count-last-week |
tail -1 |
tee >( numfmt --grouping | ts "last week: " | cat <(echo) - > /dev/stderr ) |
cat - <(
grep -Po "\d+(,\d+)+(?= \(updated on Mondays\))" "../feedsubscription.com/src/includes/my-footer.njk" |
sed 's/,//g'
) |
paste -sd+ - | bc |
numfmt --grouping |
ts "grand total:"
sent-count-last-week: rsync-logs
@ls -1t .tmp/logs/feedsubscription/app.log-*.gz |
head -1 |
xargs zcat -f |
grep '"Sending report"' |
cut -d ' ' -f 4- | # skip timestamps and stuff to get to the JSON record
jq -s 'map(.data.report.sent) | add'
# cron @monthly
docker-prune:
@docker system prune --all --force |
cat <(
echo "Subject: RES prune-docker-images"
echo "From: RES <[email protected]>";
echo
docker system df
echo
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
git-pre-commit-hook:
echo "#!/bin/sh" > .git/hooks/pre-commit
echo "" >> .git/hooks/pre-commit
echo "bash -i -c 'make pre-commit'" >> .git/hooks/pre-commit
# cron @weekly
list-sessions:
@source .env
require=$${DATA_DIR_ROOT:?envar is missing}
ls -1 $$DATA_DIR_ROOT/sessions |
grep -v "^total " |
ifne -n echo '(empty)' |
cat <(
echo "Subject: RES list-sessions"
echo "From: RES <[email protected]>"
echo ""
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
# cron @daily
watch-containers:
@docker stats \
--all \
--no-stream \
--format "table
<tr>
<td style='white-space: nowrap'>{{.ID}}
<td style='white-space: nowrap'>{{.Name}}
<td style='white-space: nowrap'>{{.CPUPerc}}
<td style='white-space: nowrap'>{{.MemUsage}}
<td style='white-space: nowrap'>{{.MemPerc}}
<td style='white-space: nowrap'>{{.NetIO}}
<td style='white-space: nowrap'>{{.BlockIO}}
<td style='white-space: nowrap'>{{.PIDs}}
" |
cat <(
echo "Subject: RES watch-containers"
echo "From: RES <[email protected]>"
echo "Content-Type: text/html"
echo ""
echo "<table border="1" cellspacing="0" cellpadding="3">"
) - |
if [ -t 1 ]; then cat; else ssmtp [email protected]; fi
# cron 59 23 * * *
404-report:
@grep -P "^`date +%F`.+\" 404 \d+ \"https://feedsubscription.com/" .tmp/logs/feedsubscription/website.log |
sed -E -e 's/^\S+ \S+ \S+ //' |
grep -v -E -s 'GET /(robots.txt|favicon.ico|.git|.env|.well-known|.vscode|info.php|sitemap.xml)' |
grep -v -E -s 'Googlebot' |
ifne -n echo '(empty)' |
cat <( \
echo "Subject: RES 404-report"; \
echo "From: RES <[email protected]>"; \
echo; \
) - |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi
deno-notes:
# Sat Dec 24 16:36:11 EET 2022
# Gave Deno a quick shot
# 1. fix imports:
# ls -1 src/**/*.ts | xargs sed -i -E -e "s/(import .* '\.[^']+).*/\1.ts';/" -e "s/(import .* ')([^.]+[^']+).*/\1npm:\2/" -e "s/(^} from '\.[^']+).*/\1.ts';/" -e "s|npm:node:|https://deno.land/[email protected]/node/|; s/(.+deno.land.+)';/\1.ts';/"
#
# 2. try running and fix everthing else:
# deno run -A --config tsconfig.json src/app/cron.ts # -- worked with a few fixes
# deno run -A --config tsconfig.json src/api/server.ts # -- failed because session-file-store
#
# Easy fixes: process.env, process.exit, process.argv, process.on
# cron @daily
unique-ips-report:
@zcat -f .tmp/logs/feedsubscription/website.log* |
grep -vF '[error]' | # skip error records
grep -vP ' "[^"]+" \d+ \d+ "[^"]+" "[^"]*([Bb]ot|nmap).*"' | # skip bots
grep -P ' "GET [^"]+" 200 ' | # only GET 200
cut -d' ' -f4 | # ip
sort -u |
wc -l |
cat <( \
echo "Subject: RES unique-ips-report"; \
echo "From: RES <[email protected]>"; \
echo; \
) - |
if [ -t 1 ]; then cat; else ifne ssmtp [email protected]; fi
# brew install goaccess
# geoip comes from https://www.maxmind.com/en/accounts/852003/geoip/downloads
access-report: rsync-logs bot-list.txt
bot_list_re="($$(cat bot-list.txt | paste -sd '|'))"
zcat -f .tmp/logs/feedsubscription/website.log* |
grep -vPi ".*$$bot_list_re.*" | # exclude some bots
cut -d ' ' -f 4- |
grep -P '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | # rows that start with an IP
grep -P '"GET /web-ui-scripts/web-ui/navbar.js HTTP/(1.1|2.0)" 200 \d+ "https://feedsubscription.com' | # greater probability of being a REAL user browser
goaccess \
-o .goaccess/report.html \
--keep-last $${DAYS:-90} \
--ignore-crawlers --unknowns-as-crawlers \
--ignore-panel HOSTS \
--ignore-panel ASN \
--exclude-ip 95.65.96.65 \
--exclude-ip 212.56.195.182 \
--geoip-database .goaccess/GeoLite2-Country.mmdb \
--log-format=COMBINED -
open .goaccess/report.html
# cron 59 23 * * *
tracking-report: bot-list.txt
@function url_decode {
while read line; do
line=$${line//%/\\x}
line=$${line//+/ }
echo -e $$line
done
}
function debug_piepeline {
if [ -v DEBUG ]; then
tee /dev/stderr
else
cat
fi
}
function debug_log() {
local message=$$1
if [ -v DEBUG ]; then
echo "++ DEBUG $$message" > /dev/stderr
fi
}
bot_list_re="($$(cat bot-list.txt | paste -sd '|'))"
debug_log "bot_list_re: $$bot_list_re"
date=$${DATE:-`date +%F`}
debug_log "date: $$date"
ls -t1r .tmp/logs/feedsubscription/website.log* |
xargs zcat -f |
grep -P "^$$date" |
grep -vPi ".*$$bot_list_re.*" | # exclude some bots
grep -vP ': (95.65.96.65) ' | # exclude some IPs
grep -vP ': 40\.9[2-4]\.\d+\.\d+ - - ' | # exclude Office 365 IP ranges mail.protection.outlook.com 40.92.0.0/15
grep -vF ' "https://feedsubscription.com/dW5zdWJzY3?id=' | # exclude reqs w/ obfuscated referrer, cause they ain’t humans
grep -vF ' "https://feedsubscription.com/?from=fzbvy-abbgfe' | # exclude reqs w/ obfuscated referrer, cause they ain’t humans
grep -P "(?<=GET /track\?data=)\S+" |
cut --delimiter=' ' --fields=1,4,10,14 | # select: timestamp, ip, request, referrer
sed \
-e 's|/track?data=||' \
-e 's|https://feedsubscription.com||' \
-e 's|https://www.feedsubscription.com||' \
-e 's/00:00 //' \
-e "s/^$${date}T//" \
| # remove noise
url_decode |
grep -v '"vid":"vlad"' | # exclude myself
grep -v '"referrer":"http://baidu.com/"' | # baidu crawler?
(
tee \
>( wc -l | ts "total events" ) \
>( grep -Po '(?<="vid":")[^"]+' | sort -u | wc -l | ts "uniq vids" ) \
>( grep -Po '(?<="referrer":)".*?"' | grep -vE '^"/' | sort | uniq -c | ts "referrer") \
>( grep -Po '(?<="vid":")[^"]+' | sort | uniq -c ) \
>( grep -Po '(?<="tid":")[^"]+' | sort | ts "click" | uniq -c ) \
>( grep -Po '(?<="tid":")(b-homepage-create-account|l-(free|ppu)-link)' | sort | ts "click" | uniq -c ) \
>( grep -Po '(?<="event":")[^"]+' | sort | uniq -c )
) 2>&1 |
cat <(
echo "Subject: RES tracking-report $$date"
echo "From: RES <[email protected]>"
echo
) - |
if [ -t 1 ] || [ -v MAKE_DEBUG ]; then cat; else ifne ssmtp [email protected]; fi
bot-list.txt: .tmp/logs/feedsubscription/website.log*
@base_re='\w*(bot|crawler|spider)\w*'
zcat -f $^ |
grep -Eoi '" [0-9]+ [0-9]+ ".*'"$$base_re" | # only lines with something-BOT-something in the UA string (or referrer)
grep -Eoi "$$base_re" |
cat <(
# Known non-humans
echo "Chrome-Lighthouse"
echo "Google-InspectionTool"
echo "GoogleImageProxy"
echo "HeadlessChrome"
echo "[email protected]"
echo "Feedly"
echo "Go-http-client"
echo "Nmap Scripting Engine"
echo "facebookexternalhit"
echo "facebookcatalog"
echo "YahooMailProxy"
echo "InternetMeasurement"
echo "zgrab"
echo "baidu.com"
echo "CFNetwork"
) - |
sort --unique --ignore-case > $@
# cron @reboot
ufw-config:
# This is necessary for the app and api containers to be able to
# access host’s external IP, which is needed when checking our own RSS
# feed.
ufw allow 80/tcp
ufw allow 443/tcp
delivery-duration:
@echo $${DELIVERY_DIR:?Missing envar}
ls -1 "$$DELIVERY_DIR"/*/*.json | wc -l | ts 'items:'
jq -r '.logRecords[].timestamp' "$$DELIVERY_DIR"/*/*.json |
sort -u |
(
tee \
>( head -1 | ts 'begin:' > /dev/stderr ) \
>( tail -1 | ts 'end: ' > /dev/stderr )
) > /dev/null
# #auth #users #logge
logins-in-last-month:
@from_date="$$(date '+%F' --date='1 month ago')T00:00:00+00:00"
zcat -f .tmp/logs/feedsubscription/api.log* |
grep '"User logged in"' |
while read timestamp _1 _2 json; do
if [[ "$$timestamp" > "$$from_date" ]]; then
jq -r .data.email <<<"$$json";
fi;
done |
sort -u |
grep -vE '(gurdiga|woffca)' |
tee >(
wc -l | ts "
total:"
)
# run weekly in dev env
docker-image-check:
@yq -r '.services[].image' docker-compose.yml |
sort -u |
if [ -v ONLY ]; then
grep -E "$${ONLY}";
elif [ -v SKIP ]; then
grep -vE "$${SKIP}";
else
cat;
fi |
while read image; do
echo "====================== $$image ======================"
if ! docker scout cves --exit-code "$$image"; then
exit 1
fi
done
all-images: app certbot delmon logger smtp-in smtp-out website resolver