-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure-docker-proxy
More file actions
executable file
·265 lines (219 loc) · 4.64 KB
/
configure-docker-proxy
File metadata and controls
executable file
·265 lines (219 loc) · 4.64 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/sh -eu
# configure-docker-proxy: configure Docker proxy settings
# Copyright (C) 2021 "Michael G. Morey" <mgmorey@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
INPUT=-
OUTPUT=/etc/systemd/system/docker.service.d/http-proxy.conf
PROXY_PARAMETER_RE='^(HTTP|NO)_PROXY='
abort() {
printf "$@" >&2
exit 1
}
assert() {
"$@" || abort '%s: Assertion failed: %s\n' "$script" "$*"
}
clean_up() {
eval rm -f ${tmpfiles-}
}
configure_docker_proxy()
{
if [ "$action" = query -o "$(id -u)" -eq 0 ]; then
output=$OUTPUT
else
output=-
fi
eval ${action}_proxy
}
create_tmpfile() {
trap clean_up EXIT INT QUIT TERM
tmpfile=$(mktemp "$@")
assert [ -n "${tmpfile}" ]
tmpfiles="${tmpfiles+$tmpfiles }'$tmpfile'"
}
filter_proxy_parameters() {
grep -E "$PROXY_PARAMETER_RE"
}
install_file() {
if mkdir -p "$(dirname "$2")"; then
if chmod a+r $"$1"; then
/bin/mv -f $"$1" "$2"
fi
fi
}
install_proxy() {
create_tmpfile
read_proxy_parameters | \
filter_proxy_parameters | \
print_proxy_configuration >$tmpfile
if [ "$(wc -l <$tmpfile)" -le 1 ]; then
return 0
fi
case "$output" in
(-)
cat $tmpfile
;;
(*)
if install_file $tmpfile "$output"; then
restart_daemon
fi
;;
esac
}
is_valid_input_file() {
case "$1" in
(-)
true
;;
(*)
test -f "$1" -a -r "$1"
;;
esac
}
parse_arguments() {
action=install
input=$INPUT
no_proxy=
proxy=
while getopts i:n:o:rqh opt; do
case $opt in
(i)
parse_input_file "$OPTARG"
;;
(n)
no_proxy="$OPTARG"
;;
(o)
output="$OPTARG"
;;
(q)
action=query
;;
(r)
action=remove
;;
(h)
usage
exit 0
;;
(\?)
exit 2
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -gt 0 ]; then
proxy="$1"
shift
fi
if [ $# -gt 0 ]; then
usage_error '%s: Too many arguments\n' "$script"
fi
}
parse_input_file() {
if is_valid_input_file "$1"; then
input="$1"
else
usage_error '%s: %s: No such file or no permissions\n' "$script" "$1"
fi
}
print_prompt() {
cat <<EOF >&2
Enter proxy parameters, one per line.
Type ^D to submit or ^C to quit:
EOF
}
print_proxy_configuration() {
printf '[%s]\n' "Service"
while read parameter; do
printf 'Environment="%s"\n' "$parameter"
done
}
query_proxy() {
docker info | grep -E '^(Http|Https|No) Proxy'
}
read_proxy_parameters() {
if [ -n "${proxy-}" ]; then
"${script_prefix}generate-proxy-parameters" \
${no_proxy:+-n "$no_proxy"} \
"$proxy"
return 0
fi
case "$input" in
(-)
print_prompt
cat
;;
(*)
cat "$input"
;;
esac
}
remove_proxy() {
if [ "$output" != - ]; then
if /bin/rm -f "$output"; then
restart_daemon
fi
fi
}
restart_daemon() {
if which systemctl >/dev/null 2>&1; then
if systemctl daemon-reload; then
if systemctl status docker >/dev/null; then
systemctl restart docker
fi
fi
fi
}
usage() {
cat <<EOF >&2
Usage: $script [-n NO-PROXY] [-o OUTPUT] PROXY-URL
$script [-i INPUT] [-o OUTPUT]
$script -q [-o OUTPUT]
$script -r [-o OUTPUT]
$script -h
Configure Docker Engine HTTP/HTTPS proxy parameters.
-h print this help text (all other options are ignored)
-i INPUT read parameters from file INPUT
-n NO-PROXY interpret NO-PROXY as a comma-separated list of exceptions
-o OUTPUT write to configuration file OUTPUT
(default value is $OUTPUT)
-q query proxy configuration
-r remove query configuration
Example input parameters:
HTTP_PROXY=http://proxy.com:80
NO_PROXY=localhost,127.0.0.0/8
Example NO-PROXY:
localhost,127.0.0.0/8
Example PROXY-URL:
http://proxy.com:80
EOF
}
usage_error() {
if [ $# -gt 0 ]; then
printf "$@" >&2
fi
printf '%s\n' '' >&2
usage
exit 2
}
script=$(basename "$0")
case "$0" in
(*/*)
script_dir=$(cd "$(dirname "$0")" && pwd)
;;
(*)
script_dir=
;;
esac
script_prefix=${script_dir:+$script_dir/}
parse_arguments "$@"
configure_docker_proxy