From 8320cec425695382fd9f960b90244a82effb2009 Mon Sep 17 00:00:00 2001 From: Lockszmith Date: Wed, 31 May 2023 22:56:18 -0400 Subject: [PATCH] Treating unfinished jobs + start_chart --- delete-all-catalogs.sh | 2 +- setup-catalog.sh | 2 +- shut-down-procedure.sh | 5 +++-- start-all-charts.sh | 4 ++-- start-chart.sh | 49 ++++++++++++++++++++++++++++++++++++++++++ startup-procedure.sh | 2 +- stop-all-chrats.sh | 12 +++++++++-- wait-for-idle-queue.sh | 23 +++++++++++++++++--- 8 files changed, 87 insertions(+), 12 deletions(-) create mode 100755 start-chart.sh diff --git a/delete-all-catalogs.sh b/delete-all-catalogs.sh index 036b293..6619e0f 100755 --- a/delete-all-catalogs.sh +++ b/delete-all-catalogs.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e diff --git a/setup-catalog.sh b/setup-catalog.sh index e5db370..d139c2d 100755 --- a/setup-catalog.sh +++ b/setup-catalog.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e diff --git a/shut-down-procedure.sh b/shut-down-procedure.sh index dd3e94e..34effc8 100755 --- a/shut-down-procedure.sh +++ b/shut-down-procedure.sh @@ -2,11 +2,12 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e ${SCRIPT_DIR}/delete-all-catalogs.sh ${SCRIPT_DIR}/wait-for-idle-queue.sh ${SCRIPT_DIR}/stop-all-chrats.sh - \ No newline at end of file + +cli -c 'system reboot' diff --git a/start-all-charts.sh b/start-all-charts.sh index 8fc5c6d..ca47a3f 100755 --- a/start-all-charts.sh +++ b/start-all-charts.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e @@ -52,4 +52,4 @@ ${SCRIPT_DIR}/wait-for-idle-queue.sh source "${SCRIPT_DIR}/start-all-charts.$(hostname).sh" ${SCRIPT_DIR}/wait-for-idle-queue.sh - \ No newline at end of file + diff --git a/start-chart.sh b/start-chart.sh new file mode 100755 index 0000000..6045b51 --- /dev/null +++ b/start-chart.sh @@ -0,0 +1,49 @@ +#! /usr/bin/env bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +USER_HOME=$HOME +[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions + +set -e + +get_charts() { + midclt call chart.release.query | jq -r " + .[] + | select( ${FILTER:-.status == \"STOPPED\"} ) + | .id + " | sort -u +} + +get_charts_by_category() { + local CATEGORY="${1}" + midclt call chart.release.query | jq -r ' + .[] + | select ( .chart_metadata.annotations."truecharts.org/catagories" == "- '"$CATEGORY"'\n" ) + | .id + ' +} + +start_chart_category() { + local CATEGORY="${1}" + printf "Starting apps by category: %s\n" "$CATEGORY" + get_charts_by_category "$CATEGORY" | while read chart; do + start_chart $chart + done + wait-for_idle_queue +} + +start_chart() { + local CHART="${1}" + printf 'Starting charts: %s... ' "$CHART" + midclt call chart.release.scale "$CHART" '{"replica_count": 1}' + printf '\n' +} + +if [[ -z "$1" ]]; then + get_charts + exit 2 +fi +${SCRIPT_DIR}/wait-for-idle-queue.sh +start_chart "${1}" +${SCRIPT_DIR}/wait-for-idle-queue.sh + diff --git a/startup-procedure.sh b/startup-procedure.sh index c1962bd..7409e95 100755 --- a/startup-procedure.sh +++ b/startup-procedure.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e diff --git a/stop-all-chrats.sh b/stop-all-chrats.sh index b89eaca..b92793b 100755 --- a/stop-all-chrats.sh +++ b/stop-all-chrats.sh @@ -2,12 +2,20 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e get_charts() { - midclt call chart.release.query | jq -r '.[] | select( .status != "STOPPED" ) | .id' + midclt call chart.release.query | jq -r ' + del(.[] + | select( .status == "STOPPED" or (.id|inside( + "traefik", + "plex-pms" + )) + )) + | .[].id + ' } stop_chart() { diff --git a/wait-for-idle-queue.sh b/wait-for-idle-queue.sh index b5a9c30..a8954f1 100755 --- a/wait-for-idle-queue.sh +++ b/wait-for-idle-queue.sh @@ -2,7 +2,7 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" -. ${SHRC_D:-$USER_HOME/.pqb.shrc.d}/01_util.functions +. ${SHRC_D:-$USER_HOME/.sz.shrc.d}/01_util.functions set -e @@ -11,12 +11,29 @@ count_unfinished_jobs() { } wait_for_idle_queue() { + local TIMEOUT="${1:-${TIMEOUT:-120}}" START="$(date +%s)" while [[ "$( count_unfinished_jobs )" -gt 0 ]]; do printf '.' + + if (( TIMEOUT > 0 && ($(date +%s) - START) > TIMEOUT )); then + printf 'TIMEOUT %s reached.\n' "$TIMEOUT" >&2 + printf '\n' + return 1 + break; + fi + sleep 2 done printf '\n' } -wait_for_idle_queue - \ No newline at end of file +abort_unfinished_jobs() { + midclt call core.get_jobs \ + | jq -r '.[] | select( .time_finished == null ) | .id ' \ + | xargs -rn1 midclt call core.job_abort +} + +wait_for_idle_queue || if [[ -z "$NO_AUTO_ABORT" ]]; then + abort_unfinished_jobs +fi +