-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpull
executable file
·77 lines (54 loc) · 1.6 KB
/
pull
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
#! /bin/sh
case "$1" in
( -h | --help )
printf "%s\n" \
"pull: Fetch an image from DockerHub into a directory." \
"" \
"Usage:" \
" pull [-h|--help] Show this help." \
" pull dir img Fetch img into dir."
exit;;
esac
# Functions.
err () { printf "${0##*/}: \e[31mError:\e[0m %s\n" "$*" >&2; exit 1; }
_get_manifest () {
curl -f -s \
-H "Authorization: Bearer $3" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"https://index.docker.io/v2/$1/manifests/$2"
}
_pull_layer () {
echo "Downloading '$1'."
curl -f -# -OL \
-H "Authorization: Bearer $3" \
-H "Accept: application/vnd.docker.image.rootfs.diff.tar.gzip" \
"https://index.docker.io/v2/$2/blobs/$1"
}
_pull_layers () {
jq -r .layers[].digest | while read d; do
_pull_layer "$d" "$1" "$2"
done
}
_pull () {
cd "$1"
_get_manifest "$2" "$3" "$4" | _pull_layers "$2" "$4"
for f in *; do
tar --force-local -xf "$f"
rm "$f"
done
}
# Process the request.
test $# -eq 2 ||
err "wrong number of arguments."
tag="${2##*:}"
img="${2%%:*}"
owner="${img%%/*}"
out="$1"
test "$img" ||
err "No image provided."
mkdir -p "$out"
test -z "$tag" -o "$tag" = "$img" && tag=latest
test -z "$owner" -o "$owner" = "$img" && img="library/$img"
echo "Pulling $img:$tag..."
until token=$(curl -sf "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$img:pull" | jq -r .token); do :; done
_pull "$out" "$img" "$tag" "$token"