echo fix in common.sh

This commit is contained in:
2026-04-05 03:55:55 +00:00
parent 729fac99d4
commit b3aeef4df4
+43 -44
View File
@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# ----------------- UNRAID OPS COMMON LIBRARY (STABLE FRAMEWORK v1) ---------------------------- # ----------------- UNRAID OPS COMMON LIBRARY (STABLE FRAMEWORK v1) ----------------------------
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# ICONS # ICONS
ICON_INFO="️" ICON_INFO="️"
ICON_WARN="⚠️" ICON_WARN="⚠️"
@@ -13,17 +13,17 @@ ICON_STOP="🛑"
ICON_RETRY="🔁" ICON_RETRY="🔁"
ICON_SYNC="🔄" ICON_SYNC="🔄"
ICON_GEAR="⚙️" ICON_GEAR="⚙️"
# OUTPUT # OUTPUT
info() { echo "$ICON_INFO [INFO] $*"; } info() { echo "$ICON_INFO [INFO] $*"; }
warn() { echo "$ICON_WARN [WARN] $*"; } warn() { echo "$ICON_WARN [WARN] $*"; }
error() { echo "$ICON_ERROR [ERROR] $*"; } error() { echo "$ICON_ERROR [ERROR] $*"; }
success() { echo "$ICON_SUCCESS [OK] $*"; } success() { echo "$ICON_SUCCESS [OK] $*"; }
log() { log() {
[[ "${ENABLE_LOGGING:-false}" == true ]] && echo "[LOG] $*" [[ "${ENABLE_LOGGING:-false}" == true ]] && echo "[LOG] $*"
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# ARG PARSER # ARG PARSER
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
@@ -31,14 +31,14 @@ parse_args() {
ENABLE_LOGGING=${ENABLE_LOGGING:-false} ENABLE_LOGGING=${ENABLE_LOGGING:-false}
DRY_RUN=${DRY_RUN:-false} DRY_RUN=${DRY_RUN:-false}
SHOW_STATUS=${SHOW_STATUS:-false} SHOW_STATUS=${SHOW_STATUS:-false}
CLEAN_ARGS=() CLEAN_ARGS=()
for ARG in "$@"; do for ARG in "$@"; do
if [[ "$ARG" == *=* ]]; then if [[ "$ARG" == *=* ]]; then
VAR="${ARG%%=*}" VAR="${ARG%%=*}"
VAL="${ARG#*=}" VAL="${ARG#*=}"
case "$VAR" in case "$VAR" in
LOG) LOG)
[[ "$VAL" == "true" ]] && ENABLE_LOGGING=true [[ "$VAL" == "true" ]] && ENABLE_LOGGING=true
@@ -67,23 +67,23 @@ parse_args() {
esac esac
fi fi
done done
PARSED_ARGS=("${CLEAN_ARGS[@]}") PARSED_ARGS=("${CLEAN_ARGS[@]}")
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# VALIDATION # VALIDATION
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
require_var() { require_var() {
[[ -z "${!1:-}" ]] && error "Missing required: $1" && exit 1 [[ -z "${!1:-}" ]] && error "Missing required: $1" && exit 1
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# HOST DETECTION # HOST DETECTION
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
detect_hosts() { detect_hosts() {
LOCAL_HOSTNAME="$(hostname)" LOCAL_HOSTNAME="$(hostname)"
if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then
LOCAL_SERVER_NAME="$HOST1" LOCAL_SERVER_NAME="$HOST1"
REMOTE_SERVER_NAME="$HOST2" REMOTE_SERVER_NAME="$HOST2"
@@ -94,82 +94,81 @@ detect_hosts() {
error "Unknown host: $LOCAL_HOSTNAME" error "Unknown host: $LOCAL_HOSTNAME"
exit 1 exit 1
fi fi
declare -A SSH_KEYS declare -A SSH_KEYS
SSH_KEYS["$HOST1|$HOST2"]="$HOST1_SSH_KEY" SSH_KEYS["$HOST1|$HOST2"]="$HOST1_SSH_KEY"
SSH_KEYS["$HOST2|$HOST1"]="$HOST2_SSH_KEY" SSH_KEYS["$HOST2|$HOST1"]="$HOST2_SSH_KEY"
SSH_KEY="${SSH_KEYS[$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME]}" SSH_KEY="${SSH_KEYS[$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME]}"
[[ -z "$SSH_KEY" ]] && error "Missing SSH key mapping" && exit 1 [[ -z "$SSH_KEY" ]] && error "Missing SSH key mapping" && exit 1
info "Host: $LOCAL_SERVER_NAME$REMOTE_SERVER_NAME" info "Host: $LOCAL_SERVER_NAME$REMOTE_SERVER_NAME"
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# REMOTE IP # REMOTE IP
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
resolve_remote_ip() { resolve_remote_ip() {
log "Resolving remote IP..." log "Resolving remote IP..."
REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null) REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null)
[[ -z "$REMOTE_SERVER" ]] && error "Failed to resolve remote IP for $REMOTE_SERVER_NAME" && exit 1 [[ -z "$REMOTE_SERVER" ]] && error "Failed to resolve remote IP for $REMOTE_SERVER_NAME" && exit 1
info "Remote IP: $REMOTE_SERVER" info "Remote IP: $REMOTE_SERVER"
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# CONTAINERS # CONTAINERS
# CRITICAL_CONTAINER_NAMES and DELAYED_CONTAINERS must be bash arrays before calling these. # CRITICAL_CONTAINER_NAMES and DELAYED_CONTAINERS must be bash arrays before calling these.
# rsync.sh handles the read -r -a conversion from profile strings. # rsync.sh handles the read -r -a conversion from profile strings.
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
RUNNING_CONTAINERS=() RUNNING_CONTAINERS=()
stop_containers() { stop_containers() {
if [[ ${#CRITICAL_CONTAINER_NAMES[@]} -eq 0 ]] || \ if [[ ${#CRITICAL_CONTAINER_NAMES[@]} -eq 0 ]] || \
[[ "${CRITICAL_CONTAINER_NAMES[*]}" == "" ]]; then [[ "${CRITICAL_CONTAINER_NAMES[*]}" == "" ]]; then
log "No containers configured for this profile, skipping stop." log "No containers configured for this profile, skipping stop."
return return
fi fi
info "Stopping containers..." info "Stopping containers..."
RUNNING_CONTAINERS=() RUNNING_CONTAINERS=()
for c in "${CRITICAL_CONTAINER_NAMES[@]}"; do for c in "${CRITICAL_CONTAINER_NAMES[@]}"; do
[[ -z "$c" ]] && continue [[ -z "$c" ]] && continue
log "Checking container: $c" info "Checking $c..."
STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \ STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \
"docker inspect -f '{{.State.Running}}' $c 2>/dev/null" 2>/dev/null || echo "false") "docker inspect -f '{{.State.Running}}' $c 2>/dev/null" 2>/dev/null || echo "false")
if [[ "$STATUS" == "true" ]]; then if [[ "$STATUS" == "true" ]]; then
warn "$ICON_STOP Stopping $c" echo "$ICON_STOP Stopping $c..."
RUNNING_CONTAINERS+=("$c") RUNNING_CONTAINERS+=("$c")
if ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $c" >/dev/null; then if ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $c" >/dev/null; then
success "$c stopped" success "$c stopped"
else else
error "Failed to stop $c" error "Failed to stop $c"
fi fi
else else
log "$c already stopped or not found" warn "$c is not running, skipping"
fi fi
done done
} }
start_containers() { start_containers() {
if [[ ${#RUNNING_CONTAINERS[@]} -eq 0 ]]; then if [[ ${#RUNNING_CONTAINERS[@]} -eq 0 ]]; then
log "No containers to restart." log "No containers to restart."
return return
fi fi
info "Starting containers..." info "Starting containers..."
for c in "${RUNNING_CONTAINERS[@]}"; do for c in "${RUNNING_CONTAINERS[@]}"; do
[[ -z "$c" ]] && continue [[ -z "$c" ]] && continue
# Check if this container needs a delay before starting
local needs_delay=false local needs_delay=false
for d in "${DELAYED_CONTAINERS[@]}"; do for d in "${DELAYED_CONTAINERS[@]}"; do
if [[ "$c" == "$d" ]]; then if [[ "$c" == "$d" ]]; then
@@ -177,13 +176,13 @@ start_containers() {
break break
fi fi
done done
if [[ "$needs_delay" == true ]]; then if [[ "$needs_delay" == true ]]; then
info "Delaying start of $c by ${CONTAINER_DELAY}s..." info "Waiting ${CONTAINER_DELAY}s before starting $c..."
sleep "$CONTAINER_DELAY" sleep "$CONTAINER_DELAY"
fi fi
info "Starting $c" echo "$ICON_RUN Starting $c..."
if ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $c" >/dev/null; then if ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $c" >/dev/null; then
success "$c started" success "$c started"
else else
@@ -191,7 +190,7 @@ start_containers() {
fi fi
done done
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# RSYNC SLOT LIMITER # RSYNC SLOT LIMITER
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
@@ -199,10 +198,10 @@ wait_for_rsync() {
: "${RETRY_COUNT:=5}" : "${RETRY_COUNT:=5}"
: "${MAX_RSYNC_PROCS:=2}" : "${MAX_RSYNC_PROCS:=2}"
: "${SLEEP:=2}" : "${SLEEP:=2}"
for i in $(seq 1 "$RETRY_COUNT"); do for i in $(seq 1 "$RETRY_COUNT"); do
COUNT=$(ps -eo cmd | grep "[r]sync .*${REMOTE_SERVER}" | wc -l || true) COUNT=$(ps -eo cmd | grep "[r]sync .*${REMOTE_SERVER}" | wc -l || true)
if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then
warn "$ICON_RETRY Waiting for rsync slot ($COUNT/$MAX_RSYNC_PROCS) — attempt $i/$RETRY_COUNT" warn "$ICON_RETRY Waiting for rsync slot ($COUNT/$MAX_RSYNC_PROCS) — attempt $i/$RETRY_COUNT"
sleep "$SLEEP" sleep "$SLEEP"
@@ -211,11 +210,11 @@ wait_for_rsync() {
return 0 return 0
fi fi
done done
error "Too many rsync processes after $RETRY_COUNT attempts, aborting." error "Too many rsync processes after $RETRY_COUNT attempts, aborting."
exit 1 exit 1
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# RSYNC OPTIONS # RSYNC OPTIONS
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
@@ -228,7 +227,7 @@ get_rsync_opts() {
log "Using default rsync opts: ${RSYNC_OPTS[*]}" log "Using default rsync opts: ${RSYNC_OPTS[*]}"
fi fi
} }
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# STATUS # STATUS
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------