-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.sh
136 lines (108 loc) · 3.85 KB
/
entrypoint.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
#!/bin/bash
# ========== CONSTANT VARIABLES ==========
# Define WGDASH if it's not already defined
: "${WGDASH:=/opt/wireguarddashboard}"
# Define CONFIGURATION_PATH if it's not already defined
: "${CONFIGURATION_PATH:=$WGDASH}"
LOG_DIR="${WGDASH}/app/src/log"
PID_FILE="${WGDASH}/app/src/gunicorn.pid"
CONFIG_FILE="${CONFIGURATION_PATH}/wg-dashboard.ini"
PY_CACHE="${WGDASH}/app/src/__pycache__"
WG_CONF_DIR="/etc/wireguard"
INITIAL_SLEEP=5
RETRY_SLEEP=5
venv_python="./venv/bin/python3"
# ========== CLEAN UP ==========
clean_up() {
echo "Starting cleanup process..."
if [ -f "$PID_FILE" ]; then
echo "Found old .pid file. Removing it."
rm "$PID_FILE"
else
echo "No .pid file found. Continuing."
fi
if [ -d "$PY_CACHE" ]; then
echo "Directory $PY_CACHE exists. Deleting it..."
rm -rf "$PY_CACHE"
echo "Python cache directory deleted."
fi
}
# ========== START CORE SERVICES ==========
start_core() {
echo "Activating Python virtual environment and starting WireGuard Dashboard service."
. "${WGDASH}/app/src/venv/bin/activate"
cd "${WGDASH}/app/src" || { echo "Failed to change directory. Exiting."; return; }
bash wgd.sh start
if [ -n "$ENABLE" ]; then
IFS=',' read -r -a interfaces <<< "$ENABLE"
for interface in "${interfaces[@]}"; do
echo "Bringing up interface: $interface"
wg-quick up "$interface"
done
else
echo "No interfaces specified in the 'ENABLE' variable."
fi
}
# ========== STOP CORE SERVICES ==========
stop_core() {
echo "Stopping WireGuard Dashboard service."
. "${WGDASH}/app/src/venv/bin/activate"
cd "${WGDASH}/app/src" || { echo "Failed to change directory. Exiting."; return; }
bash wgd.sh stop
}
# ========== SET ENVIRONMENT VARIABLES ==========
set_envvars() {
echo "Setting environment variables."
# Update timezone
if [ "${TZ}" != "$(cat /etc/timezone)" ]; then
echo "Updating timezone to ${TZ}."
ln -sf /usr/share/zoneinfo/"${TZ}" /etc/localtime
echo "${TZ}" > /etc/timezone
fi
. "${WGDASH}/app/src/venv/bin/activate"
cd "${WGDASH}/app/src" || { echo "Failed to change directory. Exiting."; return; }
${venv_python} /update_wireguard.py
echo "Configuration update complete."
}
# ========== DISPLAY LOGS ==========
display_logs() {
echo "Waiting for $INITIAL_SLEEP seconds to ensure logs are created..."
sleep "$INITIAL_SLEEP"
while true; do
echo "Checking for latest logs..."
# Check if log directory is not empty
if find "$LOG_DIR" -mindepth 1 -maxdepth 1 -type f | read -r; then
latestErrLog=$(find "$LOG_DIR" -name "error_*.log" -type f -print | sort -r | head -n 1)
latestAccLog=$(find "$LOG_DIR" -name "access_*.log" -type f -print | sort -r | head -n 1)
# Tail the latest error and access logs
if [[ -n "$latestErrLog" && -n "$latestAccLog" ]]; then
echo "Tailing logs: $latestErrLog, $latestAccLog"
tail -f "$latestErrLog" "$latestAccLog"
else
echo "No logs found to tail."
fi
else
echo "Log directory is empty."
fi
echo "Retrying in $RETRY_SLEEP seconds..."
sleep "$RETRY_SLEEP"
done
}
# ========== MAIN EXECUTION ==========
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file not found, running initial setup..."
echo "========== START CORE: =========="
start_core
echo "Waiting for $INITIAL_SLEEP seconds before stopping core..."
sleep "$INITIAL_SLEEP"
echo "========== STOP CORE: ==========="
stop_core
fi
echo "========== CLEAN UP: ============"
clean_up
echo "========== SET ENV: ============="
set_envvars
echo "========== START CORE: =========="
start_core
echo "========== SHOW LOGS: ==========="
display_logs