Repository navigation
Expand file tree
/
Copy pathsetup
More file actions
executable file
·253 lines (220 loc) · 7.22 KB
/
Copy pathsetup
File metadata and controls
executable file
·253 lines (220 loc) · 7.22 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
#!/usr/bin/env sh
echo
set -x
network_exists=1
volume_exists=1
network_name="${NETWORK_NAME:-traefik}"
name="${network_name}"
certs_volume="${network_name}_certs"
fqdn="${FQDN:-dev.localhost}"
proxy_image_name=tobygr/traefik-proxy:latest
whoami_image_name=traefik/whoami
docker network inspect "${network_name}" &>/dev/null || network_exists=0
docker volume inspect "${certs_volume}" &>/dev/null || volume_exists=0
if [ 0 = "${network_exists}" ]; then
set +x
echo "'${network_name}' network does not exists. Creating…"
set -x
docker network create --label "service=${name}-traefik-proxy" "${network_name}" || (echo "Unable to create network" && exit 1)
set +x
echo " Done!"
else
echo "'${network_name}' network already exists."
fi
echo
if [ 0 = "${volume_exists}" ]; then
echo "'${certs_volume}' volume does not exists. Creating…"
set -x
docker volume create --label "service=${name}-traefik-proxy" "${certs_volume}" || (echo "Unable to create certs volume" && exit 1)
echo " Done!"
else
echo "'${certs_volume}' volume already exists."
fi
echo
set -x
project_name="${name}-traefik-proxy"
docker pull "${proxy_image_name}"
docker pull "${whoami_image_name}"
cat <<EOF | docker compose --env-file /dev/null -f /dev/stdin -p "${project_name}" up -d
version: '3'
networks:
public:
external: true
traefik:
external: true
volumes:
certs:
name: ${certs_volume}
external: true
services:
traefik:
image: ${proxy_image_name}
restart: always
environment:
FQDN: ${fqdn}
ports:
- "80:80"
- "443:443"
networks:
- traefik
volumes:
- certs:/opt/traefik/certs
- /var/run/docker.sock:/var/run/docker.sock
labels:
service: ${name}-traefik-proxy
function: reverse-proxy
traefik.enable: true
traefik.docker.network: ${network_name}
traefik.http.routers.traefik.rule: Host(\`${name}.${fqdn}\`)
traefik.http.routers.traefik.tls: true
traefik.http.routers.traefik.entrypoints: https
traefik.http.services.traefik.loadbalancer.server.port: 8080
whoami:
image: ${whoami_image_name}
networks:
- traefik
labels:
service: ${name}-traefik-proxy
function: test-container
traefik.enable: true
traefik.docker.network: ${network_name}
traefik.http.routers.traefik-whoami.rule: Host(\`whoami.${fqdn}\`)
traefik.http.routers.traefik-whoami.tls: true
traefik.http.routers.traefik-whoami.entrypoints: https
traefik.http.services.traefik-whoami.loadbalancer.server.port: 80
EOF
set +x
if [[ 0 -ne $? ]] ; then
echo "Failed to create services"
exit 1
fi
echo
echo "Adding certificate as trustRoot to keychain"
testForCertFile () {
docker compose -p "${project_name}" exec traefik ls -als | grep -q ca.crt
return
}
echo "Waiting for certificate file…"
set -x
limit=15
testForCertFile
while [[ 1 -eq $? && $limit > 0 ]] ; do
sleep 1
limit=$((limit-1))
set +x
echo "${limit} seconds"
set -x
testForCertFile
done
set +x
testForCertFile
if [[ 1 -eq $? ]] ; then
echo "Timeout waiting for certificate file to be created"
exit 1;
else
echo "Certificate file found"
fi
set -x
mkdir /tmp/traefik-proxy || true
cert_file="/tmp/traefik-proxy/ca.crt"
docker compose -p "${project_name}" exec traefik cat /opt/traefik/certs/ca.crt > "${cert_file}"
set +x
if [[ 0 -ne $? ]] ; then
echo "Unable to fetch certificate file"
exit 1
fi
set -x
case "$(uname -s)" in
Linux*)
if [[ -d "/etc/pki/ca-trust/source/anchors" ]] ; then
system_trust_certificate_directory="/etc/pki/ca-trust/source/anchors/"
system_trust_store_update_command="update-ca-trust extract"
elif [[ -d "/usr/local/share/ca-certificates" ]] ; then
system_trust_certificate_directory="/usr/local/share/ca-certificates/"
system_trust_store_update_command="update-ca-certificates"
elif [[ -d "/etc/ca-certificates/trust-source/anchors/" ]] ; then
system_trust_certificate_directory="/etc/ca-certificates/trust-source/anchors/"
system_trust_store_update_command="trust extract-compat"
elif [[ -d "/usr/share/pki/trust/anchors" ]] ; then
system_trust_certificate_directory="/usr/share/pki/trust/anchors/"
system_trust_store_update_command="update-ca-certificates"
fi
set +x
echo "${system_trust_certificate_directory?'Unknown platform type'}" > /dev/null || exit 1
set -x
org_name=$(docker compose -p "${project_name}" exec traefik sh -c "openssl x509 -in /opt/traefik/certs/ca.crt -noout -subject -nameopt multiline | grep organizationName | sed -n 's/ *organizationName *= //p'")
cert_filename="${org_name}.crt"
sudo cp "${cert_file}" "${system_trust_certificate_directory}/${cert_filename}" || (echo "Unable to copy certificate to trust store" && exit 1)
sudo ${system_trust_store_update_command} || (echo "Unable to udpate trust store" && exit 1)
;;
Darwin*)
# This has to install to the System keychain, as trying to install to the
# user's login still requires an admin password, and then appears to
# install it in a way that is inaccessible to the user, so doesn't work.
sudo security add-trusted-cert \
-d \
-r trustRoot \
-k /Library/Keychains/System.keychain \
"${cert_file}"
if [[ 0 -ne $? ]] ; then
echo "Unable to install certificate"
exit 1
fi
;;
*)
set +x
echo "Unsupported platform. Cannot install trustRoot"
exit 1
esac
rm -Rf /tmp/traefik-proxy
set +x
echo
echo "Traefik proxy setup"
echo
echo "To view the dashboard, open https://traefik.dev.localhost"
echo
echo "To proxy an image add the container to the \`${network_name}\` network, and add the minimal"
echo "labels…"
echo
set -x
cat <<TEXT
services
nginx:
networks:
- ${network_name}
# + any other networks
labels:
traefik.enable: true
traefik.docker.network: ${network_name}
traefik.http.routers.traefik.rule: Host(\`whoami.${fqdn}\`)
traefik.http.routers.traefik.tls: true
traefik.http.routers.traefik.entrypoints: https
TEXT
set +x
echo
echo "See https://doc.traefik.io/traefik/ for details of the Traefik configuration."
echo
echo "See https://github.com/traefik/traefik-library-image for details of the underlying Docker image."
echo
echo "To list resources…"
echo
echo " # Containers…"
echo " $ docker ps --filter \"label=service=${name}-traefik-proxy\""
echo " # Networks…"
echo " $ docker network ls --filter \"label=service=${name}-traefik-proxy\""
echo " # Volumes…"
echo " $ docker volume ls --filter \"label=service=${name}-traefik-proxy\""
echo
echo
echo "To clean things up afterwards, run…"
echo
echo " # Remove the proxy containers…"
echo " $ docker compose -p "${project_name}" down"
echo " # If you want to drop the certificates volume as well…"
echo " $ docker compose -p "${project_name}" down --volumes"
echo " # remove the network"
echo " $ docker network rm ${network_name}"
echo " # remove the certificates volume"
echo " $ docker volume rm ${certs_volume}"
echo " # remove the images"
echo " $ docker rmi ${proxy_image_name} ${whoami_image_name}"