1 | #!/usr/bin/perl |
---|
2 | # ------------------------------------------------------------------------------ |
---|
3 | # (C) Crown copyright Met Office. All rights reserved. |
---|
4 | # For further details please refer to the file COPYRIGHT.txt |
---|
5 | # which you should have received as part of this distribution. |
---|
6 | # ------------------------------------------------------------------------------ |
---|
7 | |
---|
8 | use strict; |
---|
9 | use warnings; |
---|
10 | |
---|
11 | use FindBin; |
---|
12 | use lib "$FindBin::Bin/../lib"; |
---|
13 | use File::Basename qw{basename}; |
---|
14 | use File::Spec; |
---|
15 | use IO::File; |
---|
16 | use Mail::Mailer; |
---|
17 | |
---|
18 | use FCM::Admin::Config; |
---|
19 | use FCM::Admin::Runner; |
---|
20 | use FCM::Admin::System qw{ |
---|
21 | backup_svn_repository |
---|
22 | backup_trac_environment |
---|
23 | backup_trac_ini_file |
---|
24 | backup_trac_passwd_file |
---|
25 | distribute_wc |
---|
26 | get_projects_from_svn_live |
---|
27 | get_projects_from_trac_live |
---|
28 | get_users |
---|
29 | manage_users_in_svn_passwd |
---|
30 | manage_users_in_trac_passwd |
---|
31 | manage_users_in_trac_db_of |
---|
32 | }; |
---|
33 | |
---|
34 | my $THIS = basename($0); |
---|
35 | my $CONFIG = FCM::Admin::Config->instance(); |
---|
36 | |
---|
37 | my $OUT_FILE |
---|
38 | = File::Spec->catfile($CONFIG->get_fcm_home(), q{FCM}, qq{$THIS.out}); |
---|
39 | |
---|
40 | main(); |
---|
41 | |
---|
42 | sub main { |
---|
43 | # Redirects STDOUT and STDERR to the $OUT_FILE |
---|
44 | open(my $old_out, q{>&}, \*STDOUT) |
---|
45 | or die("$THIS: cannot duplicate STDOUT ($!)\n"); |
---|
46 | open(my $old_err, q{>&}, \*STDERR) |
---|
47 | or die("$THIS: cannot duplicate STDERR ($!)\n"); |
---|
48 | open(STDOUT, q{>}, $OUT_FILE) |
---|
49 | or die("$THIS: cannot redirect STDOUT ($!)\n"); |
---|
50 | open(STDERR, q{>&}, \*STDOUT) |
---|
51 | or die("$THIS: cannot redirect STDERR ($!)\n"); |
---|
52 | |
---|
53 | do_tasks(); |
---|
54 | |
---|
55 | # Restores STDOUT and STDERR |
---|
56 | open(STDERR, q{>&}, $old_err) |
---|
57 | or die("$THIS: cannot reinstate STDERR ($!)\n"); |
---|
58 | open(STDOUT, q{>&}, $old_out) |
---|
59 | or die("$THIS: cannot reinstate STDOUT ($!)\n"); |
---|
60 | |
---|
61 | notify(); |
---|
62 | } |
---|
63 | |
---|
64 | # ------------------------------------------------------------------------------ |
---|
65 | # Performs the daily update tasks. |
---|
66 | sub do_tasks { |
---|
67 | # (no argument) |
---|
68 | my $RUNNER = FCM::Admin::Runner->instance(); |
---|
69 | my @svn_projects = get_projects_from_svn_live(); |
---|
70 | my @trac_projects = get_projects_from_trac_live(); |
---|
71 | my $user_ref = undef; |
---|
72 | $RUNNER->run_continue( |
---|
73 | "retrieving user accounts", |
---|
74 | sub {$user_ref = get_users(); return 1;}, |
---|
75 | ); |
---|
76 | if (defined($user_ref)) { |
---|
77 | $RUNNER->run_continue( |
---|
78 | "updating SVN user accounts", |
---|
79 | sub {return manage_users_in_svn_passwd($user_ref)}, |
---|
80 | ); |
---|
81 | $RUNNER->run_continue( |
---|
82 | "updating Trac user accounts", |
---|
83 | sub {return manage_users_in_trac_passwd($user_ref)}, |
---|
84 | ); |
---|
85 | for my $project (@trac_projects) { |
---|
86 | $RUNNER->run_continue( |
---|
87 | "updating Trac accounts in $project", |
---|
88 | sub { |
---|
89 | return manage_users_in_trac_db_of($project, $user_ref); |
---|
90 | }, |
---|
91 | ); |
---|
92 | } |
---|
93 | } |
---|
94 | for my $project (@svn_projects) { |
---|
95 | $RUNNER->run_continue( |
---|
96 | "backing up SVN repository for $project", |
---|
97 | sub {return backup_svn_repository($project, 1, 1)}, |
---|
98 | ); |
---|
99 | } |
---|
100 | for my $project (@trac_projects) { |
---|
101 | $RUNNER->run_continue( |
---|
102 | "backing up Trac environment for $project", |
---|
103 | sub {return backup_trac_environment($project, 1)}, |
---|
104 | ); |
---|
105 | } |
---|
106 | $RUNNER->run_continue( |
---|
107 | "backing up Trac (central) INI file", |
---|
108 | sub {return backup_trac_ini_file()}, |
---|
109 | ); |
---|
110 | $RUNNER->run_continue( |
---|
111 | "backing up Trac password file", |
---|
112 | sub {return backup_trac_passwd_file()}, |
---|
113 | ); |
---|
114 | $RUNNER->run_continue( |
---|
115 | "distributing FCM to standard locations", |
---|
116 | sub {return distribute_wc()}, |
---|
117 | ); |
---|
118 | } |
---|
119 | |
---|
120 | # ------------------------------------------------------------------------------ |
---|
121 | # Notifies on completion. |
---|
122 | sub notify { |
---|
123 | # (no argument) |
---|
124 | # Reports number of arguments in subject. |
---|
125 | my @exceptions = FCM::Admin::Runner->instance()->get_exceptions(); |
---|
126 | my $subject |
---|
127 | = sprintf(qq{$THIS finished with %d error(s)}, scalar(@exceptions)); |
---|
128 | |
---|
129 | my $mailer = Mail::Mailer->new(); |
---|
130 | $mailer->open({ |
---|
131 | From => $CONFIG->get_admin_email(), |
---|
132 | To => $CONFIG->get_admin_email(), |
---|
133 | Subject => $subject, |
---|
134 | }); |
---|
135 | |
---|
136 | # Summarises the exceptions at the beginning of the message |
---|
137 | $mailer->print(qq{$subject:\n}); |
---|
138 | for my $exception (@exceptions) { |
---|
139 | $mailer->print(qq{ $exception}); |
---|
140 | } |
---|
141 | $mailer->print(qq{\n}); |
---|
142 | |
---|
143 | # Prints content of the output |
---|
144 | $mailer->print(q{#} x 72 . qq{\n}); |
---|
145 | $mailer->print(qq{# Output and error output of $THIS:\n}); |
---|
146 | $mailer->print(q{#} x 72 . qq{\n}); |
---|
147 | my $out_file_handle = IO::File->new($OUT_FILE); |
---|
148 | if (defined($out_file_handle)) { |
---|
149 | while (my $line = $out_file_handle->getline()) { |
---|
150 | $mailer->print($line); |
---|
151 | } |
---|
152 | $out_file_handle->close(); |
---|
153 | } |
---|
154 | $mailer->close(); |
---|
155 | } |
---|
156 | |
---|
157 | __END__ |
---|
158 | |
---|
159 | =head1 NAME |
---|
160 | |
---|
161 | fcm-daily-update |
---|
162 | |
---|
163 | =head1 SYNOPSIS |
---|
164 | |
---|
165 | fcm-daily-update |
---|
166 | |
---|
167 | =head1 DESCRIPTION |
---|
168 | |
---|
169 | This program performs the daily update for the FCM system. |
---|
170 | |
---|
171 | =head1 COPYRIGHT |
---|
172 | |
---|
173 | E<169> Crown copyright Met Office. All rights reserved. |
---|
174 | |
---|
175 | =cut |
---|