-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.pm
370 lines (321 loc) · 9.95 KB
/
Builder.pm
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
# Mezzanine Autobuilder Perl Module
#
# Copyright (C) 2005-2011, Michael Jennings <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies of the Software, its documentation and marketing & publicity
# materials, and acknowledgment shall be given in the documentation, materials
# and software packages that this Software was used.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# $Id: Builder.pm,v 1.2 2011/02/10 02:11:35 mej Exp $
#
package Mezzanine::Builder;
use strict;
use Exporter;
use POSIX;
use Mezzanine::Util;
our $VERSION = 0.1;
our @EXPORT = ('&preclean', '&update', '&repair', '&get_phase', '&build', '&createrepo', '&flush', '&sync');
our @EXPORT_OK = ('$VERSION');
our %EXPORT_TAGS = ( "FIELDS" => [ @EXPORT_OK, @EXPORT ] );
my $TIME_FORMAT = "[%Y-%m-%d %H:%M:%S]";
sub
tprint(@)
{
return print POSIX::strftime($TIME_FORMAT, localtime(time())), ' ', @_;
}
sub
tprintf(@)
{
print POSIX::strftime($TIME_FORMAT, localtime(time())), ' ';
return printf @_;
}
sub
new(@)
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $self;
$self = {};
bless($self, $class);
return $self->init(@_);
}
sub
get($)
{
my ($self, @keys) = @_;
my @values;
dprint &print_args(@_);
foreach my $key (@keys) {
if (($key !~ /^\w+$/) || (!exists($self->{$key}))) {
push @values, undef;
} else {
push @values, $self->{$key};
}
}
dprintf("Returning \"%s\".\n", join(", ", @values));
if (wantarray()) {
return @values;
} else {
return $values[0];
}
}
sub
set($$)
{
my ($self, %pairs) = @_;
my $final;
dprint &print_args(@_);
foreach my $key (keys(%pairs)) {
if (($key !~ /^\w+$/) || (!exists($self->{$key}))) {
$final = undef;
} elsif (defined($pairs{$key})) {
$final = $self->{$key} = $pairs{$key};
}
}
dprintf("Returning %s.\n", ((defined($final)) ? ($final) : ("<undef>")));
return $final;
}
sub
init($)
{
my ($self, $config) = @_;
$self->{"CONFIG"} = $config;
if ($config->get("TIME_FORMAT")) {
$TIME_FORMAT = $config->get("TIME_FORMAT");
}
# Constants
$self->{"ARCHDIR"} = sprintf("%s/%s", $config->get("BUILDDIR"), $config->get("ARCH"));
$self->{"SRPMDIR"} = $config->get("BUILDDIR") . "/SRPMS";
$self->{"LOGDIR"} = $self->{"ARCHDIR"} . "/00_LOGS";
foreach my $dir ("ARCHDIR", "SRPMDIR", "LOGDIR") {
&mkdirhier($self->{$dir});
}
@{$self->{"BUILDTREES"}} = $self->find_build_trees();
return $self;
}
sub
preclean()
{
my $self = shift;
my @dirs;
if ($self->get("CONFIG")->get("PRECLEAN")) {
tprint "Beginning preclean step.\n";
} else {
tprint "Skipping preclean step.\n";
return;
}
dprintf("Pre-cleaning in %s\n", &get_temp_dir());
@dirs = glob(&get_temp_dir() . "/mezzanine*");
if (scalar(@dirs)) {
dprintf("Got %d leftover temporary directories.\n", scalar(@dirs));
&nuke_tree(@dirs);
} else {
tprint "Preclean step not needed -- no leftover temporary directories.\n";
}
tprint "Preclean step complete.\n";
}
sub
repair()
{
my $self = shift;
my $buildroot = $self->{"CONFIG"}->get("BUILDROOT");
if ($self->get("CONFIG")->get("REPAIR")) {
tprint "Beginning repair step.\n";
} else {
tprint "Skipping repair step.\n";
return;
}
tprint "Repairing buildroot $buildroot.\n";
&nuke_tree(glob("$buildroot/var/lib/rpm/__db.*"));
&run_cmd("/usr/sbin/chroot", "$buildroot rpm --rebuilddb", "buildroot repair -> ", 120);
&nuke_tree(glob("$buildroot/var/lib/rpm/__db.*"));
tprint "Repair step complete.\n";
}
sub
update()
{
my $self = shift;
my $buildroot = $self->{"CONFIG"}->get("BUILDROOT");
my $err;
if ($self->get("CONFIG")->get("UPDATE")) {
tprint "Beginning update step.\n";
} else {
tprint "Skipping update step.\n";
return;
}
$err = &run_cmd("/usr/sbin/chroot", sprintf("$buildroot %s check-update", $self->{"CONFIG"}->get("DEPSOLVER")),
"buildroot update -> ");
&nuke_tree(glob("$buildroot/var/lib/rpm/__db.*"));
if ($err) {
wprint "Buildroot $buildroot has updates pending. This is probably not good.\n";
}
tprint "Update step complete.\n";
}
sub
get_phase()
{
my $self = shift;
if ($self->get("CONFIG")->get("GET")) {
tprint "Beginning get step.\n";
} else {
tprint "Skipping get step.\n";
return;
}
if (!scalar(@{$self->{"BUILDTREES"}})) {
wprint "No build trees found! Nothing to do!\n";
return;
}
foreach my $dir (@{$self->{"BUILDTREES"}}) {
my $save_cwd = &getcwd();
tprint "Getting package updates for $dir.\n";
chdir($dir);
&run_cmd("/usr/bin/revtool", "-g", "buildtree get ($dir) -> ");
chdir($save_cwd);
}
tprint "Get step complete.\n";
}
sub
build()
{
my $self = shift;
my $common_params;
if (!scalar(@{$self->{"BUILDTREES"}})) {
wprint "Nothing to build!\n";
return;
} elsif ($self->get("CONFIG")->get("BUILD")) {
tprint "Beginning build step.\n";
} else {
tprint "Skipping build step.\n";
return;
}
$common_params = sprintf("%s -b orc --di '%s' -i '%s' -L '/src.rpm\$/=%s,/rpm\$/=%s' %s",
(($self->{"CONFIG"}->get("DEBUG")) ? ("--debug") : ("")),
$self->{"CONFIG"}->get("DEPSOLVER"), $self->{"CONFIG"}->get("BUILDROOT"),
$self->{"SRPMDIR"}, $self->{"ARCHDIR"}, $self->{"CONFIG"}->get("OPTIONS"));
foreach my $tree (@{$self->{"BUILDTREES"}}) {
my $build_tag = &basename($tree);
my ($params, $err);
tprint "Building $build_tag (log is $self->{LOGDIR}/$build_tag/build.log)....\n";
&mkdirhier($self->{"LOGDIR"} . "/$build_tag");
$params = sprintf("%s -s '%s' --builddir '%s/%s' -l '%s/%s/build.log'", $common_params, $tree,
$self->{"LOGDIR"}, $build_tag, $self->{"LOGDIR"}, $build_tag);
# Indent output 3 spaces for readability.
$err = &run_cmd("/usr/bin/buildtool", $params, " ", 0);
if (($err > 0) && ($err <= 128)) {
tprint "Built $err package(s).\n";
} elsif (($err < 0) || (($err > 128) && $err < 256)) {
if ($err > 128) {
$err -= 256;
}
$err = -$err;
eprint "$err package(s) failed to build.\n";
} else {
tprint "No packages needed building.\n";
}
}
tprint "Build step complete.\n";
}
sub
createrepo()
{
my $self = shift;
if ($self->get("CONFIG")->get("CREATEREPO")) {
tprint "Beginning createrepo step.\n";
} else {
tprint "Skipping createrepo step.\n";
return;
}
foreach my $cmd ("yum-arch", "createrepo") {
local *OUTFILE;
my $outfile = $self->{"LOGDIR"} . "/$cmd.out";
my @output;
tprint "Creating repository metadata with $cmd.\n";
@output = &run_cmd("/usr/bin/$cmd", "-vv -x '00_LOGS/*' " . $self->{"ARCHDIR"}, " -> ");
if (open(OUTFILE, ">$outfile")) {
print OUTFILE join("", @output);
close(OUTFILE);
} else {
eprint "Unable to write to $outfile -- $!\n";
}
}
tprint "Createrepo step complete.\n";
}
sub
flush()
{
my $self = shift;
my @output;
if ($self->get("CONFIG")->get("FLUSH")) {
tprint "Beginning flush step.\n";
} else {
tprint "Skipping flush step.\n";
return;
}
foreach my $dir ($self->{"ARCHDIR"}, $self->{"SRPMDIR"}) {
tprint "Finding outdated packages in $dir.\n";
push @output, &run_cmd("/usr/bin/mzreposcan", "--old $dir", "outdated: ");
}
#&nuke_tree(@output);
tprint "Flush step complete.\n";
}
sub
sync()
{
my $self = shift;
my $sync_target;
if ($self->get("CONFIG")->get("SYNC")) {
tprint "Beginning sync step.\n";
} else {
tprint "Skipping sync step.\n";
return;
}
$sync_target = $self->get("CONFIG")->get("SYNCTARGET");
if ($sync_target) {
my $params = sprintf("-Hav --delete-after %s %s %s", $self->{"ARCHDIR"}, $self->{"SRPMDIR"}, $sync_target);
tprint "Syncing to $sync_target.\n";
&run_cmd("/usr/bin/rsync", $params, "-sync-> ");
} else {
tprint "Sync step unavailable -- no target.\n";
}
tprint "Sync step complete.\n";
}
### Private methods.
sub
find_build_trees()
{
my $self = shift;
my @trees;
foreach my $tree (split(':', $self->{"CONFIG"}->get("BUILDTREES"))) {
foreach my $dir (glob($tree)) {
if ((&basename($dir) eq "CVS") || (&basename($dir) eq ".svn")) {
next;
}
if ($dir =~ /^([^\`\$\(\)]*)$/) {
$dir = $1;
} else {
next;
}
if (-d $dir) {
dprint "Found build tree $dir.\n";
push @trees, $dir;
}
}
}
return @trees;
}
1;