Files

110 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
env_file="${repo_root}/.env.devprod"
fallback_env_file="${repo_root}/.env.devprod.example"
source "${repo_root}/scripts/lib-compose-project.sh"
usage() {
cat <<USAGE
Usage: ./stop-devprod.sh [options]
Stops and removes devprod containers (without deleting volumes) by running
'docker compose down --remove-orphans' against the devprod compose files.
Options:
--mode ssl|nossl|all Which devprod stack to stop (default: all)
--ssl Alias for --mode ssl
--nossl Alias for --mode nossl
--all Alias for --mode all
-h, --help Show this help
USAGE
}
mode="all"
while [[ $# -gt 0 ]]; do
case "$1" in
--mode)
mode="${2:-}"
shift 2
;;
--mode=*)
mode="${1#*=}"
shift
;;
--ssl)
mode="ssl"
shift
;;
--nossl)
mode="nossl"
shift
;;
--all)
mode="all"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
if [[ "${mode}" != "ssl" && "${mode}" != "nossl" && "${mode}" != "all" ]]; then
echo "Invalid mode: ${mode}"
usage
exit 1
fi
compose_args=()
if [[ -f "${env_file}" ]]; then
export_compose_project_name_from_env "${env_file}" || true
compose_args+=(--env-file "${env_file}")
elif [[ -f "${fallback_env_file}" ]]; then
compose_args+=(--env-file "${fallback_env_file}")
fi
run_down() {
local compose_file="$1"
local label="$2"
if [[ ! -f "${compose_file}" ]]; then
echo "Skipping missing compose file: ${compose_file}"
return 0
fi
echo "Stopping ${label} (${compose_file##*/})..."
(cd "${repo_root}" && docker compose -f "${compose_file}" "${compose_args[@]}" down --remove-orphans)
}
failures=0
if [[ "${mode}" == "ssl" || "${mode}" == "all" ]]; then
if ! run_down "${repo_root}/docker-compose.devprod.yml" "devprod SSL stack"; then
failures=1
fi
fi
if [[ "${mode}" == "nossl" || "${mode}" == "all" ]]; then
if ! run_down "${repo_root}/docker-compose.devprod.nossl.yml" "devprod no-SSL stack"; then
failures=1
fi
fi
if [[ "${failures}" -ne 0 ]]; then
echo
echo "One or more stop operations failed."
exit 1
fi
echo
echo "Done."