-
Notifications
You must be signed in to change notification settings - Fork 10
/
docker-pushmi-pullyu
executable file
·172 lines (150 loc) · 4.03 KB
/
docker-pushmi-pullyu
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
172
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
trap 'echo "$0: line $LINENO: exit status of last command: $?" >&2' ERR
trap 'cleanup' EXIT
usage() {
echo "
Usage: $0 [OPTIONS] [USER@]HOST NAME[:TAG] [NAME[:TAG]...]
Push images directly to a remote host (without a separate registry)
Options:
-s, --ssh-opts string Specify additional ssh arguments (e.g. --ssh-opts \"-i private.pem -C\")
--no-cache Do not use a cache for local registry images"
}
cleanup() {
if [ -n "${registry_container_name-}" ] && docker ps -q -f name="$registry_container_name"
then
echo "Killing the local registry..."
docker kill "$registry_container_name" >/dev/null || true
docker rm "$registry_container_name" >/dev/null || true
fi
}
flag-error() {
local message="$1"
echo "$message" >&2
usage >&2
exit 125
}
argument-error() {
local message="$1"
echo "$message" >&2
usage >&2
exit 1
}
wait-for() {
local max_wait_seconds=5
local start_time="$SECONDS"
local exit_code
while true
do
# Run the command being retried and capture the error code. This
# awkward incantation ensures correct behavior even if the errexit
# option is set.
"$@" && exit_code="$?" || exit_code="$?"
if [ "$exit_code" -eq 0 ] || [ $((SECONDS - start_time)) -ge "$max_wait_seconds" ]
then
break
else
sleep 0.1
fi
done
return "$exit_code"
}
check-registry() {
local registry="$1"
# Check if the registry is up.
docker login --username="AzureDiamond" --password="hunter2" "$registry" >/dev/null 2>&1
}
use_cache=true
args=""
while [ -n "${1+x}" ]
do
case "$1" in
-s|--ssh-opts|--ssh-opts=*)
ssh_opts="${1##--ssh-opts=}"
if [ "$ssh_opts" = "$1" ]
then
if [ "$#" -ge 2 ]
then
ssh_opts="$2"
shift
else
ssh_opts=""
fi
fi
if [ -z "$ssh_opts" ]
then
flag-error "flag needs an argument: $1"
fi
;;
--no-cache)
use_cache=false
;;
-*)
flag-error "unknown flag: $1"
;;
*)
args="$args $1"
;;
esac
shift
done
eval set -- "$args"
if [ "$#" -lt 2 ]
then
argument-error "\"$0\" requires at least 2 arguments."
fi
deploy_target="$1"
shift
image_names="$*"
# We need to connect via 127.0.0.1 specifically, otherwise docker tries to use TLS.
registry_host="127.0.0.1"
if [ "$use_cache" = true ]
then
echo "Running a local registry..."
docker volume create docker-pushmi-pullyu
registry_container_name=$(
docker run \
--detach \
--volume="docker-pushmi-pullyu:/var/lib/registry" \
--publish-all \
registry:2
)
else
echo "Running a local registry without using an image cache..."
registry_container_name=$(
docker run \
--detach \
--publish-all \
registry:2
)
fi
registry_port=$(
docker inspect "$registry_container_name" --format='
{{- range $entry := (index .NetworkSettings.Ports "5000/tcp") -}}
{{- if eq $entry.HostIp "0.0.0.0" -}}
{{- $entry.HostPort -}}
{{- end -}}
{{- end -}}
'
)
wait-for check-registry "$registry_host:$registry_port"
echo "The local registry is now usable at $registry_host:$registry_port."
for source_image_name in $image_names
do
echo "Pushing $source_image_name to $registry_host:$registry_port..."
docker tag "$source_image_name" "$registry_host:$registry_port/$source_image_name"
docker push "$registry_host:$registry_port/$source_image_name"
docker rmi "$registry_host:$registry_port/$source_image_name"
done
# shellcheck disable=SC2086,SC2087
ssh -R "$registry_port:$registry_host:$registry_port" ${ssh_opts:-} "$deploy_target" sh <<EOF
for target_image_name in $image_names
do
echo "Pulling \$target_image_name onto $deploy_target from $registry_host:$registry_port..."
docker pull "$registry_host:$registry_port/\$target_image_name" \
&& docker tag "$registry_host:$registry_port/\$target_image_name" "\$target_image_name" \
&& docker rmi "$registry_host:$registry_port/\$target_image_name"
done
EOF