-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.sh
More file actions
executable file
·259 lines (224 loc) · 4.44 KB
/
compose.sh
File metadata and controls
executable file
·259 lines (224 loc) · 4.44 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
254
255
256
257
258
259
#!/usr/bin/env bash
set -euo pipefail
## --- Base --- ##
_PROJECT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-"$0"}")" >/dev/null 2>&1 && pwd -P)"
cd "${_PROJECT_DIR}" || exit 2
# shellcheck disable=SC1091
[ -f .env ] && . .env
# Checking docker and docker-compose installed:
if ! command -v docker >/dev/null 2>&1; then
echo "[ERROR]: Not found 'docker' command, please install it first!" >&2
exit 1
fi
if ! docker info > /dev/null 2>&1; then
echo "[ERROR]: Unable to communicate with the docker daemon. Check docker is running or check your account added to docker group!" >&2
exit 1
fi
if ! docker compose > /dev/null 2>&1; then
echo "[ERROR]: 'docker compose' not found or not installed!" >&2
exit 1
fi
## --- Base --- ##
## --- Variables --- ##
_DEFAULT_SERVICE="agent-validator"
## --- Variables --- ##
## --- Functions --- ##
_build()
{
# shellcheck disable=SC2068
./scripts/build.sh ${@:-} || exit 2
# docker compose --progress=plain build ${@:-} || exit 2
}
_validate()
{
docker compose config || exit 2
}
_start()
{
if [ "${1:-}" == "-l" ]; then
shift
# shellcheck disable=SC2068
docker compose up -d --remove-orphans --force-recreate ${@:-} || exit 2
_logs "${@:-}"
else
# shellcheck disable=SC2068
docker compose up -d --remove-orphans --force-recreate ${@:-} || exit 2
fi
}
_stop()
{
if [ -z "${1:-}" ]; then
docker compose down --remove-orphans || exit 2
else
# shellcheck disable=SC2068
docker compose rm -sfv ${@:-} || exit 2
fi
}
_restart()
{
if [ "${1:-}" == "-l" ]; then
shift
_stop "${@:-}" || exit 2
_start -l "${@:-}" || exit 2
else
_stop "${@:-}" || exit 2
_start "${@:-}" || exit 2
fi
# docker compose restart ${@:-} || exit 2
}
_logs()
{
# shellcheck disable=SC2068
docker compose logs -f --tail 100 ${@:-} || exit 2
}
_list()
{
docker compose ps || exit 2
}
_ps()
{
# shellcheck disable=SC2068
docker compose top ${@:-} || exit 2
}
_stats()
{
# shellcheck disable=SC2068
docker compose stats ${@:-} || exit 2
}
_exec()
{
if [ -z "${1:-}" ]; then
echo "[ERROR]: Not found any arguments for exec command!" >&2
exit 1
fi
echo "[INFO]: Executing command inside '${_DEFAULT_SERVICE}' container..."
# shellcheck disable=SC2068
docker compose exec "${_DEFAULT_SERVICE}" ${@:-} || exit 2
}
_enter()
{
local _service="${_DEFAULT_SERVICE}"
if [ -n "${1:-}" ]; then
_service=${1}
fi
echo "[INFO]: Entering inside '${_service}' container..."
docker compose exec "${_service}" /bin/bash || exit 2
}
_images()
{
# shellcheck disable=SC2068
docker compose images ${@:-} || exit 2
}
_clean()
{
# shellcheck disable=SC2068
docker compose down -v --remove-orphans ${@:-} || exit 2
}
_update()
{
if docker compose ps | grep 'Up' > /dev/null 2>&1; then
_stop "${@:-}" || exit 2
fi
# shellcheck disable=SC2068
docker compose pull --policy always ${@:-} || exit 2
# shellcheck disable=SC2046
docker rmi -f $(docker images --filter "dangling=true" -q --no-trunc) > /dev/null 2>&1 || true
# _start "${@:-}" || exit 2
}
## --- Functions --- ##
## --- Menu arguments --- ##
_usage_help() {
cat <<EOF
USAGE: ${0} <command> [args...]
COMMANDS:
build
validate | valid | config
start | run | up
stop | down | remove | rm | delete | del
restart
logs
list
ps
stats | resource | limit
exec
enter
images
clean | clear
update | pull | download
OPTIONS:
-h, --help Show this help message.
EOF
}
if [ $# -eq 0 ]; then
echo "[ERROR]: Not found any input!" >&2
_usage_help
exit 1
fi
while [ $# -gt 0 ]; do
case "${1}" in
build)
shift
_build "${@:-}"
exit 0;;
validate | valid | config)
shift
_validate
exit 0;;
start | run | up)
shift
_start "${@:-}"
exit 0;;
stop | down | remove | rm | delete | del)
shift
_stop "${@:-}"
exit 0;;
restart)
shift
_restart "${@:-}"
exit 0;;
logs)
shift
_logs "${@:-}"
exit 0;;
list)
shift
_list
exit 0;;
ps)
shift
_ps "${@:-}"
exit 0;;
stats | resource | limit)
shift
_stats "${@:-}"
exit 0;;
exec)
shift
_exec "${@:-}"
exit 0;;
enter)
shift
_enter "${@:-}"
exit 0;;
images)
shift
_images "${@:-}"
exit 0;;
clean | clear)
shift
_clean "${@:-}"
exit 0;;
update | pull | download)
shift
_update "${@:-}"
exit 0;;
-h | --help)
_usage_help
exit 0;;
*)
echo "[ERROR]: Failed to parse argument -> ${1}!" >&2
_usage_help
exit 1;;
esac
done
## --- Menu arguments --- ##