-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
347 lines (307 loc) · 12.2 KB
/
Makefile
File metadata and controls
347 lines (307 loc) · 12.2 KB
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
# DEBIAN_ISO_URL=https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.4.0-amd64-netinst.iso
DEBIAN_ISO_URL=https://cdimage.debian.org/mirror/cdimage/archive/10.3.0/amd64/iso-cd/debian-10.3.0-amd64-netinst.iso
ISO_TARGET=debian_autoinstall.iso
IMAGE_DIR=/var/images
QCOW_FILE=$(IMAGE_DIR)/git.openelectronicslab.org.gitlab.qcow2
BACKUPS_DIR=/backups/git.openelectronicslab.org
INITIAL_DISK_SIZE=20G
KVM_CORES=2
KVM_DEBIAN_INSTALL_RAM=1G
KVM_RAM=8G
SSH_MAX_INIT_SECONDS=60
DELAY=0.1
QEMU_FORCE_SLEEP=7
OPENSSL_TMP_SUBJ="/C=NL/ST=Utrecht/L=Utrecht/O=OpenElectronicsLab/OU=Dev/CN=git.openelectronicslab.org"
RETRIES=$(shell echo "$(SSH_MAX_INIT_SECONDS)/$(DELAY)" | bc)
# $@ : target label
# $< : the first prerequisite after the colon
# $^ : all of the prerequisite files
# $* : wildcard matched part
ISO_CREATED_MARKER=iso/README.txt
default: check
check:
echo "ok"
clean:
echo "removing files generated by the build, see also 'make spotless'"
rm -rf iso $(ISO_TARGET) \
tmp.qcow2 \
git.openelectronicslab.org.qcow2 \
git.openelectronicslab.org.base.qcow2 \
git.openelectronicslab.org.gitlab.qcow2 \
tmp_gitlab_admin_passwd \
id_rsa_tmp.pub \
id_rsa_tmp \
id_rsa_host_tmp.pub \
id_rsa_host_tmp \
tmp.key \
tmp.crt
spotless:
git clean -dffx
git submodule foreach --recursive git clean -dffx
# download the base install image
debian_original.iso :
wget $(DEBIAN_ISO_URL) -O $@
# extract the contents of the image
$(ISO_CREATED_MARKER) : debian_original.iso
mkdir -pv iso
cd iso && 7z x ../$<
touch $@ # update timestamp so make knows when this was extracted
# copy the preseed file to the appropriate location (using m4 to expand macros)
# CONSIDER: could add encryption to preseed file if we decide we need it
iso/preseed/autoinstall-preseed.seed: autoinstall-preseed.seed \
$(ISO_CREATED_MARKER)
mkdir -pv iso/preseed
cp $< $@
iso/authorized_keys: $(ISO_CREATED_MARKER) id_rsa_tmp.pub \
id_rsa_host_tmp.pub id_rsa_host_tmp
cp -v ./id_rsa_tmp.pub iso/authorized_keys
cp -v ./id_rsa_host_tmp.pub iso/id_rsa_host_tmp.pub
cp -v ./id_rsa_host_tmp iso/id_rsa_host_tmp
# update the grub.cfg to do a preseeded install
# (Used for Legacy BIOS)
iso/isolinux/isolinux.cfg : isolinux.cfg $(ISO_CREATED_MARKER)
cp $< $@
# generate the new iso install image
$(ISO_TARGET): iso/preseed/autoinstall-preseed.seed \
iso/isolinux/isolinux.cfg \
iso/authorized_keys
genisoimage -o $@ -b isolinux/isolinux.bin -c isolinux/boot.cat \
-no-emul-boot -boot-load-size 4 -boot-info-table -J -R \
-V "Debian AutoInstall" iso
git.openelectronicslab.org.base.qcow2: $(ISO_TARGET)
{ lsof -i:10022; if [ $$? -eq 0 ]; then echo "port 10022 not free"; false; fi; }
qemu-img create -f qcow2 tmp.qcow2 $(INITIAL_DISK_SIZE)
qemu-system-x86_64 -hda tmp.qcow2 -cdrom $(ISO_TARGET) \
-m $(KVM_DEBIAN_INSTALL_RAM) -smp $(KVM_CORES) \
-machine type=pc,accel=kvm \
-display none \
-nic user,hostfwd=tcp:127.0.0.1:10022-:22
mv tmp.qcow2 $@
launch-qemu-gitlab: git.openelectronicslab.org.gitlab.qcow2
{ lsof -i:10022; if [ $$? -eq 0 ]; then echo "port 10022 not free"; false; fi; }
{ lsof -i:10443; if [ $$? -eq 0 ]; then echo "port 10443 not free"; false; fi; }
{ qemu-system-x86_64 -hda $< \
-m $(KVM_RAM) -smp $(KVM_CORES) -machine type=pc,accel=kvm \
-display none \
-nic user,hostfwd=tcp:127.0.0.1:10443-:443,hostfwd=tcp:127.0.0.1:10022-:22 & \
echo "$$!" > 'qemu.pid' ; }
./retry.sh $(RETRIES) $(DELAY) \
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1 \
-i ./id_rsa_tmp \
'/bin/true'
echo "check the key matches the one we generated"
ssh-keyscan -p10022 127.0.0.1 \
| grep `cat id_rsa_host_tmp.pub | cut -f2 -d' '`
echo ssh -i ./id_rsa_tmp -p10022 \
-oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1
echo "$@ kvm running"
shutdown-kvm:
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes root@127.0.0.1 \
-i ./id_rsa_tmp \
'shutdown -h -t 2 now & exit'
{ while kill -0 `cat qemu.pid`; do echo "wating for `cat qemu.pid`"; sleep 1; done }
sleep 1
echo "yay"
tmp.crt:
openssl req -newkey rsa:4096 -x509 -nodes -days 3560 \
-subj $(OPENSSL_TMP_SUBJ) \
-out tmp.crt \
-keyout tmp.key
ls -l tmp.*
tmp.key: tmp.crt
tmp_gitlab_admin_passwd:
cat /dev/urandom \
| tr -dc 'a-zA-Z0-9' \
| fold -w $${1:-32} \
| head -n 1 > ./tmp_gitlab_admin_passwd
GITLAB_SCP_FILES=tmp.key tmp.crt install-gitlab.sh tmp_gitlab_admin_passwd
git.openelectronicslab.org.gitlab.qcow2: $(GITLAB_SCP_FILES) \
git.openelectronicslab.org.base.qcow2
{ lsof -i:10022; if [ $$? -eq 0 ]; then echo "port 10022 not free"; false; fi; }
{ lsof -i:10443; if [ $$? -eq 0 ]; then echo "port 10443 not free"; false; fi; }
cp -v git.openelectronicslab.org.base.qcow2 \
git.openelectronicslab.org.pre-gitlab.qcow2
{ qemu-system-x86_64 -hda git.openelectronicslab.org.pre-gitlab.qcow2 \
-m $(KVM_RAM) -smp $(KVM_CORES) -machine type=pc,accel=kvm \
-display none \
-nic user,hostfwd=tcp:127.0.0.1:10443-:443,hostfwd=tcp:127.0.0.1:10022-:22 \
& echo "$$!" > qemu.pid; }
./retry.sh $(RETRIES) $(DELAY) \
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1 \
-i ./id_rsa_tmp \
'/bin/true'
ssh-keyscan -p10022 127.0.0.1 \
| grep `cat id_rsa_host_tmp.pub | cut -f2 -d' '`
echo ssh -i ./id_rsa_tmp -p10022 \
-oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1
scp -P10022 -oNoHostAuthenticationForLocalhost=yes \
-i ./id_rsa_tmp \
$(GITLAB_SCP_FILES) root@127.0.0.1:/root
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes root@127.0.0.1 \
-i ./id_rsa_tmp \
'bash /root/install-gitlab.sh'
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes root@127.0.0.1 \
-i ./id_rsa_tmp \
'shutdown -h -t 2 now & exit'
{ while kill -0 `cat qemu.pid`; do echo "wating for `cat qemu.pid`"; sleep 1; done }
sleep 1
mv -v git.openelectronicslab.org.pre-gitlab.qcow2 \
git.openelectronicslab.org.gitlab.qcow2
install-gitlab: git.openelectronicslab.org.gitlab.qcow2
true
kill-qemu:
-kill `ps auxw \
| grep "[q]emu-system-x86_64" \
| grep "^$(USER)" \
| sed -e "s/$(USER) *\([0-9]*\).*/\1/g"`
id_rsa_tmp:
ssh-keygen -b 4096 -t rsa -N "" -C "temporary-key" -f ./id_rsa_tmp
id_rsa_tmp.pub: id_rsa_tmp
id_rsa_host_tmp:
ssh-keygen -b 4096 -t rsa -N "" -C "temp-host-key" -f ./id_rsa_host_tmp
id_rsa_host_tmp.pub: id_rsa_host_tmp
$(IMAGE_DIR):
mkdir -pv $(IMAGE_DIR)
install: $(IMAGE_DIR) git.openelectronicslab.org.gitlab.qcow2 \
stop-qemu-git-openelectronicslab.sh \
start-qemu-git-openelectronicslab.sh \
qemu-git-openelectronicslab.service
-systemctl stop qemu-git-openelectronicslab
cp -v stop-qemu-git-openelectronicslab.sh $(IMAGE_DIR)/
cp -v start-qemu-git-openelectronicslab.sh $(IMAGE_DIR)/
cp -v qemu-git-openelectronicslab.service $(IMAGE_DIR)/
-ln -sv $(IMAGE_DIR)/qemu-git-openelectronicslab.service \
/etc/systemd/system/qemu-git-openelectronicslab.service
cp -v git.openelectronicslab.org.gitlab.qcow2 \
$(IMAGE_DIR)/git.openelectronicslab.org.gitlab.qcow2
systemctl daemon-reload
systemctl enable qemu-git-openelectronicslab
systemctl start qemu-git-openelectronicslab
./retry.sh $(RETRIES) $(DELAY) \
ssh root@git.openelectronicslab.org \
-i ./id_rsa_tmp \
'/bin/true'
scp -i ./id_rsa_tmp \
./letsencrypt.sh \
root@git.openelectronicslab.org:/root/
ssh root@git.openelectronicslab.org \
-i ./id_rsa_tmp \
'bash /root/letsencrypt.sh'
NOW=`cat now_timestamp`
git.openelectronicslab.org-tested: git.openelectronicslab.org.gitlab.qcow2
{ lsof -i:10022; if [ $$? -eq 0 ]; then echo "port 10022 not free"; false; fi; }
{ lsof -i:10443; if [ $$? -eq 0 ]; then echo "port 10443 not free"; false; fi; }
# TODO: add backup user, id_rsa_tmp will be wrong
# TODO: backup the ssh keys
date --utc +"%Y%m%dT%H%M%SZ" > now_timestamp
ssh root@git.openelectronicslab.org \
-i ./id_rsa_tmp \
'bash gitlab-backup create'
mkdir -pv $(BACKUPS_DIR)/$(NOW)
rm -fv $(BACKUPS_DIR)/latest
ln -s $(BACKUPS_DIR)/$(NOW) \
$(BACKUPS_DIR)/latest
cp -v ./id_rsa_tmp $(BACKUPS_DIR)/latest/
cp -v ./id_rsa_tmp.pub $(BACKUPS_DIR)/latest/
scp -i ./id_rsa_tmp -r \
root@git.openelectronicslab.org:/root/.ssh/authorized_keys \
root@git.openelectronicslab.org:/etc/ssh/ssh_host_rsa_key \
root@git.openelectronicslab.org:/etc/ssh/ssh_host_rsa_key.pub \
root@git.openelectronicslab.org:/etc/gitlab/gitlab-secrets.json \
root@git.openelectronicslab.org:/etc/gitlab/gitlab.rb \
root@git.openelectronicslab.org:/etc/gitlab/ssl \
root@git.openelectronicslab.org:/var/opt/gitlab/backups \
$(BACKUPS_DIR)/latest
cp -v $< git.openelectronicslab.org.gitlab-pre-restore.qcow2
{ qemu-system-x86_64 \
-hda git.openelectronicslab.org.gitlab-pre-restore.qcow2 \
-m $(KVM_RAM) -smp $(KVM_CORES) -machine type=pc,accel=kvm \
-display none \
-nic user,hostfwd=tcp:127.0.0.1:10443-:443,hostfwd=tcp:127.0.0.1:10022-:22 & echo $$! > qemu.pid ; }
./retry.sh $(RETRIES) $(DELAY) \
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1 \
-i ./id_rsa_tmp \
'/bin/true'
scp -P10022 -oNoHostAuthenticationForLocalhost=yes -i ./id_rsa_tmp \
-r $(BACKUPS_DIR)/latest/backups/* \
root@127.0.0.1:/var/opt/gitlab/backups/
scp -P10022 -oNoHostAuthenticationForLocalhost=yes -i ./id_rsa_tmp \
$(BACKUPS_DIR)/latest/gitlab.rb \
$(BACKUPS_DIR)/latest/gitlab-secrets.json \
root@127.0.0.1:/etc/gitlab/
scp -P10022 -oNoHostAuthenticationForLocalhost=yes -i ./id_rsa_tmp \
./restore-gitlab.sh \
root@127.0.0.1:/root/
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes root@127.0.0.1 \
-i ./id_rsa_tmp \
'bash /root/restore-gitlab.sh'
# TODO: restore keys and such, reconnect with new keys
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes root@127.0.0.1 \
-i ./id_rsa_tmp \
'shutdown -h -t 2 now & exit'
{ while kill -0 `cat qemu.pid`; do echo "wating for `cat qemu.pid`"; sleep 1; done }
sleep 2
{ lsof -i:10022; if [ $$? -eq 0 ]; then echo "port 10022 not free"; false; fi; }
{ lsof -i:10443; if [ $$? -eq 0 ]; then echo "port 10443 not free"; false; fi; }
mv -v git.openelectronicslab.org.gitlab-pre-restore.qcow2 \
git.openelectronicslab.org.gitlab-post-restore.qcow2
# start in "-snapshot" mode to avoid changing the file
{ qemu-system-x86_64 -snapshot \
-hda git.openelectronicslab.org.gitlab-post-restore.qcow2 \
-m $(KVM_RAM) -smp $(KVM_CORES) -machine type=pc,accel=kvm \
-display none \
-nic user,hostfwd=tcp:127.0.0.1:10443-:443,hostfwd=tcp:127.0.0.1:10022-:22 \
& echo "$$!" > qemu.pid; }
./retry.sh $(RETRIES) $(DELAY) \
ssh -p10022 -oNoHostAuthenticationForLocalhost=yes \
root@127.0.0.1 \
-i ./id_rsa_tmp \
'/bin/true'
# run verification tests
echo rails takes seemingly an eternity to start, will try for 2 minutes
./retry.sh 120 1 wget -qO- --no-check-certificate https://127.0.0.1:10443
# TODO: test more than we have Ace
wget -qO- --no-check-certificate \
https://127.0.0.1:10443/api/v4/users?username=ace-dvm \
| jq .[].name \
| grep 'Medlock'
kill `cat qemu.pid`
mv -v git.openelectronicslab.org.gitlab-post-restore.qcow2 \
git.openelectronicslab.org.gitlab-post-restore.$(NOW).qcow2
rm -fv $@
rm git.openelectronicslab.org.gitlab-post-restore.$(NOW).qcow2
touch $@
# TODO: backup should run every time "make backup" is called
# rather than depend on the presence of the "foo.tested.qcow2"
backup: git.openelectronicslab.org-tested
rm -v git.openelectronicslab.org-tested
# TODO separate the making of a backup from testing of a backup
redeploy: git.openelectronicslab.org-tested.qcow2
echo "shut down running instance"
systemctl --type=service --state=running
systemctl stop qemu-git-openelectronicslab.service
echo "copy new file into place"
mv -v $(QCOW_FILE) $(QCOW_FILE).`date --utc +"%Y%m%dT%H%M%SZ"`
cp -v git.openelectronicslab.org-tested.qcow2 $(QCOW_FILE)
systemctl start qemu-git-openelectronicslab.service
./retry.sh $(RETRIES) $(DELAY) \
ssh root@git.openelectronicslab.org \
-i ./id_rsa_tmp \
'/bin/true'
scp -i ./id_rsa_tmp \
./letsencrypt.sh \
root@git.openelectronicslab.org:/root/
ssh root@git.openelectronicslab.org \
-i ./id_rsa_tmp \
'bash /root/letsencrypt.sh'
# TODO: restore from disaster, no running instance
# restore: git.openelectronicslab.org-tested.qcow2
# take the qcow2 image and replace the running one.
# TODO: restore this to a temp instance, not the running instance
# after restore has succeeded, replace running instance
# TODO: restore the ssh keys