Trying v1 framework again
This commit is contained in:
+56
-70
@@ -20,15 +20,12 @@ set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# Load configs & helpers
|
||||
source "$SCRIPT_DIR/../Master.conf"
|
||||
source "$SCRIPT_DIR/../common.sh"
|
||||
|
||||
# ----------------------------
|
||||
# INPUT HANDLING (FIXED)
|
||||
# ----------------------------
|
||||
parse_args "$@"
|
||||
|
||||
# If first argument is NOT a flag or VAR=VALUE, treat it as DIRECTORY
|
||||
# ----------------------------- INPUT -----------------------------
|
||||
if [[ -z "$DIRECTORY" ]]; then
|
||||
if [[ "${1:-}" != "" && "${1:-}" != --* && "${1:-}" != *=* ]]; then
|
||||
DIRECTORY="$1"
|
||||
@@ -36,16 +33,13 @@ if [[ -z "$DIRECTORY" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "$DIRECTORY" ]]; then
|
||||
echo "Usage: $0 <directory-to-sync> [--dry-run] [--status] [VAR=value ...]"
|
||||
exit 1
|
||||
fi
|
||||
[[ -z "$DIRECTORY" ]] && error "Missing directory" && exit 1
|
||||
|
||||
# ----------------------------- Host & IP -----------------------------
|
||||
# ----------------------------- HOST -----------------------------
|
||||
detect_hosts
|
||||
resolve_remote_ip
|
||||
|
||||
# ----------------------------- Profile -----------------------------
|
||||
# ----------------------------- PROFILE -----------------------------
|
||||
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
|
||||
@@ -57,113 +51,105 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
|
||||
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
|
||||
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
|
||||
|
||||
# ----------------------------- Status Mode -----------------------------
|
||||
# ----------------------------- STATUS MODE -----------------------------
|
||||
if [[ "$SHOW_STATUS" == true ]]; then
|
||||
show_status
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ----------------------------- HEADER -----------------------------
|
||||
echo
|
||||
echo "$ICON_RUN Sync Starting"
|
||||
echo "--------------------------------------------"
|
||||
echo "Source: $DIRECTORY"
|
||||
echo "Destination: $REMOTE_SERVER:$DIRECTORY"
|
||||
echo "Profile: $PROFILE_NAME"
|
||||
echo "Dry Run: $DRY_RUN"
|
||||
echo "--------------------------------------------"
|
||||
|
||||
# ----------------------------- User Info -----------------------------
|
||||
echo "============================================================"
|
||||
echo " Sync Job Starting"
|
||||
echo "------------------------------------------------------------"
|
||||
echo " Source: $DIRECTORY"
|
||||
echo " Destination: $REMOTE_SERVER:$DIRECTORY"
|
||||
echo " Profile: $PROFILE_NAME"
|
||||
echo " Dry Run: $DRY_RUN"
|
||||
echo "============================================================"
|
||||
|
||||
log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT"
|
||||
log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
|
||||
log "Excludes: ${EXCLUDE_DIRS[*]}"
|
||||
|
||||
# ----------------------------- Rsync Wait -----------------------------
|
||||
# ----------------------------- RSYNC LIMIT -----------------------------
|
||||
wait_for_rsync() {
|
||||
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
||||
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
|
||||
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
|
||||
echo "Waiting for available rsync slot... ($RSYNC_COUNT running)"
|
||||
log "RSYNC_COUNT=$RSYNC_COUNT >= MAX=$MAX_RSYNC_PROCS"
|
||||
|
||||
if [[ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then
|
||||
warn "Waiting for rsync slot ($RSYNC_COUNT)"
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
echo "Too many rsync processes, exiting."
|
||||
exit 1
|
||||
|
||||
error "Too many rsync processes"
|
||||
}
|
||||
|
||||
# ----------------------------- Main -----------------------------
|
||||
main() {
|
||||
echo ""
|
||||
echo "🔍 Checking remote connectivity..."
|
||||
# ----------------------------- SUMMARY -----------------------------
|
||||
print_summary() {
|
||||
echo
|
||||
echo "================ SUMMARY ================"
|
||||
echo "Directory: $DIRECTORY"
|
||||
echo "Profile: $PROFILE_NAME"
|
||||
echo "Remote: $REMOTE_SERVER"
|
||||
echo "Duration: ${DURATION:-unknown}s"
|
||||
echo "Status: $1"
|
||||
echo "Dry Run: $DRY_RUN"
|
||||
echo "========================================"
|
||||
}
|
||||
|
||||
# ----------------------------- MAIN -----------------------------
|
||||
main() {
|
||||
|
||||
echo "$ICON_GEAR Checking remote..."
|
||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
||||
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
|
||||
echo "✅ Remote server reachable"
|
||||
success "Remote reachable"
|
||||
break
|
||||
fi
|
||||
echo "⚠️ Remote down, retrying in $SLEEP seconds..."
|
||||
warn "Retrying ($i/$RETRY_COUNT)"
|
||||
sleep "$SLEEP"
|
||||
done
|
||||
|
||||
if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
|
||||
echo "❌ Remote server unreachable. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
[[ ! $(ping -c 1 "$REMOTE_SERVER" &>/dev/null) ]] && error "Remote unreachable"
|
||||
|
||||
wait_for_rsync
|
||||
|
||||
echo ""
|
||||
echo "🛑 Stopping containers on remote..."
|
||||
echo "$ICON_STOP Stopping containers..."
|
||||
stop_containers
|
||||
|
||||
echo ""
|
||||
echo "📦 Building rsync options..."
|
||||
echo "$ICON_GEAR Building rsync opts..."
|
||||
get_rsync_opts
|
||||
|
||||
for ex in "${EXCLUDE_DIRS[@]}"; do
|
||||
RSYNC_OPTS+=(--exclude="$ex")
|
||||
done
|
||||
|
||||
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
|
||||
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
|
||||
|
||||
log "Final rsync opts: ${RSYNC_OPTS[*]}"
|
||||
echo "$ICON_RUN Running rsync..."
|
||||
|
||||
echo ""
|
||||
echo "🚀 Starting rsync..."
|
||||
|
||||
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
||||
echo "Attempt $attempt/$RETRY_COUNT"
|
||||
START=$(date +%s)
|
||||
|
||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
||||
if rsync "${RSYNC_OPTS[@]}" \
|
||||
-e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \
|
||||
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" \
|
||||
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
|
||||
|
||||
echo "✅ Rsync completed successfully"
|
||||
success "Rsync complete"
|
||||
break
|
||||
|
||||
else
|
||||
echo "❌ Rsync failed"
|
||||
warn "Rsync failed"
|
||||
|
||||
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
|
||||
echo "Retrying in $SLEEP seconds..."
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
echo "💥 All retries failed"
|
||||
start_containers
|
||||
exit 1
|
||||
fi
|
||||
[[ $i -lt $RETRY_COUNT ]] && sleep "$SLEEP"
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "🔄 Restarting containers..."
|
||||
echo "$ICON_SYNC Restarting containers..."
|
||||
start_containers
|
||||
|
||||
echo ""
|
||||
echo "🎉 Sync complete!"
|
||||
END=$(date +%s)
|
||||
DURATION=$((END - START))
|
||||
|
||||
print_summary "SUCCESS"
|
||||
}
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user