-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-manager.sh
More file actions
executable file
·347 lines (298 loc) · 8.94 KB
/
server-manager.sh
File metadata and controls
executable file
·347 lines (298 loc) · 8.94 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
# DUT Reservation Server Manager
# This script provides easy commands to manage your Flask web server
SERVICE_NAME="dut-reservation.service"
SERVER_DIR="/home/ubuntu/mkim/testbed-reservation"
LOG_FILE="$SERVER_DIR/server.log"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} DUT Reservation Server Manager${NC}"
echo -e "${BLUE}================================${NC}"
}
# Function to check if service exists
check_service() {
if ! systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
print_error "Service $SERVICE_NAME not found. Please run: ./server-manager.sh install"
exit 1
fi
}
# Function to check if running as root
check_root() {
if [[ $EUID -eq 0 ]]; then
print_error "This script should not be run as root"
exit 1
fi
}
# Function to show usage
show_usage() {
print_header
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " start - Start the web server"
echo " stop - Stop the web server"
echo " restart - Restart the web server"
echo " status - Show server status"
echo " logs - Show recent logs"
echo " logs-follow - Follow logs in real-time"
echo " install - Install the systemd service"
echo " uninstall - Remove the systemd service"
echo " manual - Start server manually (for debugging)"
echo " health - Run health check"
echo " config - Show configuration"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 start"
echo " $0 status"
echo " $0 logs"
}
# Function to kill any existing manual Flask processes
kill_manual_processes() {
print_status "Checking for existing Flask processes..."
# Find and kill any manual Flask server processes
local flask_pids=$(pgrep -f "python3.*server.py" | grep -v $$)
if [ ! -z "$flask_pids" ]; then
print_warning "Found existing Flask processes: $flask_pids"
print_status "Stopping manual Flask processes..."
for pid in $flask_pids; do
kill -TERM $pid 2>/dev/null
sleep 1
if kill -0 $pid 2>/dev/null; then
print_warning "Process $pid still running, force killing..."
kill -KILL $pid 2>/dev/null
fi
done
sleep 2
else
print_status "No existing Flask processes found"
fi
}
# Function to start the server
start_server() {
print_status "Starting DUT reservation server..."
# Kill any existing manual processes first
kill_manual_processes
# Try to start via systemd
if systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
print_status "Starting via systemd service..."
sudo systemctl start $SERVICE_NAME
if [ $? -eq 0 ]; then
print_status "Server started successfully via systemd!"
sleep 2
show_status
else
print_warning "Systemd service failed to start, trying manual start..."
start_manual_background
fi
else
print_warning "Systemd service not found, starting manually..."
start_manual_background
fi
}
# Function to start server manually in background
start_manual_background() {
print_status "Starting server manually in background..."
cd "$SERVER_DIR"
export FLASK_APP=server.py
export FLASK_ENV=production
# Start in background and redirect output
nohup python3 server.py > server.log 2>&1 &
local pid=$!
# Wait a moment and check if it started successfully
sleep 3
if kill -0 $pid 2>/dev/null; then
print_status "Server started successfully! PID: $pid"
print_status "Logs are being written to: $LOG_FILE"
sleep 2
show_status
else
print_error "Failed to start server manually"
exit 1
fi
}
# Function to stop the server
stop_server() {
print_status "Stopping DUT reservation server..."
# First try to stop via systemd
if systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
print_status "Stopping systemd service..."
sudo systemctl stop $SERVICE_NAME 2>/dev/null
fi
# Then kill any manual processes
kill_manual_processes
print_status "Server stopped successfully!"
}
# Function to restart the server
restart_server() {
print_status "Restarting DUT reservation server..."
# Stop the server (handles both systemd and manual processes)
stop_server
# Wait a moment
sleep 2
# Start the server (will try systemd first, then manual)
start_server
}
# Function to show server status
show_status() {
print_status "Server Status:"
echo ""
# Check systemd service status
if systemctl list-unit-files | grep -q "$SERVICE_NAME"; then
print_status "Systemd Service Status:"
sudo systemctl status $SERVICE_NAME --no-pager -l
echo ""
else
print_warning "Systemd service not installed"
fi
# Check for manual Flask processes
local flask_pids=$(pgrep -f "python3.*server.py" | grep -v $$)
if [ ! -z "$flask_pids" ]; then
print_status "Manual Flask Processes:"
for pid in $flask_pids; do
echo " PID: $pid - $(ps -p $pid -o cmd= --no-headers)"
done
echo ""
else
print_warning "No manual Flask processes found"
fi
# Show if server is accessible
if curl -s http://localhost:8080 > /dev/null 2>&1; then
print_status "Server is accessible at http://localhost:8080"
else
print_warning "Server may not be accessible at http://localhost:8080"
fi
# Show health dashboard
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
print_status "Health dashboard available at http://localhost:8080/health"
fi
}
# Function to show logs
show_logs() {
print_status "Recent server logs:"
echo ""
sudo journalctl -u $SERVICE_NAME --no-pager -l -n 50
}
# Function to follow logs
follow_logs() {
print_status "Following server logs (Ctrl+C to stop):"
echo ""
sudo journalctl -u $SERVICE_NAME --no-pager -l -f
}
# Function to install the service
install_service() {
print_status "Installing systemd service..."
if [ ! -f "$SERVER_DIR/dut-reservation.service" ]; then
print_error "Service file not found at $SERVER_DIR/dut-reservation.service"
exit 1
fi
sudo cp "$SERVER_DIR/dut-reservation.service" /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable $SERVICE_NAME
print_status "Service installed and enabled!"
print_status "You can now use: $0 start"
}
# Function to uninstall the service
uninstall_service() {
print_status "Uninstalling systemd service..."
sudo systemctl stop $SERVICE_NAME 2>/dev/null
sudo systemctl disable $SERVICE_NAME 2>/dev/null
sudo rm -f /etc/systemd/system/$SERVICE_NAME
sudo systemctl daemon-reload
print_status "Service uninstalled!"
}
# Function to start server manually
start_manual() {
print_status "Starting server manually (for debugging)..."
print_warning "This will run in the foreground. Press Ctrl+C to stop."
echo ""
cd "$SERVER_DIR"
export FLASK_APP=server.py
export FLASK_ENV=production
python3 server.py
}
# Function to run health check
run_health_check() {
print_status "Running health check..."
cd "$SERVER_DIR"
if [ -f "test_health_setup.py" ]; then
python3 test_health_setup.py
else
print_warning "Health check script not found"
fi
}
# Function to show configuration
show_config() {
print_status "Server Configuration:"
echo ""
echo "Service Name: $SERVICE_NAME"
echo "Server Directory: $SERVER_DIR"
echo "Log File: $LOG_FILE"
echo ""
if [ -f "$SERVER_DIR/health_config.yaml" ]; then
echo "Health Configuration:"
cat "$SERVER_DIR/health_config.yaml"
fi
}
# Main script logic
check_root
case "${1:-help}" in
start)
start_server
;;
stop)
stop_server
;;
restart)
restart_server
;;
status)
show_status
;;
logs)
show_logs
;;
logs-follow)
follow_logs
;;
install)
install_service
;;
uninstall)
uninstall_service
;;
manual)
start_manual
;;
health)
run_health_check
;;
config)
show_config
;;
help|--help|-h)
show_usage
;;
*)
print_error "Unknown command: $1"
echo ""
show_usage
exit 1
;;
esac