source: other-projects/nightly-tasks/crons-and-scripts/do-snapshots-and-latest.sh@ 29998

Last change on this file since 29998 was 29998, checked in by ak19, 9 years ago

Corrections to make_latest function.

  • Property svn:executable set to *
File size: 8.6 KB
Line 
1#!/bin/bash
2
3# This script is called each time a nightly GS file (binary or otherwise) gets uploaded to puka
4# to the caveat-emptor folder. This script is called from
5# nzdl@puka:[522]~/.ssh$ less authorized_keys
6
7
8######### GLOBAL VARS #########
9
10# http://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script
11# we want . as separator to produce: yyyy.mm.dd
12
13today=`date +%Y.%m.%d`
14
15# http://askubuntu.com/questions/367136/how-do-i-read-a-variable-from-a-file
16
17gs2version=`cat /greenstone/greenstone.org/base/next-release.txt`
18gs3version=`cat /greenstone/greenstone.org/base/next-release-greenstone3.txt`
19
20# static array of string literals representing the caveat file suffixes we're interested in
21
22suffixes=("linux" "linux-x64" "Lion-MacOS-intel.dmg" "MacOS-intel.dmg" "windows.exe" "source-component.tar.gz" "source-component.zip" "source-distribution.tar.gz" "source-distribution.zip")
23
24gs2_bin_prefix=Greenstone-$gs2version-candidate-$today-
25gs3_bin_prefix=Greenstone-$gs3version-candidate-$today-
26
27#echo "Date is $today"
28#echo "gs2ver is $gs2version"
29#echo "gs3ver is $gs3version"
30#echo "gs2_bin_prefix is $gs2_bin_prefix"
31#echo "gs3_bin_prefix is $gs3_bin_prefix"
32
33caveat_dir=/greenstone/greenstone.org/base/caveat-emptor/
34snapshots_dir=/greenstone/greenstone.org/base/release-snapshots/
35latest_dir=${caveat_dir}latest/
36
37###############################
38
39# Function that checks whether all of today's binaries for a gs_version (GS2 or GS3) are
40# present in the caveat folder
41# If so, it copies those bins over into the release-snapshots folder under new names
42
43# Non-ambiguous string concatenation: ${varname}string-literal
44# stackoverflow.com/questions/4181703/how-can-i-concatenate-string-variables-in-bash
45
46function snapshots_for_gs_version () {
47
48 # Local vs global variables in functions
49 # http://www.tldp.org/LDP/abs/html/localvar.html
50
51 local gs_version=$1
52
53 #check if we've found all gs_version nightly binaries
54 local foundallbins=true
55
56 for suffix in ${suffixes[@]}; do
57 local file=$caveat_dir
58 if [ "$gs_version" == "2" ]; then
59 file=$file$gs2_bin_prefix$suffix
60 else
61 file=$file$gs3_bin_prefix$suffix
62 fi
63
64 if [ ! -f $file ]; then
65 echo "File does not exist: $file"
66 foundallbins=false
67 #else
68 #echo "File exists: $file"
69 fi
70 done
71
72 echo ""
73
74 if [ "$foundallbins" == "true" ]; then
75 echo "All gs$gs_version binaries for $today generated"
76 echo ""
77
78 # delete the old stables
79 echo "Deleting ${snapshots_dir}Greenstone-${gs_version}s*"
80 rm ${snapshots_dir}Greenstone-${gs_version}s*
81 echo ""
82
83 # copy the gs2 binaries to release-snapshots folder
84 # under the new name Greenstone-${gs_version}sDATE-suffix, where gs_version = 2|3
85 # Use hardlinking instead of copy to save space
86
87 for suffix in ${suffixes[@]}; do
88 local file=$caveat_dir
89 if [ "$gs_version" == "2" ]; then
90 file=$file$gs2_bin_prefix$suffix
91 else
92 file=$file$gs3_bin_prefix$suffix
93 fi
94 local newfile=${snapshots_dir}Greenstone-${gs_version}s${today}-$suffix
95
96 # http://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link
97 # Q & A: The difference between hard and soft links at http://linuxgazette.tuwien.ac.at/105/pitcher.html
98 echo "Hardlinking file $file as $newfile"
99 ln $file $newfile
100
101 done
102
103 else
104 echo "Not all gs${gs_version} binaries for $today have been generated. (foundallbins: $foundallbins)"
105 fi
106
107 echo ""
108
109
110 # cdrom components only for GS2
111
112 if [ "$gs_version" == "2" ]; then
113
114 echo "GS2 CD ROM components"
115 echo ""
116
117 cd_suffixes=("linux" "linux-x64" "Lion-mac" "mac" "full-windows" "windows")
118 cdfile_src=cdrom-components-${gs2version}-candidate-${today}-
119
120 local foundallcds=true
121
122 for suffix in ${cd_suffixes[@]}; do
123
124 local file=${caveat_dir}$cdfile_src${suffix}.tar.gz
125
126 if [ ! -f $file ]; then
127 foundallcds=false
128 echo "Could not find cdrom component $file"
129 fi
130 done
131
132 # if all cdrom components present, then copy across under new names
133 if [ "$foundallcds" == "true" ]; then
134
135 # delete the old cdrom components from release-snapshots
136 echo "Deleting ${snapshots_dir}cdrom-components-2s*"
137 rm ${snapshots_dir}cdrom-components-2s*
138 echo ""
139
140 # copy across the new cdrom components
141 # use hardlinking instead of copying to save space
142
143 for suffix in ${cd_suffixes[@]}; do
144
145 local srcfile=${caveat_dir}$cdfile_src${suffix}.tar.gz
146 local destfile=${snapshots_dir}cdrom-components-2s${today}-${suffix}.tar.gz
147 echo "Hardlinking $srcfile as $destfile"
148 ln $srcfile $destfile
149 done
150 fi
151
152 echo ""
153 fi
154
155
156}
157
158# Inspect the most recently added file in the caveat folder and iff it's a GS binary or src file,
159# create a hardlink for it under the appropriate name in the 'latest' subfolder
160# and delete the earlier variant of the same in 'latest'.
161# In time may have to start including the GS documented examples archive files generated overnight.
162#
163# General: http://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
164
165function make_latest () {
166
167 echo ""
168 #echo "In MAKE_LATEST"
169
170 # Using local variable to loop through files, seen in http://mywiki.wooledge.org/ParsingLs
171 local latest
172 local file
173 local suffix
174
175 # find the latest file in caveat, since this script is called upon each upload to puka's caveat
176 # http://stackoverflow.com/questions/5885934/bash-function-to-find-newest-file-matching-pattern
177 # http://mywiki.wooledge.org/BashFAQ/003
178 # http://tldp.org/LDP/abs/html/fto.html, where f1 -nt f2 means file f1 is newer than file f2
179 # http://superuser.com/questions/687523/get-the-last-file-name-in-a-variable-in-bash
180
181 unset -v latest
182 #for file in "${caveat_dir}"*; do
183 #then file would be the full path. Instead, cd into the caveat dir to just get the filename
184 cd $caveat_dir
185 for file in *; do
186 [[ -f $file ]] && [[ "$file" != *"latest-server.exe" ]] && [[ "$file" != *"Greenstone3-latest-source.tar.gz" ]] && [[ $file -nt $latest ]] && latest=$file
187 done
188 cd -
189
190 # latest will not be undef, because this script is only ever called upon a legitimate
191 # file upload into puka's caveat-emptor location
192 # E.g. for testing, latest=Greenstone-3.07-candidate-2015.06.30-linux-x64
193 echo "Latest file in caveat is: $latest"
194
195 # Iff this is a GS binary/src file that we want to maintain in the caveat/latest folder,
196 # then remove the older version of the same file from caveat/latest folder, if any,
197 # and hardlink the latest file into caveat/latest
198
199 # Check whether the latest file ends with the binary/src suffix
200 # https://viewsby.wordpress.com/2013/09/06/bash-string-ends-with/
201 # single or double square brackets for testing conditions, [] vs [[]]
202 # http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts
203 # Need double brackets below, [[]], for string expansion with wildcard
204
205 for suffix in ${suffixes[@]}; do
206 if [[ "$latest" == *"$suffix" ]]; then
207 echo "Found that suffix matched: $suffix"
208
209 # splits the filename $latest by hyphens, -, and then gets the 2nd substring
210 # in this case the GS2 or GS3 full version number
211 # http://stackoverflow.com/questions/428109/extract-substring-in-bash
212 local matchfile=`echo $latest| cut -d'-' -f 2`
213 echo "Found gs version prefix: $matchfile"
214
215 matchfile=${latest_dir}Greenstone-${matchfile}-candidate-*-${suffix}
216
217 # remove the matching file from the caveat/latest folder
218 # we don't know it's date, so we substituted that portion with a * as wildcard
219 # E.g. matchfile=<caveat>/latest/Greenstone-2.87-candidate-*-source-component.tar.gz
220
221 echo "Removing $matchfile"
222 rm $matchfile
223
224 # make a hardlink of the $latest file into the caveat_latest folder
225 echo "Making a hardlink to ${caveat_dir}$latest in ${latest_dir}$latest"
226 ln ${caveat_dir}$latest ${latest_dir}$latest
227 #else
228 #echo "$latest did not match $suffix"
229 fi
230 done
231
232 echo ""
233}
234
235####################
236
237# the 'main'
238
239# This script is called upon each upload to puka's caveat-emptor by
240# the file nzdl@puka:[522]~/.ssh/authorized_keys
241
242# Check if conditions are true to create snapshots out of today's GS2 bins, these conditions
243# being that all src and bins of GS2 have been generated for today.
244# And if so, hardlink these src and bin files into the release-snapshots dir.
245# Repeat for GS3.
246# Finally, for each uploaded file that is a Greenstone src or binary file in caveat
247# hardlink it into the caveat/latest folder to keep a copy of the most-recent version of that
248# bin/file that was successfully generated.
249
250snapshots_for_gs_version "2"
251snapshots_for_gs_version "3"
252make_latest
Note: See TracBrowser for help on using the repository browser.