Arepa/Builder/Sbuild.pm

filter: Statementstmt 32.4% 61 / 188 Branchbran 2.3% 1 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.0% 18 / 29 Podpod 0.0% 0 / 8 totaltotal 29.9% 80 / 267 CC 26 SCAR 36.7
CC
26
Cov
27%
do_create
156.0
_get_schroot_conf
12.9
ensure_file_exists
12.0
CRAP 39.2
« Arepa/Web/Dashboard.pm ↑ Summary Arepa/Web/Auth.pm »
1 package Arepa::Builder::Sbuild;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::chmod;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Arepa;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use base qw(Arepa::Builder);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 my $schroot_config = undef;
23  
24 0 ▶sub _get_schroot_conf {
Subroutine _get_schroot_conf: not called CC=4 SCAR=25.6
25 0 my ($self) = @_;
26  
27 0 ▶ if (!defined $schroot_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schroot_config) 0 0 0
28 0 my $content = "";
29 0 ▶ for my $path ('/etc/schroot/schroot.conf', glob('/etc/schroot/chroot.d/*')) {
Subroutine BEGIN: called CC=1 SCAR=0.2
30 0 ▶ if (open F, $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, $path) { } 0 0 0
31 0 $content .= join("", <F>) . "\n";
32 0 close F;
33 }
34 else {
35 0 print STDERR "Ignoring file '$path': couldn't read\n";
36 }
37 }
38 0 $schroot_config = Config::Tiny->read_string($content);
39 }
40  
41 0 return $schroot_config;
42 }
43  
44 0 ▶sub ensure_file_exists {
Subroutine ensure_file_exists: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my ($self, $path) = @_;
46  
47 0 ▶ unless (-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 0 0 0
48 0 ▶ open F, ">$path" or croak "Couldn't create file '$path'\n";
Branch: 1 branch
BranchTrueFalseTotal
open F, ">$path" or croak("Couldn't create file '${path}'\n") 0 0 0
49 0 close F;
50 }
51 }
52  
53 0 ▶sub builder_exists {
Subroutine builder_exists: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my ($self, $builder_name) = @_;
55  
56 0 return (defined $self->_get_schroot_conf->{$builder_name});
57 }
58  
59 0 ▶sub get_builder_directory {
Subroutine get_builder_directory: not called CC=2 SCAR=17.9
Pod undocumented
60 0 my ($self, $builder_name) = @_;
61  
62 0 ▶ if ($self->builder_exists($builder_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->builder_exists($builder_name)) { } 0 0 0
63 0 return $self->_get_schroot_conf->{$builder_name}->{directory};
64 }
65 else {
66 0 croak "Can't find schroot information for builder '$builder_name'\n";
67 }
68 }
69  
70 0 ▶sub do_init {
Subroutine do_init: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my ($self, $builder) = @_;
72  
73 0 my $builder_dir = $self->get_builder_directory($builder);
74  
75 # Bind some important files to the 'host'
76 0 foreach my $etc_file (qw(resolv.conf passwd shadow group gshadow)) {
77 0 my $full_path = "$builder_dir/etc/$etc_file";
78 0 ▶ unless (-e $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $full_path) 0 0 0
79 0 $self->ensure_file_exists($full_path);
80 }
81 0 my $mount_cmd = qq(mount --bind "/etc/$etc_file" "$full_path");
82 0 my $remount_cmd = qq(mount --bind -oremount,ro) .
83 qq( "/etc/$etc_file" "$full_path");
84 0 $self->ui_module->print_title("Binding /etc/$etc_file to $full_path");
85 0 system($mount_cmd);
86 0 system($remount_cmd);
87 }
88 }
89  
90 0 ▶sub do_uninit {
Subroutine do_uninit: not called CC=3 SCAR=24.8
Pod undocumented
91 0 my ($self, $builder) = @_;
92  
93 0 my $builder_dir = $self->get_builder_directory($builder);
94  
95 # Bind some important files to the 'host'
96 0 my $ok = 1;
97 0 foreach my $etc_file (qw(resolv.conf passwd shadow group gshadow)) {
98 0 my $full_path = "$builder_dir/etc/$etc_file";
99 0 $self->ui_module->print_title("Unbinding $full_path from /etc/$etc_file");
100 0 my $r = system(qq(umount "$full_path" 2>/dev/null));
101 0 ▶ if ($r != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r != 0) 0 0 0
102 0 $ok = 0;
103 }
104 }
105 0 return $ok;
106 }
107  
108 0 ▶sub _call_sbuild {
Subroutine _call_sbuild: not called CC=2 SCAR=17.9
109 0 my ($self, $package_spec, $params, $output_dir) = @_;
110  
111 # 1) Create a temporary directory, change to it
112 0 my $tmp_dir = File::Temp::tempdir();
113 0 my $initial_dir = Cwd::cwd;
114 0 chdir $tmp_dir;
115  
116 # 2) Execute sbuild there and save output in last_build_log
117 0 $self->{last_build_log} = qx/sbuild -v $params $package_spec 2>&1/;
118 0 my $r = $CHILD_ERROR;
119  
120 # 3) Move result to the result directory
121 0 chdir $initial_dir;
122 0 ▶ if ($output_dir !~ qr,^/,) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output_dir =~ qr"^/") 0 0 0
123 0 $output_dir = File::Spec->catfile($initial_dir, $output_dir);
124 }
125 0 ▶ find({ wanted => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
126 0 ▶ if ($File::Find::name =~ /\.deb$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::name =~ /\.deb$/) 0 0 0
127 0 my $move_r = move($File::Find::name, $output_dir);
128 0 ▶ if (!$move_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $move_r) 0 0 0
129 0 print STDERR "Couldn't move $File::Find::name to $output_dir.\nCan't write to $output_dir maybe?\n";
130 }
131 }
132 },
133 0 follow => 0 },
134 $tmp_dir);
135  
136 # 4) Remove temporary directory
137 0 rmtree($tmp_dir);
138  
139 0 return $r;
140 }
141  
142 2 ▶sub _compile_package_from_spec {
Subroutine _compile_package_from_spec: called CC=2 SCAR=7.1
143 2 my ($self, $package_spec, %user_opts) = @_;
144 2 my %opts = (output_dir => '.', bin_nmu => 0, %user_opts);
145  
146 2 my $extra_opts = "";
147 2 ▶ if ($opts{bin_nmu}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"bin_nmu"}) 0 2 2
148 0 $extra_opts .= " --make-binNMU='Recompiled by Arepa' " .
149 "--binNMU='$opts{bin_nmu}' " .
150 "--maintainer='Arepa <arepa-master\@localhost>'";
151 }
152  
153 2 my $builder_name = $self->name;
154 2 my $build_params = "--chroot $builder_name -d unstable --apt-update --nolog -A $extra_opts";
155 my $r = $self->_call_sbuild($package_spec,
156 $build_params,
157 2 $opts{output_dir});
158  
159 2 return ($r == 0);
160 }
161  
162 2 ▶sub do_compile_package_from_dsc {
Subroutine do_compile_package_from_dsc: called CC=1 SCAR=0.0
Pod undocumented
163 2 my ($self, $dsc_file, %user_opts) = @_;
164 2 my %opts = (output_dir => '.', %user_opts);
165 2 return $self->_compile_package_from_spec($dsc_file,
166 %opts);
167 }
168  
169 0 ▶sub do_compile_package_from_repository {
Subroutine do_compile_package_from_repository: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my ($self, $pkg_name, $pkg_version, %user_opts) = @_;
171 0 my %opts = (output_dir => '.', %user_opts);
172 0 my $package_spec = $pkg_name . '_' . $pkg_version;
173  
174 0 return $self->_compile_package_from_spec($package_spec,
175 %opts);
176 }
177  
178 0 ▶sub do_create {
Subroutine do_create: not called CC=12 SCAR=50.5
Pod undocumented
179 0 my ($self, $builder_dir, $mirror, $distribution, %opts) = @_;
180  
181 # Strip trailing slash from the builder directory. If present, it
182 # triggers *very* weird errors when building any package with that
183 # builder (you'll see "cd: 1: can't cd to ..." in the log output)
184 0 $builder_dir =~ s,/$,,;
185 0 my $builder_name = basename($builder_dir);
186  
187 0 my $chrootd_dir = "/etc/schroot/chroot.d";
188 0 my $schroot_file = "$chrootd_dir/$builder_name";
189 0 ▶ if (-e $schroot_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $schroot_file) 0 0 0
190 0 print STDERR "Builder $builder_name already exists ($schroot_file)\n";
191 0 exit 1;
192 }
193 0 mkpath $chrootd_dir;
194 0 my $schroot_content = <<EOCONTENT;
195 [$builder_name]
196 description=Arepa autobuilder $builder_name
197 directory=$builder_dir
198 root-groups=sbuild
199 # groups=sbuild-security
200 groups=sbuild
201 #aliases=testing
202 #personality=linux32
203 EOCONTENT
204 0 $self->ui_module->print_title("Creating schroot file ($schroot_file)");
205 0 ▶ if (open F, ">$schroot_file") {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, ">$schroot_file") { } 0 0 0
206 0 print F $schroot_content;
207 0 close F;
208 }
209 else {
210 0 print STDERR "Couldn't write to file $schroot_file. Check permissions\n";
211 0 exit 1;
212 }
213  
214 0 $self->ui_module->print_title("Creating base chroot");
215 0 my $extra_opts = "";
216 0 ▶ if (defined $opts{arch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"arch"}) 0 0 0
217 0 $extra_opts .= " --arch $opts{arch}";
218 }
219 0 my $debootstrap_cmd = "debootstrap --variant=buildd $extra_opts " .
220 "$distribution '$builder_dir' $mirror";
221 0 my $r = system($debootstrap_cmd);
222 0 ▶ if ($r != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r != 0) 0 0 0
223 0 print STDERR "Error executing debootstrap: error code $r\n";
224 0 print STDERR $debootstrap_cmd, "\n";
225 0 unlink $schroot_file;
226 0 exit 1;
227 }
228  
229 # Create appropriate /etc/apt/sources.list
230 0 $self->ui_module->print_title("Creating default sources.list");
231 open SOURCESLIST, ">$builder_dir/etc/apt/sources.list" or
232 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
open SOURCESLIST, ">$builder_dir/etc/apt/sources.list" or do { print STDERR "Couldn't write to /etc/apt/sources.list"; exit 1 } 0 0 0
233 0 print STDERR "Couldn't write to /etc/apt/sources.list";
234 0 exit 1;
235 };
236 0 print SOURCESLIST <<EOSOURCES;
237 deb $mirror $distribution main
238 deb http://localhost/arepa/repository $distribution main
239 deb-src http://localhost/arepa/repository $distribution main
240 EOSOURCES
241 0 close SOURCESLIST;
242  
243 # Making sure /etc/hosts includes localhost
244 0 $self->ui_module->print_title("Checking /etc/hosts");
245 0 my $full_etc_hosts_path = "$builder_dir/etc/hosts";
246 0 $self->ensure_file_exists($full_etc_hosts_path);
247 0 ▶ if (open F, $full_etc_hosts_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, $full_etc_hosts_path) { } 0 0 0
248 0 my $contents = join("", <F>);
249 0 close F;
250 0 ▶ if (! grep /localhost/, $contents) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /localhost/, $contents) 0 0 0
251 0 ▶ if (open F, ">$full_etc_hosts_path") {
Branch: 1 branch
BranchTrueFalseTotal
if (open F, ">$full_etc_hosts_path") { } 0 0 0
252 0 print F $contents, "\n";
253 0 print F "127.0.0.1\tlocalhost\n";
254 0 close F;
255 }
256 else {
257 0 print STDERR "Couldn't update $full_etc_hosts_path\n";
258 }
259 }
260 }
261 else {
262 0 print STDERR "Couldn't check for a 'localhost' alias in $full_etc_hosts_path\n";
263 }
264  
265 # Make sure certain directories exist and are writable by the 'sbuild'
266 # group
267 0 $self->ui_module->print_title("Creating build directories");
268 0 my ($login, $pass, $uid, $gid) = getpwnam($Arepa::AREPA_MASTER_USER);
269 0 ▶ if (!defined $login) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $login) 0 0 0
270 0 croak "'" . $Arepa::AREPA_MASTER_USER . "' user doesn't exist!";
271 }
272 0 foreach my $dir (qw(build var/lib/sbuild var/lib/sbuild/srcdep-lock)) {
273 0 my $full_path = "$builder_dir/$dir";
274 0 ▶ unless (-d $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $full_path) 0 0 0
275 0 mkpath $full_path;
276 0 ▶ find({ wanted => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
277 0 chmod("g+w", $File::Find::name);
278 0 chown $uid, $gid, $File::Find::name;
279 },
280 0 follow => 0 },
281 $full_path);
282 }
283 }
284  
285 0 $self->ui_module->print_title("Binding files");
286 0 Arepa::Builder::Sbuild->init($builder_name);
287  
288 0 $self->ui_module->print_title("Updating package list");
289 0 my $update_cmd = "chroot '$builder_dir' apt-get update";
290 0 system($update_cmd);
291  
292 0 $self->ui_module->print_title("Installing build-essential and fakeroot");
293 0 my $install_cmd = "chroot '$builder_dir' apt-get -y --force-yes install " .
294 "build-essential fakeroot";
295 0 return system($install_cmd);
296 }
297  
298 1 1;
299  
300 __END__
« Arepa/Web/Dashboard.pm ↑ Summary Arepa/Web/Auth.pm »