Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1#! /usr/bin/env bash
2
3set -e -o pipefail
4
5os=
6arch=
7imageName=
8imageTag=
9imageDigest=
10finalImageName=
11finalImageTag=
12hashType=$NIX_HASH_ALGO
13hashFormat=$hashFormat
14format=nix
15
16usage(){
17 echo >&2 "syntax: nix-prefetch-docker [options] [IMAGE_NAME [IMAGE_TAG|IMAGE_DIGEST]]
18
19Options:
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
33get_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 --override-os "${os}" --override-arch "${arch}" --insecure-policy --tmpdir=$TMPDIR inspect "docker://$imageName:$imageTag" | jq '.Digest' -r
42}
43
44get_name() {
45 local imageName=$1
46 local imageTag=$2
47
48 echo "docker-image-$(echo "$imageName:$imageTag" | tr '/:' '-').tar"
49}
50
51argi=0
52argfun=""
53for 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
84done
85
86if test -z "$imageName"; then
87 usage
88fi
89
90if test -z "$os"; then
91 os=linux
92fi
93
94if test -z "$arch"; then
95 arch=amd64
96fi
97
98if test -z "$hashType"; then
99 hashType=sha256
100fi
101
102if test -z "$hashFormat"; then
103 hashFormat=base32
104fi
105
106if test -z "$finalImageName"; then
107 finalImageName="$imageName"
108fi
109
110if test -z "$finalImageTag"; then
111 if test -z "$imageTag"; then
112 finalImageTag="latest"
113 else
114 finalImageTag="$imageTag"
115 fi
116fi
117
118if test -z "$imageDigest"; then
119 imageDigest=$(get_image_digest $imageName $imageTag)
120fi
121
122sourceUrl="docker://$imageName@$imageDigest"
123
124# nix>=2.20 rejects adding symlinked paths to the store, so use realpath
125# to resolve to a physical path. https://github.com/NixOS/nix/issues/11941
126tmpPath="$(realpath "$(mktemp -d --tmpdir skopeo-copy-tmp-XXXXXXXX)")"
127trap "rm -rf \"$tmpPath\"" EXIT
128
129tmpFile="$tmpPath/$(get_name $finalImageName $finalImageTag)"
130
131if test -z "$QUIET"; then
132 skopeo --insecure-policy --tmpdir=$TMPDIR --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag" >&2
133else
134 skopeo --insecure-policy --tmpdir=$TMPDIR --override-os ${os} --override-arch ${arch} copy "$sourceUrl" "docker-archive://$tmpFile:$finalImageName:$finalImageTag" > /dev/null
135fi
136
137# Compute the hash.
138imageHash=$(nix-hash --flat --type $hashType --sri "$tmpFile")
139
140# Add the downloaded file to Nix store.
141finalPath=$(nix-store --add-fixed "$hashType" "$tmpFile")
142
143if test -z "$QUIET"; then
144 echo "-> ImageName: $imageName" >&2
145 echo "-> ImageDigest: $imageDigest" >&2
146 echo "-> FinalImageName: $finalImageName" >&2
147 echo "-> FinalImageTag: $finalImageTag" >&2
148 echo "-> ImagePath: $finalPath" >&2
149 echo "-> ImageHash: $imageHash" >&2
150fi
151
152if [ "$format" == "nix" ]; then
153cat <<EOF
154{
155 imageName = "$imageName";
156 imageDigest = "$imageDigest";
157 hash = "$imageHash";
158 finalImageName = "$finalImageName";
159 finalImageTag = "$finalImageTag";
160}
161EOF
162
163else
164
165cat <<EOF
166{
167 "imageName": "$imageName",
168 "imageDigest": "$imageDigest",
169 "hash": "$imageHash",
170 "finalImageName": "$finalImageName",
171 "finalImageTag": "$finalImageTag"
172}
173EOF
174
175fi