Files
Virtnet.bond/gitapply.sh

391 lines
13 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# Virtnet.bond - Paymenter Load Balancer Git Update Script
# ============================================================================
# 256-Color Gradient Palette (Blue to Teal)
# #0c00ff -> #0066ff -> #008dff -> #00a6ff -> #00bbe7 -> #00cd9a
C1='\e[38;5;21m' # Deep Blue
C2='\e[38;5;27m' # Blue
C3='\e[38;5;33m' # Light Blue
C4='\e[38;5;39m' # Cyan Blue
C5='\e[38;5;44m' # Cyan
C6='\e[38;5;43m' # Teal
# Standard colors
WHITE='\e[97m'
GRAY='\e[90m'
RED='\e[91m'
YELLOW='\e[93m'
GREEN='\e[92m'
BOLD='\e[1m'
DIM='\e[2m'
NC='\e[0m'
# Spinner frames
SPIN=('⣾' '⣽' '⣻' '⢿' '⡿' '⣟' '⣯' '⣷')
# Node list
NODES=(
"10.0.254.100"
"10.0.254.101"
"10.0.254.102"
"10.0.254.103"
"10.0.254.104"
"10.0.254.105"
"10.0.254.106"
"10.0.254.107"
"10.0.254.108"
"10.0.254.109"
"10.0.254.110"
)
# Get current node IP
CURRENT_IP=$(hostname -I | awk '{print $1}')
# Status tracking directory
STATUS_DIR="/tmp/virtnet_deploy_$$"
mkdir -p "$STATUS_DIR"
# SSH Key (stored securely)
SSH_KEY="/root/.ssh/virtnet_deploy"
# ============================================================================
# FUNCTIONS
# ============================================================================
print_header() {
clear
echo ""
echo -e " ${C1}██╗ ██╗${C2}██╗${C3}██████╗ ${C3}████████╗${C4}███╗ ██╗${C5}███████╗${C6}████████╗${NC}"
echo -e " ${C1}██║ ██║${C2}██║${C3}██╔══██╗${C3}╚══██╔══╝${C4}████╗ ██║${C5}██╔════╝${C6}╚══██╔══╝${NC}"
echo -e " ${C1}██║ ██║${C2}██║${C3}██████╔╝${C3} ██║ ${C4}██╔██╗ ██║${C5}█████╗ ${C6} ██║ ${NC}"
echo -e " ${C1}╚██╗ ██╔╝${C2}██║${C3}██╔══██╗${C3} ██║ ${C4}██║╚██╗██║${C5}██╔══╝ ${C6} ██║ ${NC}"
echo -e " ${C1} ╚████╔╝ ${C2}██║${C3}██║ ██║${C3} ██║ ${C4}██║ ╚████║${C5}███████╗${C6} ██║ ${NC}"
echo -e " ${C1} ╚═══╝ ${C2}╚═╝${C3}╚═╝ ╚═╝${C3} ╚═╝ ${C4}╚═╝ ╚═══╝${C5}╚══════╝${C6} ╚═╝ ${NC}"
echo -e " ${C6}${BOLD}.bond${NC}"
echo ""
echo -e " ${C4}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e " ${WHITE}${BOLD} Paymenter Load Balancer - Git Update & Apply${NC}"
echo -e " ${C4}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
spinner() {
local pid=$1
local msg=$2
local i=0
while kill -0 "$pid" 2>/dev/null; do
printf "\r ${C3}${SPIN[$i]}${NC} ${msg}"
i=$(( (i + 1) % 8 ))
sleep 0.1
done
wait "$pid"
return $?
}
task_start() {
echo -e "\n${C4}┌─${NC} ${C5}${BOLD}$1${NC}"
}
task_item() {
printf " ${C3}○${NC} $1"
}
task_done() {
printf "\r ${C6}●${NC} $1 ${GREEN}✓${NC}\n"
}
task_fail() {
printf "\r ${RED}●${NC} $1 ${RED}✗${NC}\n"
}
countdown() {
local secs=$1
for ((i=secs; i>0; i--)); do
printf "\r ${C3}◐${NC} Waiting ${C5}${BOLD}$i${NC}s "
sleep 0.25
printf "\r ${C3}◓${NC} Waiting ${C5}${BOLD}$i${NC}s "
sleep 0.25
printf "\r ${C3}◑${NC} Waiting ${C5}${BOLD}$i${NC}s "
sleep 0.25
printf "\r ${C3}◒${NC} Waiting ${C5}${BOLD}$i${NC}s "
sleep 0.25
done
printf "\r ${C6}●${NC} Ready! ${GREEN}✓${NC}\n"
}
# Run update on remote node
run_remote_update() {
local node=$1
local status_file="$STATUS_DIR/$node"
echo "pending" > "$status_file"
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 -i "$SSH_KEY" root@"$node" \
'cd /var/www/paymenter && git pull && php artisan view:clear && php artisan cache:clear && systemctl restart php8.3-fpm' \
> "$STATUS_DIR/${node}.log" 2>&1
if [ $? -eq 0 ]; then
echo "done" > "$status_file"
else
echo "failed" > "$status_file"
fi
}
# Display node status panel
print_node_status() {
local cols=3
local count=0
echo -e "\n${C4}┌─${NC} ${C5}${BOLD}Cluster Node Status${NC}"
echo -e " ${GRAY}┌─────────────────┬─────────────────┬─────────────────┐${NC}"
local row=""
for node in "${NODES[@]}"; do
if [[ "$node" == "$CURRENT_IP" ]]; then
continue
fi
local status_file="$STATUS_DIR/$node"
local status="waiting"
[[ -f "$status_file" ]] && status=$(cat "$status_file")
local icon=""
local color=""
case "$status" in
"pending") icon="◐"; color="$C3" ;;
"done") icon="●"; color="$GREEN" ;;
"failed") icon="●"; color="$RED" ;;
*) icon="○"; color="$GRAY" ;;
esac
local short_ip="${node##*.}"
row+=" ${color}${icon}${NC} .${short_ip}"
count=$((count + 1))
if [ $((count % cols)) -eq 0 ]; then
printf " │%-17s│%-17s│%-17s│\n" $row
row=""
fi
done
# Print remaining nodes
if [ -n "$row" ]; then
while [ $((count % cols)) -ne 0 ]; do
row+=" "
count=$((count + 1))
done
printf " │%-17s│%-17s│%-17s│\n" $row
fi
echo -e " ${GRAY}└─────────────────┴─────────────────┴─────────────────┘${NC}"
}
# Animated node status display
update_node_display() {
local all_done=false
while ! $all_done; do
all_done=true
# Move cursor up and redraw
tput sc # Save cursor position
echo -e "\r ${GRAY}┌─────────────────────────────────────────────────────┐${NC}"
for node in "${NODES[@]}"; do
if [[ "$node" == "$CURRENT_IP" ]]; then
continue
fi
local status_file="$STATUS_DIR/$node"
local status="waiting"
[[ -f "$status_file" ]] && status=$(cat "$status_file")
local icon="" color=""
case "$status" in
"pending") icon="⣾"; color="$C3"; all_done=false ;;
"done") icon="●"; color="$GREEN" ;;
"failed") icon="●"; color="$RED" ;;
*) icon="○"; color="$GRAY"; all_done=false ;;
esac
printf " ${GRAY}│${NC} ${color}${icon}${NC} %-15s" "$node"
done
echo -e "\r ${GRAY}└─────────────────────────────────────────────────────┘${NC}"
sleep 0.3
if ! $all_done; then
tput rc # Restore cursor position
fi
done
}
print_complete() {
echo ""
echo -e " ${C4}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e " ${C6}${BOLD}🚀 UPDATE COMPLETE${NC}"
echo ""
# Count successes and failures
local success=0 failed=0
for node in "${NODES[@]}"; do
if [[ "$node" == "$CURRENT_IP" ]]; then
success=$((success + 1))
continue
fi
local status_file="$STATUS_DIR/$node"
if [[ -f "$status_file" ]] && [[ "$(cat "$status_file")" == "done" ]]; then
success=$((success + 1))
else
failed=$((failed + 1))
fi
done
echo -e " ${GRAY}Local node updated successfully.${NC}"
echo -e " ${GREEN}●${NC} ${success} nodes updated ${RED}●${NC} ${failed} nodes failed"
echo ""
echo -e " ${C4}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
cleanup() {
rm -rf "$STATUS_DIR"
}
trap cleanup EXIT
# ============================================================================
# MAIN SCRIPT
# ============================================================================
print_header
echo -e " ${C3}→${NC} ${GRAY}Current Node:${NC} ${C5}${CURRENT_IP}${NC}"
echo -e " ${C3}→${NC} ${GRAY}Target Nodes:${NC} ${C5}${#NODES[@]} nodes in cluster${NC}"
# Random delay
sleep_time=$((RANDOM % 5 + 1))
cd /var/www/paymenter
# Prepare
task_start "Preparing Update"
countdown "$sleep_time"
# Start remote updates in background
task_start "Deploying to Remote Nodes"
for node in "${NODES[@]}"; do
if [[ "$node" != "$CURRENT_IP" ]]; then
run_remote_update "$node" &
fi
done
# Show initial node status
echo -e " ${GRAY}Background deployment started on remote nodes...${NC}"
echo ""
# Display node grid
echo -e " ${GRAY}┌───────────────┬───────────────┬───────────────┬───────────────┐${NC}"
printf " ${GRAY}│${NC}"
col=0
for node in "${NODES[@]}"; do
if [[ "$node" == "$CURRENT_IP" ]]; then
continue
fi
short="${node##*.}"
printf " ${GRAY}○${NC} .%-11s${GRAY}│${NC}" "$short"
col=$((col + 1))
if [ $((col % 4)) -eq 0 ]; then
echo ""
printf " ${GRAY}│${NC}"
fi
done
echo -e "\n ${GRAY}└───────────────┴───────────────┴───────────────┴───────────────┘${NC}"
# Local Git Pull
task_start "Local: Pulling Latest Changes"
task_item "Fetching from remote..."
git pull > /tmp/git_output.txt 2>&1 &
spinner $! "Fetching from remote..."
GIT_STATUS=$?
if [ $GIT_STATUS -ne 0 ]; then
task_fail "Git pull failed"
echo ""
cat /tmp/git_output.txt
echo ""
echo -e " ${YELLOW}↻${NC} ${YELLOW}Restarting script in 2 seconds...${NC}"
sleep 2
exec "$0" "$@"
fi
task_done "Git pull completed"
head -3 /tmp/git_output.txt | while IFS= read -r line; do
echo -e " ${DIM}$line${NC}"
done
# Clear Caches
task_start "Local: Clearing Caches"
task_item "View cache..."
php artisan view:clear > /dev/null 2>&1 &
spinner $! "View cache..."
task_done "View cache cleared"
task_item "Application cache..."
php artisan cache:clear > /dev/null 2>&1 &
spinner $! "Application cache..."
task_done "Application cache cleared"
# Restart PHP-FPM
task_start "Local: Restarting Services"
task_item "PHP 8.3 FPM..."
systemctl restart php8.3-fpm &
spinner $! "PHP 8.3 FPM..."
task_done "PHP 8.3 FPM restarted"
# Wait for all remote nodes
task_start "Waiting for Remote Nodes"
task_item "Syncing cluster..."
wait
task_done "All nodes synchronized"
# Final node status
echo ""
echo -e " ${C4}┌─${NC} ${C5}${BOLD}Final Cluster Status${NC}"
echo -e " ${GRAY}┌───────────────┬───────────────┬───────────────┬───────────────┐${NC}"
printf " ${GRAY}│${NC}"
col=0
for node in "${NODES[@]}"; do
if [[ "$node" == "$CURRENT_IP" ]]; then
continue
fi
status_file="$STATUS_DIR/$node"
status="failed"
[[ -f "$status_file" ]] && status=$(cat "$status_file")
icon=""
color=""
if [[ "$status" == "done" ]]; then
icon="●"; color="$GREEN"
else
icon="●"; color="$RED"
fi
short="${node##*.}"
printf " ${color}${icon}${NC} .%-11s${GRAY}│${NC}" "$short"
col=$((col + 1))
if [ $((col % 4)) -eq 0 ]; then
echo ""
if [ $col -lt 10 ]; then
printf " ${GRAY}│${NC}"
fi
fi
done
echo -e "\n ${GRAY}└───────────────┴───────────────┴───────────────┴───────────────┘${NC}"
# Complete
print_complete