[1578] | 1 | #!/usr/bin/perl |
---|
| 2 | # ------------------------------------------------------------------------------ |
---|
| 3 | # NAME |
---|
| 4 | # background_updates.pl |
---|
| 5 | # |
---|
| 6 | # SYNOPSIS |
---|
| 7 | # background_updates.pl <repos> <logdir> |
---|
| 8 | # |
---|
| 9 | # DESCRIPTION |
---|
| 10 | # Performs any background tasks required after each commit to the FCM |
---|
| 11 | # respository. A lock file is used to prevent mulitple instances of this |
---|
| 12 | # program from running. |
---|
| 13 | # |
---|
| 14 | # COPYRIGHT |
---|
| 15 | # (C) Crown copyright Met Office. All rights reserved. |
---|
| 16 | # For further details please refer to the file COPYRIGHT.txt |
---|
| 17 | # which you should have received as part of this distribution. |
---|
| 18 | # ------------------------------------------------------------------------------ |
---|
| 19 | |
---|
| 20 | # Standard pragmas |
---|
| 21 | use strict; |
---|
| 22 | use warnings; |
---|
| 23 | |
---|
| 24 | # Standard modules |
---|
| 25 | use File::Basename; |
---|
| 26 | use File::Spec::Functions; |
---|
| 27 | use File::Path; |
---|
| 28 | |
---|
| 29 | # ------------------------------------------------------------------------------ |
---|
| 30 | |
---|
| 31 | # Usage |
---|
| 32 | my $this = basename $0; |
---|
| 33 | |
---|
| 34 | # ------------------------------------------------------------------------------ |
---|
| 35 | |
---|
| 36 | # Arguments |
---|
| 37 | my ($repos, $logdir) = @ARGV; |
---|
| 38 | die $this, ': incorrect usage' unless $repos and $logdir; |
---|
| 39 | |
---|
| 40 | # ------------------------------------------------------------------------------ |
---|
| 41 | |
---|
| 42 | # Lock file |
---|
| 43 | my $lockfile = catfile ($logdir, $repos . '.lock'); |
---|
| 44 | my $locked = undef; |
---|
| 45 | |
---|
| 46 | # Do nothing if lock file exists |
---|
| 47 | if (-e $lockfile) { |
---|
| 48 | print "$this: Found lock file ($lockfile). Exiting.\n"; |
---|
| 49 | exit; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | # Create lock file |
---|
| 53 | open FILE, '>', $lockfile |
---|
| 54 | or die $this, ': unable to create lock file ', $lockfile; |
---|
| 55 | close FILE; |
---|
| 56 | $locked = 1; |
---|
| 57 | |
---|
| 58 | # ------------------------------------------------------------------------------ |
---|
| 59 | |
---|
| 60 | my $wc_main = '/home/h03/fcm/FCM/work/FCM'; # Location of main working copy |
---|
| 61 | my $wc_admin = '/home/h03/fcm/FCM/work/Admin'; # Location of admin working copy |
---|
| 62 | |
---|
| 63 | my $install_hook = '/home/h03/fcm/FCM/work/Admin/src/utils/install_hook.pl'; |
---|
| 64 | my $hooksrc_top = '/home/h03/fcm/FCM/work/Admin/src/hook'; |
---|
| 65 | my $hookdest_top = '/data/local/fcm/svn/live'; |
---|
| 66 | |
---|
| 67 | my $fcm_html2pdf = '/home/h03/fcm/FCM/work/Admin/src/utils/fcm_html2pdf'; |
---|
| 68 | |
---|
| 69 | my @html2pdf = ( |
---|
| 70 | { |
---|
| 71 | PATTERN => 'doc/user_guide', |
---|
| 72 | INPUT => '/home/h03/fcm/FCM/work/FCM/doc/user_guide/index.html', |
---|
| 73 | OUTPUT => 'fcm-user-guide', |
---|
| 74 | }, |
---|
| 75 | { |
---|
| 76 | PATTERN => 'doc/collaboration', |
---|
| 77 | INPUT => '/home/h03/fcm/FCM/work/FCM/doc/collaboration/index.html', |
---|
| 78 | OUTPUT => 'fcm-collaboration', |
---|
| 79 | }, |
---|
| 80 | { |
---|
| 81 | PATTERN => 'doc/standards/fortran_standard\.html', |
---|
| 82 | INPUT => '/home/h03/fcm/FCM/work/FCM/doc/standards/fortran_standard.html', |
---|
| 83 | OUTPUT => 'fcm-fortran-standard', |
---|
| 84 | }, |
---|
| 85 | { |
---|
| 86 | PATTERN => 'doc/standards/perl_standard\.html', |
---|
| 87 | INPUT => '/home/h03/fcm/FCM/work/FCM/doc/standards/perl_standard.html', |
---|
| 88 | OUTPUT => 'fcm-perl-standard', |
---|
| 89 | }, |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | my @remotes = ( # list of remote machines |
---|
| 93 | # 1st remote machine |
---|
| 94 | { |
---|
| 95 | MACHINE => 'tx01', # machine name |
---|
| 96 | LOGNAME => 'fcm', # logname |
---|
| 97 | WC => '~/FCM/work', # working copy location |
---|
| 98 | OPTIONS => [qw#-a -v |
---|
| 99 | --timeout=1800 |
---|
| 100 | --exclude='.*' |
---|
| 101 | --exclude='FCM/doc' |
---|
| 102 | --delete-excluded#], # options to "rsync" |
---|
| 103 | }, |
---|
| 104 | |
---|
| 105 | # 2nd remote machine, etc |
---|
| 106 | #{ |
---|
| 107 | # MACHINE => '', # machine name |
---|
| 108 | # LOGNAME => '', # logname |
---|
| 109 | # WC => '', # working copy location |
---|
| 110 | # OPTIONS => [], # options to "rsync" |
---|
| 111 | #}, |
---|
| 112 | ); |
---|
| 113 | |
---|
| 114 | $ENV{RSYNC_RSH} = 'rsh'; # Remote shell for "rsync" |
---|
| 115 | |
---|
| 116 | while (1) { |
---|
| 117 | # Perform "svn update" on working copy of the "trunk" of the main project |
---|
| 118 | print "$this: Updating main working copy ...\n"; |
---|
| 119 | my @update_main = qx(svn update $wc_main); |
---|
| 120 | die $this, ': unable to update working copy of FCM/trunk' if $?; |
---|
| 121 | |
---|
| 122 | # Perform "svn update" on working copy of the "trunk" of the Admin project |
---|
| 123 | print "$this: Updating admin working copy ...\n"; |
---|
| 124 | my @update_admin = qx(svn update $wc_admin); |
---|
| 125 | die $this, ': unable to update working copy of Admin/trunk' if $?; |
---|
| 126 | |
---|
| 127 | # Remove last line from each output, which should be info of the revision |
---|
| 128 | pop @update_main; |
---|
| 129 | pop @update_admin; |
---|
| 130 | |
---|
| 131 | # No updates, exit loop |
---|
| 132 | if (not @update_main and not @update_admin) { |
---|
| 133 | print "$this: No updates detected. Exiting.\n"; |
---|
| 134 | last; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | # Set FCM release number, if necessary |
---|
| 138 | if (@update_main) { |
---|
| 139 | # Get last changed revision |
---|
| 140 | my @info = qx(svn info $wc_main); |
---|
| 141 | my $rev; |
---|
| 142 | |
---|
| 143 | for (@info) { |
---|
| 144 | next unless /^Last Changed Rev: (\d+)$/; |
---|
| 145 | |
---|
| 146 | $rev = $1; |
---|
| 147 | last; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | # Update src/etc/fcm_rev |
---|
| 151 | print "$this: updating the FCM release number file ...\n"; |
---|
| 152 | my $fcm_rev = catfile ($wc_main, 'src/etc/fcm_rev'); |
---|
| 153 | open FILE, '>', $fcm_rev or die $fcm_rev, ': cannot open'; |
---|
| 154 | print FILE $rev, "\n"; |
---|
| 155 | close FILE or die $fcm_rev, ': cannot close'; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | # Re-create PDF files if necessary |
---|
| 159 | for (@html2pdf) { |
---|
| 160 | my $pattern = $_->{PATTERN}; |
---|
| 161 | next unless grep m/$pattern/, @update_main; |
---|
| 162 | |
---|
| 163 | print $this, ': Re-creating PDF for ', $_->{OUTPUT}, '...', "\n"; |
---|
| 164 | my @command = ($fcm_html2pdf, $_->{INPUT}, $_->{OUTPUT}); |
---|
| 165 | system @command; |
---|
| 166 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | # Re-install hook scripts if necessary |
---|
| 170 | if (grep m#src/hook#, @update_admin) { |
---|
| 171 | print "$this: Re-install hook scripts ...\n"; |
---|
| 172 | my @command = ($install_hook, $hooksrc_top, $hookdest_top); |
---|
| 173 | system @command; |
---|
| 174 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if (@update_main) { |
---|
| 178 | # Update remote platforms, if necessary |
---|
| 179 | my $rsh = 'rsh'; |
---|
| 180 | for my $remote (@remotes) { |
---|
| 181 | next unless $remote->{MACHINE} and $remote->{WC}; |
---|
| 182 | print "$this: Updating working copy on $remote->{MACHINE} ...\n"; |
---|
| 183 | |
---|
| 184 | # Create the top level directory for the remote working copy (if necessary) |
---|
| 185 | { |
---|
| 186 | my @command = ($rsh, $remote->{MACHINE}); |
---|
| 187 | push @command, ('-l', $remote->{LOGNAME}) if $remote->{LOGNAME}; |
---|
| 188 | push @command, (qw/mkdir -p/, $remote->{WC}); |
---|
| 189 | system @command; |
---|
| 190 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | # Sync the working copy to the remote platform |
---|
| 194 | { |
---|
| 195 | my @command = ('rsync'); |
---|
| 196 | push @command, @{ $remote->{OPTIONS} } if @{ $remote->{OPTIONS} }; |
---|
| 197 | my $rwcpath = $remote->{LOGNAME}; |
---|
| 198 | $rwcpath .= '@' . $remote->{MACHINE} . ':' . $remote->{WC}; |
---|
| 199 | push @command, ($wc_main, $rwcpath); |
---|
| 200 | system join (' ', @command); |
---|
| 201 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | # ------------------------------------------------------------------------------ |
---|
| 208 | |
---|
| 209 | END { |
---|
| 210 | # Remove lock file on exit |
---|
| 211 | unlink $lockfile if $locked; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | # ------------------------------------------------------------------------------ |
---|