-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathupload-dist-staging.sh
executable file
·219 lines (185 loc) · 6.18 KB
/
upload-dist-staging.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
#!/bin/bash
set -eo pipefail
# shellcheck source=list-all-packages.sh
source ./bin/list-all-packages.sh
function require() {
if [ -z "$2" ]; then
echo "validation failed: $1 unset"
exit 1
fi
}
require AWS_ACCESS_KEY_ID "${AWS_ACCESS_KEY_ID}"
require AWS_SECRET_ACCESS_KEY "${AWS_SECRET_ACCESS_KEY}"
require AWS_REGION "${AWS_REGION}"
require S3_BUCKET "${S3_BUCKET}"
GITSHA="$(git rev-parse HEAD)"
function package_has_changes() {
local key="$1"
local path="$2"
if [ -z "${path}" ]; then
# if no path then we can't calculate changes
echo "Path empty for package ${package}"
return 0
fi
if is_old_kubernetes "${key}" ; then
# we cannot rebuild old kubernetes packages, so we should always say there were no changes
echo "Old kubernetes package ${package}"
return 1
fi
local upstream_gitsha=
upstream_gitsha="$(aws s3api head-object --bucket "${S3_BUCKET}" --key "${key}" | grep '"gitsha":' | sed 's/[",:]//g' | awk '{print $2}')"
if [ -z "${upstream_gitsha}" ]; then
# if package doesn't exist or have a gitsha it has changes
echo "Upstream gitsha empty for package ${package}"
return 0
fi
if ( set -x; git diff --quiet "${upstream_gitsha}" -- "${path}" "${GITSHA}" -- "${path}" ) ; then
return 1
else
return 0
fi
}
# kubernetes packages before 1.24 are not available in the new yum/apt repo, and so should be copied
function is_old_kubernetes() {
local package="$1"
if echo "${package}" | grep -q "kubernetes-1.15" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.16" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.17" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.18" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.19" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.20" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.21" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.22" ; then
return 0
fi
if echo "${package}" | grep -q "kubernetes-1.23" ; then
return 0
fi
return 1
}
# containerd before 1.6, rook-ceph 1.4.x, and the rook-ceph 1.0 to 1.4 migration package do not build properly today
# as such they should be copied from prod's previous release
function is_old_addon() {
if echo "${package}" | grep -q "containerd-1.2" ; then
return 0
fi
if echo "${package}" | grep -q "containerd-1.3" ; then
return 0
fi
if echo "${package}" | grep -q "containerd-1.4" ; then
return 0
fi
if echo "${package}" | grep -q "containerd-1.5" ; then
return 0
fi
if echo "${package}" | grep -q "rook-1.4" ; then
return 0
fi
if echo "${package}" | grep -q "rookupgrade" ; then
return 0
fi
return 1
}
function build_and_upload() {
local package="$1"
echo "building package ${package}"
make "dist/${package}"
MD5="$(openssl md5 -binary "dist/${package}" | base64)"
echo "uploading package ${package} to ${S3_BUCKET} with metadata md5=\"${MD5}\",gitsha=\"${GITSHA}\""
retry 5 aws s3 cp "dist/${package}" "s3://${S3_BUCKET}/staging/${package}" \
--metadata-directive REPLACE --metadata md5="${MD5}",gitsha="${GITSHA}"
echo "cleaning up after uploading ${package}"
make clean
if [ -n "$DOCKER_PRUNE" ]; then
docker system prune --all --force
fi
}
function copy_from_prod() {
local package="$1"
echo "downloading package ${package} from prod (s3://${S3_BUCKET}/dist/${package})"
retry 5 aws s3 cp "s3://${S3_BUCKET}/dist/${package}" "dist/${package}"
MD5="$(openssl md5 -binary "dist/${package}" | base64)"
echo "uploading package ${package} to ${S3_BUCKET} with metadata md5=\"${MD5}\",gitsha=\"${GITSHA}\""
retry 5 aws s3 cp "dist/${package}" "s3://${S3_BUCKET}/staging/${package}" \
--metadata-directive REPLACE --metadata md5="${MD5}",gitsha="${GITSHA}"
echo "cleaning up after uploading ${package}"
make clean
if [ -n "$DOCKER_PRUNE" ]; then
docker system prune --all --force
fi
}
function deploy() {
local package="$1"
local path="$2"
# always upload small packages that change often
if [ "$package" = "common.tar.gz" ] \
|| [ "$package" = "join.tmpl" ] \
|| [ "$package" = "upgrade.tmpl" ] \
|| [ "$package" = "tasks.tmpl" ] \
|| [ "$package" = "install.tmpl" ] \
|| echo "${package}" | grep -q "kurl-bin-utils" ; then
echo "s3://${S3_BUCKET}/staging/${package} build and upload"
build_and_upload "${package}"
return
fi
if package_has_changes "staging/${package}" "${path}" ; then
echo "s3://${S3_BUCKET}/staging/${package} has changes"
if is_old_addon "${package}" ; then
copy_from_prod "${package}"
else
build_and_upload "${package}"
fi
else
echo "s3://${S3_BUCKET}/staging/${package} no changes in package"
fi
}
function retry {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** $count))
count=$(($count + 1))
if [ $count -lt $retries ]; then
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
function main() {
local batch="$1"
echo "Uploading ${batch} packages to s3://${S3_BUCKET}/staging/"
git fetch
# TODO: kubernetes changes do not yet take into account changes in bundles/
# These need to manually be rebuilt when changing that path.
while read -r line; do
package="$(echo "${line}" | cut -f1 -d' ')"
for pkg in ${batch}; do
if [ -n "${pkg}" ] && [ "${pkg}" = "${package}" ]; then
path="$(echo "${line}" | cut -f2 -d' ')"
deploy "${package}" "${path}"
break
fi
done
done < <(list_all)
}
main "$@"