From 850779bf21cc2b304688f29f5475bba711190620 Mon Sep 17 00:00:00 2001 From: mnissen Date: Wed, 3 Dec 2025 15:24:41 +0100 Subject: [PATCH 1/2] Remove user interaction for DWH prefix --- dwh-scripts/docker_delete_encounters.sh | 80 ++++++++++--------------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/dwh-scripts/docker_delete_encounters.sh b/dwh-scripts/docker_delete_encounters.sh index bceb782..732e931 100644 --- a/dwh-scripts/docker_delete_encounters.sh +++ b/dwh-scripts/docker_delete_encounters.sh @@ -35,36 +35,27 @@ check_date_order() { (( s <= e )) } -# Displaying running containers. DWH containers encode the DWH instance in its name -select_instance_prefix() { - local running - running=$(docker ps --format '{{.Names}}' || true) - [[ -n "$running" ]] || { echo "No running containers found." >&2; return 1; } - - local -a prefixes - mapfile -t prefixes < <(printf "%s\n" "$running" | awk -F'-' '{print $1}' | sort -u) - ((${#prefixes[@]})) || { echo "No prefixes could be derived." >&2; return 1; } - - # Use fd 3 for the actual return value; send all UI noise to stderr. - exec 3>&1 - { - echo "Select DWH to delete cases from:" - PS3="Enter number (or Ctrl-C to quit): " - local p - select p in "${prefixes[@]}"; do - if [[ -n "${p:-}" ]]; then - printf "%s\n" "$p" >&3 # ONLY this goes to captured stdout - return 0 - fi - echo "Invalid choice. Try again." - done - } 1>&2 -} + check_dwh_prefix() { + local prefix="$1" + local postgres="${prefix}-database-1" + local wildfly="${prefix}-wildfly-1" -service_running() { - local name="$1" - docker ps --format '{{.Names}}' | grep -Fqx "$name" -} + local running + running=$(docker ps --format '{{.Names}}' || true) + [[ -n "$running" ]] || { echo "No running containers found." >&2; return 1; } + + echo "$running" | grep -Fqx "$postgres" || { + echo "ERROR: PostgreSQL container '$postgres' with prefix '$prefix' is not running." >&2 + return 1 + } + + echo "$running" | grep -Fqx "$wildfly" || { + echo "ERROR: WildFly container '$wildfly' with prefix '$prefix' is not running." >&2 + return 1 + } + + return 0 + } get_container_id() { local name="$1" @@ -106,38 +97,31 @@ execute_sql() { main() { check_root - if [[ $# -ne 2 ]]; then - die "Two arguments are required. Usage: $0 yyyymmdd yyyymmdd" + if [[ $# -ne 3 ]]; then + die "Three arguments are required. Usage: $0 yyyymmdd yyyymmdd prefix" fi - local start_date="$1" end_date="$2" + local start_date="$1" end_date="$2" DWH_PREFIX="$3" validate_date "$start_date" || die "Invalid start date" validate_date "$end_date" || die "Invalid end date" check_date_order "$start_date" "$end_date" || die "Start date must be <= end date" note "Start Date: $start_date and End Date: $end_date are valid." + note "Using DWH prefix: $DWH_PREFIX" - local DWH_PREFIX - DWH_PREFIX="$(select_instance_prefix)" || die "No instance selected." - note "Selected prefix: $DWH_PREFIX" + check_dwh_prefix "$DWH_PREFIX" || die "Required container with prefix "$DWH_PREFIX" does not exist." local postgres="${DWH_PREFIX}-database-1" local wildfly="${DWH_PREFIX}-wildfly-1" - service_running "$postgres" || die "PostgreSQL service '$postgres' is not running. Please start it and try again." - - local wildfly_was_running=0 - if service_running "$wildfly"; then - wildfly_was_running=1 - local wf_id - wf_id="$(get_container_id "$wildfly")" - [[ -n "$wf_id" ]] || die "Could not resolve ID for '$wildfly'." - note "Stopping WildFly ($wildfly)…" - docker stop "$wildfly" >/dev/null + local wildfly_was_running=1 + note "Stopping WildFly ($wildfly)…" + docker stop "$wildfly" >/dev/null || wildfly_was_running=0 + if (( wildfly_was_running == 1 )); then wait_until_stopped "$wildfly" 90 - note "WildFly has stopped." + note "WildFly stopped." else - note "WildFly ($wildfly) is not running." + note "WildFly was not running." fi note "Executing SQL query on $postgres…" @@ -153,4 +137,4 @@ main() { note "Done." } -main "$@" +main "$@" \ No newline at end of file From 11512261c14c7239cceafa0848a84e6239703eb1 Mon Sep 17 00:00:00 2001 From: mnissen Date: Wed, 7 Jan 2026 18:21:01 +0100 Subject: [PATCH 2/2] User interaction was reintroduced based on the number of arguments. --- dwh-scripts/docker_delete_encounters.sh | 87 ++++++++++++++++--------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/dwh-scripts/docker_delete_encounters.sh b/dwh-scripts/docker_delete_encounters.sh index 732e931..5c5f28b 100644 --- a/dwh-scripts/docker_delete_encounters.sh +++ b/dwh-scripts/docker_delete_encounters.sh @@ -35,27 +35,31 @@ check_date_order() { (( s <= e )) } - check_dwh_prefix() { - local prefix="$1" - local postgres="${prefix}-database-1" - local wildfly="${prefix}-wildfly-1" - - local running - running=$(docker ps --format '{{.Names}}' || true) - [[ -n "$running" ]] || { echo "No running containers found." >&2; return 1; } - - echo "$running" | grep -Fqx "$postgres" || { - echo "ERROR: PostgreSQL container '$postgres' with prefix '$prefix' is not running." >&2 - return 1 - } - - echo "$running" | grep -Fqx "$wildfly" || { - echo "ERROR: WildFly container '$wildfly' with prefix '$prefix' is not running." >&2 - return 1 - } - - return 0 - } +# Displaying running containers. DWH containers encode the DWH instance in its name +select_instance_prefix() { + local running + running=$(docker ps --format '{{.Names}}' || true) + [[ -n "$running" ]] || { echo "No running containers found." >&2; return 1; } + + local -a prefixes + mapfile -t prefixes < <(printf "%s\n" "$running" | awk -F'-' '{print $1}' | sort -u) + ((${#prefixes[@]})) || { echo "No prefixes could be derived." >&2; return 1; } + + # Use fd 3 for the actual return value; send all UI noise to stderr. + exec 3>&1 + { + echo "Select DWH to delete cases from:" + PS3="Enter number (or Ctrl-C to quit): " + local p + select p in "${prefixes[@]}"; do + if [[ -n "${p:-}" ]]; then + printf "%s\n" "$p" >&3 # ONLY this goes to captured stdout + return 0 + fi + echo "Invalid choice. Try again." + done + } 1>&2 +} get_container_id() { local name="$1" @@ -66,6 +70,11 @@ get_container_id() { docker ps -aqf "name=^${name}$" || true } +service_running() { + local name="$1" + docker ps --format '{{.Names}}' | grep -Fqx "$name" +} + wait_until_stopped() { local name="$1" timeout="${2:-60}" t=0 while service_running "$name"; do @@ -97,31 +106,45 @@ execute_sql() { main() { check_root - if [[ $# -ne 3 ]]; then - die "Three arguments are required. Usage: $0 yyyymmdd yyyymmdd prefix" + if [[ $# -ne 2 && $# -ne 3 ]]; then + die "Two arguments are required. Usage: $0 yyyymmdd yyyymmdd [DWH_PREFIX]" fi - local start_date="$1" end_date="$2" DWH_PREFIX="$3" + local start_date="$1" + local end_date="$2" + local DWH_PREFIX + validate_date "$start_date" || die "Invalid start date" validate_date "$end_date" || die "Invalid end date" check_date_order "$start_date" "$end_date" || die "Start date must be <= end date" note "Start Date: $start_date and End Date: $end_date are valid." - note "Using DWH prefix: $DWH_PREFIX" - check_dwh_prefix "$DWH_PREFIX" || die "Required container with prefix "$DWH_PREFIX" does not exist." + if [[ $# -eq 3 ]]; then + DWH_PREFIX="$3" + note "DWH prefix: $DWH_PREFIX" + else + DWH_PREFIX="$(select_instance_prefix)" || die "No instance selected." + note "Selected DWH prefix: $DWH_PREFIX" + fi local postgres="${DWH_PREFIX}-database-1" local wildfly="${DWH_PREFIX}-wildfly-1" - local wildfly_was_running=1 - note "Stopping WildFly ($wildfly)…" - docker stop "$wildfly" >/dev/null || wildfly_was_running=0 - if (( wildfly_was_running == 1 )); then + service_running "$postgres" || die "PostgreSQL service '$postgres' is not running. Please start it and try again." + + local wildfly_was_running=0 + if service_running "$wildfly"; then + wildfly_was_running=1 + local wf_id + wf_id="$(get_container_id "$wildfly")" + [[ -n "$wf_id" ]] || die "Could not resolve ID for '$wildfly'." + note "Stopping WildFly ($wildfly)…" + docker stop "$wildfly" >/dev/null wait_until_stopped "$wildfly" 90 - note "WildFly stopped." + note "WildFly has stopped." else - note "WildFly was not running." + note "WildFly ($wildfly) is not running." fi note "Executing SQL query on $postgres…"