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 $ug_to_pdf = '/home/h03/fcm/FCM/work/Admin/src/utils/user_guide_to_pdf'; |
---|
68 | my $ug_index = '/home/h03/fcm/FCM/work/FCM/doc/user_guide/index.html'; |
---|
69 | |
---|
70 | my @remotes = ( # list of remote machines |
---|
71 | # 1st remote machine |
---|
72 | { |
---|
73 | MACHINE => 'tx01', # machine name |
---|
74 | LOGNAME => 'fcm', # logname |
---|
75 | WC => '~/FCM/work', # working copy location |
---|
76 | OPTIONS => [qw#-a -v |
---|
77 | --exclude='.*' |
---|
78 | --exclude='FCM/doc' |
---|
79 | --delete-excluded#], # options to "rsync" |
---|
80 | }, |
---|
81 | |
---|
82 | # 2nd remote machine, etc |
---|
83 | #{ |
---|
84 | # MACHINE => '', # machine name |
---|
85 | # LOGNAME => '', # logname |
---|
86 | # WC => '', # working copy location |
---|
87 | # OPTIONS => [], # options to "rsync" |
---|
88 | #}, |
---|
89 | ); |
---|
90 | |
---|
91 | $ENV{RSYNC_RSH} = 'rsh'; # Remote shell for "rsync" |
---|
92 | |
---|
93 | while (1) { |
---|
94 | # Perform "svn update" on working copy of the "trunk" of the main project |
---|
95 | print "$this: Updating main working copy ...\n"; |
---|
96 | my @update_main = qx(svn update $wc_main); |
---|
97 | die $this, ': unable to update working copy of FCM/trunk' if $?; |
---|
98 | |
---|
99 | # Perform "svn update" on working copy of the "trunk" of the Admin project |
---|
100 | print "$this: Updating admin working copy ...\n"; |
---|
101 | my @update_admin = qx(svn update $wc_admin); |
---|
102 | die $this, ': unable to update working copy of Admin/trunk' if $?; |
---|
103 | |
---|
104 | # Remove last line from each output, which should be info of the revision |
---|
105 | pop @update_main; |
---|
106 | pop @update_admin; |
---|
107 | |
---|
108 | # No updates, exit loop |
---|
109 | if (not @update_main and not @update_admin) { |
---|
110 | print "$this: No updates detected. Exiting.\n"; |
---|
111 | last; |
---|
112 | } |
---|
113 | |
---|
114 | # Set FCM release number, if necessary |
---|
115 | if (@update_main) { |
---|
116 | # Get last changed revision |
---|
117 | my @info = qx(svn info $wc_main); |
---|
118 | my $rev; |
---|
119 | |
---|
120 | for (@info) { |
---|
121 | next unless /^Last Changed Rev: (\d+)$/; |
---|
122 | |
---|
123 | $rev = $1; |
---|
124 | last; |
---|
125 | } |
---|
126 | |
---|
127 | # Update src/etc/fcm_rev |
---|
128 | print "$this: updating the FCM release number file ...\n"; |
---|
129 | my $fcm_rev = catfile ($wc_main, 'src/etc/fcm_rev'); |
---|
130 | open FILE, '>', $fcm_rev or die $fcm_rev, ': cannot open'; |
---|
131 | print FILE $rev, "\n"; |
---|
132 | close FILE or die $fcm_rev, ': cannot close'; |
---|
133 | } |
---|
134 | |
---|
135 | # Re-create user guide PDF if necessary |
---|
136 | if (grep m#doc/user_guide#, @update_main) { |
---|
137 | print "$this: Re-creating user guide PDF ...\n"; |
---|
138 | my @command = ($ug_to_pdf, $ug_index); |
---|
139 | system @command; |
---|
140 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
141 | } |
---|
142 | |
---|
143 | # Re-install hook scripts if necessary |
---|
144 | if (grep m#src/hook#, @update_admin) { |
---|
145 | print "$this: Re-install hook scripts ...\n"; |
---|
146 | my @command = ($install_hook, $hooksrc_top, $hookdest_top); |
---|
147 | system @command; |
---|
148 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
149 | } |
---|
150 | |
---|
151 | if (@update_main) { |
---|
152 | # Update remote platforms, if necessary |
---|
153 | my $rsh = 'rsh'; |
---|
154 | for my $remote (@remotes) { |
---|
155 | next unless $remote->{MACHINE} and $remote->{WC}; |
---|
156 | print "$this: Updating working copy on $remote->{MACHINE} ...\n"; |
---|
157 | |
---|
158 | # Create the top level directory for the remote working copy (if necessary) |
---|
159 | { |
---|
160 | my @command = ($rsh, $remote->{MACHINE}); |
---|
161 | push @command, ('-l', $remote->{LOGNAME}) if $remote->{LOGNAME}; |
---|
162 | push @command, (qw/mkdir -p/, $remote->{WC}); |
---|
163 | system @command; |
---|
164 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
165 | } |
---|
166 | |
---|
167 | # Sync the working copy to the remote platform |
---|
168 | { |
---|
169 | my @command = ('rsync'); |
---|
170 | push @command, @{ $remote->{OPTIONS} } if @{ $remote->{OPTIONS} }; |
---|
171 | my $rwcpath = $remote->{LOGNAME}; |
---|
172 | $rwcpath .= '@' . $remote->{MACHINE} . ':' . $remote->{WC}; |
---|
173 | push @command, ($wc_main, $rwcpath); |
---|
174 | system join (' ', @command); |
---|
175 | die $this, ': unable to execute ', join (' ', @command) if $?; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | # ------------------------------------------------------------------------------ |
---|
182 | |
---|
183 | END { |
---|
184 | # Remove lock file on exit |
---|
185 | unlink $lockfile if $locked; |
---|
186 | } |
---|
187 | |
---|
188 | # ------------------------------------------------------------------------------ |
---|