2015-11-19 13:11:17 +01:00
|
|
|
# Reference: docker src contrib/download-frozen-image.sh
|
|
|
|
|
|
|
|
source $stdenv/setup
|
|
|
|
|
|
|
|
# Curl flags to handle redirects, not use EPSV, handle cookies for
|
|
|
|
# servers to need them during redirects, and work on SSL without a
|
|
|
|
# certificate (this isn't a security problem because we check the
|
|
|
|
# cryptographic hash of the output anyway).
|
2016-01-27 20:48:04 +01:00
|
|
|
curl=$(command -v curl)
|
|
|
|
curl() {
|
|
|
|
[[ -n ${token:-} ]] && set -- -H "Authorization: Token $token" "$@"
|
|
|
|
$curl \
|
|
|
|
--location --max-redirs 20 \
|
|
|
|
--retry 3 \
|
|
|
|
--fail \
|
|
|
|
--disable-epsv \
|
|
|
|
--cookie-jar cookies \
|
|
|
|
--insecure \
|
|
|
|
$curlOpts \
|
|
|
|
$NIX_CURL_FLAGS \
|
|
|
|
"$@"
|
|
|
|
}
|
2015-11-19 13:11:17 +01:00
|
|
|
|
|
|
|
fetchLayer() {
|
|
|
|
local url="$1"
|
|
|
|
local dest="$2"
|
|
|
|
local curlexit=18;
|
|
|
|
|
|
|
|
# if we get error code 18, resume partial download
|
|
|
|
while [ $curlexit -eq 18 ]; do
|
|
|
|
# keep this inside an if statement, since on failure it doesn't abort the script
|
2016-01-27 20:48:04 +01:00
|
|
|
if curl -C - "$url" --output "$dest"; then
|
2015-11-19 13:11:17 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
curlexit=$?;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
return $curlexit
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:48:04 +01:00
|
|
|
headers=$(curl -o /dev/null -D- -H 'X-Docker-Token: true' \
|
|
|
|
"$indexUrl/$registryVersion/repositories/$imageName/images")
|
|
|
|
|
|
|
|
header() {
|
|
|
|
grep $1 <<< "$headers" | tr -d '\r' | cut -d ' ' -f 2
|
|
|
|
}
|
2015-11-19 13:11:17 +01:00
|
|
|
|
2016-01-27 20:48:04 +01:00
|
|
|
# this only takes the first endpoint, more may be provided
|
|
|
|
# https://docs.docker.com/v1.6/reference/api/docker-io_api/
|
|
|
|
if ! registryUrl=$(header X-Docker-Endpoints); then
|
|
|
|
echo "error: index returned no endpoint"
|
|
|
|
exit 1
|
2015-11-19 13:11:17 +01:00
|
|
|
fi
|
2016-01-27 20:48:04 +01:00
|
|
|
baseUrl="https://$registryUrl/$registryVersion"
|
2015-11-19 13:11:17 +01:00
|
|
|
|
2016-01-27 20:48:04 +01:00
|
|
|
token="$(header X-Docker-Token || true)";
|
2015-11-19 13:11:17 +01:00
|
|
|
|
|
|
|
if [ -z "$imageId" ]; then
|
2016-01-27 20:48:04 +01:00
|
|
|
imageId="$(curl "$baseUrl/repositories/$imageName/tags/$imageTag")"
|
2015-11-19 13:11:17 +01:00
|
|
|
imageId="${imageId//\"/}"
|
|
|
|
if [ -z "$imageId" ]; then
|
|
|
|
echo "error: no image ID found for ${imageName}:${imageTag}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "found image ${imageName}:${imageTag}@$imageId"
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
jshon -n object \
|
|
|
|
-n object -s "$imageId" -i "$imageTag" \
|
|
|
|
-i "$imageName" > $out/repositories
|
|
|
|
|
2016-01-27 20:48:04 +01:00
|
|
|
curl "$baseUrl/images/$imageId/ancestry" -o ancestry.json
|
2015-11-19 13:11:17 +01:00
|
|
|
|
|
|
|
layerIds=$(jshon -a -u < ancestry.json)
|
|
|
|
for layerId in $layerIds; do
|
|
|
|
echo "fetching layer $layerId"
|
|
|
|
|
|
|
|
mkdir "$out/$layerId"
|
|
|
|
echo '1.0' > "$out/$layerId/VERSION"
|
2016-01-27 20:48:04 +01:00
|
|
|
curl "$baseUrl/images/$layerId/json" | python $detjson > "$out/$layerId/json"
|
2015-11-19 13:11:17 +01:00
|
|
|
fetchLayer "$baseUrl/images/$layerId/layer" "$out/$layerId/layer.tar"
|
2016-01-27 20:48:04 +01:00
|
|
|
done
|