-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.sh
executable file
·224 lines (191 loc) · 3.68 KB
/
compose.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
#!/bin/bash
set -euo pipefail
## --- Base --- ##
# Getting path of this script file:
_PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "${_PROJECT_DIR}" || exit 2
# Loading base script:
# shellcheck disable=SC1091
source ./scripts/base.sh
exitIfNoDocker
# exitIfNotExists ".env"
# Loading .env file (if exists):
if [ -f ".env" ]; then
# shellcheck disable=SC1091
source .env
fi
## --- Base --- ##
## --- Variables --- ##
_DEFAULT_SERVICE="api"
# Extending timeout of docker compose logs:
export COMPOSE_HTTP_TIMEOUT=43200
## --- Variables --- ##
## --- Functions --- ##
_doBuild()
{
# ./scripts/build.sh || exit 2
docker compose build || exit 2
}
_doValidate()
{
docker compose config || exit 2
}
_doStart()
{
if [ "${1:-}" == "-l" ]; then
shift
# shellcheck disable=SC2068
docker compose up -d ${@:-} || exit 2
_doLogs "${@:-}"
else
# shellcheck disable=SC2068
docker compose up -d ${@:-} || exit 2
fi
}
_doStop()
{
if [ -z "${1:-}" ]; then
docker compose down || exit 2
else
# shellcheck disable=SC2068
docker compose rm -sfv ${@:-} || exit 2
fi
}
_doRestart()
{
if [ "${1:-}" == "-l" ]; then
shift
_doStop "${@:-}" || exit 2
_doStart -l "${@:-}" || exit 2
else
_doStop "${@:-}" || exit 2
_doStart "${@:-}" || exit 2
fi
# docker compose restart ${@:-} || exit 2
}
_doLogs()
{
if [ -n "${1:-}" ]; then
# docker compose logs -f --tail 100 ${@} || exit 2
# shellcheck disable=SC2068
docker compose ps -q ${@} | xargs -n 1 docker logs -f -n 100 || exit 2
else
docker compose logs -f --tail 100 || exit 2
fi
}
_doList()
{
docker compose ps || exit 2
}
_doPs()
{
# shellcheck disable=SC2068
docker compose top ${@:-} || exit 2
}
_doStats()
{
# shellcheck disable=SC2046
docker stats $(docker compose ps -q) || exit 2
}
_doExec()
{
if [ -z "${1:-}" ]; then
echoError "Not found any input."
exit 1
fi
# shellcheck disable=SC2068
docker compose exec "${_DEFAULT_SERVICE}" ${@} || exit 2
}
_doEnter()
{
_service="${_DEFAULT_SERVICE}"
if [ -n "${1:-}" ]; then
_service=${1}
fi
echoInfo "Entering '${_service}' container..."
docker compose exec "${_service}" /bin/bash || exit 2
}
_doImages()
{
# shellcheck disable=SC2068
docker compose images ${@:-} || exit 2
}
_doClean()
{
# shellcheck disable=SC2068
docker compose down -v ${@:-} || exit 2
}
_doUpdate()
{
if docker compose ps | grep 'Up' > /dev/null 2>&1; then
_doStop "${@:-}" || exit 2
fi
# shellcheck disable=SC2068
docker compose pull ${@:-} || exit 2
# shellcheck disable=SC2046
docker rmi -f $(docker images --filter "dangling=true" -q --no-trunc) > /dev/null 2>&1 || true
# _doStart "${@:-}" || exit 2
}
## --- Functions --- ##
## --- Menu arguments --- ##
_exitOnWrongParams()
{
echoInfo "USAGE: ${0} build | validate | start | stop | restart | logs | list | ps | stats | exec | enter | images | clean | update"
exit 1
}
main()
{
if [ -z "${1:-}" ]; then
echoError "Not found any input."
_exitOnWrongParams
fi
case ${1} in
build)
shift
_doBuild;;
validate | config)
shift
_doValidate;;
start | run | up)
shift
_doStart "${@:-}";;
stop | down | remove | rm | delete | del)
shift
_doStop "${@:-}";;
restart)
shift
_doRestart "${@:-}";;
logs)
shift
_doLogs "${@:-}";;
list)
_doList;;
ps)
shift
_doPs "${@:-}";;
stats | resource | limit)
shift
_doStats;;
exec)
shift
_doExec "${@:-}";;
enter)
shift
_doEnter "${@:-}";;
images)
shift
_doImages "${@:-}";;
clean)
shift
_doClean "${@:-}";;
update)
shift
_doUpdate "${@:-}";;
*)
echoError "Failed to parsing input: ${*}"
_exitOnWrongParams;;
esac
exit
}
main "${@:-}"
## --- Menu arguments --- ##