summaryrefslogtreecommitdiffstats
path: root/lib/Buildd/Conf.pm
blob: 3dca902eb58796999bb915ff9ff8c04cb8583f83 (plain)
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#
# Conf.pm: configuration library for buildd
# Copyright © 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
# Copyright © 2005 Ryan Murray <rmurray@debian.org>
# Copyright © 2006-2009 Roger Leigh <rleigh@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Buildd::Conf;

use strict;
use warnings;

use Buildd::DistConf qw();
use Buildd::UploadQueueConf qw();
use Sbuild::ConfBase;
use Sbuild::Sysconfig;
use Buildd::ClientConf qw();

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter);

    @EXPORT = qw($reread_config new setup read);
}

our $reread_config = 0;

sub setup ($);
sub read ($);

sub new {
    my $conf = Sbuild::ConfBase->new(@_);
    Buildd::Conf::setup($conf);
    Buildd::Conf::read($conf);

    return $conf;
}

sub setup ($) {
    my $conf = shift;

    my $validate_program = sub {
	my $conf = shift;
	my $entry = shift;
	my $key = $entry->{'NAME'};
	my $program = $conf->get($key);

	die "$key binary is not defined"
	    if !defined($program) || !$program;

	# Emulate execvp behaviour by searching the binary in the PATH.
	my @paths = split(/:/, $conf->get('PATH'));
	# Also consider the empty path for absolute locations.
	push (@paths, '');
	my $found = 0;
	foreach my $path (@paths) {
	    $found = 1 if (-x File::Spec->catfile($path, $program));
	}

	die "$key binary '$program' does not exist or is not executable"
	    if !$found;
    };

    my $validate_directory = sub {
	my $conf = shift;
	my $entry = shift;
	my $key = $entry->{'NAME'};
	my $directory = $conf->get($key);

	die "$key directory is not defined"
	    if !defined($directory) || !$directory;

	die "$key directory '$directory' does not exist"
	    if !-d $directory;
    };

    our $HOME = $conf->get('HOME');
    $main::HOME = $HOME; # TODO: Remove once Buildd.pm uses $conf
    my $arch = $conf->get('ARCH');

    my %buildd_keys = (
	'ADMIN_MAIL'				=> {
	    TYPE => 'STRING',
	    VARNAME => 'admin_mail',
	    GROUP => 'Mail',
	    DEFAULT => 'root',
	    HELP => 'email address for admin'
	},
	'APT_GET'				=> {
	    TYPE => 'STRING',
	    VARNAME => 'apt_get',
	    GROUP => 'Programs',
	    CHECK => $validate_program,
	    DEFAULT => 'apt-get',
	    HELP => 'Path to apt-get binary'
	},
	'DPKG_FILE_SUFFIX'	=> {
	    TYPE => 'STRING',
	    VARNAME => 'dpkg_file_suffix',
	    GROUP => 'Programs',
	    DEFAULT => '',
	    HELP => 'Value for the sbuild dpkg-file-suffix option, to be passed on to sbuild',
	},
	'BUILD_LOG_KEEP'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'build_log_keep',
	    GROUP => 'Watcher',
	    DEFAULT => 2,
	    HELP => 'Number of days until build logs are archived'
	},
	'DAEMON_LOG_FILE'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'daemon_log_file',
	    GROUP => 'Daemon',
	    IGNORE_DEFAULT => 1, # Don't dump the current home
	    DEFAULT => "$HOME/daemon.log",
	    HELP => 'Main buildd daemon log file'
	},
	'DAEMON_LOG_KEEP'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'daemon_log_keep',
	    GROUP => 'Watcher',
	    DEFAULT => 7,
	    HELP => 'Number of days until old daemon logs are archived in a .tar.gz file'
	},
	'DAEMON_LOG_ROTATE'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'daemon_log_rotate',
	    GROUP => 'Watcher',
	    DEFAULT => 1,
	    HELP => 'Number how many days until daemon logs are rotated (one is kept as daemon.log.old, others are moved to old-logs and gzipped)'
	},
	'DAEMON_LOG_SEND'			=> {
	    TYPE => 'BOOL',
	    VARNAME => 'daemon_log_send',
	    GROUP => 'Watcher',
	    DEFAULT => 1,
	    HELP => 'email rotated daemon logs to the admin?'
	},
	'DELAY_AFTER_GIVE_BACK'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'delay_after_give_back',
	    GROUP => 'Daemon',
	    DEFAULT => 8 * 60, # 8 hours
	    HELP => 'Time to avoid packages that have automatically been given back by sbuild (in minutes)'
	},
	'ERROR_MAIL_WINDOW'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'error_mail_window',
	    GROUP => 'Mail',
	    DEFAULT => 8*60*60,
	    HELP => 'If more than five error mails are received within the specified time (in seconds), do not forward (to avoid possible mail loops)'
	},
	'IDLE_SLEEP_TIME'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'idle_sleep_time',
	    GROUP => 'Daemon',
	    DEFAULT => 5*60,
	    HELP => 'Time to sleep when idle (in seconds) between wanna-build --list=needs-build calls)'
	},
	'LOG_QUEUED_MESSAGES'			=> {
	    TYPE => 'BOOL',
	    VARNAME => 'log_queued_messages',
	    GROUP => 'Mail',
	    DEFAULT => 0,
	    HELP => 'Log success messages from upload queue daemon?'
	},
	'MAX_SBUILD_FAILS'				=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'max_sbuild_fails',
	    GROUP => 'Daemon',
	    DEFAULT => 2,
	    HELP => 'Maximum number of times sbuild can fail before sleeping'
	},
	'MIN_FREE_SPACE'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'min_free_space',
	    GROUP => 'Daemon',
	    DEFAULT => 50*1024,
	    HELP => 'Minimum free space (in KiB) on build filesystem'
	},
	'NICE_LEVEL'				=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'nice_level',
	    GROUP => 'Build options',
	    DEFAULT => 10,
	    HELP => 'Nice level to run sbuild.  Dedicated build daemons should not be niced.'
	},
	'NO_DETACH'				=> {
	    TYPE => 'BOOL',
	    VARNAME => 'no_detach',
	    GROUP => 'Daemon',
	    DEFAULT => 0,
	    HELP => 'Disable becoming a daemon, for debugging purposes.  Set to 1 to stop daemonising, otherwise set to 0 to become a daemon.'
	},
	'NO_WARN_PATTERN'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'no_warn_pattern',
	    GROUP => 'Watcher',
	    DEFAULT => '^build/(SKIP|REDO|SBUILD-GIVEN-BACK|buildd\.pid|[^/]*.ssh|chroot-[^/]*|current-[^/]*)$',
	    HELP => 'Don\'t complain about old files if they match the regexp.'
	},
	'PIDFILE'                               => {
	    TYPE => 'STRING',
	    VARNAME => 'pidfile',
	    GROUP => 'Daemon',
# Set once running as a system service.
#          DEFAULT => "${Sbuild::Sysconfig::paths{'LOCALSTATEDIR'}/run/buildd.pid"
	    IGNORE_DEFAULT => 1, # Don't dump the current home
	    DEFAULT => "$HOME/build/buildd.pid",
	    HELP => 'PID file to identify running daemon.'
	},
	'PKG_LOG_KEEP'				=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'pkg_log_keep',
	    GROUP => 'Watcher',
	    DEFAULT => 7,
	    HELP => 'Number of days until to package logs are archived'
	},
	'SHOULD_BUILD_MSGS'			=> {
	    TYPE => 'BOOL',
	    VARNAME => 'should_build_msgs',
	    GROUP => 'Daemon',
	    DEFAULT => 1,
	    HELP => 'Should buildd send "Should I build" messages?'
	},
	'STATISTICS_MAIL'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'statistics_mail',
	    GROUP => 'Watcher',
	    DEFAULT => 'root',
	    HELP => 'email address for statistics summaries'
	},
	'STATISTICS_PERIOD'			=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'statistics_period',
	    GROUP => 'Watcher',
	    DEFAULT => 7,
	    HELP => 'Period for statistic summaries (days)'
	},
	'SUDO'					=> {
	    TYPE => 'STRING',
	    VARNAME => 'sudo',
	    GROUP => 'Programs',
	    CHECK => $validate_program,
	    DEFAULT => 'sudo',
	    HELP => 'Path to sudo binary'
	},
	'WARNING_AGE'				=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'warning_age',
	    GROUP => 'Watcher',
	    DEFAULT => 7,
	    HELP => 'Age (in days) after which a warning is issued for files in upload and dirs in build'
	},
	'CONFIG_TIME'				=> {
	    TYPE => 'NUMERIC',
	    VARNAME => 'config_time',
	    GROUP => '__INTERNAL',
	    DEFAULT => {},
	    HELP => 'Time configuration was last read'
	},
	'DISTRIBUTIONS'                         => {
	    TYPE => 'ARRAY:HASH:SCALAR',
	    VARNAME => 'distributions',
	    GROUP => 'Build options',
	    DEFAULT => [],
	    IGNORE_DEFAULT => 1, # Don't dump class to config
	    HELP => 'List of distributions that buildd should take packages from',
	    EXAMPLE =>
'$distributions = [
	{
		# name of the suite to build (also used to query wanna-build)
		dist_name => ["unstable", "testing"],

		# architecture to be built (will be passed to sbuild and can be
		# used to compute wanna_build_db_name)
		built_architecture => undef,

		# host on which wanna-build is run
		wanna_build_ssh_host => "buildd.debian.org",

		# user as who we are going to connect to the host running wanna-build
		wanna_build_ssh_user => "buildd_arch",

		# SSH control socket path for ssh -S option
		wanna_build_ssh_socket => "",

		# Additional SSH options used when connecting
		wanna_build_ssh_options => [],

		# database used for wanna-build
		wanna_build_db_name => "arch/build-db",

		# Username to use for wanna-build.
		wanna_build_db_user => $Buildd::username,

		# Local queue directory where binaries are stored before uploaded
		# by dupload. You need to configure this directory in
		# @upload_queues to get packages uploaded from there.
		dupload_local_queue_dir => "upload",

		# list of packages which shouldn\'t be picked up by buildd
		no_auto_build => [],

		# list of packages which should only be taken if there absolutely
		# nothing else to do (probably packages included in no_auto_build
		# because they take too long)
		weak_no_auto_build => [],

		# regex used to filter out unwanted packages:
		#no_build_regex => "^(contrib/|non-free/)?non-US/",

		# regex used to filter packages to build:
		#build_regex => "",

		# mail addr of buildd admin handling packages from this distribution
		logs_mailed_to => $admin_mail,

		# schroot name (or alias) of the chrooted environment to use for
		# building (will be passed to sbuild). sbuild\'s default is
		# the first of $distribution-$arch-sbuild, $distribution-sbuild,
		# $distribution-$arch and $distribution.
		sbuild_chroot => undef,

	}
];'
	},
	'UPLOAD_QUEUES'                         => {
	    TYPE => 'ARRAY:HASH:SCALAR',
	    VARNAME => 'upload_queues',
	    GROUP => 'Uploader',
	    DEFAULT => [],
	    IGNORE_DEFAULT => 1, # Don't dump class to config
	    HELP => 'Package upload queues',
	    EXAMPLE =>
'$upload_queues = [
	{
		# Local queue directory where binaries are stored before uploaded
		# by dupload.
		dupload_local_queue_dir => "upload",

		# Upload site for buildd-upload to pass to dupload(1); see
		# /etc/dupload.conf for possible values.
		dupload_archive_name => "anonymous-ftp-master",
	},

	{
		# Local queue directory where binaries are stored before uploaded
		# by dupload.
		dupload_local_queue_dir => "upload-security",

		# Upload site for buildd-upload to pass to dupload(1); see
		# /etc/dupload.conf for possible values.
		dupload_archive_name => "security",
	}
];'
	});

    $conf->set_allowed_keys(\%buildd_keys);
    Buildd::ClientConf::setup($conf);
}

sub read ($) {
    my $conf = shift;

    my $HOME = $conf->get('HOME');

    my $global = $Sbuild::Sysconfig::paths{'BUILDD_CONF'};
    my $user = "$HOME/.builddrc";
    my %config_time = ();
    my $user_time = 0;

    my $reread = 0;

    sub ST_MTIME () { 9 }

    my @config_files = ($global, $user);

    $reread = 1 if $reread_config;

    foreach (@config_files) {
	if (-r $_) {
	    $config_time{$_} = 0;
	    my @stat = stat($_);
	    if (!defined($conf->get('CONFIG_TIME')->{$_}) ||
		$conf->get('CONFIG_TIME')->{$_} < $stat[ST_MTIME]) {
		$config_time{$_} = $stat[ST_MTIME];
		$reread = 1;
	    }
	}
    }

    # For compatibility only.  Non-scalars are deprecated.
    my $deprecated_init = <<END;
# Variables are undefined, so config will default to DEFAULT if unset.
my \$defaults;
my \@distributions;
undef \@distributions;
my \@upload_queues;
undef \@upload_queues;

#legacy fields:
my \@weak_no_auto_build;
undef \@weak_no_auto_build;
my \$build_regex = undef; # Should this be user settable?
my \@no_auto_build;
undef \@no_auto_build;
my \$no_build_regex = undef;
my \@take_from_dists;
undef \@take_from_dists;
my \$sshcmd = undef;
my \$sshsocket = undef;
my \$wanna_build_user = undef;
my \$wanna_build_dbbase = undef;
END

    my $deprecated_setup = '';

    my $custom_setup = <<END;
if (\$sshcmd && \$sshcmd =~ /^\\s*(\\S+)\\s+(.+)/) {
    my \$rest = \$2;
    \$conf->set('SSH', \$1);

    #Try to pry the user out:
    if (\$rest =~ /(-l\\s*(\\S+))\\s+/) {
	\$wanna_build_ssh_user = \$2;
	#purge this from the rest:
	\$rest =~ s/\\Q\$1//;
    } elsif (\$rest =~ /\\s+(\\S+)\@/) {
	\$wanna_build_ssh_user = \$1;
	\$rest =~ s/\\Q\$1\\E\@//;
    }

    #Hope that the last argument is the host:
    if (\$rest =~ /\\s+(\\S+)\\s*\$/) {
	\$wanna_build_ssh_host = \$1;
	\$rest =~ s/\\Q\$1//;
    }

    #rest should be options:
    if (\$rest !~ /\\s*/) {
	\$wanna_build_ssh_options = [split \$rest];
    }
}

if (\$sshsocket) {
    \$wanna_build_ssh_socket = \$sshsocket;
}

if (\$wanna_build_user) {
    \$wanna_build_db_user = \$wanna_build_user;
}

if (\$wanna_build_dbbase) {
    \$wanna_build_db_name = \$wanna_build_dbbase;
}

#Convert old config, if needed:
my \@distributions_info;
if (\@take_from_dists) {
    for my \$dist (\@take_from_dists) {
	my \%entry;

	\$entry{DIST_NAME} = \$dist;
	\$entry{SSH} = \$ssh;

	if (\$dist =~ /security/) {
	    \$entry{DUPLOAD_LOCAL_QUEUE_DIR} = 'upload-security';
	}
	if (\$build_regex) {
	    \$entry{BUILD_REGEX} = \$build_regex;
	}
	if (\$no_build_regex) {
	    \$entry{NO_BUILD_REGEX} = \$build_regex;
	}
	if (\@no_auto_build) {
	    \$entry{NO_AUTO_BUILD} = \\\@no_auto_build;
	}
	if (\@weak_no_auto_build) {
	    \$entry{WEAK_NO_AUTO_BUILD} = \\\@weak_no_auto_build;
	}

	\$entry{WANNA_BUILD_DB_NAME} = \$wanna_build_db_name;
	\$entry{WANNA_BUILD_DB_USER} = \$wanna_build_db_user;
	\$entry{WANNA_BUILD_SSH_HOST} = \$wanna_build_ssh_host;
	\$entry{WANNA_BUILD_SSH_USER} = \$wanna_build_ssh_user;
	\$entry{WANNA_BUILD_SSH_SOCKET} = \$wanna_build_ssh_socket;
	\$entry{WANNA_BUILD_SSH_OPTIONS} = \$wanna_build_ssh_options;
                \$entry{WANNA_BUILD_API} = 0;

	my \$dist_config = Buildd::DistConf::new_hash(CHECK=>$conf->{'CHECK'},
						      HASH=>\\\%entry);

	push \@distributions_info, \$dist_config;
    }
} else {
    my \@dists = ();
    push \@dists, \@{\$distributions} if defined \$distributions;

    if (\@distributions) {
	warn 'W: \@distributions is deprecated; please use the array reference \$distributions[]\n';
	push \@dists, \@distributions;
    }

    for my \$raw_entry (\@dists) {
	my \%entry;
	my \@dist_names;

	#Find out for which distributions this entry is intended:
	for my \$key (keys \%\$raw_entry) {
	    if (uc(\$key) eq "DIST_NAME") {
		if (ref(\$raw_entry->{\$key}) eq "ARRAY") {
		    push \@dist_names, \@{\$raw_entry->{\$key}};
		} else {
		    push \@dist_names, \$raw_entry->{\$key};
		}
	    }
	}

	for my \$key (keys \%\$raw_entry) {
	    if (uc(\$key) ne "DIST_NAME") {
		\$entry{uc(\$key)} = \$raw_entry->{\$key};
	    }
	}

                for my \$key (keys \%\$defaults) {
                    if (uc(\$key) ne "DIST_NAME" && not defined \$entry{uc(\$key)}) {
                        \$entry{uc(\$key)} = \$defaults->{\$key};
                    }
                }

                \$entry{WANNA_BUILD_API} //= 1;


	#We need this to pass this to Buildd::Client:
                \$entry{SSH} = \$ssh;

	#Make one entry per distribution, it's easier later on:
	for my \$dist (\@dist_names) {
	    \$entry{'DIST_NAME'} = \$dist;
                    my \$dist_config = Buildd::DistConf::new_hash(HASH=>\\\%entry);
                    push \@distributions_info, \$dist_config;
	}
    }
}

\$conf->set('DISTRIBUTIONS', \\\@distributions_info);

my \@queues = ();
push \@queues, \@{\$upload_queues} if defined \$upload_queues;

if (\@upload_queues) {
    warn 'W: \@upload_queues is deprecated; please use the array reference \$upload_queues[]\n';
    push \@queues, \@upload_queues;
}

if (\@queues) {
    my \@upload_queue_configs;
    for my \$raw_entry (\@queues) {
	my \%entry;
	for my \$key (keys \%\$raw_entry) {
	    \$entry{uc(\$key)} = \$raw_entry->{\$key};
	}

	my \$queue_config = Buildd::UploadQueueConf::new_hash(CHECK=>$conf->{'CHECK'},
							      HASH=>\\\%entry);

	push \@upload_queue_configs, \$queue_config;
    }
    \$conf->set('UPLOAD_QUEUES', \\\@upload_queue_configs);
} else {
    push \@{\$conf->get('UPLOAD_QUEUES')},
	Buildd::UploadQueueConf::new_hash(CHECK=>$conf->{'CHECK'},
					  HASH=>
	    {
		DUPLOAD_LOCAL_QUEUE_DIR => 'upload',
		DUPLOAD_ARCHIVE_NAME    => 'anonymous-ftp-master'
	    }
	),
	Buildd::UploadQueueConf::new_hash(CHECK=>$conf->{'CHECK'},
					  HASH=>
	    {
		DUPLOAD_LOCAL_QUEUE_DIR => 'upload-security',
		DUPLOAD_ARCHIVE_NAME    => 'security'
	    }
	);
}

# Set here to allow user to override.
if (-t STDIN && -t STDOUT && \$conf->get('NO_DETACH')) {
    \$conf->_set_default('VERBOSE', 1);
} else {
    \$conf->_set_default('VERBOSE', 0);
}
END

    $conf->read(\@config_files, $deprecated_init, $deprecated_setup,
		$custom_setup);

    # Update times
    if ($reread) {
	foreach (@config_files) {
	    if (-r $_) {
		$conf->get('CONFIG_TIME')->{$_} = $config_time{$_};
	    }
	}
    }
}

1;