forked from fawney19/Aether
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·287 lines (250 loc) · 7.69 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·287 lines (250 loc) · 7.69 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
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
#!/usr/bin/env bash
# One-click updater for Docker Compose deployments.
#
# This updates the app container image and recreates only the app service. It is
# intentionally not a hot patch of the running Rust process.
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
MODE="auto"
COMPOSE_DIR=""
APP_SERVICE="app"
COMPOSE_WAIT_TIMEOUT_SECS=120
COMPOSE_HEALTHCHECK_POLL_INTERVAL_SECS=2
NO_PULL=false
FORCE_RECREATE=false
SHOW_LOGS=false
LOCAL_BUILD=false
PREPARE_ONLY=false
COMPOSE_FILES=()
COMPOSE=()
COMPOSE_ARGS=()
usage() {
cat <<'EOF'
Usage: ./update.sh [options]
Update Aether Docker Compose deployment in one command.
Options:
--mode MODE auto, compose, single-node, or local-build
auto uses docker-compose.yml in the current directory
--compose-dir DIR deployment directory, default: current directory
-f, --compose-file FILE compose file path; can be provided multiple times
--service NAME app service name, default: app
--no-pull skip docker compose pull
--prepare pull the latest app image only, do not recreate app
--force-recreate force recreate the app container
--logs follow app logs after update
-h, --help show help
Examples:
./update.sh
./update.sh --mode single-node
./update.sh --compose-dir /opt/aether/compose
./update.sh --mode local-build
EOF
}
die() {
echo "ERROR: $*" >&2
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
--mode)
[[ $# -ge 2 ]] || die "--mode requires a value"
MODE="$2"
shift 2
;;
--compose-dir)
[[ $# -ge 2 ]] || die "--compose-dir requires a value"
COMPOSE_DIR="$2"
shift 2
;;
-f|--compose-file)
[[ $# -ge 2 ]] || die "--compose-file requires a value"
COMPOSE_FILES+=("$2")
shift 2
;;
--service)
[[ $# -ge 2 ]] || die "--service requires a value"
APP_SERVICE="$2"
shift 2
;;
--no-pull)
NO_PULL=true
shift
;;
--prepare)
PREPARE_ONLY=true
shift
;;
--force-recreate)
FORCE_RECREATE=true
shift
;;
--logs)
SHOW_LOGS=true
shift
;;
--local-build)
MODE="local-build"
LOCAL_BUILD=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown argument: $1"
;;
esac
done
case "$MODE" in
auto|compose|single-node|local-build)
;;
*)
die "unsupported mode: ${MODE}; expected auto, compose, single-node, or local-build"
;;
esac
if [[ "${MODE}" == "local-build" || "${LOCAL_BUILD}" == "true" ]]; then
[[ "${PREPARE_ONLY}" != "true" ]] || die "--prepare is only supported for Docker Compose deployments"
deploy_script="${SCRIPT_DIR}/deploy.sh"
[[ -f "${deploy_script}" ]] || die "local-build mode requires deploy.sh next to update.sh"
args=()
if [[ "${FORCE_RECREATE}" == "true" ]]; then
args+=(--force)
fi
exec bash "${deploy_script}" "${args[@]}"
fi
resolve_compose_cli() {
if [[ "${#COMPOSE[@]}" -gt 0 ]]; then
return
fi
if docker compose version >/dev/null 2>&1; then
COMPOSE=(docker compose)
return
fi
if command -v docker-compose >/dev/null 2>&1; then
COMPOSE=(docker-compose)
return
fi
die "docker compose or docker-compose is required"
}
compose() {
"${COMPOSE[@]}" "${COMPOSE_ARGS[@]}" "$@"
}
compose_config() {
compose config "$@"
}
resolve_compose_cli
docker info >/dev/null 2>&1 || die "Docker is not running"
if [[ -z "${COMPOSE_DIR}" ]]; then
COMPOSE_DIR="$(pwd -P)"
fi
COMPOSE_DIR="$(cd -- "${COMPOSE_DIR}" && pwd -P)"
resolve_compose_file() {
local filename="$1"
if [[ "${filename}" = /* ]]; then
printf '%s\n' "${filename}"
else
printf '%s\n' "${COMPOSE_DIR}/${filename}"
fi
}
resolve_default_compose_files() {
case "${MODE}" in
compose)
COMPOSE_FILES=("docker-compose.yml")
;;
single-node)
if [[ -f "${COMPOSE_DIR}/docker-compose.single-node.yml" ]]; then
COMPOSE_FILES=("docker-compose.single-node.yml")
else
COMPOSE_FILES=("docker-compose.yml")
fi
;;
auto)
if [[ -f "${COMPOSE_DIR}/docker-compose.yml" ]]; then
COMPOSE_FILES=("docker-compose.yml")
elif [[ -f "${COMPOSE_DIR}/docker-compose.single-node.yml" ]]; then
COMPOSE_FILES=("docker-compose.single-node.yml")
else
die "no docker-compose.yml or docker-compose.single-node.yml found in ${COMPOSE_DIR}"
fi
;;
esac
}
if [[ "${#COMPOSE_FILES[@]}" -eq 0 ]]; then
resolve_default_compose_files
fi
COMPOSE_ARGS+=(--project-directory "${COMPOSE_DIR}")
for file in "${COMPOSE_FILES[@]}"; do
resolved_file="$(resolve_compose_file "${file}")"
[[ -f "${resolved_file}" ]] || die "compose file not found: ${resolved_file}"
COMPOSE_ARGS+=(-f "${resolved_file}")
done
services="$(compose_config --services)"
if ! grep -qx "${APP_SERVICE}" <<< "${services}"; then
die "service '${APP_SERVICE}' not found in compose config"
fi
echo ">>> Compose directory: ${COMPOSE_DIR}"
echo ">>> App service: ${APP_SERVICE}"
compose_pull_app() {
compose pull "${APP_SERVICE}"
}
compose_up_app() {
local wait_for_health="${1:-false}"
local -a up_args=(up -d)
if [[ "${FORCE_RECREATE}" == "true" ]]; then
up_args+=(--force-recreate)
fi
if [[ "${wait_for_health}" == "true" ]]; then
up_args+=(--wait --wait-timeout "${COMPOSE_WAIT_TIMEOUT_SECS}")
fi
up_args+=("${APP_SERVICE}")
compose "${up_args[@]}"
}
wait_healthy() {
local timeout="${1:-${COMPOSE_WAIT_TIMEOUT_SECS}}"
local elapsed=0
echo ">>> Waiting for ${APP_SERVICE} to become healthy (timeout ${timeout}s)..."
while (( elapsed < timeout )); do
local container_id
local state
container_id="$(compose ps -q "${APP_SERVICE}" 2>/dev/null | head -n 1)"
if [[ -z "${container_id}" ]]; then
sleep "${COMPOSE_HEALTHCHECK_POLL_INTERVAL_SECS}"
elapsed=$(( elapsed + COMPOSE_HEALTHCHECK_POLL_INTERVAL_SECS ))
continue
fi
state="$(docker inspect --format='{{.State.Health.Status}}' \
"${container_id}" 2>/dev/null || true)"
if [[ "${state}" == "healthy" ]]; then
echo ">>> Container is healthy."
return 0
fi
sleep "${COMPOSE_HEALTHCHECK_POLL_INTERVAL_SECS}"
elapsed=$(( elapsed + COMPOSE_HEALTHCHECK_POLL_INTERVAL_SECS ))
done
echo ">>> WARNING: health check timed out after ${timeout}s."
return 1
}
if [[ "${PREPARE_ONLY}" == "true" ]]; then
echo ">>> Preparing update by pulling latest image for ${APP_SERVICE}..."
compose_pull_app
echo ">>> Done."
echo ">>> Note: image is downloaded. Recreate ${APP_SERVICE} to apply the update."
exit 0
fi
if [[ "${NO_PULL}" != "true" ]]; then
echo ">>> Pulling latest image for ${APP_SERVICE}..."
compose_pull_app
fi
echo ">>> Recreating ${APP_SERVICE}..."
compose_up_app true || {
echo ">>> Compose up with --wait failed; falling back to simple recreate..."
compose_up_app false
wait_healthy || true
}
echo ">>> Current services:"
compose ps
echo ">>> Done."
if [[ "${SHOW_LOGS}" == "true" ]]; then
compose logs -f "${APP_SERVICE}"
fi