-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.sh
executable file
·133 lines (109 loc) · 2.31 KB
/
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
#!/usr/bin/env bash
GREEN='\033[0;32m'
LGREEN='\e[92m'
YELLOW='\e[33m'
LYELLOW='\e[93m'
BLUE='\e[34m'
LBLUE='\e[94m'
LGRAY='\e[37m'
NC='\033[0m' # No Color
INVERT='\e[7m'
NI='\e[27m'
_verbose=1
_dockerComposeV2=0
if [ -f ~/.docker/cli-plugins/docker-compose ] || [ -f /usr/local/lib/docker/cli-plugins/docker-compose ]; then
printf "%bEnable docker compose v2%b\n" \
"${YELLOW}" "${NC}"
_dockerComposeV2=1
else
REQUIRED_PKG="docker-compose-plugin"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG 2>/dev/null | grep "install ok installed")
if [ "" != "$PKG_OK" ]; then
printf "%bEnable docker compose v2%b\n" \
"${YELLOW}" "${NC}"
_dockerComposeV2=1
fi
fi
serviceName=teamspeak
fileConfig=swarm.yml
printCommand() {
printf "%bCommand: %b$*%b" \
"${BLUE}" "${LGREEN}" "${NC}" | sed -z "s/\n/\\\n/g" &&
printf "\n"
}
_command() {
command="$*"
if [ "${_verbose}" != "0" ]; then
printCommand "${command}"
fi
eval "${command}" ||
return 10
}
_docker() {
_command docker "$@"
}
dockerCompose() {
if [ "${_dockerComposeV2}" = "1" ]; then
_docker compose "$@"
return
fi
_command docker-compose "$@"
}
_pwgen() {
_command pwgen "$@"
}
generatePasswords() {
if [ ! -f "./secrets/db_root_password.txt" ]; then
_pwgen "20 1 >./secrets/db_root_password.txt" && _pwgen "20 1 >./secrets/db_password.txt" && _pwgen "20 1 >./secrets/serveradmin_password.txt"
fi
}
dockerConfig() {
_dockerComposeV2=0
dockerCompose config "$@"
}
dockerStack() {
_docker stack "$@"
}
dockerDeploy() {
dockerStack deploy "$@"
}
dockerSwarm() {
_docker swarm "$@"
}
generateSwarmConfig() {
dockerConfig "| sed -e \"s/$(readlink -f "$(dirname "$0")" | sed -e "s/\//\\\\\//g")/\./\" | sed -E \"s,(cpus: )([0-9\.]+),\1'\2',g\" | sed -zE \"s,[ ]{2}(db):\s*condition: service_started\s*\n,- \1\n,g\" > ${fileConfig}"
}
stackDeploy() {
generateSwarmConfig &&
dockerDeploy -c ${fileConfig} ${serviceName}
}
stackRm() {
dockerStack rm ${serviceName}
}
swarmInit() {
dockerSwarm init
}
composeUp() {
_docker compose up "$@"
}
firstCommand="$1"
shift
case "${firstCommand}" in
upd | "")
composeUp -d
;;
rm)
stackRm
;;
secrets)
generatePasswords
;;
swarm-init)
swarmInit
;;
*)
echo "Commands: [upd|rm|secrets|swarm-init]" >&2
exit 1
;;
esac
exit $?