-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevpush.sh
More file actions
executable file
·171 lines (153 loc) · 5.31 KB
/
devpush.sh
File metadata and controls
executable file
·171 lines (153 loc) · 5.31 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
set -o errexit
# set -x
FORCE=no
RESINOS_REGISTRY="registry.hub.docker.com"
# input: device type and os verison docker hub repository
function usage()
{
echo "usage: devpush.sh -d devicetype -v version -r repository -s imagefolder [--force]"
}
function urlencode() {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "%s" "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
function image_exists() {
# Try to fetch the manifest of a repo:tag combo, to check for the existence of that
# repo and tag.
# Currently only works with v2 registries
# The return value is "no" if can't access that manifest, and "yes" if we can find it
local REGISTRY=$1
local REPO=$2
local TAG=$3
local exists=no
local REGISTRY_URL="https://${REGISTRY}/v2"
local MANIFEST="${REGISTRY_URL}/${REPO}/manifests/${TAG}"
local response
# Check
response=$(curl --retry 10 --write-out "%{http_code}" --silent --output /dev/null "${MANIFEST}")
if [ "$response" = 401 ]; then
# 401 is "Unauthorized", have to grab the access tokens from the provided endpoint
local auth_header
local realm
local service
local scope
local token
local response_auth
auth_header=$(curl --retry 10 -I --silent "${MANIFEST}" |grep -i www-authenticate)
# The auth_header looks as
# Www-Authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:resin/resinos:pull"
# shellcheck disable=SC2001
realm=$(echo "$auth_header" | sed 's/.*realm="\([^,]*\)",.*/\1/' )
# shellcheck disable=SC2001
service=$(echo "$auth_header" | sed 's/.*,service="\([^,]*\)",.*/\1/' )
# shellcheck disable=SC2001
scope=$(echo "$auth_header" | sed 's/.*,scope="\([^,]*\)".*/\1/' )
# Grab the token from the appropriate address, and retry the manifest query with that
token=$(curl --retry 10 --silent "${realm}?service=${service}&scope=${scope}" | jq -r '.access_token // .token')
response_auth=$(curl --retry 10 --write-out "%{http_code}" --silent --output /dev/null -H "Authorization: Bearer ${token}" "${MANIFEST}")
if [ "$response_auth" = 200 ]; then
exists=yes
fi
elif [ "$response" = 200 ]; then
exists=yes
fi
echo "${exists}"
}
while [ "$1" != "" ]; do
case $1 in
'-d' | '--device-type' )
shift
deviceType=$1
;;
'-v' | '--version' )
shift
version=$1
;;
'-r' | '--repository' )
shift
repository=$1
;;
'-s' | '--storage' )
shift
storage=$1
;;
'-f' | '--force' )
FORCE=yes
;;
* )
usage
exit 1
esac
shift
done
missingparams="no"
if [ -z "${deviceType}" ] ; then
echo "Please set the target device type with '-d' or '--device-type', such as raspberrypi3"
missingparams="yes"
fi
if [ -z "${version}" ] ; then
echo "Please set the OS version with '-v' or '--version', such as 2.38.0+rev1"
missingparams="yes"
fi
if [ -z "${repository}" ] ; then
echo "Please set the repository with '-r' or '--repository', such as 'resin/resinos'"
missingparams="yes"
fi
if [ -z "${storage}" ] ; then
echo "Please set S3 domain with '-s' or '--storage', such as https://resin-production-img-cloudformation.s3.amazonaws.com/images/"
missingparams="yes"
fi
if [ "${missingparams}" != "no" ] ; then
exit 1
fi
imageTag="$(echo "$version" | tr + _)-${deviceType}"
imageName="${repository}:${imageTag}"
echo "Image name: ${imageName}"
# delete image if it exists locally
docker rmi -f "${imageName}" || true
# check if image is already up there
if [ "$(image_exists "$RESINOS_REGISTRY" "$repository" "$imageTag")" = "yes" ]; then
if [ "$FORCE" = "yes" ] ; then
echo "WARN: image already exists in ${repository}, but we are going to overwrite it."
docker rmi -f "${imageName}"
else
echo "Finished: image already exists in ${repository}, nothing to do."
exit
fi
else
echo "Image ${imageName} does not exists yet, good "
fi
# download file from S3
echo "Getting file from s3"
imageFile=$(mktemp -u "balenaos-image.${deviceType-generic}.${version-noversion}.XXXXXX.docker")
url_base="${storage}${deviceType}/$(urlencode "${version}")"
url="${url_base}/resin-image.docker"
result=$(curl --silent -L "${url}" --write-out '%{http_code}' -o "${imageFile}")
if [ "$result" != "200" ]; then
echo "Couldn't download image: http code ${result}"
# Cleanup
rm "${imageFile}" || true
exit 2
fi
result=$(docker load -q -i "${imageFile}") || { echo "Couldn't load file into docker, giving up."; exit 3; }
rm "${imageFile}"
# shellcheck disable=SC2001
loaded_image=$(echo "$result" | sed 's/^Loaded image.* //')
# push to docker hub
docker tag "${loaded_image}" "${imageName}"
docker push "${imageName}"
# cleanup
docker rmi -f "${loaded_image}" "${imageName}" || true
echo "DONE: ${imageName}"
echo "DONE: ${imageName}" >> devpush.log