-
Notifications
You must be signed in to change notification settings - Fork 4
/
owntracks-setup.yml
467 lines (398 loc) · 14.5 KB
/
owntracks-setup.yml
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
---
- name: OwnTracks Quick Setup
hosts: localhost
connection: local
gather_facts: true
vars_files: configuration.yaml
vars:
ot_repo: "bookworm"
packages:
- lego
- net-tools
- mosquitto
- mosquitto-clients
- nginx
- php-fpm
- python3-passlib
- jq
- jo
- lua5.4
- lua-socket
- lua-sec
- python3-paho-mqtt
- logrotate
- python3-cryptography
- cron
override: "/etc/systemd/system/ot-recorder.service.d/override.conf"
mosquitto_user: "mosquitto"
mosquitto_group: "mosquitto"
tasks:
- name: "system: template out sys.info"
template:
src: files/system/sys.info.j2
dest: "./sys.info"
mode: 0444
- name: "verify some requirements"
assert:
that:
- "ansible_version.full is version('2.4', '>=')"
- "ansible_distribution_release in [ 'bookworm', 'jammy', 'mantic', 'noble' ]"
- "'community.general.htpasswd' is community.general.a_module"
# - "'community.crypto.x509_certificate_info' is community.crypto.a_module"
- name: "detect: acme"
set_fact:
have_acme: "{{ email is defined | ternary(1, 0) | int }}"
scheme: "{{ email is defined | ternary('https', 'http') }}"
tags: nginx, lego
# _
# ___ _ _ ___| |_ ___ _ __ ___
# / __| | | / __| __/ _ \ '_ ` _ \
# \__ \ |_| \__ \ || __/ | | | | |
# |___/\__, |___/\__\___|_| |_| |_|
# |___/
- name: "system: install OwnTracks repository key"
get_url:
url: https://raw.githubusercontent.com/owntracks/recorder/master/etc/repo.owntracks.org.gpg.key
dest: /etc/apt/trusted.gpg.d/owntracks.asc
- name: "system: install OwnTracks repository"
apt_repository:
repo: "deb http://repo.owntracks.org/debian {{ ot_repo }} main"
state: present
- name: "system: install required packages"
apt:
name: "{{ packages }}"
update_cache: true
- name: "system: create directories"
file:
path: "{{ item }}"
state: directory
mode: 0755
loop:
- "{{ otdir }}"
- "{{ userdata }}"
- "{{ otdir }}/tls"
- "{{ otdir }}/lego"
- name: "system: add passwords to userdata"
set_fact:
friends: "{{ friends | mkpasswords(otdir + '/userdata') }}"
tags: web, pw, otrc
- name: "system: create password files in userdata"
copy:
content: "{{ item.password | string + '\n' }}"
dest: "{{ otdir }}/userdata/{{ item.username }}.pass"
mode: 0400
loop: "{{ friends | unique(case_sensitive=false, attribute='username') }}"
loop_control:
label: "{{ item.username }}"
tags: web
- name: "system: generate user .otrc files in userdata"
template:
src: files/otrc.j2
dest: "{{ otdir }}/userdata/{{ item.username|lower }}-{{ item.devicename|lower }}.otrc"
group: www-data
mode: 0440
loop: "{{ friends }}"
loop_control:
label: "{{ item.username|lower }}-{{ item.devicename|lower }}"
tags: web, otrc
# __ _ _
# _____ ____ _ _ __ / _(_) | ___
# / __\ \ /\ / / _` | '_ \| |_| | |/ _ \
# \__ \\ V V / (_| | |_) | _| | | __/
# |___/ \_/\_/ \__,_| .__/|_| |_|_|\___|
# |_|
- name: "system: create swapfile"
command: dd if=/dev/zero of=/swapfile bs=1M count=2048 creates=/swapfile
register: swapcreated
when: ansible_swaptotal_mb < 1
- name: "system: set permissions on swapfile"
file:
path: /swapfile
mode: 0600
when: swapcreated.changed
- name: "system: mkswap"
command: mkswap /swapfile
when: swapcreated.changed and (ansible_swaptotal_mb < 1)
- name: "system: add swapfile to fstab"
lineinfile:
dest: /etc/fstab
regexp: "swapfile"
line: "/swapfile none swap sw 0 0"
state: present
when: swapcreated.changed and (ansible_swaptotal_mb < 1)
- name: "system: activate swap"
command: swapon -a
when: swapcreated.changed
# _
# | | ___ __ _ ___
# | |/ _ \/ _` |/ _ \
# | | __/ (_| | (_) |
# |_|\___|\__, |\___/
# |___/
- block:
- name: "lego: template out certificate/key installer"
template:
src: files/lego/installcerts.sh.j2
dest: "{{ otdir }}/lego/installcerts.sh"
mode: 0555
- name: "lego: template out enroller"
template:
src: files/lego/enroll.sh.j2
dest: "{{ otdir }}/lego/enroll.sh"
mode: 0555
- name: "lego: enroll at letsencrypt"
command:
argv:
- "{{ otdir }}/lego/enroll.sh"
- name: "lego: get certificate information"
community.crypto.x509_certificate_info:
path: "{{ otdir }}/tls/cert.crt"
register: cert
- name: "lego: display enrolled certificate subject"
debug:
msg: "Certificate Subject: {{ cert.subject.commonName }}"
- name: "lego: install cron job for letsencrypt renewals"
cron:
name: letsencrypt-ACME-certificate-renewal
minute: "{{ 59 | random(seed=dns_domain) }}"
hour: "04"
user: root
job: "[ -x {{ otdir }}/lego/enroll.sh ] && {{ otdir }}/lego/enroll.sh"
when: 'have_acme|int'
tags: lego
# _ _ _
# _ __ ___ ___ ___ __ _ _ _(_) |_| |_ ___
# | '_ ` _ \ / _ \/ __|/ _` | | | | | __| __/ _ \
# | | | | | | (_) \__ \ (_| | |_| | | |_| || (_) |
# |_| |_| |_|\___/|___/\__, |\__,_|_|\__|\__\___/
# |_|
- name: "mosquitto: template configuration for OwnTracks"
template:
src: files/mosquitto/owntracks.conf.j2
dest: "{{ mosquittodir }}/conf.d/owntracks.conf"
owner: "{{ mosquitto_user }}"
group: "{{ mosquitto_group }}"
mode: 0440
# validate: "mosquitto --test-config " # FIXME: in 2.1
notify: restart_mosquitto
- name: "mosquitto: build mosquitto.pw password file from user passwords"
template:
src: files/mosquitto/mosquitto.pw.j2
dest: "{{ mosquittodir }}/mosquitto.pw"
owner: "{{ mosquitto_user }}"
group: "{{ mosquitto_group }}"
mode: 0440
notify: restart_mosquitto
# - name: Populate users in mosquitto.pw password file with generated passwords in *.pw
# command:
# argv:
# - mosquitto_passwd
# - -b
# - "{{ mosquittodir }}/mosquitto.pw"
# - "{{ item }}"
# - "{{ lookup('password', item + '.pw') }}"
# loop: "{{ [ 'recorder' ] + friends|map(attribute='username') }}"
- name: "mosquitto: template mosquitto.acl"
template:
src: files/mosquitto/mosquitto.acl.j2
dest: "{{ mosquittodir }}/mosquitto.acl"
owner: "{{ mosquitto_user }}"
group: "{{ mosquitto_group }}"
mode: 0440
# validate: "mosquitto --test-config " # FIXME: in 2.1
notify: restart_mosquitto
- name: "mosquitto: ensure $HOME/.config/ exists"
file:
path: "{{ lookup('env', 'HOME') }}/.config"
state: directory
mode: 0755
- name: "mosquitto: template $HOME/.config/mosquitto_sub"
template:
src: files/mosquitto/mosquitto_sub.j2
dest: "{{ lookup('env', 'HOME') }}/.config/mosquitto_sub"
mode: 0444
- name: "mosquitto: template $HOME/.config/mosquitto_pub"
template:
src: files/mosquitto/mosquitto_pub.j2
dest: "{{ lookup('env', 'HOME') }}/.config/mosquitto_pub"
mode: 0444
- name: "mosquitto: template out sample userpub program"
template:
src: files/mosquitto/userpub.py.j2
dest: "/usr/local/bin/userpub"
mode: 0755
- name: "mosquitto: template out sample cardpub program"
template:
src: files/mosquitto/cardpub.sh.j2
dest: "/usr/local/bin/cardpub"
mode: 0755
# _ _
# ___ | |_ _ __ ___ ___ ___ _ __ __| | ___ _ __
# / _ \| __|____| '__/ _ \/ __/ _ \| '__/ _` |/ _ \ '__|
# | (_) | ||_____| | | __/ (_| (_) | | | (_| | __/ |
# \___/ \__| |_| \___|\___\___/|_| \__,_|\___|_|
#
- name: "ot-recorder: install package"
apt:
name: "ot-recorder"
state: latest
notify: restart_recorder
- name: "ot-recorder: template ot-recorder defaults file"
template:
src: files/recorder/ot-recorder.default.j2
dest: "/etc/default/ot-recorder"
notify: restart_recorder
- name: "ot-recorder: check whether we need initialization"
stat:
path: "/var/spool/owntracks/recorder/store/ghash/data.mdb"
register: mdb
- name: "ot-recorder: initialize backend (nondestructive)"
command:
argv:
- /usr/sbin/ot-recorder
- --initialize
when: not mdb.stat.exists
- name: "ot-recorder: create service unit override directory"
file:
path: "/etc/systemd/system/ot-recorder.service.d"
state: directory
mode: 0755
- name: "ot-recorder: populate systemd override file"
copy:
content: |
[Service]
# works as environment variable only (not in ot-recorder defaults)
Environment="OTR_LMDBSIZE={{ 5 * 1024 * 1024 * 1024 }}"
dest: "{{ override }}"
mode: 0444
when: override is not exists
- name: "ot-recorder: add/delete keys for users which have them"
shell:
cmd: >
printf "{{ item.username | lower }}-{{ item.devicename | lower }} {{ item.secret|default('DELETE') }}"
| ocat --load=keys
loop: "{{ friends }}"
loop_control:
label: "{{ item.username|lower }}-{{ item.devicename|lower }}"
tags: web, pw
- name: "ot-recorder: launch service"
service:
name: ot-recorder
enabled: true
state: started
# _
# _ __ __ _(_)_ __ __ __
# | '_ \ / _` | | '_ \\ \/ /
# | | | | (_| | | | | |> <
# |_| |_|\__, |_|_| |_/_/\_\
# |___/
- block:
- name: "nginx: create idempotent random cookie value"
set_fact:
cookie_value: "{{ lookup('password', '{{ nginxdir }}/cookie-value.pw') }}"
notify: restart_nginx
- name: "nginx: add users to htpasswd"
community.general.htpasswd:
path: "{{ userdata }}/htpasswd"
name: "{{ item.username }}"
password: "{{ item.password }}"
owner: root
group: www-data
mode: 0444
loop: "{{ friends | unique(case_sensitive=false, attribute='username') }}"
loop_control:
label: "{{ item.username }}"
- name: "nginx: template nginx.conf"
template:
src: files/nginx/nginx.conf.j2
dest: "{{ nginxdir }}/nginx.conf"
mode: 0444
notify: restart_nginx
- name: "nginx: template owntracks site configuration"
template:
src: files/nginx/owntracks.j2
dest: "{{ nginxdir }}/sites-enabled/owntracks"
mode: 0444
notify: restart_nginx
# - name: "nginx: generate dhparam file"
# community.crypto.openssl_dhparam:
# path: "{{ nginxdir }}/dh4096.pem
# size: 4096
# mode: 0444
- name: "nginx: create owntracks/ directory in Web root"
file:
path: "{{ otweb }}"
state: directory
mode: 0755
- name: "nginx: install OwnTrack's index.php"
template:
src: files/nginx/index.php.j2
dest: "{{ otweb }}/index.php"
mode: 0444
tags: page
- name: "nginx: install OwnTrack's logo into document root"
copy:
src: files/mosquitto/assets/logo-owntracks-grayscale-96x96.jpg
dest: "{{ otweb }}/logo-owntracks-grayscale-96x96.jpg"
mode: 0444
tags: page
- name: "nginx: install OwnTrack's otrc.php"
template:
src: files/nginx/otrc.php.j2
dest: "{{ webroot }}/owntracks/otrc.php"
mode: 0444
tags: otrc
- name: "nginx: install favicon.ico"
copy:
src: files/nginx/favicon.ico
dest: "{{ webroot }}/favicon.ico"
mode: 0444
tags: nginx
# __ _ _
# / _|_ __ ___ _ __ | |_ ___ _ __ __| |
# | |_| '__/ _ \| '_ \| __/ _ \ '_ \ / _` |
# | _| | | (_) | | | | || __/ | | | (_| |
# |_| |_| \___/|_| |_|\__\___|_| |_|\__,_|
#
- name: "frontend: ensure directory is removed before upgrade"
file:
path: "{{ otweb }}/frontend"
state: absent
tags: fe
- name: "frontend: create frontend/ directory in Web root"
file:
path: "{{ otweb }}/frontend"
state: directory
mode: 0755
tags: fe
- name: "frontend: unpack dist"
unarchive:
src: files/frontend/frontend-dist.tar.gz
dest: "{{ otweb }}/frontend"
remote_src: no
tags: fe
- name: "frontend: template out config.js"
template:
src: files/frontend/config.js.j2
dest: "{{ otweb }}/frontend/config/config.js"
mode: 0444
tags: fe
# _ _ _
# | |__ __ _ _ __ __| | | ___ _ __ ___
# | '_ \ / _` | '_ \ / _` | |/ _ \ '__/ __|
# | | | | (_| | | | | (_| | | __/ | \__ \
# |_| |_|\__,_|_| |_|\__,_|_|\___|_| |___/
#
handlers:
- name: restart_mosquitto
service:
name: mosquitto
state: restarted
- name: restart_nginx
service:
name: nginx
state: restarted
- name: restart_recorder
service:
name: ot-recorder
state: restarted