-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-accessories.sh
More file actions
executable file
·517 lines (437 loc) · 16.6 KB
/
update-accessories.sh
File metadata and controls
executable file
·517 lines (437 loc) · 16.6 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/bash
# Extract image names and versions for accessories from deploy*.yml files
# Also checks for newer versions available on Docker Hub
# Usage: ./extract-accessories.sh [output_format|update]
# output_format: "table" (default), "json", or "csv"
# update: Update deploy files with new versions
# Check for required dependencies
if ! command -v jq &> /dev/null; then
echo "Error: jq is required but not installed. Please install jq to use this script." >&2
exit 1
fi
OUTPUT_FORMAT="${1:-table}"
CONFIG_DIR="config"
CACHE_DIR="/tmp/docker-registry-cache"
CACHE_TTL=3600 # 1 hour cache
UPDATE_MODE=false
UPDATE_ALL=false
JSON_ITEMS=() # Array to collect JSON items for update processing
# Validate config directory exists
if [ ! -d "$CONFIG_DIR" ]; then
echo "Error: Config directory '$CONFIG_DIR' not found." >&2
exit 1
fi
# Check if update mode is requested
if [ "$OUTPUT_FORMAT" = "update" ]; then
UPDATE_MODE=true
OUTPUT_FORMAT="json"
elif [ "$OUTPUT_FORMAT" = "update-all" ]; then
UPDATE_MODE=true
UPDATE_ALL=true
OUTPUT_FORMAT="json"
fi
# Create cache directory
mkdir -p "$CACHE_DIR"
# Colors for table output
if [ "$OUTPUT_FORMAT" = "table" ]; then
BOLD='\033[1m'
NC='\033[0m' # No Color
fi
# Function to check if a string looks like a semantic version
is_semantic_version() {
local tag="$1"
# Match patterns like: 1.0.0, v1.0.0, 1.0, v1.0, 2025.10.0, etc.
if [[ "$tag" =~ ^v?[0-9]+(\.[0-9]+)*$ ]]; then
return 0
fi
return 1
}
# Function to normalize version for comparison (remove 'v' prefix)
normalize_version() {
local version="$1"
echo "$version" | sed 's/^v//'
}
# Function to compare two semantic versions
# Returns 1 if version1 > version2, 0 if equal, -1 if version1 < version2
compare_versions() {
local v1=$(normalize_version "$1")
local v2=$(normalize_version "$2")
# Handle simple case where versions are equal
if [ "$v1" = "$v2" ]; then
echo 0
return
fi
# Use printf and basic string comparison with padding
# Split by dots and compare each part numerically
local IFS='.'
local -a parts1=($v1)
local -a parts2=($v2)
local len=${#parts1[@]}
if [ ${#parts2[@]} -gt $len ]; then
len=${#parts2[@]}
fi
for ((i=0; i<len; i++)); do
local p1=${parts1[$i]:-0}
local p2=${parts2[$i]:-0}
# Remove non-numeric suffixes for comparison
p1=${p1%%[^0-9]*}
p2=${p2%%[^0-9]*}
p1=${p1:-0}
p2=${p2:-0}
if [ $p1 -gt $p2 ]; then
echo 1
return
elif [ $p1 -lt $p2 ]; then
echo -1
return
fi
done
echo 0
}
# Function to update a deploy file with a new version and SHA256
update_deploy_file() {
local file="$1"
local accessory="$2"
local old_version="$3"
local new_version="$4"
local new_sha256="$5" # New SHA256 digest (optional)
if [ ! -f "$file" ]; then
echo "ERROR: File not found: $file"
return 1
fi
# Prepare the new image line
local new_image_line=""
if [ -n "$new_sha256" ] && [ "$new_sha256" != "unknown" ]; then
new_image_line="${new_version}@sha256:${new_sha256}"
else
new_image_line="${new_version}"
fi
# Read the file and find the accessory section, then update the version
local found=false
local in_accessory=false
local updated=false
local temp_file=$(mktemp)
while IFS= read -r line; do
# Check if we're entering this accessory section
if [[ "$line" =~ ^[[:space:]]{2}${accessory}:[[:space:]]*$ ]]; then
in_accessory=true
echo "$line" >> "$temp_file"
continue
fi
# If we were in the accessory section and hit another key at same level, we're done
if $in_accessory && [[ "$line" =~ ^[[:space:]]{2}[a-zA-Z_] ]] && ! [[ "$line" =~ ^[[:space:]]{2}${accessory} ]]; then
in_accessory=false
fi
# If we're in the right accessory section, look for the image line
if $in_accessory && [[ "$line" =~ image:[[:space:]]*(.*) ]]; then
local image_line="${BASH_REMATCH[1]}"
# Extract image name (everything before :version@sha256...)
local image_name=$(echo "$image_line" | sed 's/:.*$//')
# Replace entire image line with new image:version@sha256
local updated_line="${image_name}:${new_image_line}"
if [ "$image_line" != "$updated_line" ]; then
# Get indentation from original line
local indent=$(echo "$line" | sed 's/[^ \t].*$//')
echo "${indent}image: $updated_line" >> "$temp_file"
updated=true
found=true
in_accessory=false
continue
fi
fi
echo "$line" >> "$temp_file"
done < "$file"
if [ "$updated" = true ]; then
mv "$temp_file" "$file"
echo "Updated $file: $accessory ($old_version → $new_version)"
return 0
else
rm "$temp_file"
echo "WARNING: Could not find version $old_version for $accessory in $file"
return 1
fi
}
# Function to get SHA256 digest for a specific image tag from Docker Hub
get_image_sha256() {
local image="$1"
local tag="$2"
local namespace=""
local repo_name=""
# Parse image name
if [[ "$image" == *"/"* ]]; then
namespace="${image%/*}"
repo_name="${image##*/}"
else
namespace="library"
repo_name="$image"
fi
local cache_file="$CACHE_DIR/${namespace}_${repo_name}_${tag}.sha256.cache"
# Check cache
if [ -f "$cache_file" ]; then
local file_age=$(($(date +%s) - $(stat -c%Y "$cache_file" 2>/dev/null || stat -f%m "$cache_file" 2>/dev/null || echo 0)))
if [ $file_age -lt $CACHE_TTL ]; then
cat "$cache_file"
return 0
fi
fi
# Query Docker Hub API for tag info
local api_url="https://hub.docker.com/v2/repositories/${namespace}/${repo_name}/tags/${tag}"
local tag_info=$(curl -s "$api_url" 2>/dev/null)
# Validate JSON response
if ! echo "$tag_info" | jq empty 2>/dev/null; then
echo "unknown"
return 1
fi
# Extract digest from tag info - it's in the "images" array
local digest=$(echo "$tag_info" | jq -r '.images[0].digest // empty' 2>/dev/null)
if [ -z "$digest" ]; then
# If Docker Hub API doesn't have it, try to get it from the registry
# This requires a different approach - use curl with manifest request
digest=$(curl -s -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"https://registry-1.docker.io/v2/${namespace}/${repo_name}/manifests/${tag}" 2>/dev/null | \
jq -r '.config.digest // empty' 2>/dev/null)
fi
if [ -z "$digest" ]; then
digest="unknown"
else
# Strip "sha256:" prefix if present (API returns it with prefix)
digest="${digest#sha256:}"
fi
# Cache the result
echo "$digest" > "$cache_file"
echo "$digest"
}
# Function to get latest version from Docker Hub
get_latest_version() {
local image="$1"
local namespace=""
local repo_name=""
# Parse image name
if [[ "$image" == *"/"* ]]; then
namespace="${image%/*}"
repo_name="${image##*/}"
else
# Official Docker Hub image (no namespace)
namespace="library"
repo_name="$image"
fi
local cache_file="$CACHE_DIR/${namespace}_${repo_name}.cache"
# Check cache
if [ -f "$cache_file" ]; then
local file_age=$(($(date +%s) - $(stat -c%Y "$cache_file" 2>/dev/null || stat -f%m "$cache_file" 2>/dev/null || echo 0)))
if [ $file_age -lt $CACHE_TTL ]; then
cat "$cache_file"
return 0
fi
fi
# Query Docker Hub API - get multiple tags
local api_url="https://hub.docker.com/v2/repositories/${namespace}/${repo_name}/tags"
local tags_json=$(curl -s "$api_url?page_size=100" 2>/dev/null)
# Validate JSON response
if ! echo "$tags_json" | jq empty 2>/dev/null; then
echo "unknown"
return 1
fi
# Extract tag names and filter to only semantic versions
local all_tags=$(echo "$tags_json" | jq -r '.results[]?.name // empty' 2>/dev/null)
local latest_tag=""
local latest_version=""
while IFS= read -r tag; do
# Skip empty lines
[ -z "$tag" ] && continue
# Filter out known non-version tags
if echo "$tag" | grep -qi 'latest\|main\|master\|dev\|develop\|nightly\|alpha\|beta\|sha256\|digest'; then
continue
fi
# Only consider semantic versions
if is_semantic_version "$tag"; then
if [ -z "$latest_version" ]; then
latest_tag="$tag"
latest_version="$tag"
else
# Compare versions
local cmp_result=$(compare_versions "$tag" "$latest_version")
if [ "$cmp_result" = "1" ]; then
latest_tag="$tag"
latest_version="$tag"
fi
fi
fi
done <<< "$all_tags"
if [ -z "$latest_tag" ]; then
latest_tag="unknown"
fi
# Cache the result
echo "$latest_tag" > "$cache_file"
echo "$latest_tag"
}
case "$OUTPUT_FORMAT" in
json)
echo "["
first=true
;;
csv)
echo "File,Accessory,Image,Version,LatestVersion,UpdateAvailable"
;;
table)
printf "${BOLD}%-35s %-25s %-50s %-20s %-20s${NC}\n" "File" "Accessory" "Image" "Version" "Latest"
printf "%.0s-" {1..150}
echo
;;
esac
# Temporary file for storing JSON items when in update mode
JSON_TEMP_FILE=""
if [ "$UPDATE_MODE" = true ]; then
JSON_TEMP_FILE=$(mktemp)
fi
# Find all deploy*.yml files and process them
find "$CONFIG_DIR" -maxdepth 1 -name "deploy*.yml" -type f | sort | while read file; do
filename=$(basename "$file")
# Extract accessories section and parse each one
# Match lines like: " accessory_name:" or " image: ..."
current_accessory=""
in_accessories=false
while IFS= read -r line; do
# Check if we're entering the accessories section
if [[ "$line" =~ ^accessories: ]]; then
in_accessories=true
continue
fi
# Stop processing if we hit another top-level key after accessories
if $in_accessories && [[ "$line" =~ ^[a-zA-Z] ]]; then
in_accessories=false
continue
fi
# If we're in the accessories section, look for accessory names and images
if $in_accessories; then
# Match accessory names (lines starting with 2 spaces, not 4)
if [[ "$line" =~ ^[[:space:]]{2}[a-zA-Z_] ]] && ! [[ "$line" =~ ^[[:space:]]{4} ]]; then
current_accessory=$(echo "$line" | sed 's/^ //; s/:.*$//')
fi
# Match image lines
if [[ "$line" =~ "image:" ]]; then
# Extract the image part after "image:"
image_full=$(echo "$line" | sed 's/.*image:[[:space:]]*//')
# Parse image:version@digest format
# Remove the @sha256:... part
image_with_version="${image_full%%@*}"
# Split image and version
# Format is: image_name:version
if [[ "$image_with_version" =~ : ]]; then
image="${image_with_version%:*}"
version="${image_with_version##*:}"
else
image="$image_with_version"
version="latest"
fi
# Get latest version from Docker Hub
latest_version=$(get_latest_version "$image")
# Determine if update is available (simple string comparison)
update_available=""
if [ "$version" != "$latest_version" ] && [ "$latest_version" != "unknown" ]; then
update_available="*"
fi
# Prepare JSON object
json_obj="{\"file\": \"$filename\", \"accessory\": \"$current_accessory\", \"image\": \"$image\", \"version\": \"$version\", \"latest_version\": \"$latest_version\", \"update_available\": $([ -n "$update_available" ] && echo 'true' || echo 'false')}"
# If in update mode, write to temp file
if [ "$UPDATE_MODE" = true ] && [ -n "$JSON_TEMP_FILE" ]; then
echo "$json_obj" >> "$JSON_TEMP_FILE"
fi
# Output in requested format
case "$OUTPUT_FORMAT" in
json)
if [ "$first" = false ]; then
echo ","
fi
echo -n " $json_obj"
first=false
;;
csv)
echo "$filename,$current_accessory,$image,$version,$latest_version,$update_available"
;;
table)
if [ -n "$update_available" ]; then
# Highlight outdated versions
printf "%-35s %-25s %-50s %-20s %-20s %s\n" "$filename" "$current_accessory" "$image" "$version" "$latest_version" "UPDATE"
else
printf "%-35s %-25s %-50s %-20s %-20s\n" "$filename" "$current_accessory" "$image" "$version" "$latest_version"
fi
;;
esac
fi
fi
done < "$file"
done
if [ "$UPDATE_MODE" = true ]; then
# In update mode, suppress normal output
:
else
# Normal output mode
case "$OUTPUT_FORMAT" in
json)
echo ""
echo "]"
;;
esac
fi
# If update mode is enabled, process the updates from temp file
if [ "$UPDATE_MODE" = true ] && [ -n "$JSON_TEMP_FILE" ] && [ -f "$JSON_TEMP_FILE" ]; then
echo "" >&2
echo "=== Updating deploy files with new versions ===" >&2
# Count total updates available
total_updates=$(grep -c '"update_available": true' "$JSON_TEMP_FILE" 2>/dev/null || echo 0)
if [ "$total_updates" -eq 0 ]; then
echo "No updates available!" >&2
rm -f "$JSON_TEMP_FILE"
exit 0
fi
echo "Found $total_updates updates available:" >&2
# Process each item from temp file
while IFS= read -r json_obj; do
# Extract fields using jq
file=$(echo "$json_obj" | jq -r '.file' 2>/dev/null)
accessory=$(echo "$json_obj" | jq -r '.accessory' 2>/dev/null)
image=$(echo "$json_obj" | jq -r '.image' 2>/dev/null)
version=$(echo "$json_obj" | jq -r '.version' 2>/dev/null)
latest_version=$(echo "$json_obj" | jq -r '.latest_version' 2>/dev/null)
update_available=$(echo "$json_obj" | jq -r '.update_available' 2>/dev/null)
if [ "$update_available" = "true" ]; then
# Construct full file path
full_path="$CONFIG_DIR/$file"
# Fetch SHA256 digest for the new version
echo " Fetching SHA256 digest for $image:$latest_version..." >&2
new_sha256=$(get_image_sha256 "$image" "$latest_version")
if [ "$new_sha256" = "unknown" ]; then
echo " WARNING: Could not fetch SHA256 digest for $image:$latest_version" >&2
fi
# Check if in automatic mode
if [ "$UPDATE_ALL" = true ]; then
# Auto-update without prompting
echo "" >&2
echo "Updating $accessory in $file" >&2
echo " $version → $latest_version" >&2
if [ "$new_sha256" != "unknown" ]; then
echo " SHA256: ${new_sha256:0:12}..." >&2
fi
update_deploy_file "$full_path" "$accessory" "$version" "$latest_version" "$new_sha256" 2>&1 | sed 's/^/ /' >&2
else
# Prompt user for confirmation
echo "" >&2
echo "Update available: $accessory in $file" >&2
echo " Current: $version" >&2
echo " Latest: $latest_version" >&2
if [ "$new_sha256" != "unknown" ]; then
echo " SHA256: ${new_sha256:0:12}..." >&2
fi
read -p " Update? (y/n): " -n 1 -r confirm >&2
echo "" >&2
if [[ $confirm =~ ^[Yy]$ ]]; then
update_deploy_file "$full_path" "$accessory" "$version" "$latest_version" "$new_sha256" 2>&1 | sed 's/^/ /' >&2
fi
fi
fi
done < "$JSON_TEMP_FILE"
rm -f "$JSON_TEMP_FILE"
echo "" >&2
echo "=== Update Complete ===" >&2
fi