Files
Logic/run_docker_modules.sh
T

125 lines
2.2 KiB
Bash
Raw Normal View History

2026-04-27 04:31:32 +03:30
#!/usr/bin/env bash
set -euo pipefail
BUILD_FLAG=0
TARGET_MODULES=()
usage() {
cat <<'EOF'
Usage:
./run_docker_modules.sh [--build] [module ...]
Options:
--build Run `docker compose up --build -d`
-h, --help Show this help message
Examples:
./run_docker_modules.sh
./run_docker_modules.sh --build
./run_docker_modules.sh Ai Backend
./run_docker_modules.sh --build Ai SensorHub
EOF
}
while [[ "$#" -gt 0 ]]; do
case "$1" in
--build)
BUILD_FLAG=1
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
while [[ "$#" -gt 0 ]]; do
TARGET_MODULES+=("$1")
shift
done
;;
-*)
echo "Unknown option: $1" >&2
usage
exit 1
;;
*)
TARGET_MODULES+=("$1")
shift
;;
esac
done
find_compose_file() {
local module_dir="$1"
local candidates=(
"docker-compose.yaml"
"docker-compose.yml"
"compose.yaml"
"compose.yml"
)
local candidate
for candidate in "${candidates[@]}"; do
if [[ -f "${module_dir}/${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
return 1
}
collect_modules_with_compose() {
local dir
for dir in */ ; do
dir="${dir%/}"
[[ -d "$dir" ]] || continue
if find_compose_file "$dir" >/dev/null; then
printf '%s\n' "$dir"
fi
done
}
run_module() {
local module_dir="$1"
local compose_file
local compose_path
if [[ ! -d "$module_dir" ]]; then
echo "Skipping ${module_dir}: directory not found."
return 0
fi
if ! compose_file="$(find_compose_file "$module_dir")"; then
echo "Skipping ${module_dir}: no docker compose file found."
return 0
fi
compose_path="${module_dir}/${compose_file}"
echo "Starting ${module_dir} with ${compose_file}"
if [[ "$BUILD_FLAG" -eq 1 ]]; then
docker compose -f "${compose_path}" up --build -d
else
docker compose -f "${compose_path}" up -d
fi
}
if [[ "${#TARGET_MODULES[@]}" -eq 0 ]]; then
mapfile -t TARGET_MODULES < <(collect_modules_with_compose)
fi
if [[ "${#TARGET_MODULES[@]}" -eq 0 ]]; then
echo "No modules with docker compose files found."
exit 0
fi
for module_dir in "${TARGET_MODULES[@]}"; do
run_module "$module_dir"
done
echo "Done."