summaryrefslogtreecommitdiffstats
path: root/lib/Buildd/ClientConf.pm
blob: e38eb7c12a1a0f7efc7754d387f9a454685ef2de (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
#
# ClientConf.pm: configuration library for wanna-build clients
# 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::ClientConf;

use strict;
use warnings;

use Sbuild::Sysconfig;
use File::Spec;

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

    @ISA = qw(Exporter);

    @EXPORT = qw(setup);
}

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(/:/, $ENV{'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_ssh = sub {
	my $conf = shift;
	my $entry = shift;

# TODO: Provide self, config and entry contexts, which functions to
# get at needed data.  Provide generic configuration functions.
#
	$validate_program->($conf, $conf->{'KEYS'}->{'SSH'});

	my $ssh = $conf->get('SSH');
	my $sshuser = $conf->get('WANNA_BUILD_SSH_USER');
	my $sshhost = $conf->get('WANNA_BUILD_SSH_HOST');
	my @sshoptions = @{$conf->get('WANNA_BUILD_SSH_OPTIONS')};
	my $sshsocket = $conf->get('WANNA_BUILD_SSH_SOCKET');

	my @command = ();

	if ($sshhost) {
	    push (@command, $ssh);
	    push (@command, '-l', $sshuser) if $sshuser;
	    push (@command, '-S', $sshsocket) if $sshsocket;
	    push (@command, @sshoptions) if @sshoptions;
	    push (@command, $sshhost);
	}

	$conf->set('WANNA_BUILD_SSH_CMD', \@command);
    };

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

    my %db_keys = (
	'SSH'					=> {
	    TYPE => 'STRING',
	    VARNAME => 'ssh',
	    GROUP => 'Programs',
	    DEFAULT => 'ssh',
	    CHECK => $validate_ssh,
	    HELP => 'Path to ssh binary'
	},
	'WANNA_BUILD_SSH_CMD'			=> {
	    TYPE => 'STRING',
	    GROUP => '__INTERNAL',
	    DEFAULT => '',
	    HELP => 'Command to run wanna-build (set automatically from the other wanna-build options)'
	},
	'WANNA_BUILD_SSH_USER'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_ssh_user',
	    GROUP => 'wanna-build client',
	    DEFAULT => '',
	    CHECK => $validate_ssh,
	    HELP => 'Username for SSH connection'
	},
	'WANNA_BUILD_SSH_HOST'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_ssh_host',
	    GROUP => 'wanna-build client',
	    DEFAULT => '',
	    CHECK => $validate_ssh,
	    HELP => 'Host for SSH connection'
	},
	'WANNA_BUILD_SSH_SOCKET'		=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_ssh_socket',
	    GROUP => 'wanna-build client',
	    DEFAULT => '',
	    CHECK => $validate_ssh,
	    HELP => 'Socket for SSH connection'
	},
	'WANNA_BUILD_SSH_OPTIONS'		=> {
	    TYPE => 'ARRAY:STRING',
	    VARNAME => 'wanna_build_ssh_options',
	    GROUP => 'wanna-build client',
	    DEFAULT => [],
	    CHECK => $validate_ssh,
	    HELP => 'SSH options.  Note this is an array reference.'
	},
	'WANNA_BUILD_DB_NAME'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_db_name',
	    GROUP => 'wanna-build client',
	    DEFAULT => undef,
	    HELP => 'Database name'
	},
	'WANNA_BUILD_DB_USER'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_db_user',
	    GROUP => 'wanna-build client',
	    DEFAULT => $conf->get('USERNAME'),
	    # arch:all packages must not differ depending on which user is
	    # building the source package, so don't show the default for
	    # example config and man page generation
	    IGNORE_DEFAULT => 1,
	    HELP => 'Database user'
	},
	'BUILT_ARCHITECTURE'			=> {
	    TYPE => 'STRING',
	    VARNAME => 'wanna_build_built_architecture',
	    GROUP => 'wanna-build client',
	    DEFAULT => $arch,
	    # the $native_arch is different depending on the machine where
	    # sbuild is built but arch:all packages must not differ depending on
	    # the architecture they are built on, so don't show the default for
	    # example config and man page generation
	    IGNORE_DEFAULT => 1,
	    HELP => 'Architecture for database'
	});

    $conf->set_allowed_keys(\%db_keys);
}

1;