This commit is contained in:
2026-03-24 01:33:40 +00:00
parent 312f8d251f
commit 221aa3d1ef
3 changed files with 134 additions and 97 deletions
+16 -51
View File
@@ -1,7 +1,20 @@
#!/bin/bash
#-----------------------------------------------------------------------------------------------
#--------------------- Common Shared Functions -------------------------------------------------
# --------------------- 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 ------------------
#-----------------------------------------------------------------------------------------------
# Load Master.conf
@@ -19,7 +32,7 @@ load_master_conf() {
fi
}
# Parse CLI arguments
# Parse CLI args
parse_args() {
DRY_RUN=${DRY_RUN:-false}
@@ -57,59 +70,11 @@ validate_int() {
fi
}
# Require variable
# 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 hosts + SSH key + Tailscale IP
detect_hosts() {
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"
# 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
# Validate SSH_KEYS
if [[ "$(declare -p SSH_KEYS 2>/dev/null)" != declare\ -A* ]]; then
echo "Error: SSH_KEYS is not a valid associative array."
exit 1
fi
KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME"
# Debug (safe to keep)
echo "DEBUG: KEY_ID=$KEY_ID"
echo "DEBUG: Available keys: ${!SSH_KEYS[@]}"
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"
}