128 lines
4.6 KiB
Bash
128 lines
4.6 KiB
Bash
#!/usr/bin/env bash
|
|
# Run the separate monitoring project against the isolated demo stand.
|
|
set -euo pipefail
|
|
|
|
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
|
|
monitoring_file="$root/deploy/docker/docker-compose.monitoring.yml"
|
|
demo_example_file="$root/deploy/demo/demo.env.example"
|
|
docker_cli="${DOCKER:-docker}"
|
|
|
|
fail() {
|
|
printf 'demo monitoring: %s\n' "$1" >&2
|
|
exit 2
|
|
}
|
|
|
|
read_env_value() {
|
|
local file=$1 key=$2 line value=''
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
if [[ "$line" == "$key="* ]]; then
|
|
value=${line#*=}
|
|
fi
|
|
done < "$file"
|
|
printf '%s' "$value"
|
|
}
|
|
|
|
if [[ "$#" -ne 1 ]]; then
|
|
fail 'usage: demo-monitoring.sh config|up|down|logs'
|
|
fi
|
|
action=$1
|
|
case "$action" in
|
|
config | up | down | logs) ;;
|
|
*) fail 'usage: demo-monitoring.sh config|up|down|logs' ;;
|
|
esac
|
|
|
|
if [[ "${DEMO_MODE:-}" != isolated ]]; then
|
|
fail 'refusing to use demo monitoring: set DEMO_MODE=isolated'
|
|
fi
|
|
|
|
env_file="${ENV_FILE:-$root/.env}"
|
|
demo_env_file="${DEMO_ENV_FILE:-$root/deploy/demo/demo.env}"
|
|
[[ -f "$env_file" ]] || fail "missing env file $env_file"
|
|
[[ -f "$demo_example_file" ]] || fail "missing demo env example $demo_example_file"
|
|
[[ -f "$demo_env_file" ]] || fail "missing demo env file $demo_env_file"
|
|
|
|
configured_project=$(read_env_value "$demo_env_file" DEMO_PROJECT)
|
|
if [[ -z "$configured_project" && "$demo_env_file" != "$demo_example_file" ]]; then
|
|
configured_project=$(read_env_value "$demo_example_file" DEMO_PROJECT)
|
|
fi
|
|
project="${DEMO_PROJECT:-${configured_project:-lincodex-demo}}"
|
|
ordinary_project="${APP_PROJECT:-app}"
|
|
configured_ordinary_project=$(read_env_value "$env_file" APP_PROJECT)
|
|
if [[ ! "$project" =~ ^[a-z0-9][a-z0-9_-]*$ || "$project" != *demo* ]]; then
|
|
fail 'refusing to use demo monitoring: DEMO_PROJECT must be a Compose project name that contains demo'
|
|
fi
|
|
if [[ "$project" == "$ordinary_project" || ( -n "$configured_ordinary_project" && "$project" == "$configured_ordinary_project" ) ]]; then
|
|
fail 'refusing to use demo monitoring: DEMO_PROJECT must differ from the ordinary project APP_PROJECT'
|
|
fi
|
|
|
|
monitoring_project="${project}-monitoring"
|
|
ordinary_monitoring_project="${MONITORING_PROJECT:-monitoring}"
|
|
if [[ "$monitoring_project" == "$ordinary_monitoring_project" ]]; then
|
|
fail 'refusing to use demo monitoring: its Compose project must differ from MONITORING_PROJECT'
|
|
fi
|
|
|
|
grafana_port=$(read_env_value "$demo_env_file" DEMO_GRAFANA_PORT)
|
|
if [[ -z "$grafana_port" && "$demo_env_file" != "$demo_example_file" ]]; then
|
|
grafana_port=$(read_env_value "$demo_example_file" DEMO_GRAFANA_PORT)
|
|
fi
|
|
grafana_port="${grafana_port:-3001}"
|
|
[[ "$grafana_port" =~ ^[1-9][0-9]{0,4}$ ]] || fail 'DEMO_GRAFANA_PORT must be a port number'
|
|
(( grafana_port <= 65535 )) || fail 'DEMO_GRAFANA_PORT must be between 1 and 65535'
|
|
|
|
resolve_socket() {
|
|
local endpoint context configured_socket socket_path
|
|
configured_socket="${DOCKER_SOCKET_PATH:-$(read_env_value "$env_file" DOCKER_SOCKET_PATH)}"
|
|
if [[ -n "${DEMO_DOCKER_SOCKET_PATH:-}" ]]; then
|
|
socket_path=$DEMO_DOCKER_SOCKET_PATH
|
|
elif [[ -n "$configured_socket" && "$configured_socket" != /var/run/docker.sock ]]; then
|
|
socket_path=$configured_socket
|
|
else
|
|
endpoint="${DOCKER_HOST:-}"
|
|
if [[ -z "$endpoint" ]]; then
|
|
context=$("$docker_cli" context show) || fail 'could not read the active Docker context'
|
|
endpoint=$("$docker_cli" context inspect "$context" --format '{{ (index .Endpoints "docker").Host }}') ||
|
|
fail 'could not read the Docker endpoint from the active context'
|
|
fi
|
|
case "$endpoint" in
|
|
unix://*) socket_path=${endpoint#unix://} ;;
|
|
*) fail 'demo monitoring requires a local Unix Docker socket; the active Docker endpoint is not local' ;;
|
|
esac
|
|
fi
|
|
|
|
case "$socket_path" in
|
|
unix://*) socket_path=${socket_path#unix://} ;;
|
|
esac
|
|
[[ "$socket_path" == /* ]] || fail 'the Docker socket path must be absolute'
|
|
if [[ "$action" != config && ! -S "$socket_path" ]]; then
|
|
fail "Docker socket not found at $socket_path; check the active Docker context"
|
|
fi
|
|
printf '%s' "$socket_path"
|
|
}
|
|
|
|
socket_path=$(resolve_socket)
|
|
env_files=(--env-file "$env_file" --env-file "$demo_example_file")
|
|
if [[ "$demo_env_file" != "$demo_example_file" ]]; then
|
|
env_files+=(--env-file "$demo_env_file")
|
|
fi
|
|
|
|
compose() {
|
|
APP_PROJECT="$project" APP_NETWORK="${project}_default" APP_STORAGE_NETWORK="${project}_storage" \
|
|
DOCKER_SOCKET_PATH="$socket_path" GRAFANA_PORT="$grafana_port" \
|
|
"$docker_cli" compose "${env_files[@]}" -p "$monitoring_project" -f "$monitoring_file" "$@"
|
|
}
|
|
|
|
case "$action" in
|
|
config)
|
|
compose config --quiet
|
|
;;
|
|
up)
|
|
compose up -d
|
|
;;
|
|
down)
|
|
compose down
|
|
;;
|
|
logs)
|
|
compose logs -f
|
|
;;
|
|
esac
|