#!/usr/bin/env bash
set -euo >/dev/null 2>&1
wait_for_container_healthy() {
local container_name="$1"
local check_interval=${2:-10}
if [ -z "${container_name}" ]; then
error "wait_for_container_healthy function missing required parameter: container name"
fi
info "Waiting for container [${container_name}] to reach Healthy status (infinite wait - press Ctrl+C to interrupt)"
local start_time=$(date +%s)
local health_status
local current_time
local elapsed_time
while true; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
health_status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}no_healthcheck{{end}}' "${container_name}" 2>/dev/null)
case "${health_status}" in
"healthy")
echo ""
success "Container [${container_name}] reached Healthy status! (total wait ${elapsed_time} seconds)"
return
;;
"starting")
echo -n "."
;;
"unhealthy")
echo ""
error "Container [${container_name}] health check failed (unhealthy)!"
;;
"no_healthcheck")
echo ""
error "Container [${container_name}] has no health check configured, cannot wait for Healthy status!"
;;
*)
info "Container [${container_name}] unknown status (${health_status}), waited ${elapsed_time} seconds..."
;;
esac
sleep "${check_interval}"
done
}
check_containers() {
local container_str="$1"
local container
if [ -z "${container_str}" ]; then
info "No containers to check (empty string received)"
return
fi
IFS=' ' read -r -a containers <<< "${container_str}"
for container in "${containers[@]}"; do
if [ -n "${container}" ]; then
wait_for_container_healthy "${container}"
fi
done
}
wait_for_mysql() {
local mysql_container=${DEPLOY_VARS["MYSQL_DOCKER"]}
local db_password=${DEPLOY_VARS["DB_ROOT_PASSWORD"]}
local check_interval=3
info "Waiting for MySQL container [${mysql_container}] to start (infinite wait - press Ctrl+C to interrupt)"
while true; do
if docker exec -i "${mysql_container}" mysql -u root -p"${db_password}" -h 127.0.0.1 -e "SELECT 1" 2>/dev/null; then
success "MySQL container [${mysql_container}] is fully ready!"
return
fi
echo -n "."
sleep "${check_interval}"
done
}
create_all_dbs() {
local db_names=(
"${RUNTIME_VARS["AGENT_DB_NAME"]}"
"${RUNTIME_VARS["OPS_DB_NAME"]}"
"${DEPLOY_VARS["DEEPSEARCH_DB_NAME"]}"
"${DEPLOY_VARS["RUNTIME_DB_NAME"]}"
)
for db_name in "${db_names[@]}"; do
create_db "${db_name}"
done
}
create_db() {
local mysql_container=${DEPLOY_VARS["MYSQL_DOCKER"]}
local db_password=${DEPLOY_VARS["DB_ROOT_PASSWORD"]}
local db_name="$1"
info "Checking if database ${db_name} is created"
docker exec -i "${mysql_container}" mysql -u root -p"${db_password}" -h 127.0.0.1 << EOF
CREATE DATABASE IF NOT EXISTS ${db_name} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EOF
if [ $? -eq 0 ]; then
success "Database ${db_name} is created"
else
error "Database ${db_name} creation failed! Check container logs or network connection"
fi
}
check_container_running() {
local container_name="$1"
if ! docker ps -a --format "{{.Names}}" | grep -qw "$container_name"; then
error "Container $container_name does not exist"
fi
local status=$(docker inspect --format "{{.State.Status}}" "$container_name" 2>/dev/null)
if [[ "$status" != "running" ]]; then
error "Container $container_name is not running. Current status: $status"
fi
success "Container $container_name is running"
}
check_container_healthy() {
local container_name="$1"
check_container_running ${container_name}
local health=$(docker inspect --format "{{.State.Health.Status}}" "$container_name" 2>/dev/null)
if [[ "$health" == "unhealthy" ]]; then
error "Container $container_name is unhealthy"
fi
success "Container $container_name is healthy"
}