-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundleimages
More file actions
executable file
·79 lines (61 loc) · 2.12 KB
/
bundleimages
File metadata and controls
executable file
·79 lines (61 loc) · 2.12 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
#!/usr/bin/env bash
set -euo pipefail
show_help() {
cat <<EOF
Reads Docker image tags from stdin, dedupes, and saves them to a tar archive.
Usage: $(basename "$0") [-o|--output file]
Examples:
echo "postgres:13.4" | $(basename "$0") -o /path/container-images.tar
$(basename "$0") < images.manifest
Expected format of a manifest is one image per line:
postgres:13.4
qdrant/qdrant:v1.13.3-gpu-nvidia
registry.octo-cx-prod.runshiftup.com/octo-cx/common/docker-hub/coturn:25.9.18-edge-alpine
Options:
-o, --output FILE Output tar file path (default: /tmp/container-images-TIMESTAMP.tar)
-h, --help Show this help message
EOF
}
trap 'echo "Interrupted. Killing all jobs..."; kill 0; exit 1' SIGINT
archive_bundle_file="/tmp/container-images-$(date +%Y%m%d%H%M%S).tar"
while [[ $# -gt 0 ]]; do
case $1 in
-o|--output) archive_bundle_file="$2"; shift 2 ;;
-h|--help) show_help; exit 0 ;;
*) show_help >&2; exit 2 ;;
esac
done
echo "Waiting for stdin. Run $(basename "$0") -h for help"
# normalize output path
if [[ "$archive_bundle_file" != /* ]]; then
archive_bundle_file="$(pwd)/$archive_bundle_file"
fi
# ensure output dir exists
mkdir -p "$(dirname "$archive_bundle_file")"
save_image () {
image=$1
safe_filename="$(printf '%s' "$image" | tr '/:' '__')"
image_file="$tmpdir/${safe_filename}.tar"
docker pull "$image" >/dev/null
docker save -o "$image_file" "$image"
}
# strip comments/blanks, dedupe
images=$(grep -vE '^\s*(#|$)' | sed 's/^\s*//;s/\s*$//' | sort -u)
# fresh workspace
tmpdir="$(mktemp -d)"
echo "Created temp dir $tmpdir"
echo "Starting pull and save..."
while IFS= read -r image_spec; do
[[ -z "$image_spec" ]] && continue
echo " $image_spec"
save_image "$image_spec" &
done <<<"$images"
#synchronize
echo "Waiting for pull and save to complete (slow)"
wait
echo "Done"
echo 'Creating tar archive (slow)'
( cd "$tmpdir" && tar -cf "$archive_bundle_file" *.tar )
# compressing large files is not always the best thing to do
# zstd -T0 -q -f --rm "$archive_bundle_file" && archive_bundle_file="${archive_bundle_file}.zst"
echo "Created $archive_bundle_file in ${SECONDS} seconds"