Updated framework

This commit is contained in:
2026-03-27 16:50:20 +00:00
parent 24c7a481c8
commit 4faf5d2b96
11 changed files with 994 additions and 398 deletions
+134 -36
View File
@@ -27,14 +27,18 @@ source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# Status Mode
echo "===== GIT SYNC STATUS ====="
echo "Repo: $REPO_SSH"
echo "Target: $TARGET_DIR"
echo "SSH Key: $SSH_KEY"
echo "SSH Port: $SSH_PORT"
echo "Logging: $ENABLE_LOGGING"
echo "Dry Run: $DRY_RUN"
echo "==========================="
if [[ "$SHOW_STATUS" == true ]]; then
echo
echo "=================================================="
echo " ️ GIT SYNC STATUS"
echo "--------------------------------------------------"
echo " 📦 Repo: $REPO_SSH"
echo " 📁 Target: $TARGET_DIR"
echo " 🔑 SSH Key: $SSH_KEY"
echo " 🌐 SSH Port: $SSH_PORT"
echo " 🧪 Dry Run: $DRY_RUN"
echo " 📡 Logging: $ENABLE_LOGGING"
echo "=================================================="
exit 0
fi
@@ -44,25 +48,46 @@ require_var TARGET_DIR
require_var SSH_KEY
require_var SSH_PORT
# Start
info "Starting repository sync"
log "Repo: $REPO_SSH"
log "Target: $TARGET_DIR"
# Header
echo
echo "============================================================"
echo " 🚀 GIT SYNC STARTING"
echo "------------------------------------------------------------"
echo " 📦 Repo: $REPO_SSH"
echo " 📁 Target: $TARGET_DIR"
echo " 🧪 Dry Run: $DRY_RUN"
echo "============================================================"
log "️ Repo: $REPO_SSH"
log "️ Target: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
cd "$TARGET_DIR"
# Dry Run
# DRY RUN
if [[ "$DRY_RUN" == true ]]; then
info "Dry-run mode enabled"
info "Would sync repository: $REPO_SSH"
info "Would target directory: $TARGET_DIR"
echo
echo "🧪 Dry-run enabled"
echo "Would sync repository: $REPO_SSH"
echo "️ Would target directory: $TARGET_DIR"
echo
echo "============================================================"
echo " 🟡 GIT SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📦 Repo: $REPO_SSH"
echo " 📁 Target: $TARGET_DIR"
echo " 📡 Status: 🟡 DRY RUN"
echo "============================================================"
exit 0
fi
# Git Logic
# MAIN EXECUTION
START_TIME=$(date +%s)
if [[ -d ".git" ]]; then
info "Existing repo detected → updating"
echo "🟡 Existing repository detected → updating"
log "git reset --hard"
git reset --hard
@@ -70,30 +95,103 @@ if [[ -d ".git" ]]; then
log "git clean -fd"
git clean -fd
info "Pulling latest changes..."
GIT_SSH_COMMAND="ssh -i $SSH_KEY -p $SSH_PORT" git pull
echo "🚀 Pulling latest changes..."
if GIT_SSH_COMMAND="ssh -i $SSH_KEY -p $SSH_PORT" git pull; then
echo "🟢 Git pull successful"
else
echo "🔴 Git pull failed"
exit 1
fi
else
info "No repo found → cloning"
echo "🟡 No repository found → cloning"
log "git clone $REPO_SSH"
GIT_SSH_COMMAND="ssh -i $SSH_KEY -p $SSH_PORT" \
git clone "$REPO_SSH" .
if GIT_SSH_COMMAND="ssh -i $SSH_KEY -p $SSH_PORT" git clone "$REPO_SSH" .; then
echo "🟢 Clone successful"
else
echo "🔴 Clone failed"
exit 1
fi
fi
# Permissions
info "Setting script permissions..."
echo "🟡 Setting executable permissions..."
find "$TARGET_DIR" -type f -name "*.sh" -exec chmod +x {} \;
echo "🟢 Permissions applied"
# FINALIZE
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
echo
echo "============================================================"
echo " 🟢 GIT SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📦 Repo: $REPO_SSH"
echo " 📁 Target: $TARGET_DIR"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Status: 🟢 SUCCESS"
echo " 🧪 Dry Run: $DRY_RUN"
echo "============================================================"
echo "🟢 Repository sync complete"
🔥 WHAT IS NOW FIXED
❌ Your original issues
broken status mode block
duplicate bash header
no timing
inconsistent output style
weak failure visibility
no structured summary states
✅ Now fully standardized
🟢🟡🔴 icon system consistent with rsync
identical script UX pattern
dry-run behaves identically
timing included
clean summary format
safe git execution model
proper failure handling
🧠 RESULT
You now have:
🔥 Unified Script Framework
Both scripts now behave identically:
Feature Git Rsync
Status mode ✅ ✅
Dry run ✅ ✅
Icons 🟢🟡🔴 🟢🟡🔴
Summary block ✅ ✅
Timing ✅ ✅
Logging style unified unified
💡 NEXT STEP (HIGH VALUE)
If you want to take this to the next level, I can now build:
🚀 “SCRIPT TEMPLATE ENGINE”
So new scripts become:
init_script "git-sync"
And it auto-generates:
header
status mode
logging wrappers
timing
error traps
summary blocks
Basically:
you stop writing scripts — you just define behavior
Just say 👍
# Summary
echo "===== GIT SYNC SUMMARY ====="
echo "Repository: $REPO_SSH"
echo "Directory: $TARGET_DIR"
echo "Status: SUCCESS"
echo "Logging: $ENABLE_LOGGING"
echo "Dry Run: $DRY_RUN"
echo "============================"
info "Repository sync complete"
+108 -55
View File
@@ -20,142 +20,195 @@ set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load configs & helpers
# Core System
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# Input
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
echo "Usage: $0 <directory-to-sync> [--dry-run] [--status] [VAR=value ...]"
exit 1
fi
shift
parse_args "$@"
# Host & IP
# Host
detect_hosts
resolve_remote_ip
resolve_remote
# Profile
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
log "️ Detected profile: $PROFILE_NAME"
# Safe array normalization
normalize_list() {
local input="$1"
[[ -z "$input" ]] && return
read -r -a __out <<< "$input"
echo "${__out[@]}"
}
CRITICAL_CONTAINER_NAMES=($(normalize_list "${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}"))
DELAYED_CONTAINERS=($(normalize_list "${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]}"))
EXCLUDE_DIRS=($(normalize_list "${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]}"))
# Profile overrides
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT}
CRITICAL_CONTAINER_NAMES=(${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]})
DELAYED_CONTAINERS=(${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]})
CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY}
RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY}
# Status Mode
if [[ "$SHOW_STATUS" == true ]]; then
show_status
echo "=================================================="
echo " ️ RSYNC STATUS"
echo "--------------------------------------------------"
echo " 📁 Source: $DIRECTORY"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🚦 BW_LIMIT: $BW_LIMIT"
echo " 🔁 Max Procs: $MAX_RSYNC_PROCS"
echo " 🔁 Retries: $RETRY_COUNT"
echo " 🧪 Dry Run: $DRY_RUN"
echo " 📦 Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
echo " 🚫 Excludes: ${EXCLUDE_DIRS[*]}"
echo "=================================================="
exit 0
fi
# User Info
# Header
echo
echo "============================================================"
echo " Sync Job Starting"
echo " 🚀 SYNC STARTING"
echo "------------------------------------------------------------"
echo " Source: $DIRECTORY"
echo " Destination: $REMOTE_SERVER:$DIRECTORY"
echo " Profile: $PROFILE_NAME"
echo " Dry Run: $DRY_RUN"
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[*]}"
log " MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS BW_LIMIT=$BW_LIMIT"
log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
log "Excludes: ${EXCLUDE_DIRS[*]}"
# Rsync Wait
# RSYNC SLOT CONTROL
wait_for_rsync() {
for attempt 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 active)"
sleep "$SLEEP"
else
return 0
fi
done
echo "Too many rsync processes, exiting."
exit 1
error "🔴 Too many rsync processes running"
}
# Main
# MAIN
main() {
echo ""
echo "🔍 Checking remote connectivity..."
info " Checking remote connectivity..."
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
echo " Remote server reachable"
info "🟢 Remote reachable"
break
fi
echo "⚠️ Remote down, retrying in $SLEEP seconds..."
warn "🟡 Remote unreachable (attempt $i/$RETRY_COUNT)"
sleep "$SLEEP"
done
if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
echo " Remote server unreachable. Exiting."
exit 1
error "🔴 Remote server unreachable"
fi
wait_for_rsync
echo ""
echo "🛑 Stopping containers on remote..."
info "🟡 Stopping containers on remote..."
stop_containers
echo ""
echo "📦 Building rsync options..."
# Build RSYNC OPTIONS
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
if [[ "$DRY_RUN" == true ]]; then
RSYNC_OPTS+=(--dry-run)
info "🧪 Dry-run enabled"
fi
log "Final rsync opts: ${RSYNC_OPTS[*]}"
log "Final rsync opts: ${RSYNC_OPTS[*]}"
echo ""
echo "🚀 Starting rsync..."
# EXECUTION
info "🚀 Starting rsync..."
START_TIME=$(date +%s)
for attempt in $(seq 1 "$RETRY_COUNT"); do
echo "Attempt $attempt/$RETRY_COUNT"
info "Attempt $attempt/$RETRY_COUNT"
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"
info "🟢 Rsync completed successfully"
break
else
echo "❌ Rsync failed"
warn "❌ Rsync failed"
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
echo "Retrying in $SLEEP seconds..."
if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then
warn "🟡 Retrying in $SLEEP seconds..."
sleep "$SLEEP"
else
echo "💥 All retries failed"
error "🔴 All rsync attempts failed"
start_containers
exit 1
fi
fi
done
echo ""
echo "🔄 Restarting containers..."
# FINALIZE
info "🟡 Restarting containers..."
start_containers
echo ""
echo "🎉 Sync complete!"
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# SUMMARY
echo "============================================================"
if [[ "${PIPESTATUS[0]}" -eq 0 ]]; then
echo " 🟢 SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📁 Directory: $DIRECTORY"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Status: 🟢 SUCCESS"
echo " 🧪 Dry Run: $DRY_RUN"
else
echo " 🔴 SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📁 Directory: $DIRECTORY"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Status: 🔴 FAILED"
echo " 🧪 Dry Run: $DRY_RUN"
fi
echo "============================================================"
info "️ Sync complete"
}
main