2006-09-30 03:21:26 +04:00
|
|
|
#
|
|
|
|
# Base class for all Steps.
|
|
|
|
#
|
|
|
|
|
|
|
|
package Bootstrap::Step;
|
2007-03-10 00:20:05 +03:00
|
|
|
|
2006-12-05 22:12:59 +03:00
|
|
|
use IO::Handle;
|
2007-02-01 06:50:47 +03:00
|
|
|
use File::Spec::Functions;
|
2007-03-10 00:20:05 +03:00
|
|
|
use POSIX qw(strftime);
|
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
use Bootstrap::Config;
|
|
|
|
use MozBuild::Util qw(RunShellCommand Email);
|
2007-03-10 00:20:05 +03:00
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
use base 'Exporter';
|
2007-03-10 00:20:05 +03:00
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
our @EXPORT = qw(catfile);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
|
|
|
my $DEFAULT_TIMEOUT = 3600;
|
2007-07-06 02:58:32 +04:00
|
|
|
my $DEFAULT_LOGFILE = 'default.log';
|
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-07-06 02:58:32 +04:00
|
|
|
my $cmdArgs = exists($args{'cmdArgs'}) ? $args{'cmdArgs'} : [];
|
2006-10-07 05:29:11 +04:00
|
|
|
my $dir = $args{'dir'};
|
2007-07-06 02:58:32 +04:00
|
|
|
my $timeout = exists($args{'timeout'}) ? $args{'timeout'} :
|
|
|
|
$DEFAULT_TIMEOUT;
|
|
|
|
my $ignoreExitValue = exists($args{'ignoreExitValue'}) ?
|
|
|
|
$args{'ignoreExitValue'} : 0;
|
2006-10-07 05:29:11 +04:00
|
|
|
my $rv = '';
|
2007-03-10 00:20:05 +03:00
|
|
|
my $config = new Bootstrap::Config();
|
2006-10-07 05:29:11 +04:00
|
|
|
|
2007-07-06 02:58:32 +04:00
|
|
|
my $logFile = exists($args{'logFile'}) ? $args{'logFile'} :
|
|
|
|
catfile($config->Get(var => 'logDir'), $DEFAULT_LOGFILE);
|
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
if (ref($cmdArgs) ne 'ARRAY') {
|
2007-03-10 00:20:05 +03:00
|
|
|
die("ASSERT: Bootstrap::Step::Shell(): cmdArgs is not an array ref\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
my %runShellCommandArgs = (command => $cmd,
|
|
|
|
args => $cmdArgs,
|
|
|
|
timeout => $timeout,
|
|
|
|
logfile => $logFile);
|
|
|
|
|
|
|
|
if ($config->Exists(var => 'dumpLogs')) {
|
|
|
|
if ($config->Get(var => 'dumpLogs')) {
|
|
|
|
$runShellCommandArgs{'output'} = 1;
|
|
|
|
}
|
2007-02-01 06:50:47 +03:00
|
|
|
}
|
|
|
|
|
2006-10-07 05:29:11 +04:00
|
|
|
if ($dir) {
|
2007-03-10 00:20:05 +03:00
|
|
|
$runShellCommandArgs{'dir'} = $dir;
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2007-03-10 00:20:05 +03:00
|
|
|
$this->Log(msg => 'Running shell command' .
|
|
|
|
(defined($dir) ? " in $dir" : '') . ':');
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => ' arg0: ' . $cmd);
|
2007-02-01 06:50:47 +03:00
|
|
|
my $argNum = 1;
|
|
|
|
foreach my $arg (@{$cmdArgs}) {
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => ' arg' . $argNum . ': ' . $arg);
|
2007-02-01 06:50:47 +03:00
|
|
|
$argNum += 1;
|
|
|
|
}
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => 'Starting time is ' . $this->CurrentTime());
|
|
|
|
$this->Log(msg => 'Logging output to ' . $logFile);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => 'Timeout: ' . $timeout);
|
2006-11-10 02:28:20 +03:00
|
|
|
|
2007-03-10 00:20:05 +03:00
|
|
|
$rv = RunShellCommand(%runShellCommandArgs);
|
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'};
|
2007-03-10 00:20:05 +03:00
|
|
|
my $signalNum = $rv->{'signalNum'};
|
2006-10-07 05:29:11 +04:00
|
|
|
my $dumpedCore = $rv->{'dumpedCore'};
|
|
|
|
if ($timedOut) {
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => "output: $rv->{'output'}") if $rv->{'output'};
|
2007-03-10 00:20:05 +03:00
|
|
|
die('FAIL shell call timed out after ' . $timeout . ' seconds');
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
2007-03-10 00:20:05 +03:00
|
|
|
if ($signalNum) {
|
|
|
|
$this->Log(msg => 'WARNING shell recieved signal ' . $signalNum);
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
if ($dumpedCore) {
|
2007-02-08 01:14:24 +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");
|
|
|
|
}
|
2007-02-22 11:21:39 +03:00
|
|
|
if ($ignoreExitValue) {
|
|
|
|
$this->Log(msg => "Exit value $rv->{'output'}, but ignoring as told");
|
2007-03-10 00:20:05 +03:00
|
|
|
} elsif ($exitValue) {
|
2006-11-10 02:28:20 +03:00
|
|
|
if ($exitValue != 0) {
|
2007-02-08 01:14:24 +03:00
|
|
|
$this->Log(msg => "output: $rv->{'output'}") if $rv->{'output'};
|
2006-11-10 02:28:20 +03:00
|
|
|
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)) {
|
2007-02-08 01:14:24 +03: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
|
2007-02-08 01:14:24 +03:00
|
|
|
$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)) {
|
2007-02-08 01:14:24 +03:00
|
|
|
die("No log file specified");
|
2006-11-10 02:28:20 +03:00
|
|
|
}
|
|
|
|
|
2007-02-08 01:14:24 +03:00
|
|
|
open (FILE, "< $log") or die("Cannot open file $log: $!");
|
2006-10-07 05:29:11 +04:00
|
|
|
my @contents = <FILE>;
|
2007-02-08 01:14:24 +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) {
|
2007-02-08 01:14:24 +03:00
|
|
|
die("Errors in log ($log): \n\n @errors");
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($checkFor) {
|
|
|
|
if (not grep(/$checkFor/i, @contents)) {
|
2007-02-08 01:14:24 +03:00
|
|
|
die("$checkFor is not present in file $log");
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($checkForOnly) {
|
|
|
|
if (not grep(/$checkForOnly/i, @contents)) {
|
2007-02-08 01:14:24 +03:00
|
|
|
die("$checkForOnly is not present in file $log");
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
my @errors = grep(!/$checkForOnly/i, @contents);
|
|
|
|
if (@errors) {
|
2007-02-08 01:14:24 +03:00
|
|
|
die("Errors in log ($log): \n\n @errors");
|
2006-10-07 05:29:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-09 04:23:34 +03:00
|
|
|
sub CurrentTime {
|
2006-11-10 02:28:20 +03:00
|
|
|
my $this = shift;
|
|
|
|
|
|
|
|
return strftime("%T %D", localtime());
|
|
|
|
}
|
|
|
|
|
2007-02-01 06:50:47 +03:00
|
|
|
# Overridden by child if needed
|
2007-02-09 04:23:34 +03:00
|
|
|
sub Push {
|
2007-02-01 06:50:47 +03:00
|
|
|
my $this = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Overridden by child if needed
|
2007-02-09 04:23:34 +03:00
|
|
|
sub Announce {
|
2007-02-01 06:50:47 +03:00
|
|
|
my $this = shift;
|
|
|
|
}
|
|
|
|
|
2007-02-09 04:23:34 +03:00
|
|
|
sub SendAnnouncement {
|
2007-02-01 06:50:47 +03:00
|
|
|
my $this = shift;
|
|
|
|
my %args = @_;
|
|
|
|
|
|
|
|
my $config = new Bootstrap::Config();
|
|
|
|
|
2007-02-08 01:14:24 +03:00
|
|
|
my $blat = $config->Get(var => 'blat');
|
|
|
|
my $sendmail = $config->Get(var => 'sendmail');
|
2007-02-09 04:23:34 +03:00
|
|
|
my $from = $config->Get(var => 'from');
|
|
|
|
my $to = $config->Get(var => 'to');
|
|
|
|
my @ccList = $config->Exists(var => 'cc') ? split(/[,\s]+/,
|
|
|
|
$config->Get(var => 'cc')) : ();
|
2007-03-26 03:58:02 +04:00
|
|
|
my $hostname = $config->SystemInfo(var => 'hostname');
|
2007-02-08 01:14:24 +03:00
|
|
|
|
2007-03-26 03:58:02 +04:00
|
|
|
my $subject = $hostname . ' - ' . $args{'subject'};
|
|
|
|
my $message = $hostname . ' - ' . $args{'message'};
|
2007-02-01 06:50:47 +03:00
|
|
|
|
|
|
|
eval {
|
|
|
|
Email(
|
2007-02-08 01:14:24 +03:00
|
|
|
blat => $blat,
|
|
|
|
sendmail => $sendmail,
|
2007-02-01 06:50:47 +03:00
|
|
|
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;
|