-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap_modules.pl
executable file
·402 lines (326 loc) · 9.11 KB
/
bootstrap_modules.pl
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
#!/usr/bin/perl
#!/disks/patric-common/runtime/bin/perl
use File::Basename;
use strict;
use Cwd qw(abs_path getcwd);
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
my $top_dir = dirname(abs_path($0));
my $runtime_modules_dir = "$top_dir/runtime-modules";
our $have_template;
eval {
require Template;
$have_template++;
};
my($help, $dest, $module_dat);
my $rpm_name;
my $build_rpm;
my $rpm_sandbox = "rpm-sandbox";
my $rpm_version;
#
# Determine OS release and version if possible.
#
my($distro, $version) = find_distro();
my($build_dep_tool, $build_dep_key);
if ($distro eq "CentOS")
{
$build_dep_tool = "yum install";
$build_dep_key = "rpm-build-dep";
}
elsif ($distro eq "Ubuntu")
{
$build_dep_tool = "apt-get install";
$build_dep_key = "apt-build-dep";
}
print STDERR "Building for distro $distro version $version\n";
my $installwatch;
my $install_build_dependencies;
my($help, $dest, $module_dat);
my @added_path;
my $fail_ok = 0;
GetOptions('h' => \$help,
'help' => \$help,
'fail-ok' => \$fail_ok,
'd=s' => \$dest,
'm=s' => \$module_dat,
"build-rpm" => \$build_rpm,
"rpm-name=s" => \$rpm_name,
"rpm-version=s" => \$rpm_version,
"rpm-sandbox=s" => \$rpm_sandbox,
"path=s" => \@added_path,
"install-build-dependencies" => \$install_build_dependencies,
) or pod2usage(0);
pod2usage(-exitstatus => 0,
-output => \*STDOUT,
-verbose => 2,
-noperldoc => 1,
) if (defined $help or (!defined $dest) or (!defined $module_dat));
if ($build_rpm)
{
if (! -d $rpm_sandbox)
{
die "RPM sandbox directory $rpm_sandbox does not exist\n";
}
$rpm_sandbox = abs_path($rpm_sandbox);
for my $p (qw(BUILD RPMS SOURCES SPECS SRPMS))
{
if (! -d "$rpm_sandbox/$p")
{
mkdir($p) or die "Cannot mkdir $p: $!\n";
}
}
}
$ENV{PATH} = join(":", "$dest/bin", @added_path, $ENV{PATH});
$ENV{CPATH} = "$dest/include";
$ENV{LIBRARY_PATH} = "$dest/lib";
my $mbase = basename($module_dat);
my $start_cwd = getcwd();
my $log_dir = "$start_cwd/logs.$mbase";
-d $log_dir || mkdir $log_dir || die "Cannot mkdir $log_dir: $!";
my $log_dir = abs_path($log_dir);
$ENV{TARGET} = $dest;
if (! -d $dest)
{
mkdir $dest or die "Cannot mkdir $dest: $!";
}
for my $dir (qw(bin lib etc man))
{
if (! -d "$dest/$dir")
{
mkdir "$dest/$dir" or die "Cannot mkdir $dest/$dir: $!";
}
}
open(DAT, "<", $module_dat) or die "Cannot open $module_dat: $!";
$| = 1;
my @modules;
my %modules;
my %attribs;
my %meta;
my @fails;
while (<DAT>)
{
chomp;
s/^\s*//;
if (/^\#\s+(\S+)\s+(.*)$/)
{
$attribs{$1} = $2;
}
elsif (/^\#\#\s+(\S+)\s+(.*?)\s*$/)
{
push(@{$meta{$1}}, $2);
}
next if /^\#/;
my($dir, $cmd) = split(/\s+/, $_, 2);
my $mod_dir = "$runtime_modules_dir/$dir";
die "error parsing $module_dat" unless ($mod_dir && $cmd);
die "directory $mod_dir does not exist" unless -d $mod_dir;
die "directory $mod_dir is not executable" unless -e $mod_dir;
die "directory $mod_dir is not writable" unless -w $mod_dir;
my $rec = [$dir, $dir, $cmd, { %attribs }, { %meta }];
push(@modules, $rec);
push(@{$modules{$dir}}, $rec);
%attribs = ();
%meta = ();
}
close(DAT);
$rpm_version = $meta{'rpm-version'} if $meta{'rpm-version'} && !$rpm_version;
$rpm_name = $meta{'rpm-name'} if $meta{'rpm-name'} && !$rpm_name;
#
# Rewrite dir-tag element ($rec[1]) for the
# directories that have more than one build record.
#
for my $dir (keys %modules)
{
my $l = $modules{$dir};
if (@$l > 1)
{
for my $i (0..$#$l)
{
my $n = $i + 1;
$l->[$i]->[1] = "${dir}_$n";
}
}
}
if ($install_build_dependencies)
{
my @deps = map { my($dir, $tag, $cmd, $attribs, $meta) = @$_;
my $b = $meta->{"rpm-build-dep"};
ref($b) ? @$b : () } @modules;
if (@deps)
{
print STDERR "Installing dependent RPMs @deps\n";
my @cmd = ("sudo", "yum", "-y", "install", @deps);
my $rc = system(@cmd);
$rc == 0 or die "Failed to install deps: @cmd\n";
}
}
my %save = %ENV;
for my $mod (@modules)
{
my($dir, $tag, $cmd, $attribs, $meta) = @$mod;
if ($meta->{"os-skip"}->[0] eq $distro)
{
print "Skipping build of $tag on $distro\n";
next;
}
if (-f "$log_dir/built.$tag")
{
print "$tag already built\n";
next;
}
%ENV = %save;
for my $env (keys %$attribs)
{
print "setenv $env = $attribs->{$env}\n";
$ENV{$env} = $attribs->{$env};
}
my $to_run;
if ($installwatch)
{
$to_run = "cd $runtime_modules_dir/$dir; $installwatch -o $log_dir/install_data.$tag $cmd 2>&1";
}
else
{
$to_run = "cd $runtime_modules_dir/$dir; $cmd 2>&1";
}
open(LOG, ">", "$log_dir/$tag") or die "Cannot open logfile $log_dir/$tag: $!";
open(RUN, "$to_run |") or die "Cannot open pipe $to_run: $!";
print LOG "$to_run\n";
print "$to_run\n";
while (<RUN>)
{
s/%/%%/g;
printf "%-10s $_", $tag;
print LOG $_;
}
if (!close(RUN))
{
if ($!)
{
die "Error running $to_run: $!\n";
}
else
{
if ($fail_ok)
{
my $msg = "Command $to_run failed with nonzero status $?\n";
warn $msg;
push(@fails, $msg);
}
else
{
die "Command $to_run failed with nonzero status $?\n";
}
}
}
system("touch", "$log_dir/built.$tag");
close(LOG);
}
if (@fails)
{
warn scalar(@fails) . " failures found:\n";
warn $_ foreach @fails;
}
system("git describe --always --tags > $dest/VERSION") == 0 or die "could not write VERSION file to $dest";
&build_rpm if $build_rpm;
sub build_rpm
{
my $spec_dir = "$rpm_sandbox/SPECS";
my $rpm_name_base = "$rpm_name-$rpm_version";
my $rel;
die "RPM build requires a perl with Template installed\n" unless $have_template;
for my $p (<$spec_dir/$rpm_name_base*>)
{
my($r) = $p =~ /$rpm_name_base-(\d+)/;
$rel = $r if ($r > $rel);
}
$rel++;
my $spec = "$spec_dir/$rpm_name-$rpm_version-$rel.spec";
print "Create $spec\n";
my $templ = Template->new(RELATIVE => 1);
my $build_root = dirname($dest);
my $base_name = basename($dest);
my $vars = {
name => $rpm_name,
version => $rpm_version,
release => $rel,
summary => "Bootstrap build from $module_dat",
root_path => $build_root,
base_name => $base_name,
};
$templ->process("bootstrap_spec.tt", $vars, $spec);
my @cmd = ("rpmbuild",
-D => "_topdir $rpm_sandbox",
-D => "_builddir $rpm_sandbox/BUILD",
-D => "_rpmdir $rpm_sandbox/RPMS",
-D => "_sourcedir $rpm_sandbox/SOURCES",
-D => "_specdir $rpm_sandbox/SPECS",
-D => "_srcrpmdir $rpm_sandbox/SRPMS",
'-bb', $spec);
print "@cmd\n";
my $rc = system(@cmd);
if ($rc != 0)
{
die "Error $rc building RPM with @cmd\n";
}
}
sub find_distro
{
my $lsb_release = find_in_path("lsb_release");
if ($lsb_release)
{
my $distro = `$lsb_release -i -s`;
if (!defined($distro))
{
warn "$lsb_release -i -s failed with $!; setting using Unknown distro\n";
return("Unknown", "Unknown");
}
chomp $distro;
my $version = `$lsb_release -r -s`;
if (!defined($version))
{
warn "$lsb_release -r -s failed with $!; setting using Unknown version\n";
return($distro, "Unknown");
}
chomp $version;
return($distro, $version);
}
return("Unknown", "Unknown");
}
sub find_in_path
{
my($cmd) = @_;
for my $p (split(/:/, $ENV{PATH}))
{
if (-x "$p/$cmd")
{
return "$p/$cmd";
}
}
return undef;
}
=pod
=head1 NAME
bootstrap_modules.pl
=head1 SYNOPSIS
=over
=item bootstrap_modules.pl -d /kb/runtime -m ./my_modules.dat
=back
=head1 DESCRIPTION
The bootstrap.pl script reads a module.dat and runs a builder for each module. A module is defined as a directory that contains a builder script. For example, kb_blast is a directory that represents a blast module. In that directory is a script that installs blast.
=head1 CONVENTIONS
The module directory may contain a suffix. If there is no suffix, it is assumed that the installer inside that directory will properly install the module on all supported operating systems. If there is a suffix on the module directory in the form _ubuntu and _centos, then the installer inside that directory will properly install the module on the named operating system.
Examples include bootstrap_ubuntu and bootstrap_centos. In side bootstrap_ubuntu the builder script could use apt-get to install ubuntu modules and inside the bootstrap_centos directory the builder script could use yum to install centos modules.
=head1 MODULES FILE
The modules.dat file contains a space delimited set of module directories and builer scripts. You can think of this as the module directory being the key and the name of the builder script being the value if you want. In sort, the named builder script associated with each module directory will be executed.
=head1 COMMAND-LINE OPTIONS
=over
=item -h, --help This documentation
=item -d Destination target for runtime (ie /kb/runtime)
=item -m Name of the modules.dat file
=item --install-build-dependencies When specified, this will install rpms specified in the modules.dat file.
=back
=head1 AUTHORS
Robert Olson, Tom Brettin
=cut