-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathopik.sh
executable file
·320 lines (280 loc) · 10.3 KB
/
opik.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REQUIRED_CONTAINERS=("opik-clickhouse-1" "opik-mysql-1" "opik-python-backend-1" "opik-redis-1" "opik-frontend-1" "opik-backend-1" "opik-minio-1")
generate_uuid() {
if command -v uuidgen >/dev/null 2>&1; then
uuidgen
else
cat /proc/sys/kernel/random/uuid 2>/dev/null || date +%s%N
fi
}
print_usage() {
echo "Usage: opik.sh [OPTION]"
echo ""
echo "Options:"
echo " --verify Check if all containers are healthy"
echo " --info Display welcome system status, only if all containers are running"
echo " --stop Stop all containers and clean up"
echo " --debug Enable debug mode (verbose output)"
echo " --help Show this help message"
echo ""
echo "If no option is passed, the script will start missing containers and then show the system status."
}
check_docker_status() {
# Ensure Docker is running
if ! docker info >/dev/null 2>&1; then
echo "❌ Docker is not running or not accessible. Please start Docker first."
exit 1
fi
}
check_containers_status() {
local show_output="${1:-false}"
local all_ok=true
check_docker_status
for container in "${REQUIRED_CONTAINERS[@]}"; do
status=$(docker inspect -f '{{.State.Status}}' "$container" 2>/dev/null)
health=$(docker inspect -f '{{.State.Health.Status}}' "$container" 2>/dev/null)
if [[ "$status" != "running" ]]; then
echo "❌ $container is not running (status=$status)"
all_ok=false
elif [[ "$health" != "" && "$health" != "healthy" ]]; then
echo "❌ $container is running but not healthy (health=$health)"
all_ok=false
else
[[ "$show_output" == "true" ]] && echo "✅ $container is running and healthy"
fi
done
$all_ok && return 0 || return 1
}
start_missing_containers() {
check_docker_status
uuid=$(generate_uuid)
start_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
send_install_report "$uuid" "false" "$start_time"
[[ "$DEBUG_MODE" == true ]] && echo "🔍 Checking required containers..."
all_running=true
for container in "${REQUIRED_CONTAINERS[@]}"; do
status=$(docker inspect -f '{{.State.Status}}' "$container" 2>/dev/null)
if [[ "$status" != "running" ]]; then
[[ "$DEBUG_MODE" == true ]] && echo "🔴 $container is not running (status: ${status:-not found})"
all_running=false
else
[[ "$DEBUG_MODE" == true ]] && echo "✅ $container is already running"
fi
done
echo "🔄 Starting missing containers..."
cd "$script_dir/deployment/docker-compose"
if [[ "${BUILD_MODE}" = "true" ]]; then
export COMPOSE_BAKE=true
fi
docker compose up -d ${BUILD_MODE:+--build}
echo "⏳ Waiting for all containers to be running and healthy..."
max_retries=60
interval=1
all_running=true
for container in "${REQUIRED_CONTAINERS[@]}"; do
retries=0
[[ "$DEBUG_MODE" == true ]] && echo "⏳ Waiting for $container..."
while true; do
status=$(docker inspect -f '{{.State.Status}}' "$container" 2>/dev/null)
health=$(docker inspect -f '{{.State.Health.Status}}' "$container" 2>/dev/null)
if [[ "$status" != "running" ]]; then
echo "❌ $container failed to start (status: $status)"
break
fi
if [[ "$health" == "healthy" ]]; then
[[ "$DEBUG_MODE" == true ]] && echo "✅ $container is now running and healthy!"
break
elif [[ "$health" == "starting" ]]; then
[[ "$DEBUG_MODE" == true ]] && echo "⏳ $container is starting... retrying (${retries}s)"
sleep "$interval"
retries=$((retries + 1))
if [[ $retries -ge $max_retries ]]; then
echo "⚠️ $container is still not healthy after ${max_retries}s"
all_running=false
break
fi
else
echo "❌ $container health state is '$health'"
all_running=false
break
fi
done
done
if $all_running; then
send_install_report "$uuid" "true" "$start_time"
fi
}
stop_containers() {
check_docker_status
echo "🛑 Stopping all required containers..."
cd "$script_dir/deployment/docker-compose"
docker compose stop
echo "✅ All containers stopped and cleaned up!"
}
print_banner() {
check_docker_status
frontend_port=$(docker inspect -f '{{ (index (index .NetworkSettings.Ports "5173/tcp") 0).HostPort }}' opik-frontend-1 2>/dev/null)
ui_url="http://localhost:${frontend_port:-5173}"
echo ""
echo "╔═════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ 🚀 OPIK PLATFORM 🚀 ║"
echo "║ ║"
echo "╠═════════════════════════════════════════════════════════════════╣"
echo "║ ║"
echo "║ ✅ All services started successfully! ║"
echo "║ ║"
echo "║ 📊 Access the UI: ║"
echo "║ $ui_url ║"
echo "║ ║"
echo "║ 🛠️ Configure the Python SDK: ║"
echo "║ \$ python --version ║"
echo "║ \$ pip install opik ║"
echo "║ \$ opik configure ║"
echo "║ ║"
echo "║ 📚 Documentation: https://www.comet.com/docs/opik/ ║"
echo "║ ║"
echo "║ 💬 Need help? Join our community: https://chat.comet.com ║"
echo "║ ║"
echo "╚═════════════════════════════════════════════════════════════════╝"
}
# Check installation
send_install_report() {
uuid="$1"
event_completed="$2" # Pass "true" to send install_completed
start_time="$3" # Optional: start time in ISO 8601 format
if [ "$OPIK_USAGE_REPORT_ENABLED" != "true" ] && [ "$OPIK_USAGE_REPORT_ENABLED" != "" ]; then
[[ "$DEBUG_MODE" == true ]] && echo "[DEBUG] Usage reporting is disabled. Skipping install report."
return
fi
INSTALL_MARKER_FILE="$script_dir/.opik_install_reported"
if [ -f "$INSTALL_MARKER_FILE" ]; then
[[ "$DEBUG_MODE" == true ]] && echo "[DEBUG] Install report already sent; skipping."
return
fi
# Check if either curl or wget is available
if command -v curl >/dev/null 2>&1; then
HTTP_TOOL="curl"
elif command -v wget >/dev/null 2>&1; then
HTTP_TOOL="wget"
else
[[ "$DEBUG_MODE" == true ]] && echo "[WARN] Neither curl nor wget is available; skipping usage report."
return
fi
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [ "$event_completed" = "true" ]; then
event_type="opik_install_completed"
end_time="$timestamp"
json_payload=$(cat <<EOF
{
"anonymous_id": "$uuid",
"event_type": "$event_type",
"event_properties": {
"start_time": "$start_time",
"end_time": "$end_time"
}
}
EOF
)
else
event_type="opik_install_started"
json_payload=$(cat <<EOF
{
"anonymous_id": "$uuid",
"event_type": "$event_type",
"event_properties": {
"start_time": "$start_time"
}
}
EOF
)
fi
url="https://stats.comet.com/notify/event/"
if [ "$HTTP_TOOL" = "curl" ]; then
curl -s -X POST -H "Content-Type: application/json" -d "$json_payload" "$url" >/dev/null 2>&1
else
tmpfile=$(mktemp)
echo "$json_payload" > "$tmpfile"
wget --quiet --method POST --header="Content-Type: application/json" --body-file="$tmpfile" -O /dev/null "$url"
rm -f "$tmpfile"
fi
if [ $event_type = "install_completed" ]; then
touch "$INSTALL_MARKER_FILE"
[[ "$DEBUG_MODE" == true ]] && echo "[DEBUG] Post-install report sent successfully."
else
[[ "$DEBUG_MODE" == true ]] && echo "[DEBUG] Install started report sent successfully."
fi
}
# Default: no debug
DEBUG_MODE=false
# Main logic
case "$1" in
--verify)
echo "🔍 Verifying container health..."
check_containers_status "true"
exit $?
;;
--info)
echo "ℹ️ Checking if all containers are up before displaying system status..."
if check_containers_status "true"; then
print_banner
exit 0
else
echo "⚠️ Some containers are not running/healthy. Please run './opik.sh' to start them."
exit 1
fi
;;
--stop)
stop_containers
exit 0
;;
--help)
print_usage
exit 0
;;
--debug)
DEBUG_MODE=true
echo "🐞 Debug mode enabled."
echo "🔍 Checking container status and starting missing ones..."
start_missing_containers
sleep 2
echo "🔄 Re-checking container status..."
if check_containers_status; then
print_banner
else
echo "⚠️ Some containers are still not healthy. Please check manually using './opik.sh --verify'"
exit 1
fi
;;
--build)
BUILD_MODE=true
echo "🔍 Checking container status and starting missing ones..."
start_missing_containers
sleep 2
echo "🔄 Re-checking container status..."
if check_containers_status; then
print_banner
else
echo "⚠️ Some containers are still not healthy. Please check manually using './opik.sh --verify'"
exit 1
fi
;;
"")
echo "🔍 Checking container status and starting missing ones..."
start_missing_containers
sleep 2
echo "🔄 Re-checking container status..."
if check_containers_status "true"; then
print_banner
else
echo "⚠️ Some containers are still not healthy. Please check manually using './opik.sh --verify'"
exit 1
fi
;;
*)
echo "❌ Unknown option: $1"
print_usage
exit 1
;;
esac