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
|
# How to use:
#
# rex commons nsd_master nsd_slaves
#
# Why use Rex to automate my servers? Because Rex is KISS, Puppet, SALT and Chef
# are not. So, why not use Ansible then? To use Ansible correctly you should also
# install Python on the target machines (not mandatory, though. But better).
# Rex is programmed in Perl and there is already Perl in the base system of OpenBSD.
# Also, I find Perl > Python (my personal opinion).
use Rex -feature => ['1.4'];
use Rex::Logger;
use File::Slurp;
# REX CONFIG SECTION
group frontends => 'blowfish.buetow.org', 'fishfinger.buetow.org';
group dnsmaster => 'blowfish.buetow.org';
group dnsslaves => 'fishfinger.buetow.org';
user 'rex';
sudo TRUE;
parallelism 5;
# CUSTOM (PERL-ish) CONFIG SECTION (what Rex can't do by itself)
# Note we using anonymous subs here. This is so we can pass the subs as
# Rex template variables too.
# Gather IPv6 addresses based on hostname.
our $ipv6address = sub {
my $hostname = shift;
return '2a01:4f8:c17:20f1::42' if $hostname eq 'blowfish';
return '2a03:6000:6f67:624::99' if $hostname eq 'fishfinger';
Rex::Logger::info("Unable to determine IPv6 address for $hostname", 'error');
return '::1';
};
# Bootstrapping the FQDN based on the server IP as the hostname and domain
# facts aren't set yet due to the myname file in the first place.
our $fqdns = sub {
my $ipv4 = shift;
return 'blowfish.buetow.org' if $ipv4 eq '23.88.35.144';
return 'fishfinger.buetow.org' if $ipv4 eq '46.23.94.99';
Rex::Logger::info("Unable to determine hostname for $ipv4", 'error');
return 'HOSTNAME-UNKNOWN.buetow.org';
};
# To determine whether te server is te primary or the secondary.
our $is_primary = sub {
my $ipv4 = shift;
$fqdns->($ipv4) eq 'blowfish.buetow.org';
};
our $filewalk;
our $filewalk = sub {
my $dir = shift;
my @files;
opendir my $dh, $dir or die $!;
while (my $entry = readdir $dh) {
next if $entry eq '.' or $entry eq '..';
if (-d "$dir/$entry") {
push @files, $_ for $filewalk->("$dir/$entry");
} elsif (-f "$dir/$entry") {
push @files, "$dir/$entry";
} else {
Rex::Logger::info("Unsupported file type for $dir/$entry", 'error');
}
}
closedir $dh;
return @files;
};
# The secret store. Note to myself: "geheim cat rexfilesecrets.txt"
our $secrets = sub { read_file './secrets/' . shift };
our @dns_zones = qw/buetow.org dtail.dev foo.surf foo.zone irregular.ninja sidewalk.ninja snonux.de snonux.me snonux.land/;
our @acme_hosts = qw/buetow.org paul.buetow.org tmp.buetow.org dory.buetow.org footos.buetow.org dtail.dev foo.zone irregular.ninja snonux.land/;
# UTILITY TASKS
task 'id', group => 'frontends', sub { say run 'id' };
task 'dump_info', group => 'frontends', sub { dump_system_information };
# OPENBSD TASKS SECTION
desc 'Install base stuff';
task 'base', group => 'frontends',
sub {
pkg 'tig', ensure => present;
pkg 'vger', ensure => present;
pkg 'zsh', ensure => present;
append_if_no_such_line '/etc/rc.conf.local', 'pkg_scripts="uptimed httpd failunderd dserver"';
file '/etc/myname',
content => template('./etc/myname.tpl', fqdns => $fqdns),
owner => 'root',
group => 'wheel',
mode => '644';
};
desc 'Setup uptimed';
task 'uptimed', group => 'frontends',
sub {
pkg 'uptimed', ensure => present;
service 'uptimed', ensure => 'started';
};
desc 'Setup rsync';
task 'rsync', group => 'frontends',
sub {
pkg 'rsync', ensure => present;
file '/etc/rsyncd.conf',
content => template('./etc/rsyncd.conf.tpl'),
owner => 'root',
group => 'wheel',
mode => '644';
file '/usr/local/bin/rsync.sh',
content => template('./scripts/rsync.sh.tpl',
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '755';
append_if_no_such_line '/etc/daily.local', '/usr/local/bin/rsync.sh';
};
desc 'Configure the gemtexter sites';
task 'gemtexter', group => 'frontends',
sub {
file '/usr/local/bin/gemtexter.sh',
content => template('./scripts/gemtexter.sh.tpl',
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '744';
file '/etc/daily.local',
ensure => 'present',
owner => 'root',
group => 'wheel',
mode => '644';
append_if_no_such_line '/etc/daily.local', '/usr/local/bin/gemtexter.sh';
};
desc 'Configure ACME client';
task 'acme', group => 'frontends',
sub {
file '/etc/acme-client.conf',
content => template('./etc/acme-client.conf.tpl',
acme_hosts => \@acme_hosts,
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '644';
file '/usr/local/bin/acme.sh',
content => template('./scripts/acme.sh.tpl',
acme_hosts => \@acme_hosts,
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '744';
file '/etc/daily.local',
ensure => 'present',
owner => 'root',
group => 'wheel',
mode => '644';
append_if_no_such_line '/etc/daily.local', '/usr/local/bin/acme.sh';
};
desc 'Invoke ACME client';
task 'acme_invoke', group => 'frontends',
sub {
say run '/usr/local/bin/acme.sh';
};
desc 'Setup httpd';
task 'httpd', group => 'frontends',
sub {
append_if_no_such_line '/etc/rc.conf.local', 'httpd_flags=';
#delete_lines_according_to qr{httpd_flags}, '/etc/rc.conf.local';
file '/etc/httpd.conf',
content => template('./etc/httpd.conf.tpl',
acme_hosts => \@acme_hosts,
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { service 'httpd' => 'restart' };
service 'httpd', ensure => 'started';
};
desc 'Setup inetd';
task 'inetd', group => 'frontends',
sub {
append_if_no_such_line '/etc/rc.conf.local', 'inetd_flags=';
file '/etc/inetd.conf',
source => './etc/inetd.conf',
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { service 'inetd' => 'restart' };
service 'inetd', ensure => 'started';
};
desc 'Setup relayd';
task 'relayd', group => 'frontends',
sub {
append_if_no_such_line '/etc/rc.conf.local', 'relayd_flags=';
file '/etc/relayd.conf',
content => template('./etc/relayd.conf.tpl',
ipv6address => $ipv6address,
is_primary => $is_primary),
owner => 'root',
group => 'wheel',
mode => '600',
on_change => sub { service 'relayd' => 'restart' };
service 'relayd', ensure => 'started';
};
desc 'Setup OpenSMTPD';
task 'smtpd', group => 'frontends',
sub {
Rex::Logger::info('Dealing with mail aliases');
file '/etc/mail/aliases',
source => './etc/mail/aliases',
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { say run 'newaliases' };
Rex::Logger::info('Dealing with mail virtual domains');
file '/etc/mail/virtualdomains',
source => './etc/mail/virtualdomains',
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { service 'smtpd' => 'restart' };
Rex::Logger::info('Dealing with mail virtual users');
file '/etc/mail/virtualusers',
source => './etc/mail/virtualusers',
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { service 'smtpd' => 'restart' };
Rex::Logger::info('Dealing with smtpd.conf');
file '/etc/mail/smtpd.conf',
content => template('./etc/mail/smtpd.conf.tpl'),
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { service 'smtpd' => 'restart' };
service 'smtpd', ensure => 'started';
};
desc 'Setup DNS server';
task 'nsd_master', group => 'dnsmaster',
sub {
my $restart = FALSE;
append_if_no_such_line '/etc/rc.conf.local', 'nsd_flags=';
Rex::Logger::info('Dealing with master DNS key');
file '/var/nsd/etc/key.conf',
content => template('./var/nsd/etc/key.conf.tpl',
nsd_key => $secrets->('/var/nsd/etc/nsd_key.txt')),
owner => 'root',
group => '_nsd',
mode => '640',
on_change => sub { $restart = TRUE };
Rex::Logger::info('Dealing with master DNS config');
file '/var/nsd/etc/nsd.conf',
content => template('./var/nsd/etc/nsd.conf.master.tpl',
dns_zones => \@dns_zones),
owner => 'root',
group => '_nsd',
mode => '640',
on_change => sub { $restart = TRUE };
for my $zone (@dns_zones) {
Rex::Logger::info("Dealing with DNS zone $zone");
file "/var/nsd/zones/master/$zone.zone",
content => template("./var/nsd/zones/master/$zone.zone.tpl"),
owner => 'root',
group => 'wheel',
mode => '644',
on_change => sub { $restart = TRUE };
}
service 'nsd' => 'restart' if $restart;
service 'nsd', ensure => 'started';
};
desc 'Setup DNS slaves';
task 'nsd_slaves', group => 'dnsslaves',
sub {
my $restart = FALSE;
Rex::Logger::info('Dealing with slave DNS key');
file '/var/nsd/etc/key.conf',
content => template('./var/nsd/etc/key.conf.tpl',
nsd_key => $secrets->('/var/nsd/etc/nsd_key.txt')),
owner => 'root',
group => '_nsd',
mode => '640',
on_change => sub { $restart = TRUE };
Rex::Logger::info('Dealing with slave DNS config');
file '/var/nsd/etc/nsd.conf',
content => template('./var/nsd/etc/nsd.conf.slave.tpl',
dns_zones => \@dns_zones),
owner => 'root',
group => '_nsd',
mode => '640',
on_change => sub { $restart = TRUE };
service 'nsd' => 'restart' if $restart;
service 'nsd', ensure => 'started';
};
desc 'Setup DTail';
task 'dtail', group => 'frontends',
sub {
my $restart = FALSE;
file '/etc/rc.d/dserver',
content => template('./etc/rc.d/dserver.tpl'),
owner => 'root',
group => 'wheel',
mode => '755',
on_change => sub { $restart = TRUE };
file '/etc/dserver',
ensure => 'directory',
owner => 'root',
group => 'wheel',
mode => '755';
file '/etc/dserver/dtail.json',
content => template('./etc/dserver/dtail.json.tpl'),
owner => 'root',
group => 'wheel',
mode => '755',
on_change => sub { $restart = TRUE };
file '/usr/local/bin/dserver-update-key-cache.sh',
content => template('./scripts/dserver-update-key-cache.sh.tpl'),
owner => 'root',
group => 'wheel',
mode => '500';
append_if_no_such_line '/etc/daily.local', '/usr/local/bin/dserver-update-key-cache.sh';
service 'dserver' => 'restart' if $restart;
service 'dserver', ensure => 'started';
};
desc 'Setup failunderd';
task 'failunderd', group => 'frontends',
sub {
};
# COMBINED TASKS SECTION
desc 'Common configs of all hosts';
task 'commons', group => 'frontends',
sub {
base();
uptimed();
httpd();
gemtexter();
acme();
acme_invoke();
inetd();
relayd();
smtpd();
rsync();
dtail();
failunderd();
};
1;
# vim: syntax=perl
|