@@ -0,0 +1,173 @@
1+#! /usr/bin/env bash
2+
3+set -e -o pipefail
4+
5+os=
6+arch=
7+imageName=
8+imageTag=
9+imageDigest=
10+finalImageName=
11+finalImageTag=
12+hashType=$NIX_HASH_ALGO
13+hashFormat=$hashFormat
14+format=nix
15+
16+usage(){
17+ echo >&2 "syntax: nix-prefetch-docker [options] [IMAGE_NAME [IMAGE_TAG|IMAGE_DIGEST]]
18+
19+Options:
20+ --os os OS to fetch image for
21+ --arch linux Arch to fetch image for
22+ --image-name name Name of the image to fetch
23+ --image-tag tag Image tag
24+ --image-digest digest Image digest
25+ --final-image-name name Desired name of the image
26+ --final-image-tag tag Desired image tag
27+ --json Output result in json format instead of nix
28+ --quiet Only print the final result
29+"
30+ exit 1
31+}
32+
33+get_image_digest(){
34+ local imageName=$1
35+ local imageTag=$2
36+
37+ if test -z "$imageTag"; then
38+ imageTag="latest"
39+ fi
40+
41+ skopeo inspect "docker://$imageName:$imageTag" | jq '.Digest' -r
42+}
43+
44+get_name() {
45+ local imageName=$1
46+ local imageTag=$2
47+
48+ echo "docker-image-$(echo "$imageName:$imageTag" | tr '/:' '-').tar"
49+}
50+
51+argi=0
52+argfun=""
53+for arg; do
54+ if test -z "$argfun"; then
55+ case $arg in
56+ --os) argfun=set_os;;
57+ --arch) argfun=set_arch;;
58+ --image-name) argfun=set_imageName;;
59+ --image-tag) argfun=set_imageTag;;
60+ --image-digest) argfun=set_imageDigest;;
61+ --final-image-name) argfun=set_finalImageName;;
62+ --final-image-tag) argfun=set_finalImageTag;;
63+ --quiet) QUIET=true;;
64+ --json) format=json;;
65+ --help) usage; exit;;
66+ *)
67+ : $((++argi))
68+ case $argi in
69+ 1) imageName=$arg;;
70+ 2) [[ $arg == *"sha256"* ]] && imageDigest=$arg || imageTag=$arg;;
71+ *) exit 1;;
72+ esac
73+ ;;
74+ esac
75+ else
76+ case $argfun in
77+ set_*)
78+ var=${argfun#set_}
79+ eval $var=$arg
80+ ;;
81+ esac
82+ argfun=""
83+ fi
84+done
85+
86+if test -z "$imageName"; then
87+ usage
88+fi
89+
90+if test -z "$os"; then
91+ os=linux
92+fi
93+
94+if test -z "$arch"; then
95+ arch=amd64
96+fi
97+
98+if test -z "$hashType"; then
99+ hashType=sha256
100+fi
101+
102+if test -z "$hashFormat"; then
103+ hashFormat=base32
104+fi
105+
106+if test -z "$finalImageName"; then
107+ finalImageName="$imageName"
108+fi
109+
110+if test -z "$finalImageTag"; then
111+ if test -z "$imageTag"; then
112+ finalImageTag="latest"
113+ else
114+ finalImageTag="$imageTag"
115+ fi
116+fi
117+
118+if test -z "$imageDigest"; then
119+ imageDigest=$(get_image_digest $imageName $imageTag)
120+fi
121+
122+sourceUrl="docker://$imageName@$imageDigest"
123+
124+tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/skopeo-copy-tmp-XXXXXXXX")"
125+trap "rm -rf \"$tmpPath\"" EXIT
126+
127+tmpFile="$tmpPath/$(get_name $finalImageName $finalImageTag)"
128+
129+if test -z "$QUIET"; then
130+ skopeo --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag"
131+else
132+ skopeo --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag" > /dev/null
133+fi
134+
135+# Compute the hash.
136+imageHash=$(nix-hash --flat --type $hashType --base32 "$tmpFile")
137+
138+# Add the downloaded file to Nix store.
139+finalPath=$(nix-store --add-fixed "$hashType" "$tmpFile")
140+
141+if test -z "$QUIET"; then
142+ echo "-> ImageName: $imageName"
143+ echo "-> ImageDigest: $imageDigest"
144+ echo "-> FinalImageName: $finalImageName"
145+ echo "-> FinalImageTag: $finalImageTag"
146+ echo "-> ImagePath: $finalPath"
147+ echo "-> ImageHash: $imageHash"
148+fi
149+
150+if [ "$format" == "nix" ]; then
151+cat <<EOF
152+{
153+ imageName = "$imageName";
154+ imageDigest = "$imageDigest";
155+ sha256 = "$imageHash";
156+ finalImageName = "$finalImageName";
157+ finalImageTag = "$finalImageTag";
158+}
159+EOF
160+
161+else
162+
163+cat <<EOF
164+{
165+ "imageName": "$imageName",
166+ "imageDigest": "$imageDigest",
167+ "sha256": "$imageHash",
168+ "finalImageName": "$finalImageName",
169+ "finalImageTag": "$finalImageTag"
170+}
171+EOF
172+
173+fi