-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy.sh
More file actions
315 lines (264 loc) · 10.7 KB
/
deploy.sh
File metadata and controls
315 lines (264 loc) · 10.7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
# =============================================================================
# deploy.sh - Bash version of deploy.py
# Performs the same operations: chown, restart Apache, wake up sites
# =============================================================================
# =============================================================================
# CONFIGURATION - Edit these values as needed
# =============================================================================
# Web server service name (change this if using nginx, gunicorn, etc.)
WEB_SERVER_SERVICE="httpd"
# URLs for each service
declare -A urls=(
["LinuxReport2"]="https://linuxreport.net"
["CovidReport2"]="https://covidreport.org"
["aireport"]="https://aireport.keithcu.com"
["trumpreport"]="https://trumpreport.info"
["pvreport"]="https://pvreport.org"
["spacereport"]="https://news.spaceelevatorwiki.com"
)
# =============================================================================
# FUNCTIONS
# =============================================================================
# Function to run a command with error handling
run_command() {
local cmd=$1
local check=${2:-true}
if [[ "$check" == "true" ]]; then
if ! eval "$cmd" 2>/dev/null; then
echo "Error running command: $cmd"
exit 1
fi
else
eval "$cmd" 2>/dev/null || true
fi
}
# Function to change ownership for a directory
chown_directory() {
local dir=$1
echo "Changing ownership for $dir..."
run_command "cd \"$dir\" && sudo chown -R http:http * && cd .."
}
# Function to wake up a site with curl
wake_up_site() {
local dir=$1
local url=$2
local output_file="/tmp/deploy_${dir}.out"
local start_time=$(date +%s.%3N)
# Clear output file
> "$output_file"
# Format site name for display
local site_name=$(echo "$dir" | sed 's/Report2/Report/g' | sed 's/report/Report/g')
# Print when this task starts
echo "🔄 $site_name | Starting at ${start_time}s" >> "$output_file"
max_attempts=5
attempt=1
while [ $attempt -le $max_attempts ]; do
# Make the request with User-Agent header
# Use LinuxReportDeployBot user agent to prevent triggering refreshes/background updates
# This ensures deploy requests don't trigger background refreshes when starting the app
response=$(curl -s -w "HTTP_STATUS:%{http_code}" -H "User-Agent: LinuxReportDeployBot" --connect-timeout 10 --max-time 10 "$url")
http_status=$(echo "$response" | grep "HTTP_STATUS:" | cut -d: -f2)
response_content=$(echo "$response" | grep -v "HTTP_STATUS:")
# Extract title using improved logic (search first 15 lines for title tag)
title=""
line_number=1
# Look for title tag in the first 15 lines
while [ $line_number -le 15 ] && [ -z "$title" ]; do
title_line=$(echo "$response_content" | sed -n "${line_number}p")
if echo "$title_line" | grep -q "<title>.*</title>"; then
title=$(echo "$title_line" | sed 's/<title>//' | sed 's/<\/title>//' | xargs)
break
fi
line_number=$((line_number + 1))
done
# If no title found in first 15 lines, use the first non-empty line as fallback
if [ -z "$title" ]; then
line_number=1
while [ $line_number -le 15 ]; do
title_line=$(echo "$response_content" | sed -n "${line_number}p" | xargs)
if [ -n "$title_line" ] && ! echo "$title_line" | grep -q "^<!" && ! echo "$title_line" | grep -q "^<html"; then
title="$title_line"
break
fi
line_number=$((line_number + 1))
done
fi
# Check if we got a successful response with content
if [[ -n "$title" ]]; then
status_icon="✅"
status_text="SUCCESS"
break
elif [[ "$http_status" == "000" ]]; then
status_icon="❌"
status_text="CONNECTION FAILED"
else
status_icon="⚠️"
status_text="EMPTY RESPONSE"
fi
if [ $attempt -lt $max_attempts ]; then
attempt=$((attempt + 1))
sleep 3
else
status_icon="💥"
status_text="FAILED"
title="No title found"
break
fi
done
# Calculate duration
local end_time=$(date +%s.%3N)
local duration=$(echo "$end_time - $start_time" | bc -l)
# Format the output in columns
printf "%s %-15s | %-3s | %-40s | %.2fs\n" "$status_icon" "$site_name" "$http_status" "$title" "$duration" >> "$output_file"
# Return success status
if [[ "$status_text" == "SUCCESS" ]]; then
echo "SUCCESS" >> "$output_file"
else
echo "FAILED" >> "$output_file"
fi
}
# Function to wake up all sites for a specific round
wake_up_sites_round() {
local round_name=$1
local round_number=$2
echo "Step $round_number: $round_name..."
echo "===================================================================================================="
printf "%-15s | %-3s | %-40s | %s\n" "Site" "Status" "Title" "Duration"
echo "----------------------------------------------------------------------------------------------------"
local start_time=$(date +%s.%3N)
# Submit all tasks to background
for dir in "${!urls[@]}"; do
url="${urls[$dir]}"
wake_up_site "$dir" "$url" &
sleep 0.05 # 50ms delay between submissions (like Python version)
done
# Monitor progress in real-time
local completed=0
local total=${#urls[@]}
local success_count=0
while [ $completed -lt $total ]; do
completed=0
success_count=0
for dir in "${!urls[@]}"; do
output_file="/tmp/deploy_${dir}.out"
if [[ -f "$output_file" ]]; then
# Check if we have a final result (contains SUCCESS or FAILED at the end)
if grep -q "^SUCCESS$" "$output_file" || grep -q "^FAILED$" "$output_file"; then
completed=$((completed + 1))
# Display result if we haven't shown it yet
if ! grep -q "^DISPLAYED:" "$output_file"; then
# Get the formatted line (second to last line)
local result_line=$(tail -n 2 "$output_file" | head -n 1)
echo "$result_line"
echo "DISPLAYED:" >> "$output_file" # Mark as displayed
# Count successes
if grep -q "^SUCCESS$" "$output_file"; then
success_count=$((success_count + 1))
fi
fi
fi
fi
done
if [ $completed -lt $total ]; then
echo -n "⏳ Waiting for sites... ($completed/$total complete) "
sleep 1
echo ""
fi
done
local end_time=$(date +%s.%3N)
local duration=$(echo "$end_time - $start_time" | bc -l)
echo "----------------------------------------------------------------------------------------------------"
echo "✅ $success_count/$total sites deployed successfully!"
printf "⏱️ Total time: %.2f seconds\n" "$duration"
echo "===================================================================================================="
# Return values (we'll use global variables since bash functions can't return multiple values)
ROUND_SUCCESS_COUNT=$success_count
ROUND_DURATION=$duration
}
# Function to wake up all sites with two rounds
wake_up_all_sites_concurrent() {
# First round
wake_up_sites_round "Waking up all sites" 3
local first_success=$ROUND_SUCCESS_COUNT
local first_duration=$ROUND_DURATION
# Wait 1 second after initial warm-up
echo ""
echo "⏳ Waiting 1 second before second round..."
sleep 1
# Second round
wake_up_sites_round "Second round - waking up all sites again" 4
local second_success=$ROUND_SUCCESS_COUNT
local second_duration=$ROUND_DURATION
}
# Function to warm up sites only
warm_up_sites_only() {
echo "🔥 Warming up all sites only..."
echo "===================================================================================================="
# Single round of warm-up
wake_up_sites_round "Warming up all sites" 1
local success_count=$ROUND_SUCCESS_COUNT
local duration=$ROUND_DURATION
printf "🔥 Warm-up complete! %d/%d sites warmed up in %.2f seconds\n" "$success_count" "${#urls[@]}" "$duration"
}
# Function to parse command line arguments
parse_arguments() {
WARMUP_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
--warmup-only)
WARMUP_ONLY=true
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --warmup-only Only warm up sites without chown or restart"
echo " -h, --help Show this help message"
echo ""
echo "Description:"
echo " Deploy and warm up sites with enhanced timing and status reporting"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
}
# =============================================================================
# MAIN EXECUTION
# =============================================================================
# Parse command line arguments
parse_arguments "$@"
if [[ "$WARMUP_ONLY" == true ]]; then
# Just warm up sites
warm_up_sites_only
else
# Full deployment
overall_start=$(date +%s.%3N)
# Step 1: Change ownership for all directories
echo "Step 1: Changing ownership for all directories..."
for dir in "${!urls[@]}"; do
chown_directory "$dir"
done
# Step 2: Restart web server
echo "Step 2: Restarting web server..."
run_command "sudo systemctl restart \"$WEB_SERVER_SERVICE\""
sleep 0.25
# Step 3: Wake up all sites concurrently
wake_up_all_sites_concurrent
overall_end=$(date +%s.%3N)
total_duration=$(echo "$overall_end - $overall_start" | bc -l)
printf "🚀 Deployment complete! Total time: %.2f seconds\n" "$total_duration"
fi
# Clean up temp files
for dir in "${!urls[@]}"; do
output_file="/tmp/deploy_${dir}.out"
if [[ -f "$output_file" ]]; then
rm "$output_file"
fi
done