Getting order correct

This commit is contained in:
2026-03-24 20:04:38 +00:00
parent 6df103061e
commit b6e39cc8c4
3 changed files with 129 additions and 91 deletions
+66 -53
View File
@@ -3,21 +3,14 @@
#-----------------------------------------------------------------------------------------------
# --------------------- Common Shared Functions for Unraid Scripts -----------------------------
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust in Master.conf as needed ----------------------
#-----------------------------------------------------------------------------------------------
#
# Nothing to adjust here, all user variables should be in Master.conf
#
# This is just a script that has all common functions and logic that are shared
# between the different scripts.
#
# This way we can avoid code duplication and have a single source
# of truth for all common functions and logic.
#-----------------------------------------------------------------------------------------------
#---------------End Of User Variables, Please adjust in Master.conf as needed ------------------
# This file contains ONLY reusable logic.
# No code should execute on load — only functions.
# All user configuration lives in Master.conf
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
# Load Master.conf
#-----------------------------------------------------------------------------------------------
load_master_conf() {
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)"
@@ -32,7 +25,9 @@ load_master_conf() {
fi
}
# Parse CLI args
#-----------------------------------------------------------------------------------------------
# Parse CLI args (flags + VAR=value overrides)
#-----------------------------------------------------------------------------------------------
parse_args() {
DRY_RUN=${DRY_RUN:-false}
@@ -49,74 +44,92 @@ parse_args() {
fi
else
case "$ARG" in
--dry-run|-n) DRY_RUN=true ;;
--dry-run|-n)
DRY_RUN=true
;;
--help|-h)
echo "Usage: script [--dry-run|-n] [VAR=value ...]"
exit 0 ;;
exit 0
;;
*)
echo "Warning: Unknown argument $ARG" ;;
echo "Warning: Unknown argument $ARG"
;;
esac
fi
done
}
# Validate integer
#-----------------------------------------------------------------------------------------------
# Validate integer (non-negative)
#-----------------------------------------------------------------------------------------------
validate_int() {
local name="$1"
local value="$2"
if ! [[ "${value:-}" =~ ^[0-9]+$ ]]; then
echo "Error: $name must be a non-negative integer."
exit 1
fi
}
#-----------------------------------------------------------------------------------------------
# Require variable to be set
#-----------------------------------------------------------------------------------------------
require_var() {
local var="$1"
if [[ -z "${!var:-}" ]]; then
echo "Error: Required variable $var is not set."
exit 1
fi
}
# Detect local hostname
LOCAL_HOSTNAME="$(hostname)"
if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then
LOCAL_SERVER_NAME="$HOST1"
REMOTE_SERVER_NAME="$HOST2"
elif [[ "$LOCAL_HOSTNAME" == "$HOST2" ]]; then
LOCAL_SERVER_NAME="$HOST2"
REMOTE_SERVER_NAME="$HOST1"
else
echo "Error: Local hostname ($LOCAL_HOSTNAME) not recognized."
exit 1
fi
echo "Local server: $LOCAL_SERVER_NAME"
echo "Remote server: $REMOTE_SERVER_NAME"
#-----------------------------------------------------------------------------------------------
#--------------------------- Remote Server Ip Resolution ----------------------------------------
# Detect hosts + networking + SSH key
#-----------------------------------------------------------------------------------------------
# Resolve Tailscale IP dynamically
REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null)
if [[ -z "$REMOTE_SERVER" ]]; then
echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME"
exit 1
fi
detect_hosts() {
# Ensure required config exists
require_var HOST1
require_var HOST2
#-----------------------------------------------------------------------------------------------
#----------------------------- SSH Key Mapping --------------------------------------------------
#-----------------------------------------------------------------------------------------------
# Detect local hostname
LOCAL_HOSTNAME="$(hostname)"
declare -A SSH_KEYS
SSH_KEYS["$HOST1|$HOST2"]="/root/.ssh/Gmer4Lfe-rsync-key"
SSH_KEYS["$HOST2|$HOST1"]="/root/.ssh/Jayred365-rsync-key"
if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then
LOCAL_SERVER_NAME="$HOST1"
REMOTE_SERVER_NAME="$HOST2"
elif [[ "$LOCAL_HOSTNAME" == "$HOST2" ]]; then
LOCAL_SERVER_NAME="$HOST2"
REMOTE_SERVER_NAME="$HOST1"
else
echo "Error: Local hostname ($LOCAL_HOSTNAME) not recognized."
exit 1
fi
KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME"
if [[ -n "${SSH_KEYS[$KEY_ID]}" ]]; then
SSH_KEY="${SSH_KEYS[$KEY_ID]}"
else
echo "Error: No SSH key defined for $LOCAL_SERVER_NAME$REMOTE_SERVER_NAME"
exit 1
fi
echo "Using SSH key: $SSH_KEY"
echo "Local server: $LOCAL_SERVER_NAME"
echo "Remote server: $REMOTE_SERVER_NAME"
# Resolve Tailscale IP
REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null)
if [[ -z "$REMOTE_SERVER" ]]; then
echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME"
exit 1
fi
# SSH Key Mapping (kept here for stability)
declare -A SSH_KEYS
SSH_KEYS["$HOST1|$HOST2"]="/root/.ssh/Gmer4Lfe-rsync-key"
SSH_KEYS["$HOST2|$HOST1"]="/root/.ssh/Jayred365-rsync-key"
KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME"
if [[ -n "${SSH_KEYS[$KEY_ID]}" ]]; then
SSH_KEY="${SSH_KEYS[$KEY_ID]}"
else
echo "Error: No SSH key defined for $LOCAL_SERVER_NAME$REMOTE_SERVER_NAME"
exit 1
fi
echo "Using SSH key: $SSH_KEY"
}