2006-09-30 03:21:26 +04:00
|
|
|
#
|
|
|
|
# Base class for all Steps.
|
|
|
|
#
|
|
|
|
|
|
|
|
package Bootstrap::Step;
|
2006-12-05 22:12:59 +03:00
|
|
|
use IO::Handle;
|
2007-02-01 06:50:47 +03:00
|
|
|
use File::Spec::Functions;
|
|
|
|
use Bootstrap::Config;
|
|
|
|
use MozBuild::Util qw(RunShellCommand Email);
|
2006-11-10 02:28:20 +03:00
|
|
|
use POSIX qw(strftime);
|
2007-02-01 06:50:47 +03:00
|
|
|
use base 'Exporter';
|
|
|
|
our @EXPORT = qw(catfile);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
|
|
|
my $DEFAULT_TIMEOUT = 3600;
|
2006-10-07 05:29:11 +04:00
|
|
|
|
2006-09-30 03:21:26 +04:00
|
|
|
sub new {
|
|
|
|
my $proto = shift;
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
my $this = {};
|
|
|
|
bless($this, $class);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub Shell {
|
|
|
|
my $this = shift;
|
|
|
|
my %args = @_;
|
|
|
|
my $cmd = $args{'cmd'};
|
2007-02-01 06:50:47 +03:00
|
|
|
my $cmdArgs = defined($args{'cmdArgs'}) ? $args{'cmdArgs'} : [];
|
2006-10-07 05:29:11 +04:00
|
|
|
my $dir = $args{'dir'};
|
2006-11-10 02:28:20 +03:00
|
|
|
my $timeout = $args{'timeout'} ? $args{'timeout'} : $DEFAULT_TIMEOUT;
|
|
|
|
my $logFile = $args{'logFile'};
|
2006-10-07 05:29:11 +04:00
|
|
|
my $rv = '';
|
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
if (ref($cmdArgs) ne 'ARRAY') {
|
|
|
|
die "ASSERT: Bootstrap::Step(): cmdArgs is not an array ref\n"
|
|
|
|
}
|
|
|
|
|
2006-10-07 05:29:11 +04:00
|
|
|
if ($dir) {
|
2006-11-10 02:28:20 +03:00
|
|
|
$this->Log('msg' => 'Changing directory to ' . $dir);
|
|
|
|
chdir($dir) or die "Cannot chdir to $dir: $!";
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
$this->Log('msg' => 'Running shell command:');
|
|
|
|
$this->Log('msg' => ' arg0: ' . $cmd);
|
|
|
|
my $argNum = 1;
|
|
|
|
foreach my $arg (@{$cmdArgs}) {
|
|
|
|
$this->Log('msg' => ' arg' . $argNum . ': ' . $arg);
|
|
|
|
$argNum += 1;
|
|
|
|
}
|
2006-11-10 02:28:20 +03:00
|
|
|
$this->Log('msg' => 'Starting time is ' . $this->CurrentTime());
|
2007-02-01 06:50:47 +03:00
|
|
|
$this->Log('msg' => 'Logging output to ' . $logFile);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
$this->Log('msg' => 'Timeout: ' . $timeout);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2006-10-07 05:29:11 +04:00
|
|
|
if ($timeout) {
|
2006-12-05 22:12:59 +03:00
|
|
|
$rv = RunShellCommand(
|
2007-02-01 06:50:47 +03:00
|
|
|
'command' => $cmd,
|
|
|
|
'args' => $cmdArgs,
|
|
|
|
'timeout' => $timeout,
|
|
|
|
'logfile' => $logFile,
|
2006-10-07 05:29:11 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-09-30 03:21:26 +04:00
|
|
|
my $exitValue = $rv->{'exitValue'};
|
2006-10-07 05:29:11 +04:00
|
|
|
my $timedOut = $rv->{'timedOut'};
|
|
|
|
my $signalName = $rv->{'signalName'};
|
|
|
|
my $dumpedCore = $rv->{'dumpedCore'};
|
|
|
|
if ($timedOut) {
|
2006-11-10 02:28:20 +03:00
|
|
|
$this->Log('msg' => "output: $rv->{'output'}") if $rv->{'output'};
|
2006-10-07 05:29:11 +04:00
|
|
|
die("FAIL shell call timed out after $timeout seconds");
|
|
|
|
}
|
|
|
|
if ($signalName) {
|
2007-02-01 06:50:47 +03:00
|
|
|
$this->Log('msg' => 'WARNING shell recieved signal' . $signalName);
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
if ($dumpedCore) {
|
2006-11-10 02:28:20 +03:00
|
|
|
$this->Log('msg' => "output: $rv->{'output'}") if $rv->{'output'};
|
2006-10-07 05:29:11 +04:00
|
|
|
die("FAIL shell call dumped core");
|
|
|
|
}
|
2006-11-10 02:28:20 +03:00
|
|
|
if ($exitValue) {
|
|
|
|
if ($exitValue != 0) {
|
|
|
|
$this->Log('msg' => "output: $rv->{'output'}") if $rv->{'output'};
|
|
|
|
die("shell call returned bad exit code: $exitValue");
|
|
|
|
}
|
|
|
|
}
|
2006-10-07 05:29:11 +04:00
|
|
|
|
2006-11-10 02:28:20 +03:00
|
|
|
if ($rv->{'output'} && not defined($logFile)) {
|
2006-10-07 05:29:11 +04:00
|
|
|
$this->Log('msg' => "output: $rv->{'output'}");
|
2006-09-30 03:21:26 +04:00
|
|
|
}
|
2006-11-10 02:28:20 +03:00
|
|
|
|
|
|
|
# current time
|
|
|
|
$this->Log('msg' => 'Ending time is ' . $this->CurrentTime());
|
2006-09-30 03:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sub Log {
|
|
|
|
my $this = shift;
|
|
|
|
my %args = @_;
|
|
|
|
my $msg = $args{'msg'};
|
|
|
|
print "log: $msg\n";
|
|
|
|
}
|
|
|
|
|
2006-10-07 05:29:11 +04:00
|
|
|
sub CheckLog {
|
|
|
|
my $this = shift;
|
|
|
|
my %args = @_;
|
|
|
|
|
|
|
|
my $log = $args{'log'};
|
|
|
|
my $notAllowed = $args{'notAllowed'};
|
|
|
|
my $checkFor = $args{'checkFor'};
|
|
|
|
my $checkForOnly = $args{'checkForOnly'};
|
|
|
|
|
2006-11-10 02:28:20 +03:00
|
|
|
if (not defined($log)) {
|
|
|
|
die "No log file specified";
|
|
|
|
}
|
|
|
|
|
|
|
|
open (FILE, "< $log") or die "Cannot open file $log: $!";
|
2006-10-07 05:29:11 +04:00
|
|
|
my @contents = <FILE>;
|
2006-11-10 02:28:20 +03:00
|
|
|
close FILE or die "Cannot close file $log: $!";
|
2006-10-07 05:29:11 +04:00
|
|
|
|
|
|
|
if ($notAllowed) {
|
|
|
|
my @errors = grep(/$notAllowed/i, @contents);
|
|
|
|
if (@errors) {
|
2006-11-10 02:28:20 +03:00
|
|
|
die "Errors in log ($log): \n\n @errors \n";
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($checkFor) {
|
|
|
|
if (not grep(/$checkFor/i, @contents)) {
|
2006-11-10 02:28:20 +03:00
|
|
|
die "$checkFor is not present in file $log \n";
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($checkForOnly) {
|
|
|
|
if (not grep(/$checkForOnly/i, @contents)) {
|
2006-11-10 02:28:20 +03:00
|
|
|
die "$checkForOnly is not present in file $log \n";
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
my @errors = grep(!/$checkForOnly/i, @contents);
|
|
|
|
if (@errors) {
|
2006-11-10 02:28:20 +03:00
|
|
|
die "Errors in log ($log): \n\n @errors \n";
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-10 02:28:20 +03:00
|
|
|
sub CurrentTime() {
|
|
|
|
my $this = shift;
|
|
|
|
|
|
|
|
return strftime("%T %D", localtime());
|
|
|
|
}
|
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
# Overridden by child if needed
|
|
|
|
sub Push() {
|
|
|
|
my $this = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Overridden by child if needed
|
|
|
|
sub Announce() {
|
|
|
|
my $this = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub SendAnnouncement() {
|
|
|
|
my $this = shift;
|
|
|
|
my %args = @_;
|
|
|
|
|
|
|
|
my $config = new Bootstrap::Config();
|
|
|
|
|
|
|
|
my $from = $args{'from'} ? $args{'from'} : $config->Get(var => 'from');
|
|
|
|
my $to = $args{'to'} ? $args{'to'} : $config->Get(var => 'to');
|
|
|
|
my $cc = $args{'cc'} ? $args{'cc'} : $config->Get(var => 'cc');
|
|
|
|
my $subject = $args{'subject'};
|
|
|
|
my $message = $args{'message'};
|
|
|
|
|
|
|
|
my @ccList = split(', ', $cc);
|
|
|
|
|
|
|
|
eval {
|
|
|
|
Email(
|
|
|
|
from => $from,
|
|
|
|
to => $to,
|
|
|
|
cc => \@ccList,
|
|
|
|
subject => $subject,
|
|
|
|
message => $message,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
die("Could not send announcement email: $@");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-30 03:21:26 +04:00
|
|
|
1;
|