-
Notifications
You must be signed in to change notification settings - Fork 14
/
automated-updates.sh
executable file
·221 lines (179 loc) · 6.12 KB
/
automated-updates.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env bash
vers=()
###
# Checks if the version variables are equal to each other; if versionGreaterThan evalutes to true, the version string
# that will be passed to ./shared/release.sh will be generated and added to the vers array
###
versionEqual() {
newVersion=$1
currentVersion=$2
if [ "$newVersion" = "$currentVersion" ]; then
echo "Current version $currentVersion matches $newVersion. Does not need an update"
return 1
else
versionGreaterThan "$newVersion" "$currentVersion"
fi
}
###
# versionGreaterThan checks if the version is greater, in which case we return a code that will then be used to determine whether
# a version string will be generated
###
versionGreaterThan() {
if [ "$(printf '%s\n' "$newVersion" "$currentVersion" | sort -V | head -n1)" = "$currentVersion" ]; then
echo "Parsed version $newVersion is greater than $currentVersion"
return 0
else
echo "Parsed version $newVersion is not greater than $currentVersion"
return 1
fi
}
###
# directory check is a helper function to check the given directory in order to generate a version string. It's enabled by the
# searchTerm variable comparing the newVersion to the currentVersion
# NOTE: because images have varying build parameters, the builtParam variable should be specified within a specific image repo
# otherwise, the default is ""
###
directoryCheck() {
local directory=$1
local searchTerm=$2
if [ -z "$builtParam" ]; then
builtParam=""
fi
if [ -d "$directory" ]; then
currentVersion=$searchTerm
echo "directory $directory exists; checking for matching versions: $currentVersion"
versionEqual "$newVersion" "$currentVersion"
else
generateVersionString "$newVersion" "$builtParam"
return 1
fi
}
###
# generateVersions will help parse out the versions needed. this functions similarly to what already exists in ./shared/gen-dockerfiles
# however, since we are pulling from a source, some parsing needs to happen; an example of ${cut} would be something like:
# $(cut -d 'v' -f2 <<< "$version")" or just "$version" if no edits need to be made
###
generateVersions () {
local cut=$1
if [[ -n $cut ]]; then
newVersion=${cut}
else
newVersion=${version}
fi
if [[ $newVersion =~ ([0-9]+\.[0-9]+\.[0-9]) ]]; then
majorMinor=${newVersion%.*}
else
majorMinor=${newVersion}
fi
if [[ $majorMinor =~ ([0-9]+\.[0-9]+\.[0-9]) ]]; then
majorMinor=${majorMinor%.*}
fi
}
###
# some cimgs require a separate parameter to specify things like a URL. this function builds the full string to be included
# in the vers array
###
generateVersionString() {
local version=$1
if [ -n "$builtParam" ]; then
versionString=${version}${builtParam}
else
versionString=${version}
fi
vers+=( "$versionString" )
}
###
# this function attempts to find the values associated with a variable in a specified Dockerfile. This is the basis for comparison
# e.g newVersion vs currentVersion or an entire string, URL, etc that may need to be replaced
#
###
generateSearchTerms () {
local searchFor=$1
local searchFile=$2
local trimCharacters=$3
currVer=$(grep -m 1 "$searchFor" "$searchFile" | head -1)
if [[ "$currVer" =~ = ]]; then
currVer=$(cut -d "=" -f2 <<< "$currVer")
else
currVer=$(awk -F ' ' '{print $3}' <<< "$currVer")
fi
SEARCH_TERM=$(trimmer "$trimCharacters" <<< "$currVer")
export SEARCH_TERM
}
# just in case a variable needs to be trimmed. space separated list of characters to be trimmed.
trimmer() {
tr -d "$@"
}
# some images, like clojure and android, require specific URLs parsed from the web
getParsedURL() {
local URL=$1
local searchString=$2
parsedURL=$(curl -sSL "$URL" | grep -m 1 "$searchString" | awk -F '/' '{ print $NF }' | tr -d "\"")
export parsedURL
}
###
# replaceVersions, instead of tracking the version being parsed, simply gets the latest version/url for a specific software and
# replaces what is in the Dockerfile by specifying the "search term", which should exist in the Dockerfile as an ENV
# variable. The "software version" represents the actual version string of the software in a given direectory's Dockerfile
###
replaceVersions() {
local searchTerm=$1
local softwareVersion=$2
# isVersion is to differentiate between replacing a version string or a URL
local isVersion=$3
currentVersion=$searchTerm
if [ "$isVersion" == true ]; then
versionEqual "$newVersion" "$softwareVersion"
if [[ $(eval echo $?) -eq 0 ]]; then
# shellcheck disable=SC2154
sed -i.bak "s!$softwareVersion!""$newVersion"'!g' "$templateFile"
find . -name \*.bak -type f -delete
fi
else
# shellcheck disable=SC2154
sed -i.bak "s!$softwareVersion!""$newVersion"'!g' "$templateFile"
find . -name \*.bak -type f -delete
fi
}
declare -A quarters
quarters["01"]="01"
quarters["04"]="04"
quarters["07"]="07"
quarters["10"]="10"
export quarters
###
# generateDatedTags generates tags in the form '2023.03'
###
generateDatedTags() {
CURRENTMONTH=$(date +%m)
CURRENTYEAR=$(date +%Y)
RELEASE="$CURRENTYEAR.$CURRENTMONTH"
export RELEASE
}
checkMonth() {
if [[ ${quarters[${RELEASE##*.}]+exists} && $TEMPLATEMONTH != "${RELEASE##*.}" ]]; then
STRING_TO_REPLACE="$TEMPLATEYEAR.$TEMPLATEMONTH"
fi
export STRING_TO_REPLACE
}
replaceDatedTags() {
local templateFile=$1
local interval=$2
TEMPLATEYEAR=$(grep -m 1 "FROM" "$templateFile" | head -1 | cut -d : -f2 | cut -d . -f1)
TEMPLATEMONTH=$(grep -m 1 "FROM" "$templateFile" | head -1 | cut -d : -f2 | cut -d . -f2 | cut -d - -f1)
generateDatedTags
checkMonth
[[ $interval == "monthly" ]] && STRING_TO_REPLACE="$TEMPLATEYEAR.$TEMPLATEMONTH"
[[ -n $STRING_TO_REPLACE ]] && sed -i.bak "s|$STRING_TO_REPLACE|$RELEASE|g" "$templateFile"
rm -rf ./*.bak
}
# Only used for the deploy suite of cimgs: gcp, azure, aws. namespace and parent are sourced from the repo's manifest
releaseDeployImage() {
local version=$1
if docker buildx imagetools inspect "$namespace/$parent:$RELEASE-node" &> /dev/null; then
./shared/release.sh "$version.1"
else
echo "$namespace/$parent:$RELEASE-node does not exist"
exit 0
fi
}