-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMakefile
203 lines (174 loc) · 6.51 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
NAME = mathb
FQDN = $(NAME).in
MAIL = $(FQDN)@yahoo.com
help:
@echo 'Usage: make [target]'
@echo
@echo 'Targets to run on live server:'
@echo ' setup Install Debian packages and Quicklisp for website.'
@echo ' https Reinstall live website and serve with Nginx via HTTPS.'
@echo ' http Reinstall live website and serve with Nginx via HTTP.'
@echo ' rm Uninstall live website.'
@echo ' backup Create live server data backup.'
@echo ' review Review posts listed in /tmp/review.txt.'
@echo ' follow-log Follow logs on live server.'
@echo ' follow-post Follow POST logs on live server.'
@echo ' post-log Filter logs to find all successful posts.'
@echo ' top-get-log Filter logs to find most popular pages.'
@echo
@echo 'Low-level targets:'
@echo ' live Generate live website.'
@echo ' site Generate local website.'
@echo
@echo 'Development targets:'
@echo ' opt Create directories at /opt for testing.'
@echo ' run Run application.'
@echo ' test Test application code.'
@echo ' checks Validate consistency within configuration.'
@echo ' pub Publish updated website on live server.'
@echo ' force-pub Reset website on live server and publish website.'
@echo ' pull-backup Pull a backup of data from live server.'
@echo
@echo 'Default target:'
@echo ' help Show this help message.'
# Targets for Live Server
# -----------------------
setup:
apt-get update
apt-get -y install nginx certbot sbcl
rm -rf /opt/quicklisp.lisp /opt/quicklisp
curl https://beta.quicklisp.org/quicklisp.lisp -o /opt/quicklisp.lisp
sbcl --load /opt/quicklisp.lisp \
--eval '(quicklisp-quickstart:install :path "/opt/quicklisp/")' \
--quit
chown -R www-data:www-data /opt/quicklisp
https: http wait-http
@echo Setting up HTTPS website ...
certbot certonly -n --agree-tos -m '$(MAIL)' --webroot \
-w '/var/www/$(FQDN)' -d '$(FQDN),www.$(FQDN)'
(crontab -l | sed '/::::/d'; cat etc/crontab) | crontab
ln -snf "$$PWD/etc/nginx/https.$(FQDN)" '/etc/nginx/sites-enabled/$(FQDN)'
systemctl restart nginx
@echo Done; echo
http: rm live mathb
@echo Setting up HTTP website ...
ln -snf "$$PWD/_live" '/var/www/$(FQDN)'
ln -snf "$$PWD/etc/nginx/http.$(FQDN)" '/etc/nginx/sites-enabled/$(FQDN)'
ln -snf "$$PWD/etc/logrotate" /etc/logrotate.d/mathb
systemctl restart nginx
echo 127.0.0.1 '$(NAME)' >> /etc/hosts
@echo Done; echo
wait-http:
@echo Waiting for HTTP website to start ...
while ! curl http://localhost:4242/; do sleep 1; echo Retrying ...; done
@echo Done; echo
mathb:
@echo Setting up mathb ...
mkdir -p /opt/cache/ /opt/data/mathb/ /opt/log/mathb/
chown -R www-data:www-data /opt/cache/ /opt/data/mathb/ /opt/log/mathb/
systemctl enable "/opt/mathb.in/etc/mathb.service"
systemctl daemon-reload
systemctl start mathb
@echo Done; echo
rm: checkroot
@echo Removing website ...
rm -f /etc/logrotate/mathb
rm -f '/etc/nginx/sites-enabled/$(FQDN)'
rm -f '/var/www/$(FQDN)'
systemctl restart nginx
sed -i '/$(NAME)/d' /etc/hosts
@echo
@echo Removing mathb ...
-systemctl stop mathb
-systemctl disable mathb
systemctl daemon-reload
@echo
@echo Following crontab entries left intact:
crontab -l | grep -v "^#" || :
@echo Done; echo
checkroot:
@echo Checking if current user is root ...
[ $$(id -u) = 0 ]
@echo Done; echo
backup:
tar -caf "/opt/cache/mathb-$$(date "+%Y-%m-%d_%H-%M-%S").tgz" -C /opt/data/ mathb/
ls -1 /opt/cache/mathb-*.tgz | sort -r | tail -n +100 | xargs rm -vf
ls -lh /opt/cache/
df -h /
review: checkroot
mkdir -p /tmp/deleted
[ -f /tmp/review.txt ]
for f in $$(cat /tmp/review.txt); do \
reply=e; \
echo "Editing $$f ..."; \
while [ "$$reply" = e ]; do \
emacs "$$f"; \
echo; head "$$f"; printf "\n\n...\n\n"; tail "$$f"; echo; \
reply=; \
while ! printf '%s' "$$reply" | grep -qE '^(e|d|n|q)$$'; do \
printf "Action for $$f (d, e, n, q)? "; \
read reply; \
[ "$$reply" = d ] && mv "$$f" /tmp/deleted; \
[ "$$reply" = q ] && exit; \
done; \
done; \
done; echo Done; echo
follow-log:
tail -F /opt/log/mathb/mathb.log
follow-post:
tail -F /opt/log/mathb/mathb.log | grep POST
post-log:
tail -F /opt/log/mathb/mathb.log | grep written
top-get-log:
grep ' 200 ' /opt/log/mathb/mathb.log* | grep -o 'GET /[0-9]*' | sort | uniq -c | sort -nr | nl | less
# Low-Level Targets
# -----------------
live: site
@echo Setting up live directory ...
mv _live _gone || :
mv _site _live
rm -rf _gone
@echo Done; echo
site:
@echo Setting up site directory ...
rm -rf _site/
mkdir -p _site/css/ _site/js/
cp -R web/css/* _site/css/
cp -R web/js/* _site/js/
cp -R web/img/* _site/
git -C _site/js/ clone -b 1.2.0 --depth 1 https://github.com/susam/texme.git
git -C _site/js/ clone -b v4.1.0 --depth 1 https://github.com/markedjs/marked.git
git -C _site/js/ clone -b 3.2.2 --depth 1 https://github.com/mathjax/mathjax.git
rm -rf _site/js/texme/.git
rm -rf _site/js/marked/.git/
rm -rf _site/js/mathjax/.git/
@echo Done; echo
# Development Targets
# -------------------
opt:
sudo mkdir -p /opt/data/mathb/ /opt/log/mathb/
sudo cp -R meta/data/* /opt/data/mathb/
sudo chown -R "$$USER" /opt/data/mathb/ /opt/log/mathb/
run:
sbcl --load mathb.lisp
test:
sbcl --noinform --eval "(defvar *quit* t)" --load test.lisp
checks:
# Ensure http.mathb.in and https.mathb.in are consistent.
sed -n '1,/limit_req_status/p' etc/nginx/http.mathb.in > /tmp/http.mathb.in
sed -n '1,/limit_req_status/p' etc/nginx/https.mathb.in > /tmp/https.mathb.in
diff -u /tmp/http.mathb.in /tmp/https.mathb.in
sed -n '/server_name [^w]/,/^}/p' etc/nginx/http.mathb.in > /tmp/http.mathb.in
sed -n '/server_name [^w]/,/^}/p' etc/nginx/https.mathb.in > /tmp/https.mathb.in
diff -u /tmp/http.mathb.in /tmp/https.mathb.in
@echo Done; echo
pub:
git push
ssh -t mathb.in "cd /opt/mathb.in/ && sudo git pull && sudo cp meta/data/post/0/0/*.txt /opt/data/mathb/post/0/0/ && sudo chown -R www-data:www-data meta/data/post/0/0/*.txt && sudo make live && sudo systemctl restart nginx mathb && sudo systemctl --no-pager status nginx mathb"
force-pub:
git push -f
ssh -t mathb.in "cd /opt/mathb.in/ && sudo git reset --hard HEAD~5 && sudo git pull && sudo cp meta/data/post/0/0/*.txt /opt/data/mathb/post/0/0/ && sudo chown -R www-data:www-data meta/data/post/0/0/*.txt && sudo make live && sudo systemctl restart nginx mathb && sudo systemctl --no-pager status nginx mathb"
pull-backup:
mkdir -p ~/bkp/
ssh mathb.in "tar -czf - -C /opt/data/ mathb/" > ~/bkp/mathb-$$(date "+%Y-%m-%d_%H-%M-%S").tgz
ls -lh ~/bkp/