This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 574
/
compose
executable file
·371 lines (328 loc) · 12.8 KB
/
compose
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash -e
SUB_CMD_NAME="compose"
cmd_desc() {
echo "For running Docker Compose related commands"
}
cmd_usage() {
echo "usage: ${CMD_NAME} ${SUB_CMD_NAME} [<options>] <subcommand>"
echo "Options:"
printf " %-12s %s\n" "-m <name>" "The name of the Docker Machine to target"
printf " %-12s %s\n" "-f <path>" "Additional override file for Docker Compose, can be specified more than once"
printf " %-12s %s\n" "-F <path>" "File to use for Docker Compose (in place of default), can be specified more than once"
printf " %-12s %s\n" "-l <driver>" "The logging driver to use, specify stdout to prevent the use of one"
printf " %-12s %s\n" "-v <driver>" "The volume driver to use"
printf " %-12s %s\n" "-n <name>" "The custom network to create (if not present) and use"
printf " %-12s %s\n" "-i <ip>" "The public IP that the proxy will be accessed from (only required when not using Docker Machine)"
}
help() {
cmd_usage
echo
echo "Available subcommands are:"
printf " %-22s %s\n" "init" "Initialises ADOP"
printf " %-22s %s\n" "init <--without-load>" "Initialises ADOP without loading the platform"
printf " %-22s %s\n" "init <--without-pull>" "Initialises ADOP without pulling images"
printf " %-22s %s\n" "init <--with-stdout>" "Initialises ADOP with logs being sent to stdout, overriding any specified logging driver"
printf " %-22s %s\n" "up" "docker-compose up for ADOP"
printf " %-22s %s\n" "gen-certs <path>" "Generate client certificates for TLS-enabled Machine and copy to <path> in Jenkins Slave"
printf " %-22s %s\n" "<command>" "Runs 'docker-compose <command>' for ADOP, where <command> is not listed above"
printf " %-22s %s\n" "help" "Prints this help information"
echo
}
pretty_sleep() {
secs=${1:-60}
tool=${2:-service}
while [ $secs -gt 0 ]; do
echo -ne "$tool unavailable, sleeping for: $secs\033[0Ks\r"
sleep 1
: $((secs--))
done
echo "$tool was unavailable, so slept for: ${1:-60} secs"
}
prep_env() {
# If the proxy IP has not been set work out the TARGET_HOST
# Else just use it
if [ -z "${PROXY_IP}" ]; then
# If MACHINE_NAME is not the default or it is and it exists
# Else fall back to localhost
if [ "${MACHINE_NAME}" != "${DEFAULT_MACHINE_NAME}" ] || ([ "${MACHINE_NAME}" = "${DEFAULT_MACHINE_NAME}" ] && $(docker-machine ip ${MACHINE_NAME} > /dev/null 2>&1) ); then
# Check the machine exists first
if ! $(docker-machine ip ${MACHINE_NAME} > /dev/null 2>&1) ; then
echo "The specified Docker Machine does not exist: ${MACHINE_NAME}"
echo "HINT: Either specify one with 'adop compose -m <name>', or use 'eval \$(docker-machine env ${MACHINE_NAME})'"
exit 1
else
# Set the machine
eval $(docker-machine env $MACHINE_NAME --shell bash)
export TARGET_HOST=$(docker-machine ip $MACHINE_NAME)
export LOGSTASH_HOST=$(docker-machine ip $MACHINE_NAME)
fi
else
echo "Docker Machine is not available or the default machine does not exist, defaulting to localhost"
echo "HINT: Either specify one with 'adop compose -m <name>', or use 'eval \$(docker-machine env ${MACHINE_NAME})'"
export TARGET_HOST=localhost
export LOGSTASH_HOST=localhost
fi
else
export TARGET_HOST=${PROXY_IP}
export LOGSTASH_HOST=${PROXY_IP}
fi
source ${CONF_DIR}/conf/env.provider.sh
source ${CONF_DIR}/credentials.generate.sh
source ${CONF_DIR}/env.config.sh
if [ -f "${CONF_DIR}/env.override.sh" ]; then
echo "Using ${CONF_DIR}/env.override.sh to override default values for environment variable."
source ${CONF_DIR}/env.override.sh
fi
}
init() {
while [[ $1 ]]; do
case "$1" in
--without-pull)
export PULL="NO"
shift
;;
--without-load)
export LOAD="NO"
shift
;;
--with-stdout)
export LOGGING_DRIVER="stdout"
shift
;;
*)
echo "Unrecognized option: $1"
help
exit 1
;;
esac
done
echo '
### ######## ####### ########
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ## ## ## ########
######### ## ## ## ## ##
## ## ## ## ## ## ##
## ## ######## ####### ##
'
echo "* Initialising ADOP"
# Load variables
prep_env
# Create the network
echo "* Setting up Docker Network"
create_network
# Run the Docker compose commands
if [ "${PULL}" = "NO" ]; then
echo "* Skipping Pulling Docker Images"
else
echo "* Pulling Docker Images"
run_compose pull
fi
echo "* Bringing up ADOP..."
run_compose up -d
# Wait for Jenkins and Gerrit to come up before proceeding
echo "* Waiting for the Platform to become available - this can take a few minutes"
TOOL_SLEEP_TIME=30
until [[ $(docker exec jenkins curl -I -s -u "${JENKINS_PLATFORM_USERNAME}":"${PASSWORD_JENKINS}" localhost:8080/jenkins/|head -n 1|cut -d$' ' -f2) == 200 ]]; do pretty_sleep ${TOOL_SLEEP_TIME} Jenkins; done
until [[ $(docker exec gerrit curl -I -s -u "${GERRIT_PLATFORM_USERNAME}":"${PASSWORD_GERRIT}" localhost:8080/gerrit/|head -n 1|cut -d$' ' -f2) == 200 ]]; do pretty_sleep ${TOOL_SLEEP_TIME} Gerrit; done
# Trigger Load_Platform in Jenkins
if [ "${LOAD}" = "NO" ]; then
echo "* Skipping Loading the Platform"
else
echo "* Loading the Platform"
docker exec jenkins curl -s -X POST -u "${JENKINS_PLATFORM_USERNAME}":"${PASSWORD_JENKINS}" localhost:8080/jenkins/job/Load_Platform/buildWithParameters --data token=gAsuE35s
fi
# Generate and copy the certificates to jenkins slave if TLS is enabled
if [ "${DOCKER_TLS_VERIFY}" = "1" ]; then
gen_certs "${DOCKER_CLIENT_CERT_PATH}"
else
echo "DOCKER_TLS_VERIFY not set to 1, skipping certificate generation"
fi
# Wait for Nginx to come up before proceeding
echo "* Waiting for Nginx to become available"
until [[ $(curl -k -I -s -u ${INITIAL_ADMIN_USER}:${INITIAL_ADMIN_PASSWORD_PLAIN} ${PROTO}://${TARGET_HOST}/|head -n 1|cut -d$' ' -f2) == 200 ]]; do pretty_sleep 5 Nginx; done
# Tell the user something useful
echo
echo '##########################################################'
echo
echo "SUCCESS, your new ADOP instance is ready!"
echo
echo "Run these commands in your shell:"
echo ' source ./conf/env.provider.sh'
echo ' source credentials.generate.sh'
echo ' source env.config.sh'
echo
echo "You can check if any variables are missing with: ./adop compose config | grep 'WARNING'"
echo
echo "Navigate to http://${TARGET_HOST} in your browser to use your new DevOps Platform!"
echo "Login using the following credentials:"
echo " Username: ${INITIAL_ADMIN_USER}"
echo " Password: ${INITIAL_ADMIN_PASSWORD_PLAIN}"
}
create_network() {
if ! docker network create ${CUSTOM_NETWORK_NAME} &> /dev/null; then
echo "Network already exists: ${CUSTOM_NETWORK_NAME}"
else
echo "Created Docker network: ${CUSTOM_NETWORK_NAME}"
fi
}
gen_certs() {
echo "Generating client certificates for TLS-enabled Engine"
CERT_PATH=$1
if [ -z ${CERT_PATH} ]; then
echo "
Usage :
gen-certs <docker_client_certificate_path>
<docker_client_certificate_path>:
This is the path of the certificate on jenkins slave container
to be able to run docker commands against docker swarm.
Note - absolute path is required.
Example:
gen-certs /root/.docker
"
exit 1
fi
####
# Windows Git bash terminal identifies
# /CN=client as a path and appends the absolute path
# of parent directory to it
####
HOST_OS=$(uname)
CLIENT_SUBJ="/CN=client"
if echo "${HOST_OS}" | grep -E "MINGW*" >/dev/null
then
CLIENT_SUBJ="//CN=client"
fi
####
# Fresh start
####
TEMP_CERT_PATH="${HOME}/docker_certs"
rm -rf ${TEMP_CERT_PATH}
mkdir -p ${TEMP_CERT_PATH}
####
# * Generate the client private key
# * Generate signed certificate
# * Generate the client certificate
####
set +e
openssl genrsa -out ${TEMP_CERT_PATH}/key.pem 4096 &> /dev/null
openssl req -subj "${CLIENT_SUBJ}" -new -key ${TEMP_CERT_PATH}/key.pem -out ${TEMP_CERT_PATH}/client.csr &> /dev/null
echo "extendedKeyUsage = clientAuth" > ${TEMP_CERT_PATH}/extfile.cnf
openssl x509 -req -days 365 -sha256 -in ${TEMP_CERT_PATH}/client.csr -CA ${HOME}/.docker/machine/certs/ca.pem -CAkey ${HOME}/.docker/machine/certs/ca-key.pem -CAcreateserial -CAserial temp.seq -out ${TEMP_CERT_PATH}/cert.pem -extfile ${TEMP_CERT_PATH}/extfile.cnf &> /dev/null
set -e
cp ${HOME}/.docker/machine/certs/ca.pem ${TEMP_CERT_PATH}/ca.pem
docker --tlsverify --tlscacert=${HOME}/.docker/machine/certs/ca.pem --tlscert=${TEMP_CERT_PATH}/cert.pem --tlskey=${TEMP_CERT_PATH}/key.pem -H=${DOCKER_HOST} version &> /dev/null
####
# * Check if certificates were generated successfully
####
CERT_FILE="${TEMP_CERT_PATH}/cert.pem"
CA_FILE="${TEMP_CERT_PATH}/ca.pem"
KEY_FILE="${TEMP_CERT_PATH}/key.pem"
for file in $CERT_FILE $CA_FILE $KEY_FILE
do
if [[ -s $file ]]; then
echo "${file} was generated successfully..."
else
echo "${file} was not generated successfully..."
echo "This may be due to OpenSSL failing to generate cert.pem."
echo "Please run your shell window in Administrator mode or with root access and re-run the quickstart script with the same flags provided in this run."
exit 1
fi
done
####
# * Remove unnecessary files
# * Copy the certificates to slave
####
echo "Uploading certificates to Jenkins Slave at: ${CERT_PATH}"
rm -f ${TEMP_CERT_PATH}/extfile.cnf ${TEMP_CERT_PATH}/client.csr
set +e
docker exec jenkins-slave rm -rf ${CERT_PATH}
docker cp ${TEMP_CERT_PATH} jenkins-slave:${CERT_PATH}
set -e
}
run_compose() {
# Load variables
prep_env
compose_cmd=$1
shift
# Setting Compose File Lists
if [ "${LOGGING_DRIVER}" = "stdout" ]; then
ADOPFILEOPTS="-f ${CLI_DIR}/docker-compose.yml -f ${CLI_DIR}/etc/volumes/${VOLUME_DRIVER}/default.yml"
echo "* Sending Docker container logs to stdout..."
else
ADOPFILEOPTS="-f ${CLI_DIR}/docker-compose.yml -f ${CLI_DIR}/etc/volumes/${VOLUME_DRIVER}/default.yml -f ${CLI_DIR}/etc/logging/${LOGGING_DRIVER}/default.yml"
fi
ELKFILEOPTS="-f ${CLI_DIR}/compose/elk.yml"
# If total overrides have been provided then just use them
# Else use "our" file list
if [ ! -z "${TOTAL_OVERRIDES}" ]; then
docker-compose ${TOTAL_OVERRIDES} ${compose_cmd} "$@"
else
# Handle up vs up -d so that we can launch the main Compose file
if [ "${compose_cmd}" = "up" ] && [[ $@ != *"-d"* ]]; then
echo "* Starting ELK with 'up -d' instead of just the requested 'up'"
docker-compose ${ELKFILEOPTS} ${compose_cmd} "$@" -d
else
docker-compose ${ELKFILEOPTS} ${compose_cmd} "$@"
fi;
echo
docker-compose ${ADOPFILEOPTS} ${OVERRIDES} ${compose_cmd} "$@"
fi
}
# Defaults
DEFAULT_MACHINE_NAME="default"
export MACHINE_NAME=${DOCKER_MACHINE_NAME:-${DEFAULT_MACHINE_NAME}}
export VOLUME_DRIVER=local
export LOGGING_DRIVER=syslog
export CUSTOM_NETWORK_NAME=local_network
export OVERRIDES=""
export TOTAL_OVERRIDES=""
export PULL="YES"
# Parameters
while getopts "m:f:F:v:l:n:i:" opt; do
case $opt in
m)
export MACHINE_NAME=${OPTARG}
;;
f)
export OVERRIDES="${OVERRIDES} -f ${OPTARG}"
;;
F)
export TOTAL_OVERRIDES="${TOTAL_OVERRIDES} -f ${OPTARG}"
;;
l)
export LOGGING_DRIVER="${OPTARG}"
;;
v)
export VOLUME_DRIVER="${OPTARG}"
;;
n)
export CUSTOM_NETWORK_NAME="${OPTARG}"
;;
i)
export PROXY_IP="${OPTARG}"
;;
*)
echo "Invalid parameter(s) or option(s)."
cmd_usage
exit 1
;;
esac
done
shift $(($OPTIND -1))
SUBCOMMAND_OPT="${1:-help}"
# Only shift if there are other parameters
if [ $# -ge 1 ]; then
shift
fi
case ${SUBCOMMAND_OPT} in
"cmd_desc"|"help"|"init")
${SUBCOMMAND_OPT} "$@"
;;
"gen-certs")
gen_certs "$@"
;;
*)
run_compose ${SUBCOMMAND_OPT} "$@"
;;
esac