-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rexfile.old
434 lines (373 loc) · 14.8 KB
/
Rexfile.old
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
=head1 NAME
Rexfile - Rex task configuration for CPANTesters
=head1 SYNOPSIS
# Deploy web server configuration
rex deploy_www_config
=head1 DESCRIPTION
This file defines all the L<Rex|http://rexify.org> tasks used to deploy
and configure the CPANTesters servers.
=head1 SEE ALSO
L<Rex|http://rexify.org>
=cut
use Rex -feature => [ 1.4 ];
use Rex::Commands::Sync;
use Term::ReadKey;
use File::Basename qw( basename );
#######################################################################
# Groups
group www => 'cpantesters3.dh.bytemark.co.uk';
group backend => 'cpantesters3.dh.bytemark.co.uk';
group db => 'cpantesters3.dh.bytemark.co.uk';
group monitor => 'monitor.preaction.me';
group api => 'cpantesters3.dh.bytemark.co.uk';
#######################################################################
# Settings
set backend_root_dir => '/media/backend/cpantesters';
set www_root_dir => '/var/www';
set db_root_dir => '/media/backend/mysql';
set monitor_root_dir => '/var/icinga';
set common_packages => [ qw/
build-essential git perl-doc perl vim logrotate ack-grep cpanminus
/ ];
set www_packages => [qw/ apache2 mysql-client /];
set backend_packages => [qw/ mysql-client libdbd-mysql-perl postfix rsync-daemon proftpd /];
set db_packages => [qw/ mysql-server mysql-client libdbd-mysql-perl /];
set monitor_packages => [qw/ icinga2-web icinga2 icinga2-ido-mysql mariadb-server apache-httpd php-7.0.8p0 php-pdo_mysql-7.0.8p0 /];
set api_packages => [qw/ perlbrew libmysqlclient-dev /];
set perl_version => '5.24.0';
set perlbrew_root => '/opt/local/perlbrew';
set backend_repo_map => {
cpandevel => 'CPAN::Testers::WWW::Development',
cpanstats => 'CPAN::Testers::WWW::Statistics',
'cpanstats-excel' => 'CPAN::Testers::WWW::Statistics::Excel',
generate => 'CPAN::Testers::Data::Generator',
'generate-mailer' => 'CPAN::Testers::WWW::Generator::Mailer',
release => 'CPAN::Testers::Data::Release',
'reports-mailer' => 'CPAN::Testers::WWW::Reports::Mailer',
uploads => 'CPAN::Testers::Data::Uploads',
'uploads-mailer' => 'CPAN::Testers::Data::Uploads::Mailer',
};
# Deploy a Labyrinth site by deploying the vhost/ directory as the root, then
# deploying the lib/ directory inside the vhost/cgi-bin/ directory.
set www_repo_map => {
cpanadmin => 'https://github.com/barbie/cpan-testers-www-admin.git',
cpanprefs => 'https://github.com/barbie/cpan-testers-www-preferences.git',
reports => 'https://github.com/barbie/cpan-testers-www-reports.git',
};
set www_labyrinth_version => {
reports => 5.32,
cpanadmin => 5.27,
cpanprefs => 5.00,
};
set www_dirs => {
cpan => '/media/web/CPAN',
backpan => '/media/web/BACKPAN',
pass => '/media/web/www/reports/html/stats',
stats => '/media/web/www/cpanstats',
devel => '/media/web/www/cpandevel',
prefs => '/media/web/www/cpanprefs/html',
admin => '/media/web/www/cpanadmin/html',
static => '/media/web/www/reports/html/static',
reports => '/media/web/www/reports/html',
};
set www_app_config_files => [qw(
perl.org.conf
cpantesters.org.conf
)];
set www_maintenance_config_file => '000-maintenance.conf';
#######################################################################
# Environments
# The Vagrant VM for development purposes
environment vm => sub {
group www => '192.168.127.127'; # the Vagrant VM IP
group backend => '192.168.127.127'; # the Vagrant VM IP
group db => '192.168.127.127'; # the Vagrant VM IP
group api => '192.168.127.127'; # the Vagrant VM IP
user 'vagrant';
# XXX: Does this only work with virtualbox?
private_key '.vagrant/machines/default/virtualbox/private_key';
};
#######################################################################
# Tasks
desc 'Prepare the machine for a CPANTesters role by installing OS packages';
task prepare =>
sub {
ensure_sudo_password();
Rex::Logger::info( "Checking common packages" );
sudo sub {
install package => $_ for @{ get 'common_packages' };
};
};
desc 'Deploy Perl to the server using perlbrew';
task deploy_perl =>
sub {
my $root = get 'perlbrew_root';
my $perl_version = get 'perl_version';
my %env = (
env => { PERLBREW_ROOT => $root }
);
ensure_sudo_password();
sudo sub {
pkg 'perlbrew', ensure => 'present';
file $root, ensure => 'directory';
run 'perlbrew init', %env;
Rex::Logger::info( 'Installing Perl ' . $perl_version );
run 'perlbrew install ' . $perl_version, %env;
run 'perlbrew install-cpanm', %env;
run 'perlbrew exec cpanm local::lib', %env;
};
};
desc 'Deploy the CPANTesters backend';
task deploy_backend =>
group => [qw( backend )],
sub {
my $root = get 'backend_root_dir';
ensure_sudo_password();
Rex::Logger::info( "Checking backend packages" );
sudo sub {
install package => $_ for @{ get 'backend_packages' };
};
Rex::Logger::info( "Deploying logrotate.conf" );
sudo sub {
file $root . '/etc',
ensure => 'directory';
file $root . '/etc/logrotate.conf',
source => './etc/logrotate.conf',
owner => 'root',
group => 'root',
;
};
Rex::Logger::info( "Deploying backend scripts" );
sudo sub {
sync_up 'backend/*', $root;
};
Rex::Logger::info( "Deploying crontabs" );
sudo sub {
file '/tmp/deploy', ensure => 'directory';
file '/tmp/deploy/crontab-testers',
source => './etc/crontab/backend',
;
Rex::Logger::info( "Checking crontab diff" );
run 'crontab -u barbie -l > /tmp/deploy/crontab-testers.previous';
my $diff = scalar run 'diff -u /tmp/deploy/crontab-testers.previous /tmp/deploy/crontab-testers';
if ( $? == 0 ) {
Rex::Logger::info( "No changes in crontab" );
}
elsif ( $? != 1 ) {
Rex::Logger::info( "Error running diff: $?" );
}
else {
say $diff;
Rex::Logger::info( "Updating crontab" );
say scalar run 'crontab -u barbie /tmp/deploy/crontab-testers';
}
};
};
desc 'Deploy the CPANTesters web config';
task deploy_www_config =>
group => [qw( www )],
sub {
my @sites;
LOCAL {
@sites = grep { $_ ne get 'www_maintenance_config_file' }
map { basename $_ }
glob 'etc/apache2/vhost/*';
};
ensure_sudo_password();
Rex::Logger::info( "Checking www packages" );
sudo sub {
install package => $_ for @{ get 'www_packages' };
};
Rex::Logger::info( "Deploying httpd configs" );
sudo sub {
Rex::Logger::info( "Syncing apache configs" );
#sync_up 'etc/apache2/conf', '/etc/apache2/conf-available';
#run 'a2enconf ' . $_ for qw( log );
run 'a2dissite ' . $_ for split ' ', run 'ls -l /etc/apache2/sites-available';
sync_up 'etc/apache2/vhost', '/etc/apache2/sites-available';
Rex::Logger::info( "Enabling sites: " . join " ", @sites );
run 'a2ensite ' . $_ for @sites;
Rex::Logger::info( 'Enabling apache modules' );
run 'a2enmod ' . $_ for qw( remoteip );
Rex::Logger::info( "Restarting apache service" );
service apache2 => 'restart';
};
};
desc 'Deploy the CPANTesters web app';
task deploy_www =>
group => [qw( www )],
sub {
my $root = get 'www_root_dir';
my %dist = %{ get 'www_repo_map' };
ensure_sudo_password();
Rex::Logger::info( "Updating web app distributions" );
for my $dist ( keys %dist ) {
LOCAL {
run 'mkdir -p dist';
if ( -d "dist/$dist" ) {
run "cd dist/$dist && git pull";
}
else {
run "cd dist && git clone --depth 1 $dist{$dist} $dist";
}
}
}
Rex::Logger::info( "Deploying web app distributions" );
my %labyrinth_version = %{ get 'www_labyrinth_version' };
for my $dist ( keys %dist ) {
Rex::Logger::info( "Staging: $dist" );
run "mkdir -p /tmp/dist/$dist";
sync_up "dist/$dist/vhost/.", "/tmp/dist/$dist/.";
run "mkdir -p /tmp/dist/$dist/cgi-bin/lib";
sync_up "dist/$dist/lib/.", "/tmp/dist/$dist/cgi-bin/lib/.";
my $lversion = $labyrinth_version{ $dist };
Rex::Logger::info( "Installing Labyrinth $lversion" );
run "cpanm -l /tmp/dist/$dist/cgi-bin/lib Labyrinth\@$lversion";
Rex::Logger::info( "Installing Labyrinth::Plugin::Core $lversion" );
run "cpanm -l /tmp/dist/$dist/cgi-bin/lib Labyrinth::Plugin::Core\@5.19";
Rex::Logger::info( "Diffing: $dist" );
my $diff = scalar run "diff -ur $root/$dist /tmp/dist/$dist";
if ( $? == 0 ) {
Rex::Logger::info( "No changes in dist. Skipping." );
next;
}
elsif ( $? != 1 ) {
Rex::Logger::info( "Error running diff: $?. Skipping." );
next;
}
say $diff;
print "Install this dist? (Y/n): ";
my $install = ReadLine(0);
print "\n";
if ( lc $install =~ /^y?\n$/ ) {
Rex::Logger::info( "Installing: $dist" );
#run "rsync -av /tmp/dist/$dist $root/$dist";
}
else {
Rex::Logger::info( "Skipping $dist due to user input" );
}
}
};
desc 'Deploy the CPANTesters database';
task deploy_db =>
sub {
ensure_sudo_password();
Rex::Logger::info( "Checking DB packages" );
sudo sub {
install package => $_ for @{ get 'db_packages' };
};
Rex::Logger::info( "Checking mysqld service" );
sudo sub {
my %mysql_vars = (
datadir => get( 'db_root_dir' ),
);
file '/etc/mysql/my.cnf',
content => template( 'etc/mysql/my.cnf', %mysql_vars ),
;
service mysqld => ensure => 'started';
};
};
desc 'Deploy the monitoring server';
task deploy_monitor =>
group => 'monitor',
sub {
ensure_sudo_password();
# Install packages
sudo sub {
run 'mkdir -p ' . get 'monitor_root_dir';
run 'chown -R _icinga:_icinga ' . get 'monitor_root_dir';
run 'mkdir -p /var/mysql';
run 'chown -R _mysql:_mysql /var/mysql';
run 'mkdir -p /var/www/etc/icingaweb2';
run 'rcctl enable apache2';
run 'rcctl enable icinga2';
run 'rcctl enable mysqld';
run 'icinga2 feature enable ido-mysql';
run 'icinga2 feature enable command';
run 'icingacli setup token create';
run 'mysqladmin -u root password root';
run 'mysqladmin -u root createdb icinga';
run 'mysql -u root -p icinga < /usr/local/share/icinga2-ido-mysql/schema/mysql.sql';
run 'cp /var/www/conf/modules{.sample,}/php-7.0.conf';
run 'chmod -R ugo+rw /var/www/etc/icingaweb2';
# /etc/apache2/httpd2.conf
# LoadModule mod_rewrite.so
# Needed for icingaweb2 to remove "index.php" from paths
# Include /etc/apache2/vhost/*.conf
# Needed to load vhost configs
# /etc/fstab / wxallowed
# Needed for php to work on newer OpenBSD
# /etc/php-7.0.ini pdo_mysql.so
# Load PDO mysql
### Distributed monitoring
# http://docs.icinga.org/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring#distributed-monitoring-automation
# Set up monitor master
run 'icinga2 node setup --master';
# Cert gets made in /etc/icinga2/pki/ca.crt to copy to client
# Set up monitor clients
# $ apt-get install icinga2
# $ icinga2 feature enable api
# $ mkdir -p /etc/icinga2/pki
# $ chown -R icinga:icinga /etc/icinga2/pki
# $ icinga2 pki new-cert --cn <client-fqdn> \
# --key /etc/icinga2/pki/<client-fqdn>.key \
# --cert /etc/icinga2/pki/<client-fqdn>.cert
# Copy the trusted master certificate as /etc/icinga2/pki/master.crt
# $ icinga2 pki save-cert --key /etc/icinga2/pki/<client-fqdn>.key \
# --cert /etc/icinga2/pki/<client-fqdn>.crt \
# --trustedcert /etc/icinga2/pki/master.crt \
# --host <master-fqdn>
# $ icinga2 node setup --ticket <ticket> \
# --endpoint <master-fqdn> \
# --zone <client-fqdn> \
# --master_host <master-fqdn> \
# --trustedcert /etc/icinga2/pki/master.crt \
# --accept-config
# $ service icinga2 restart
run 'rcctl restart apache2';
run 'rcctl restart icinga2';
};
};
desc 'Start maintenance mode';
task 'start_maintenance' =>
group => [qw( www )],
sub {
ensure_sudo_password();
sudo sub {
Rex::Logger::info( 'Syncing maintenance site' );
run 'mkdir -p /var/www/maintenance';
sync_up 'var/www/maintenance', '/var/www/maintenance';
Rex::Logger::info( 'Disabling app sites' );
run 'a2dissite ' . $_ for @{ get 'www_app_config_files' };
Rex::Logger::info( 'Enabling maintenance site' );
run 'a2ensite ' . get 'www_maintenance_config_file';
Rex::Logger::info( 'Restarting apache' );
service apache2 => 'reload';
# XXX: Purge the Fastly cache
}
};
desc 'Stop maintenance mode';
task 'stop_maintenance' =>
group => [qw( www )],
sub {
ensure_sudo_password();
sudo sub {
Rex::Logger::info( 'Enabling app sites' );
run 'a2ensite ' . $_ for @{ get 'www_app_config_files' };
Rex::Logger::info( 'Disabling maintenance site' );
run 'a2dissite ' . get 'www_maintenance_config_file';
Rex::Logger::info( 'Restarting apache' );
service apache2 => 'reload';
# XXX: Purge the Fastly cache
}
};
#######################################################################
# Subroutines
sub ensure_sudo_password {
return if sudo_password();
print 'Password to use for sudo: ';
ReadMode('noecho');
sudo_password ReadLine(0);
ReadMode('restore');
print "\n";
}