Changeset 1762 for trunk/gsdl/bin/script


Ignore:
Timestamp:
2000-12-07T18:18:21+13:00 (24 years ago)
Author:
sjboddie
Message:

Added support for the new LogEvents, EmailEvents, EmailUserEvents and
MailServer configuration options (though they haven't actually been
implemented yet).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/bin/script/build

    r1753 r1762  
    44# windows (build.bat is in bin\windows)
    55
     6# Note that this script has grown over time and now has many options for
     7# use when called from within the collector. If it appears to
     8# over-complicate things a little, that's why.
     9
    610package build;
    711
     12use lib qq($ENV{'GSDLHOME'}/perllib/cpan);
    813use FileHandle;
    914use File::Copy;
     15use Mail::Sendmail;
     16
    1017
    1118BEGIN {
     
    8592if (-e &util::filename_cat ($cdir, $collection, ".kill")) {
    8693    &util::rm (&util::filename_cat ($cdir, $collection, ".kill"));
     94}
     95
     96# get maintainer email address from main.cfg
     97my $maintainer = "NULL";
     98my $main_cfg = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "main.cfg");
     99my $cfgdata = &cfgread::read_cfg_file ($main_cfg, "maintainer");
     100if (defined $cfgdata->{'maintainer'} && $cfgdata->{'maintainer'} =~ /\w/) {
     101    $maintainer = $cfgdata->{'maintainer'};
     102}
     103# if maintainer is "NULL" email_events should be disabled
     104if ($maintainer =~ /^NULL$/i) {
     105    $email_events = "";
    87106}
    88107
     
    120139    print STDERR "                           archives.org\n";
    121140    print STDERR "   -out                    Filename or handle to print output status to.\n";
    122     print STDERR "                           The default is STDERR\n\n";
     141    print STDERR "                           The default is STDERR\n";
     142    print STDERR "   -log_events             Log important events (collection built successfully etc.)\n";
     143    print STDERR "                           to event_log_file\n";
     144    print STDERR "   -event_log_file file    File to append important events to (defaults to\n";
     145    print STDERR "                           " . &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt") . "\n";
     146    print STDERR "   -email_events addr      Comma separated list of email addresses to mail details\n";
     147    print STDERR "                           of important collection building events\n";
     148    print STDERR "   -mail_server server     The outgoing (SMTP) mail server to be used by email_events.\n";
     149    print STDERR "                           email_events will be disabled if mail_server isn't set\n";
     150    print STDERR "   -event_header file      File containing a header to go on any event messages. If not\n";
     151    print STDERR "                           specified build will create a generic header\n\n";
    123152}
    124153
     
    250279    } else {
    251280        # no import or archives
    252         print $out "build: ERROR: The $collection collection has no import or archives data.\n";
     281        my $msg = "build: ERROR: The $collection collection has no import or archives data.\n";
     282        print $out $msg;
     283        &log_event ($msg);
    253284        &final_out (1) if $use_out;
    254285        die "\n";
     
    265296        my $olddir = &util::filename_cat ($collectdir, $collection);
    266297        if (-d $newdir) {
    267         print $out "build: Could not install collection as $newdir\n";
    268         print $out "       already exists. Collection will remain at\n";
    269         print $out "       $olddir\n";
     298        my $msg = "build: Could not install collection as $newdir\n" .
     299            "       already exists. Collection will remain at\n$olddir\n";
     300
     301        print $out $msg;
     302        &log_event ($msg);
    270303        &final_out (4) if $use_out;
    271304        die "\n";
    272305        }
    273306        if (!&File::Copy::move ($olddir, $newdir)) {
    274         print $out "build: Failed to install collection to $newdir\n";
    275         print $out "       Collection will remain at $olddir\n";
     307        my $msg = "build: Failed to install collection to $newdir\n" .
     308            "       Collection will remain at $olddir\n";
     309        print $out $msg;
     310        &log_event ($msg);
    276311        &final_out (5) if $use_out;
    277312        die "\n";
     
    280315    }
    281316
     317    &log_event ("The $collection collection was built successfully\n");
    282318    &final_out (0) if $use_out;
    283319}
     
    303339    }
    304340    } else {
     341    my $msg = "build: ERROR: import.pl failed\n";
     342    print $out "\n$msg";
     343    &log_event ($msg);
    305344    &final_out (2) if $use_out;
    306     print $out "\nimport.pl failed\n";
    307345    die "\n";
    308346    }
     
    329367    }
    330368    } else {
     369    my $msg = "build: ERROR: buildcol.pl failed\n";
     370    print $out "\n$msg";
     371    &log_event ($msg);
    331372    &final_out (3) if $use_out;
    332     print $out "\nbuildcol.pl failed\n";
    333373    die "\n";
    334374    }
     
    387427    }
    388428}
     429
     430sub log_event {
     431    my ($msg) = @_;
     432
     433    return unless ($log_events || $email_events);
     434
     435    # get the event header
     436    my $eheader = "";
     437    if ($event_header ne "" && open (HEADER, $event_header)) {
     438    undef $/;
     439    $eheader = HEADER;
     440    $/ = "\n";
     441    close HEADER;
     442    } else {
     443    $eheader = "[Build Event]\n";
     444    $eheader .= "Date: " . scalar localtime() . "\n";
     445    $eheader .= "Collection: $collection\n";
     446    }
     447   
     448    if ($log_events) {
     449    my $fail = 0;
     450    # append the event to the event log file
     451    if ($event_log_file eq "" || !open (LOG, ">>$event_log_file")) {
     452        # log file defaults to $GSDLHOME/etc/events.txt
     453        $event_log_file = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "events.txt");
     454        if (!open (LOG, ">>$event_log_file")) {
     455        print $out "build: ERROR: Couldn't open event log file $event_log_file\n";
     456        $fail = 1;
     457        }
     458    }
     459    if (!$fail) {
     460        print LOG $eheader;
     461        print LOG $msg;
     462        close LOG;
     463    }
     464    }
     465   
     466    if ($email_events) {
     467    # if mail_server isn't set email_events does nothing
     468    if ($mail_server eq "") {
     469        print $out "build: WARNING: mail_server was not set - email_events option was ignored\n";
     470        return;
     471    }
     472   
     473    my %mail = ('SMTP' => $mail_server,
     474            'To' => $email_events,
     475            'From' => $maintainer,
     476            'Subject' => 'Greenstone Build Event'
     477            );
     478    $mail{'Message'} = $eheader . $msg;
     479   
     480    if (!sendmail %mail) {
     481        print $out "build: ERROR sending mail to $email_events\n";
     482        print $out "'$Mail::Sendmail::error'\n";
     483    }
     484    }
     485}
     486
    389487
    390488sub parse_args {
     
    402500             'dontinstall', \$dontinstall,
    403501             'save_archives', \$save_archives,
    404              'out/.*/STDERR', \$out)) {
     502             'out/.*/STDERR', \$out,
     503             'log_events', \$log_events,
     504             'event_log_file/.*/', \$event_log_file,
     505             'email_events/.*/', \$email_events,
     506             'mail_server/.*/', \$mail_server,
     507             'event_header/.*/', \$event_header)) {
    405508   
    406509    &print_usage();
Note: See TracChangeset for help on using the changeset viewer.