-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngc2oci
executable file
·111 lines (94 loc) · 3.2 KB
/
ngc2oci
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
#
# ngc2oci - Simple shell tool to pull from Nvidia Cloud and create an OCI image.
# Copyright (C) 2018 Vanessa Sochat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
function help() {
cat <<EOF
Tool to pull an image from an Nvidia Cloud registry and extract the contents
to a folder to be used as OCI image.
Example usage:
$0 --repo tensorflow --image tensorflow --tag 17.10 --oauthtoken $token
EOF
exit 1
}
# Ensure functions available
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -f "${HERE}/dockerhub2oci" ]; then
echo "Cannot find dockerhub2oci, exiting."
exit 1
fi
# Defaults
registry="nvcr.io"
repo="nvidia"
oauthtoken="unset"
image="tensorflow"
tag="17.10"
cmd=""
cachedir=${TMPDIR:-/tmp}/ngc2oci
writable_dirs=false
TEMP=$(getopt -o h --long repo:,image:,tag:,oauthtoken:,cachedir:,writable-dirs,help -n $0 -- "$@")
if [ $? != 0 ] ; then usage; exit 1; fi
eval set -- "$TEMP"
while true ; do
case "$1" in
--repo)
repo=$2;
shift 2;
;;
--image)
image=$2;
shift 2;
;;
--tag)
tag=$2;
shift 2;
;;
--cachedir)
cachedir=$2;
shift 2;
cmd="$cmd --cachedir $cachedir";
;;
--oauthtoken)
oauthtoken=$2;
shift 2;
;;
--writable-dirs)
writable_dirs=true;
shift 1;
cmd="$cmd --writable_dirs";
;;
-h|--help) help; exit 0;;
--) shift; break;;
*) echo "Internal error!"; exit 1;;
esac
done
# Put command together to pass on to dockerhub2oci
cmd="--registry nvcr.io $cmd --repo $repo --image $image --tag $tag --strip-headers";
img_full="${repo}/${image}"
auth_uri="https://authn.nvidia.com/token"
auth_uri_full="${auth_uri}?service=registry&expires_in=900&scope=repository:${img_full}:pull"
# If nvidia is desired, a --token is required!
if [ "${oauthtoken}" == "unset" ]; then
echo "You must provide an Nvidia Cloud token with --token"
exit 1
fi
# The nvidia cloud token still needs to be converted to base64
token=$(echo -n "\$oauthtoken:$oauthtoken" | base64 -w 0 | tr -d " ")
token=$(curl -s -H "Authorization: Basic ${token}" -H "Content-Type: application/json; charset=utf-8" -H "Accept: application/json" ${auth_uri_full} | jq -r .token)
# Then call dockerhub2oci
echo $cmd
export DOCKERHUB2OCI_TOKEN=$token
exec ${HERE}/dockerhub2oci $cmd