Skip to content

Commit 4885f0b

Browse files
committed
Add "VSize" to "Supported tags and respective Dockerfile links"
1 parent 768a5b2 commit 4885f0b

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

generate-dockerfile-links-partial.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22
set -e
33

4+
thisDir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
5+
46
repo="$1"
57
if [ -z "$repo" ]; then
68
echo >&2 "usage: $0 repo"
@@ -14,6 +16,7 @@ unset IFS
1416

1517
repoDirs=()
1618
declare -A repoDirTags=()
19+
declare -A repoDirFirstTag=()
1720

1821
for line in "${lines[@]}"; do
1922
tag="$(echo "$line" | awk -F ': +' '{ print $1 }')"
@@ -24,6 +27,9 @@ for line in "${lines[@]}"; do
2427
repoDirTags["$repoDir"]+=', '
2528
fi
2629
repoDirTags["$repoDir"]+='`'"$tag"'`'
30+
if [ -z "${repoDirFirstTag[$repoDir]}" ]; then
31+
repoDirFirstTag["$repoDir"]="$tag"
32+
fi
2733
done
2834

2935
for repoDir in "${repoDirs[@]}"; do
@@ -51,7 +57,7 @@ for repoDir in "${repoDirs[@]}"; do
5157

5258
url="https://$gitUrl/blob/$commit/${dir}Dockerfile"
5359

54-
echo '- ['"${repoDirTags["$repoDir"]}"' (*'"${dir}Dockerfile"'*)]('"$url"')'
60+
echo '- ['"${repoDirTags["$repoDir"]}"' (*'"${dir}Dockerfile"'*)]('"$url"') [VSize: '"$("$thisDir/get-image-data.sh" vsize "$repo" "${repoDirFirstTag["$repoDir"]}")"']'
5561
done
5662

5763
echo

get-image-data.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
set -e
3+
4+
usage() {
5+
echo "usage: $0 [options] data image [tag]"
6+
echo " ie: $0 vsize scratch"
7+
echo " $0 id debian latest"
8+
echo " $0 -v vsize golang cross"
9+
}
10+
11+
opts="$(getopt -o 'h?v' --long 'help,verbose' -- "$@" || { usage >&2 && false; })"
12+
eval set -- "$opts"
13+
14+
verbose=
15+
while true; do
16+
flag="$1"
17+
shift
18+
case "$flag" in
19+
--help|-h|'-?')
20+
usage
21+
exit 0
22+
;;
23+
--verbose|-v) verbose=1 ;;
24+
--)
25+
break
26+
;;
27+
*)
28+
{
29+
echo "error: unknown flag: $flag"
30+
usage
31+
} >&2
32+
exit 1
33+
;;
34+
esac
35+
done
36+
37+
data="$1"
38+
image="$2"
39+
tag="${3:-latest}"
40+
41+
if [ -z "$data" -o -z "$image" -o -z "$tag" ]; then
42+
usage >&2
43+
exit 1
44+
fi
45+
46+
humanSizeUnits=( B KB MB GB TB )
47+
[ "$verbose" ] && humanSizeScale=3 || humanSizeScale=1
48+
human_size() {
49+
bytes="$1"
50+
unit=0
51+
unitBytes="$1"
52+
while unitBytes="$(echo "scale=0; $bytes / (1000 ^ $unit)" | bc -l)" && [ "$unitBytes" -gt 1000 ]; do
53+
if [ ! "${humanSizeUnits[$(( unit + 1 ))]}" ]; then
54+
break
55+
fi
56+
unit="$(( unit + 1 ))"
57+
done
58+
echo "$(echo "scale=$humanSizeScale; $bytes / (1000 ^ $unit)" | bc -l) ${humanSizeUnits[$unit]}"
59+
}
60+
61+
token="$(curl -sSL -o /dev/null -D- -H 'X-Docker-Token: true' "https://index.docker.io/v1/repositories/$image/images" | awk -F ':[[:space:]]*|\r' '$1 == "X-Docker-Token" { print $2 }')"
62+
63+
imageId="$(curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/repositories/$image/tags/$tag")"
64+
imageId="${imageId%\"}"
65+
imageId="${imageId#\"}"
66+
67+
case "$data" in
68+
id)
69+
echo "$imageId"
70+
;;
71+
vsize)
72+
total="$(docker inspect -f '{{printf "%.0f" .VirtualSize}}' "$imageId" 2>/dev/null || true)"
73+
if [ "$total" ]; then
74+
echo "$(human_size $total)"
75+
exit
76+
fi
77+
78+
ancestry=( $(curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/images/$imageId/ancestry" | sed -r 's/^\["|"\]//g; s/",[[:space:]]*"/ /g') )
79+
80+
total=0
81+
for ancestor in "${ancestry[@]}"; do
82+
size="$(curl -sSL -H "Authorization: Token $token" "https://registry-1.docker.io/v1/images/$ancestor/json" | awk -F '"Size":[[:space:]]*' '{ sub(/(,|}).*/, "", $2); if ($2 == "") { print 0 } else { print $2 } }')"
83+
total="$(echo "$total + $size" | bc)"
84+
if [ "$verbose" ]; then
85+
echo "${ancestor:0:12}: $(human_size $size)"
86+
fi
87+
done
88+
if [ "$verbose" ]; then
89+
echo -n 'total: '
90+
fi
91+
echo "$(human_size $total)"
92+
;;
93+
*)
94+
echo >&2 "unknown data type requested: $data"
95+
exit 1
96+
;;
97+
esac

0 commit comments

Comments
 (0)