-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaetherlink-server.sh
154 lines (130 loc) · 3.67 KB
/
aetherlink-server.sh
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
#!/bin/bash
# AetherLink Server Runner
# This script manages the AetherLink server process with proper error handling,
# logging, and process management.
set -euo pipefail
# Configuration
AETHERLINK_HOME="${AETHERLINK_HOME:-${HOME}/.aetherlink}"
CONFIG_FILE="${AETHERLINK_HOME}/config/aetherlink_config.json"
LOG_DIR="${AETHERLINK_HOME}/logs"
PID_FILE="${AETHERLINK_HOME}/aetherlink.pid"
CADDY_BIN="./caddy"
# Logging configuration
LOG_FILE="${LOG_DIR}/server.log"
ERROR_LOG="${LOG_DIR}/error.log"
# Ensure required directories exist
setup_directories() {
local dirs=("${LOG_DIR}" "${AETHERLINK_HOME}/config" "${AETHERLINK_HOME}/certs")
for dir in "${dirs[@]}"; do
if ! mkdir -p "${dir}"; then
echo "Error: Failed to create directory: ${dir}" >&2
exit 1
fi
done
}
# Validate configuration file
validate_config() {
if [ ! -f "${CONFIG_FILE}" ]; then
echo "Error: Configuration file not found: ${CONFIG_FILE}" >&2
exit 1
fi
if ! "${CADDY_BIN}" validate --config "${CONFIG_FILE}" > /dev/null 2>&1; then
echo "Error: Invalid configuration file" >&2
exit 1
fi
}
# Check if required binaries are available
check_requirements() {
if [ ! -x "${CADDY_BIN}" ]; then
echo "Error: Caddy binary not found or not executable: ${CADDY_BIN}" >&2
exit 1
fi
}
# Check if server is already running
check_running() {
if [ -f "${PID_FILE}" ]; then
local pid
pid=$(cat "${PID_FILE}")
if kill -0 "${pid}" 2>/dev/null; then
echo "Error: Server is already running with PID ${pid}" >&2
exit 1
else
rm -f "${PID_FILE}"
fi
fi
}
# Setup signal handlers
setup_signal_handlers() {
trap cleanup SIGTERM SIGINT
}
# Cleanup function
cleanup() {
echo "Shutting down AetherLink server..."
if [ -f "${PID_FILE}" ]; then
local pid
pid=$(cat "${PID_FILE}")
kill "${pid}" 2>/dev/null || true
rm -f "${PID_FILE}"
fi
exit 0
}
# Start the server
start_server() {
echo "Starting AetherLink server..."
# Start Caddy in background
"${CADDY_BIN}" run --config "${CONFIG_FILE}" \
--adapter caddyfile \
--pidfile "${PID_FILE}" >> "${LOG_FILE}" 2>> "${ERROR_LOG}" &
local pid=$!
echo "${pid}" > "${PID_FILE}"
# Wait for server to start
local retries=0
local max_retries=30
local started=false
while [ ${retries} -lt ${max_retries} ]; do
if curl -s -o /dev/null http://localhost:2019/config/; then
started=true
break
fi
sleep 1
((retries++))
done
if [ "${started}" = true ]; then
echo "AetherLink server started successfully (PID: ${pid})"
echo "Log file: ${LOG_FILE}"
echo "Error log: ${ERROR_LOG}"
else
echo "Error: Failed to start server after ${max_retries} seconds" >&2
cleanup
exit 1
fi
}
# Monitor server health
monitor_server() {
local pid
pid=$(cat "${PID_FILE}")
while true; do
if ! kill -0 "${pid}" 2>/dev/null; then
echo "Error: Server process died unexpectedly" >&2
cleanup
exit 1
fi
if ! curl -s -o /dev/null http://localhost:2019/config/; then
echo "Warning: Server health check failed" >&2
# Log the failure but don't exit - let the process try to recover
fi
sleep 30
done
}
main() {
# Setup
setup_directories
check_requirements
validate_config
check_running
setup_signal_handlers
# Start and monitor
start_server
monitor_server
}
main "$@"