-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-version.sh
More file actions
executable file
·408 lines (351 loc) · 11.2 KB
/
update-version.sh
File metadata and controls
executable file
·408 lines (351 loc) · 11.2 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env bash
#
# Update component versions across kustomization files
#
# Usage:
# ./update-version.sh <component> <new-version>
# ./update-version.sh --list <component>
# ./update-version.sh --dry-run <component> <new-version>
# ./update-version.sh --check <component> <new-version>
#
# Examples:
# ./update-version.sh strimzi 0.51.0
# ./update-version.sh apicurio-registry 3.1.8
# ./update-version.sh streamshub-console 0.12.0
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Color output helpers
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Component configuration
COMPONENT_LABEL=""
GITHUB_REPO=""
VERSION_REGEX=""
COMPONENT_FILES=()
setup_strimzi() {
COMPONENT_LABEL="Strimzi"
GITHUB_REPO="strimzi/strimzi-kafka-operator"
VERSION_REGEX='releases/download/\K[0-9]+\.[0-9]+\.[0-9]+'
COMPONENT_FILES=(
"${SCRIPT_DIR}/components/core/base/strimzi-operator/kustomization.yaml"
"${SCRIPT_DIR}/components/core/stack/kafka/kustomization.yaml"
)
}
setup_apicurio_registry() {
COMPONENT_LABEL="Apicurio Registry"
GITHUB_REPO="Apicurio/apicurio-registry"
VERSION_REGEX='Apicurio/apicurio-registry/\K[0-9]+\.[0-9]+\.[0-9]+'
COMPONENT_FILES=(
"${SCRIPT_DIR}/components/core/base/apicurio-registry-operator/kustomization.yaml"
)
}
setup_streamshub_console() {
COMPONENT_LABEL="StreamsHub Console"
GITHUB_REPO="streamshub/console"
VERSION_REGEX='releases/download/\K[0-9]+\.[0-9]+\.[0-9]+'
COMPONENT_FILES=(
"${SCRIPT_DIR}/components/core/base/streamshub-console-operator/kustomization.yaml"
)
}
setup_prometheus_operator() {
COMPONENT_LABEL="Prometheus Operator"
GITHUB_REPO="prometheus-operator/prometheus-operator"
VERSION_REGEX='releases/download/v\K[0-9]+\.[0-9]+\.[0-9]+'
COMPONENT_FILES=(
"${SCRIPT_DIR}/components/metrics/base/prometheus-operator/kustomization.yaml"
)
}
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS] <component> <new-version>
Update component versions in kustomization files.
Components:
strimzi Strimzi Kafka Operator
apicurio-registry Apicurio Registry Operator
streamshub-console StreamsHub Console Operator
prometheus-operator Prometheus Operator (metrics overlay)
Arguments:
component The component to update
new-version The version to update to (e.g., 0.51.0 or 3.1.8)
Options:
-c, --check Only check if the release exists on GitHub (no changes made)
-d, --dry-run Show what would be changed without making changes
-l, --list [N|all] List available versions (default: 20, or 'all')
-h, --help Show this help message
Examples:
$(basename "$0") --list strimzi # List latest 20 Strimzi versions
$(basename "$0") --list all strimzi # List all Strimzi versions
$(basename "$0") --check strimzi 0.51.0 # Verify release exists
$(basename "$0") --dry-run strimzi 0.51.0 # Preview changes
$(basename "$0") strimzi 0.51.0 # Update Strimzi to version
$(basename "$0") apicurio-registry 3.1.8 # Update Apicurio Registry
$(basename "$0") streamshub-console 0.12.0 # Update StreamsHub Console
$(basename "$0") prometheus-operator 0.90.0 # Update Prometheus Operator
EOF
exit 0
}
# Set up component configuration based on name
setup_component() {
local component="$1"
case "$component" in
strimzi)
setup_strimzi
;;
apicurio-registry)
setup_apicurio_registry
;;
streamshub-console)
setup_streamshub_console
;;
prometheus-operator)
setup_prometheus_operator
;;
*)
error "Unknown component: ${component}"
error "Valid components: strimzi, apicurio-registry, streamshub-console, prometheus-operator"
exit 1
;;
esac
}
# Get current version from the first component file
get_current_version() {
grep -oP "$VERSION_REGEX" "${COMPONENT_FILES[0]}" | head -1
}
# List available releases from GitHub
list_releases() {
local limit="${1:-20}"
local is_all=false
local per_page="$limit"
if [[ "$limit" == "all" ]]; then
is_all=true
per_page=100
fi
info "Fetching available ${COMPONENT_LABEL} releases from GitHub..."
echo ""
if ! command -v curl &> /dev/null; then
error "curl is required to list releases"
exit 1
fi
local releases=""
if [[ "$is_all" == "true" ]]; then
local page=1
while true; do
local page_releases
page_releases=$(curl -s "https://api.github.com/repos/${GITHUB_REPO}/releases?per_page=100&page=${page}" \
| grep '"tag_name":' \
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/' || true)
if [[ -z "$page_releases" ]]; then
break
fi
if [[ -n "$releases" ]]; then
releases="${releases}"$'\n'"${page_releases}"
else
releases="$page_releases"
fi
((page++)) || true
if [[ $page -gt 10 ]]; then
break
fi
done
else
releases=$(curl -s "https://api.github.com/repos/${GITHUB_REPO}/releases?per_page=${per_page}" \
| grep '"tag_name":' \
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/' \
| head -n "$limit")
fi
if [[ -z "$releases" ]]; then
error "Failed to fetch releases from GitHub"
exit 1
fi
local current_version
current_version=$(get_current_version)
local count
count=$(echo "$releases" | wc -l | tr -d ' ')
if [[ "$is_all" == "true" ]]; then
echo "Available ${COMPONENT_LABEL} versions (all ${count}):"
else
echo "Available ${COMPONENT_LABEL} versions (latest ${limit}):"
fi
echo ""
while IFS= read -r version; do
if [[ "$version" == "$current_version" ]]; then
echo " ${version} (current)"
else
echo " ${version}"
fi
done <<< "$releases"
echo ""
info "View all releases: https://github.com/${GITHUB_REPO}/releases"
}
# Check if release exists on GitHub
check_release_exists() {
local version="$1"
local release_url="https://github.com/${GITHUB_REPO}/releases/tag/${version}"
info "Checking if ${COMPONENT_LABEL} release ${version} exists..."
if command -v curl &> /dev/null; then
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" -L "$release_url")
if [[ "$http_code" == "200" ]]; then
info "Release ${version} exists on GitHub"
return 0
else
error "Release ${version} not found on GitHub (HTTP ${http_code})"
error "Check available releases at: https://github.com/${GITHUB_REPO}/releases"
return 1
fi
else
warn "curl not available, skipping release check"
return 0
fi
}
# Cross-platform sed -i
sed_inplace() {
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "$@"
else
sed -i "$@"
fi
}
# Update version in a file
update_file() {
local file="$1"
local old_version="$2"
local new_version="$3"
local dry_run="$4"
if [[ ! -f "$file" ]]; then
warn "File not found: ${file}"
return 1
fi
if ! grep -q "$old_version" "$file"; then
warn "Version ${old_version} not found in ${file}"
return 1
fi
if [[ "$dry_run" == "true" ]]; then
echo "Would update: ${file}"
echo " Old version: ${old_version}"
echo " New version: ${new_version}"
echo " Changes:"
while IFS= read -r line; do
echo " - ${line}"
echo " + ${line//$old_version/$new_version}"
done < <(grep "$old_version" "$file")
else
sed_inplace "s/${old_version}/${new_version}/g" "$file"
info "Updated: ${file}"
fi
}
main() {
local check_release=false
local dry_run=false
local list_releases_flag=false
local list_limit="20"
local new_version=""
local component_arg=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--check)
check_release=true
shift
;;
-d|--dry-run)
dry_run=true
shift
;;
-l|--list)
list_releases_flag=true
shift
if [[ $# -gt 0 && ( "$1" == "all" || "$1" =~ ^[0-9]+$ ) ]]; then
list_limit="$1"
shift
fi
;;
-h|--help)
usage
;;
-*)
error "Unknown option: $1"
usage
;;
*)
if [[ -z "$component_arg" ]]; then
component_arg="$1"
elif [[ -z "$new_version" ]]; then
new_version="$1"
else
error "Too many arguments"
usage
fi
shift
;;
esac
done
if [[ -z "$component_arg" ]]; then
error "No component specified"
usage
fi
setup_component "$component_arg"
if [[ "$list_releases_flag" == "true" ]]; then
list_releases "$list_limit"
exit 0
fi
if [[ -z "$new_version" ]]; then
error "No version specified"
usage
fi
if ! [[ "$new_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
error "Invalid version format: ${new_version}"
error "Expected format: X.Y.Z (e.g., 0.51.0)"
exit 1
fi
local current_version
current_version=$(get_current_version)
if [[ -z "$current_version" ]]; then
error "Could not determine current ${COMPONENT_LABEL} version"
exit 1
fi
info "Current ${COMPONENT_LABEL} version: ${current_version}"
info "Target ${COMPONENT_LABEL} version: ${new_version}"
if [[ "$current_version" == "$new_version" ]]; then
warn "Already at version ${new_version}"
exit 0
fi
if [[ "$check_release" == "true" ]]; then
if check_release_exists "$new_version"; then
exit 0
else
exit 1
fi
fi
echo ""
local files_updated=0
for file in "${COMPONENT_FILES[@]}"; do
if update_file "$file" "$current_version" "$new_version" "$dry_run"; then
((files_updated++)) || true
fi
done
echo ""
if [[ "$dry_run" == "true" ]]; then
info "Dry run complete. ${files_updated} file(s) would be updated."
else
info "Updated ${files_updated} file(s) from ${current_version} to ${new_version}"
echo ""
info "Next steps:"
echo " 1. Review the changes: git diff"
echo " 2. Test the deployment: kubectl kustomize overlays/core/base/ && kubectl kustomize overlays/core/stack/"
echo " 3. Commit the changes: git add -A && git commit -m 'Update ${COMPONENT_LABEL} to ${new_version}'"
fi
}
main "$@"