-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·216 lines (180 loc) · 4.84 KB
/
Copy pathutils.sh
File metadata and controls
executable file
·216 lines (180 loc) · 4.84 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
#!/usr/bin/env bash
set -euo pipefail
PROJECT_ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)
DEVELOPMENT_DIR="${PROJECT_ROOT}/development"
BASE_COMPOSE_FILE="${DEVELOPMENT_DIR}/docker-compose.yml"
INFRA_SERVICES=(
event-sourcing-event-store
event-sourcing-projection-store
event-sourcing-email-server
event-sourcing-file-storage
event-sourcing-event-bus
)
usage() {
cat <<'EOF'
Usage: ./utils.sh <command>
Commands:
run Start infrastructure containers and run the backend locally
infra-up Start only the supporting infrastructure containers
infra-down Stop and remove the supporting infrastructure containers
infra-status Show status for the supporting infrastructure containers
help Display this help message
EOF
}
require_command() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: required command '$cmd' not found in PATH." >&2
exit 1
fi
}
init_compose_command() {
if docker compose version >/dev/null 2>&1; then
DOCKER_COMPOSE=(docker compose)
elif command -v docker-compose >/dev/null 2>&1; then
DOCKER_COMPOSE=(docker-compose)
else
echo "Error: docker compose is required." >&2
exit 1
fi
}
compose_cmd() {
local files=( -f "${BASE_COMPOSE_FILE}" )
"${DOCKER_COMPOSE[@]}" --project-directory "${DEVELOPMENT_DIR}" "${files[@]}" "$@"
}
make_local_compose_override() {
require_command npx
local tmp_override
tmp_override=$(mktemp 2>/dev/null || mktemp -t docker-compose.local.override)
local override_json
override_json='{
"services": {
"event-sourcing-event-store": {
"ports": [ "5432:5432" ]
},
"event-sourcing-projection-store": {
"ports": [ "27017:27017" ]
},
"event-sourcing-email-server": {
"ports": [ "1025:1025" ]
},
"event-sourcing-event-bus": {
"depends_on": {
"event-sourcing-event-store": { "condition": "service_healthy" }
}
}
}
}'
if ! echo "${override_json}" | npx yaml >"${tmp_override}" 2>/dev/null; then
echo "Error: Failed to generate docker compose override via YAML CLI." >&2
return 1
fi
echo "${tmp_override}"
}
wait_for_container() {
local container="$1"
local retries=30
while (( retries > 0 )); do
local status
if ! status=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container" 2>/dev/null); then
sleep 1
((retries--))
continue
fi
if [[ "$status" == "healthy" ]]; then
echo "${container} is healthy"
return 0
elif [[ "$status" == "running" ]]; then
echo "${container} is running"
return 0
elif [[ "$status" == "exited" ]]; then
echo "Error: ${container} has exited" >&2
exit 1
fi
echo "Waiting for ${container} (status: ${status})..."
sleep 2
((retries--))
done
echo "Error: Timed out waiting for ${container} to start properly." >&2
exit 1
}
wait_for_infra() {
echo "Checking container status..."
for container in "${INFRA_SERVICES[@]}"; do
wait_for_container "$container"
done
echo ""
echo "All infrastructure containers are running successfully!"
}
start_infra() {
local local_override
local_override=$(make_local_compose_override)
trap '[[ -n "${local_override}" && -f "${local_override}" ]] && rm -f "${local_override}"' EXIT
compose_cmd up -d --no-deps "${INFRA_SERVICES[@]}"
wait_for_infra
}
stop_infra() {
compose_cmd stop "${INFRA_SERVICES[@]}" >/dev/null 2>&1 || true
compose_cmd rm -f "${INFRA_SERVICES[@]}" >/dev/null 2>&1 || true
}
infra_status() {
compose_cmd ps --format "table {{.Name}}\t{{.Status}}"
}
run_backend() {
echo "Starting backend with local environment..."
(cd "${PROJECT_ROOT}" && npm run watch)
}
command_run() {
start_infra
run_backend
}
command_infra_up() {
start_infra
}
command_infra_down() {
echo "Stopping infrastructure..."
local local_override
local_override=$(make_local_compose_override)
trap '[[ -n "${local_override}" && -f "${local_override}" ]] && rm -f "${local_override}"' EXIT
stop_infra
echo "Infrastructure stopped."
}
command_infra_status() {
local local_override
local_override=$(make_local_compose_override)
trap '[[ -n "${local_override}" && -f "${local_override}" ]] && rm -f "${local_override}"' EXIT
infra_status
}
main() {
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
require_command docker
init_compose_command
local command="${1:-help}"
shift || true
case "$command" in
run)
command_run "$@"
;;
infra-up)
command_infra_up "$@"
;;
infra-down)
command_infra_down "$@"
;;
infra-status)
command_infra_status "$@"
;;
help|--help|-h)
usage
;;
*)
echo "Unknown command: $command" >&2
usage
exit 1
;;
esac
}
main "$@"