source: LMDZ6/branches/Amaury_dev/tools/fcm/sbin/fcm-daily-update @ 5095

Last change on this file since 5095 was 5095, checked in by abarral, 5 months ago

Revert cosp*/ from the trunk, as it's external code
Add missing bits from FCM2 source

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1#!/usr/bin/perl
2#-------------------------------------------------------------------------------
3# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
4#
5# This file is part of FCM, tools for managing and building source code.
6#
7# FCM is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# FCM is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with FCM. If not, see <http://www.gnu.org/licenses/>.
19#-------------------------------------------------------------------------------
20
21use strict;
22use warnings;
23
24use FindBin;
25use lib "$FindBin::Bin/../lib";
26use File::Basename qw{basename};
27use File::Spec::Functions qw{catfile};
28use IO::File;
29use Mail::Mailer;
30use Time::Piece qw{gmtime};
31
32use FCM::Admin::Config;
33use FCM::Admin::Runner;
34use FCM::Admin::System qw{
35    backup_svn_repository
36    backup_trac_environment
37    backup_trac_files
38    get_projects_from_svn_live
39    get_projects_from_trac_live
40    get_users
41    housekeep_svn_hook_logs
42    manage_users_in_svn_passwd
43    manage_users_in_trac_passwd
44    manage_users_in_trac_db_of
45};
46
47my $THIS = basename($0);
48my $CONFIG = FCM::Admin::Config->instance();
49my $UTIL = $FCM::Admin::Config::UTIL;
50
51if (!caller()) {
52    main(@ARGV);
53}
54
55sub main {
56    local(@ARGV) = @_;
57
58    # Redirects STDOUT and STDERR to the $out_file
59    open(my $old_out, q{>&}, \*STDOUT)
60        || die("$THIS: cannot duplicate STDOUT ($!)\n");
61    open(my $old_err, q{>&}, \*STDERR)
62        || die("$THIS: cannot duplicate STDERR ($!)\n");
63    my $log_dir = $UTIL->file_tilde_expand($CONFIG->get_log_dir());
64    my $now = gmtime();
65    my $day_of_week = lc($now->day_of_week()); # 0=sun, 1=mon, etc
66    my $day = lc($now->day()); # lower case day of week, e.g. sun, mon
67    my $out_file = catfile($log_dir, "$THIS-$day_of_week$day.log");
68    open(STDOUT, q{>}, $out_file)
69        || die("$THIS: cannot redirect STDOUT ($!)\n");
70    open(STDERR, q{>&}, \*STDOUT)
71        || die("$THIS: cannot redirect STDERR ($!)\n");
72
73    do_tasks();
74
75    # Restores STDOUT and STDERR
76    open(STDERR, q{>&}, $old_err)
77        || die("$THIS: cannot reinstate STDERR ($!)\n");
78    open(STDOUT, q{>&}, $old_out)
79        || die("$THIS: cannot reinstate STDOUT ($!)\n");
80
81    notify($out_file);
82}
83
84# ------------------------------------------------------------------------------
85# Performs the daily update tasks.
86sub do_tasks {
87    # (no argument)
88    my $RUNNER = FCM::Admin::Runner->instance();
89    my @svn_projects = get_projects_from_svn_live();
90    my @trac_projects = get_projects_from_trac_live();
91    my $user_ref = undef;
92    $RUNNER->run_continue(
93        "retrieving user accounts",
94        sub {$user_ref = get_users(); 1;},
95    );
96    if (defined($user_ref)) {
97        if ($CONFIG->get_svn_passwd_file()) {
98            $RUNNER->run_continue(
99                "updating SVN user accounts",
100                sub {manage_users_in_svn_passwd($user_ref)},
101            );
102        }
103        if ($CONFIG->get_trac_passwd_file()) {
104            $RUNNER->run_continue(
105                "updating Trac user accounts",
106                sub {manage_users_in_trac_passwd($user_ref)},
107            );
108        }
109        for my $project (@trac_projects) {
110            $RUNNER->run_continue(
111                "updating Trac accounts in $project",
112                sub {manage_users_in_trac_db_of($project, $user_ref)},
113            );
114        }
115    }
116    for my $project (@svn_projects) {
117        $RUNNER->run_continue(
118            "housekeep SVN repository logs for $project",
119            sub {housekeep_svn_hook_logs($project)},
120        );
121        $RUNNER->run_continue(
122            "backing up SVN repository for $project",
123            sub {backup_svn_repository({}, $project)},
124        );
125    }
126    for my $project (@trac_projects) {
127        $RUNNER->run_continue(
128            "backing up Trac environment for $project",
129            sub {backup_trac_environment({}, $project)},
130        );
131    }
132    $RUNNER->run_continue("backing up Trac files", \&backup_trac_files);
133}
134
135# ------------------------------------------------------------------------------
136# Notifies on completion.
137sub notify {
138    my ($out_file) = @_;
139    # Reports number of arguments in subject.
140    my @exceptions = FCM::Admin::Runner->instance()->get_exceptions();
141    my $subject
142        = sprintf(qq{$THIS finished with %d error(s)}, scalar(@exceptions));
143
144    my $mailer = Mail::Mailer->new();
145    $mailer->open({
146        From    => $CONFIG->get_notification_from(),
147        To      => $CONFIG->get_admin_email(),
148        Subject => $subject,
149    });
150
151    # Summarises the exceptions at the beginning of the message
152    $mailer->print(qq{$subject:\n});
153    for my $exception (@exceptions) {
154        $mailer->print(qq{    $exception});
155    }
156    $mailer->print(qq{\n});
157
158    # Prints content of the output
159    $mailer->print(q{#} x 72 . qq{\n});
160    $mailer->print(qq{# Output and error output of $THIS:\n});
161    $mailer->print(q{#} x 72 . qq{\n});
162    my $out_file_handle = IO::File->new($out_file);
163    if (defined($out_file_handle)) {
164        while (my $line = $out_file_handle->getline()) {
165            $mailer->print($line);
166        }
167        $out_file_handle->close();
168    }
169    $mailer->close();
170}
171
172__END__
173
174=head1 NAME
175
176fcm-daily-update
177
178=head1 SYNOPSIS
179
180    fcm-daily-update
181
182=head1 DESCRIPTION
183
184This program performs the daily update for the FCM system.
185
186=head1 COPYRIGHT
187
188Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
189
190=cut
Note: See TracBrowser for help on using the repository browser.