-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathxshok-docker.sh
409 lines (354 loc) · 14 KB
/
xshok-docker.sh
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env bash
################################################################################
# This is property of eXtremeSHOK.com
# You are free to use, modify and distribute, however you may not remove this notice.
# Copyright (c) Adrian Jon Kriel :: [email protected]
################################################################################
#
# Script updates can be found at: https://github.com/extremeshok/xshok-docker
#
# Starts your docker-compose based containers properly
#
# Automatically creates volume directories and touches volume files
# Stops all running containers
# Removes any orphaned containers
# Pull/download dependencies and images
# Forces recreation of all containers and will build required containers
#
# License: BSD (Berkeley Software Distribution)
#
################################################################################
#
# Assumptions: Docker and Docker-compose Installed
#
# Tested on KVM, VirtualBox and Dedicated Server
#
# Notes:
# Script must be placed into the same directory as the docker-compose.yml
#
# Usage:
# wget https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-init-docker.sh -O xshok-init-docker.sh && chmod +x xshok-init-docker.sh
# bash xshok-init-docker.sh
#
################################################################################
#
# THERE ARE NO USER CONFIGURABLE OPTIONS IN THIS SCRIPT
#
################################################################################
PWD="/datastore"
EPACE=' '
source "${PWD}/.env"
## GLOBALS
DIRNAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "${DIRNAME}" || exit 1
################# Script Info
script_version="1.2"
script_version_date="2020-12-21"
################# SUPPORTING FUNCTIONS :: START
# Check if the current running user is the root user, otherwise return false
function xshok_is_root() {
id_bin="$(command -v id 2> /dev/null)"
if [ "$($id_bin -u)" == 0 ] ; then
return 0
else
return 1 # Not root
fi
}
################# SUPPORTING FUNCTIONS :: END
################# XSHOK FUNCTIONS :: START
################# docker start
xshok_docker_up(){
#Automatically create required volume dirs
## remove all comments
TEMP_COMPOSE="/tmp/xs_$(date +"%s")"
sed -e '1{/^#!/ {p}}; /^[\t\ ]*#/d;/\.*#.*/ {/[\x22\x27].*#.*[\x22\x27]/ !{:regular_loop s/\(.*\)*[^\]#.*/\1/;t regular_loop}; /[\x22\x27].*#.*[\x22\x27]/ {:special_loop s/\([\x22\x27].*#.*[^\x22\x27]\)#.*/\1/;t special_loop}; /\\#/ {:second_special_loop s/\(.*\\#.*[^\]\)#.*/\1/;t second_special_loop}}' docker-compose.yml > "$TEMP_COMPOSE"
mkdir -p "${PWD}/volumes/"
VOLUMEDIR_ARRAY=$(grep "device:.*\${PWD}/volumes/" "$TEMP_COMPOSE")
for VOLUMEDIR in $VOLUMEDIR_ARRAY ; do
if [[ $VOLUMEDIR =~ "\${PWD}" ]]; then
VOLUMEDIR="${VOLUMEDIR/\$\{PWD\}\//}"
if [ ! -d "$VOLUMEDIR" ] ; then
if [ ! -f "$VOLUMEDIR" ] && [[ $VOLUMEDIR != *.* ]] ; then
echo "Creating dir: $VOLUMEDIR"
mkdir -p "$VOLUMEDIR"
chmod 777 "$VOLUMEDIR"
elif [ ! -d "$VOLUMEDIR" ] && [[ $VOLUMEDIR == *.* ]] ; then
echo "Creating file: $VOLUMEDIR"
touch -p "$VOLUMEDIR"
fi
fi
fi
done
rm -f "$TEMP_COMPOSE"
docker-compose down --remove-orphans
# detect if there are any running containers and manually stop and remove them
if docker ps -q 2> /dev/null ; then
docker stop $(docker ps -q)
sleep 1
docker rm $(docker ps -q)
fi
docker-compose pull --include-deps
docker-compose up -d --force-recreate --build
}
################# docker stop
xshok_docker_down(){
docker-compose down
sync
docker stop $(docker ps -q)
sync
}
################# docker restart
xshok_docker_restart(){
docker-compose down
sync
sleep 3
docker-compose up -d
}
################# docker prune
xshok_docker_prune(){
echo "Removing images, will NOT remove volumes"
docker system prune -a -f
}
################# XSHOK FUNCTIONS :: END
################# XSHOK ADVANCED FUNCTIONS :: START
################# docker boot
xshok_docker_boot(){
if [ ! -d "/etc/systemd/system/" ] ; then
echo "ERROR: systemd not detected"
exit 1
fi
# remove beginning /
THISNAME="${DIRNAME#/}"
# remove ending /
THISNAME="${THISNAME%/}"
# space with -
THISNAME="${THISNAME// /-}"
# / with _
THISNAME="${THISNAME//\/_}"
# remove .
THISNAME="${THISNAME//\.}"
echo "Generating Systemd service"
cat << EOF > "/etc/systemd/system/docker-${THISNAME}.service"
[Unit]
Description=Docker Compose ${THISNAME} Service
Requires=docker.service
After=docker.service
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
TimeoutStartSec=0
RemainAfterExit=yes
WorkingDirectory=${DIRNAME}
EOF
if [ -f "${DIRNAME}/xshok-docker.sh" ] ; then
echo "ExecStart=/bin/bash ${DIRNAME}/xshok-docker.sh --start" >> "/etc/systemd/system/docker-${THISNAME}.service"
echo "ExecStop=/bin/bash ${DIRNAME}/xshok-docker.sh --stop" >> "/etc/systemd/system/docker-${THISNAME}.service"
echo "ExecReload=/bin/bash ${DIRNAME}/xshok-docker.sh --reload" >> "/etc/systemd/system/docker-${THISNAME}.service"
else
echo "ExecStart=/usr/local/bin/docker-compose up -d --force-recreate --build" >> "/etc/systemd/system/docker-${THISNAME}.service"
echo "ExecStop=/usr/local/bin/docker-compose down" >> "/etc/systemd/system/docker-${THISNAME}.service"
echo "ExecStop=/usr/local/bin/docker-compose restart" >> "/etc/systemd/system/docker-${THISNAME}.service"
fi
echo "Created: /etc/systemd/system/docker-${THISNAME}.service"
systemctl daemon-reload
systemctl enable "docker-${THISNAME}"
echo "Available Commands:"
echo "Start-> systemctl start docker-${THISNAME}"
echo "Stop-> systemctl stop docker-${THISNAME}"
echo "Reload-> systemctl reload docker-${THISNAME}"
}
################# docker visulise
xshok_docker_visualise(){
TEMP_VIS="/tmp/xs_vis_$(date +"%s")/"
mkdir -p "${TEMP_VIS}"
if [ -d "${TEMP_VIS}" ] && [ -w "${TEMP_VIS}" ] ; then
echo "Generating Docker Visualisations"
chmod 777 "${TEMP_VIS}"
docker-compose config > "${TEMP_VIS}/docker-compose.yml"
docker run --rm -it --name dcv -v "${TEMP_VIS}:/input:rw" pmsipilot/docker-compose-viz render -m image -o docker-vis-full.png --horizontal --force /input/docker-compose.yml
mv -f "${TEMP_VIS}/docker-vis-full.png" "${DIRNAME}/docker-vis-full.png"
docker run --rm -it --name dcv -v "${TEMP_VIS}:/input:rw" pmsipilot/docker-compose-viz render -m image -o docker-vis-novols.png --horizontal --no-volumes --force /input/docker-compose.yml
mv -f "${TEMP_VIS}/docker-vis-novols.png" "${DIRNAME}/docker-vis-novols.png"
rm -rf "${TEMP_VIS}"
echo "Completed, images saved to ${DIRNAME}"
else
echo "ERROR, failed to create temp dir or not writable: ${TEMP_VIS}"
exit 1
fi
}
xshok_docker_maintenance(){
df -h /
if command -v apt-get > /dev/null; then
apt-get autoremove
apt-get clean
fi
#empty journalctl
if command -v journalctl > /dev/null; then
journalctl --vacuum-size=500M
fi
#remove error logs
rm -f /datastore/*/logs/error.log.*
df -h /
}
# Check for a new version
function check_new_version() {
found_upgrade="no"
# shellcheck disable=SC2086
latest_version="$(curl --compressed --connect-timeout "30" --remote-time --location --retry "3" --max-time "30" "https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-docker.sh" 2> /dev/null | grep "^script_version=" | head -n1 | cut -d '"' -f 2)"
if [ "$latest_version" != "" ] ; then
# shellcheck disable=SC2183,SC2086
if [ "$(printf "%02d%02d%02d%02d" ${latest_version//./ })" -gt "$(printf "%02d%02d%02d%02d" ${script_version//./ })" ] ; then
echo "------------------------------"
echo "ALERT: New version : v${latest_version} @ https://github.com/extremeshok/docker-webserver"
found_upgrade="yes"
fi
fi
if [ "$found_upgrade" == "yes" ] ; then
echo "Quickly upgrade, run the following command as root:"
echo "bash xshok-admin.sh --upgrade"
fi
}
# Auto upgrade the master.conf and the
function xshok_upgrade() {
if ! xshok_is_root ; then
echo "ERROR: Only root can run the upgrade"
exit 1
fi
echo "Checking for updates ..."
found_upgrade="no"
latest_version="$(curl --compressed --connect-timeout "30" --remote-time --location --retry "3" --max-time "30" "https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-docker.sh" 2> /dev/null | grep "^script_version=" | head -n1 | cut -d '"' -f 2)"
if [ "$latest_version" != "" ] ; then
# shellcheck disable=SC2183,SC2086
if [ "$(printf "%02d%02d%02d%02d" ${latest_version//./ })" -gt "$(printf "%02d%02d%02d%02d" ${script_version//./ })" ] ; then
found_upgrade="yes"
echo "ALERT: Upgrading script from v${script_version} to v${latest_version}"
echo "Downloading https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-docker.sh"
curl --fail --compressed --connect-timeout "30" --remote-time --location --retry "3" --max-time "30" --time-cond "${DIRNAME}/xshok-docker.sh.tmp" --output "${DIRNAME}/xshok-docker.sh.tmp" "https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-docker.sh" 2> /dev/null
ret=$?
if [ "$ret" -ne 0 ] ; then
echo "ERROR: Could not download https://raw.githubusercontent.com/extremeshok/xshok-docker/master/xshok-docker.sh"
exit 1
fi
# Detect to make sure the entire script is avilable, fail if the script is missing contents
if [ "$(tail -n 1 "${DIRNAME}/xshok-docker.sh.tmp" | head -n 1 | cut -c 1-7)" != "exit \$?" ] ; then
echo "ERROR: Downloaded xshok-admin.sh is incomplete, please re-run"
exit 1
fi
# Copy over permissions from old version
OCTAL_MODE="$(stat -c "%a" "${DIRNAME}/xshok-docker.sh")"
echo "Inserting update process..."
# Generate the update script
cat > "/datastore/xshok_update_script.sh" << EOF
#!/usr/bin/env bash
echo "Running update process"
# Overwrite old file with new
if ! mv -f "${DIRNAME}/xshok-docker.sh.tmp" "${DIRNAME}/xshok-docker.sh" ; then
echo "ERROR: failed moving ${DIRNAME}/xshok-docker.sh.tmp to ${DIRNAME}/xshok-docker.sh"
rm -f \$0
exit 1
fi
if ! chmod "$OCTAL_MODE" "${DIRNAME}/xshok-docker.sh" ; then
echo "ERROR: unable to set permissions on ${DIRNAME}/xshok-docker.sh"
rm -f \$0
exit 1
fi
echo "Completed"
#remove the tmp script before exit
rm -f \$0
EOF
# Replaced with $0, so code will update and then call itself with the same parameters it had
#exec "${0}" "$@"
bash_bin="$(command -v bash 2> /dev/null)"
exec "$bash_bin" "/datastore/xshok_update_script.sh"
echo "Running once as root"
fi
fi
if [ "$found_upgrade" == "no" ] ; then
echo "No updates available"
fi
}
################# XSHOK ADVANCED FUNCTIONS :: END
echo "eXtremeSHOK.com Docker ${script_version} (${script_version_date})"
help_message(){
echo -e "\033[1mDOCKER OPTIONS\033[0m"
echo "${EPACE}-u | --up | --start | --init"
echo "${EPACE}${EPACE} start docker-compose.yml"
echo "${EPACE}-d | --down | --stop"
echo "${EPACE}${EPACE} stop all dockers and d cker-compose"
echo "${EPACE}-r | --restart | --quickupdown | --quick-up-down | --reload"
echo "${EPACE}${EPACE} quickly restart docker-compose"
echo "${EPACE}-R | --reset | --updown | --up-down"
echo "${EPACE}${EPACE} reset docker-compose (down and then up)"
echo "${EPACE}-p | --prune | --clean"
echo "${EPACE}${EPACE} stop and remove dockers, will NOT remove volumes"
echo -e "\033[1mADVANCED OPTIONS\033[0m"
echo "${EPACE}-b | --boot | --service | --systemd"
echo "${EPACE}${EPACE} creates a systemd service to start docker and run docker-compose.yml on boot"
echo "${EPACE}-v | --vis | --visuliser"
echo "${EPACE}${EPACE} make pretty images of the docker-compose topology"
echo -e "\033[1mGENERAL OPTIONS\033[0m"
echo "${EPACE}--upgrade"
echo "${EPACE}${EPACE} upgrades the script to the latest version"
echo "${EPACE}-H, --help"
echo "${EPACE}${EPACE}Display help and exit."
}
if [ -z "${1}" ]; then
help_message
check_new_version
exit 1
fi
# VALIDATION
if [ ! -f "docker-compose.yml" ] ; then
echo "ERROR: docker-compose.yml not found, script must be run in the same directory"
exit 1
fi
if ! docker-compose config > /dev/null ; then
echo "ERROR: docker-compose.yml failed config check"
exit 1
fi
while [ ! -z "${1}" ]; do
case ${1} in
-[hH] | --help )
help_message
;;
-u | --up | --start | --init )
xshok_docker_up
;;
-d | --down | --stop )
xshok_docker_down
;;
-r | --restart | --quickupdown | --quick-up-down | --reload )
xshok_docker_restart
;;
-R | --reset | --updown | --up-down )
xshok_docker_down
xshok_docker_up
;;
-p | --prune | --clean | --purge )
xshok_docker_down
xshok_docker_prune
;;
## ADVANCED
-b | --boot | --service | --systemd )
xshok_docker_boot
;;
-v | --vis | --visuliser )
xshok_docker_visualise
;;
-m | --maintenance )
xshok_docker_maintenance
;;
--upgrade)
xshok_upgrade
shift
;;
*)
help_message
check_new_version
;;
esac
shift
done
# And lastly we exit, Note: the exit is always on the 2nd last line
exit $?