-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_worker.sh
More file actions
executable file
·178 lines (148 loc) · 5.08 KB
/
setup_worker.sh
File metadata and controls
executable file
·178 lines (148 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
set -e
PROJECT_DIR="$1"
if [[ -z "$PROJECT_DIR" ]]; then
echo "Usage: $0 <project_directory>"
exit 1
fi
PROJECT_DIR=$(realpath "$PROJECT_DIR")
echo "🔧 Distributed Gradle Build - Worker Setup"
echo "========================================"
echo ""
echo "This script prepares a worker machine for distributed building"
echo "that will utilize its CPU and memory for Gradle tasks."
echo ""
read -p "Enter master IP address: " MASTER_IP
echo ""
echo "Setting up worker for distributed builds with master: $MASTER_IP"
echo ""
# Install required packages
echo "📦 Installing required packages..."
sudo apt update
sudo apt install -y rsync openjdk-17-jdk jq curl htop
# Detect worker resources
echo "🖥️ Detecting worker resources..."
WORKER_CORES=$(nproc --all)
WORKER_MEMORY_GB=$(free -g | awk '/^Mem:/{print $2}')
WORKER_DISK_GB=$(df -BG . | tail -1 | awk '{print $4}' | sed 's/G//')
echo " CPU Cores: $WORKER_CORES"
echo " Memory: ${WORKER_MEMORY_GB}GB"
echo " Available Disk: ${WORKER_DISK_GB}GB"
echo ""
# Create worker directories
echo "📁 Creating worker directories..."
mkdir -p "$PROJECT_DIR"
mkdir -p "$PROJECT_DIR/.distributed/{logs,cache,metrics}"
mkdir -p "$PROJECT_DIR/.gradle"
echo " Project: $PROJECT_DIR"
echo " Distributed logs: $PROJECT_DIR/.distributed/logs"
echo " Distributed cache: $PROJECT_DIR/.distributed/cache"
echo ""
# Test SSH connectivity to master
echo "🔗 Testing SSH connectivity to master..."
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "$MASTER_IP" echo "OK" >/dev/null 2>&1; then
echo "❌ Error: SSH to master $MASTER_IP failed."
echo "💡 Setup passwordless SSH with: ssh-copy-id $MASTER_IP"
echo " Or run: ./setup_passwordless_ssh.sh"
exit 1
else
echo "✅ SSH to master $MASTER_IP: OK"
fi
echo ""
# Create worker environment file
WORKER_ENV="$PROJECT_DIR/.distributed/worker.env"
cat <<EOF > "$WORKER_ENV"
# Distributed Gradle Build Worker Configuration
# Generated by setup_worker.sh on $(date)
export WORKER_ID="\$(hostname)-\$(date +%s)"
export MASTER_IP="$MASTER_IP"
export PROJECT_DIR="$PROJECT_DIR"
export WORKER_CORES="$WORKER_CORES"
export WORKER_MEMORY_GB="$WORKER_MEMORY_GB"
export WORKER_AVAILABLE_DISK_GB="$WORKER_DISK_GB"
export WORKER_READY=true
EOF
echo "⚙️ Worker configuration created: $WORKER_ENV"
echo ""
# Create worker status script
WORKER_STATUS_SCRIPT="$PROJECT_DIR/.distributed/worker_status.sh"
cat <<'EOF' > "$WORKER_STATUS_SCRIPT"
#!/bin/bash
# Worker status reporting for distributed builds
source "$(dirname "$0")/worker.env"
# Current resource usage
CPU_LOAD=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
MEMORY_USED_GB=$(free -g | awk '/^Mem:/{print $3}')
MEMORY_PCT=$((MEMORY_USED_GB * 100 / WORKER_MEMORY_GB))
DISK_USED_GB=$(df -BG . | tail -1 | awk '{print $3}' | sed 's/G//')
DISK_PCT=$((DISK_USED_GB * 100 / WORKER_AVAILABLE_DISK_GB))
cat <<STATUS
{
"worker_id": "$WORKER_ID",
"master_ip": "$MASTER_IP",
"cpu_cores": $WORKER_CORES,
"cpu_load": $CPU_LOAD,
"memory_total_gb": $WORKER_MEMORY_GB,
"memory_used_gb": $MEMORY_USED_GB,
"memory_usage_pct": $MEMORY_PCT,
"disk_total_gb": $WORKER_AVAILABLE_DISK_GB,
"disk_used_gb": $DISK_USED_GB,
"disk_usage_pct": $DISK_PCT,
"ready": $WORKER_READY,
"timestamp": $(date +%s)
}
STATUS
EOF
chmod +x "$WORKER_STATUS_SCRIPT"
echo "✅ Worker status script created: $WORKER_STATUS_SCRIPT"
echo ""
# Create worker health monitor
WORKER_MONITOR_SCRIPT="$PROJECT_DIR/.distributed/worker_monitor.sh"
cat <<'EOF' > "$WORKER_MONITOR_SCRIPT"
#!/bin/bash
# Worker health monitor for distributed builds
source "$(dirname "$0")/worker.env"
LOG_FILE="$(dirname "$0")/logs/worker_monitor.log"
METRICS_FILE="$(dirname "$0")/metrics/worker_metrics.json"
mkdir -p "$(dirname "$LOG_FILE")" "$(dirname "$METRICS_FILE")"
while true; do
# Get current status
STATUS="$($(dirname "$0")/worker_status.sh)"
# Log to file
echo "$(date '+%Y-%m-%d %H:%M:%S') - $STATUS" >> "$LOG_FILE"
# Update metrics JSON
echo "$STATUS" > "$METRICS_FILE"
# Sleep for monitoring interval
sleep 30
done &
echo $! > "$(dirname "$0")/worker_monitor.pid"
EOF
chmod +x "$WORKER_MONITOR_SCRIPT"
echo "✅ Worker monitor script created: $WORKER_MONITOR_SCRIPT"
echo ""
# Start worker monitor
echo "🚀 Starting worker health monitor..."
"$WORKER_MONITOR_SCRIPT"
MONITOR_PID=$(cat "$PROJECT_DIR/.distributed/worker_monitor.pid")
echo " Monitor started with PID: $MONITOR_PID"
echo ""
echo "🎯 WORKER SETUP COMPLETE"
echo "========================="
echo ""
echo "Worker Configuration Summary:"
echo " Worker ID: $(hostname)-$(date +%s)"
echo " Master: $MASTER_IP"
echo " CPU Cores: $WORKER_CORES"
echo " Memory: ${WORKER_MEMORY_GB}GB"
echo " Project Directory: $PROJECT_DIR"
echo ""
echo "Services Started:"
echo " Health Monitor: PID $MONITOR_PID"
echo " Status Script: $WORKER_STATUS_SCRIPT"
echo ""
echo "This worker is now ready for distributed builds and will"
echo "contribute its CPU and memory to Gradle build tasks."
echo ""
echo "To stop monitoring: kill $MONITOR_PID"
echo "To check status: $WORKER_STATUS_SCRIPT"
echo ""