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