-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·251 lines (213 loc) · 6.53 KB
/
setup
File metadata and controls
executable file
·251 lines (213 loc) · 6.53 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
#!/bin/bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="${ROOT_DIR}/.venv"
MAIN_SCRIPT="${ROOT_DIR}/main.py"
REQUIREMENTS_FILE="${ROOT_DIR}/requirements.txt"
SERVICE_FILE="/etc/systemd/system/meshrunner.service"
SCRIPT_NAME="$(basename "$0")"
PIWHEELS_SIMPLE_URL="https://www.piwheels.org/simple"
GREEN="\e[32m"
RED="\e[31m"
RESET="\e[0m"
require_checkout() {
if [ ! -f "$MAIN_SCRIPT" ]; then
echo -e "${RED}Error: main.py not found in ${ROOT_DIR}.${RESET}"
exit 1
fi
if [ ! -f "$REQUIREMENTS_FILE" ]; then
echo -e "${RED}Error: requirements.txt not found in ${ROOT_DIR}.${RESET}"
exit 1
fi
}
cache_sudo_credentials() {
echo "Caching sudo credentials..."
sudo -v
}
ensure_runtime_prerequisites() {
if ! command -v python3 >/dev/null 2>&1; then
echo -e "${RED}Error: python3 is required but not installed.${RESET}"
echo "Install python3 in the base image before running setup."
exit 1
fi
if ! python3 -c "import venv" >/dev/null 2>&1; then
echo -e "${RED}Error: python3 venv support is required but not installed.${RESET}"
echo "Install python3-venv in the base image before running setup."
exit 1
fi
}
ensure_update_prerequisites() {
ensure_runtime_prerequisites
if ! command -v git >/dev/null 2>&1; then
echo -e "${RED}Error: git is required for --update but is not installed.${RESET}"
echo "Install git in the base image before running setup --update."
exit 1
fi
}
system_architecture() {
if command -v dpkg >/dev/null 2>&1; then
dpkg --print-architecture
else
uname -m
fi
}
should_use_piwheels() {
[ "$(system_architecture)" = "armhf" ]
}
bootstrap_venv() {
require_checkout
ensure_runtime_prerequisites
if [ ! -d "$VENV_DIR" ]; then
echo -e "${GREEN}Creating virtual environment in ${VENV_DIR}...${RESET}"
python3 -m venv "$VENV_DIR"
fi
echo -e "${GREEN}Installing Python dependencies into ${VENV_DIR}...${RESET}"
if should_use_piwheels; then
echo "Using piwheels as an extra package index for armhf installs."
"$VENV_DIR/bin/python" -m pip install --disable-pip-version-check --prefer-binary \
--extra-index-url "$PIWHEELS_SIMPLE_URL" \
-r "$REQUIREMENTS_FILE"
else
"$VENV_DIR/bin/python" -m pip install --disable-pip-version-check --prefer-binary \
-r "$REQUIREMENTS_FILE"
fi
echo -e "${GREEN}MeshRunner environment is ready.${RESET}"
}
install_meshrunner() {
cache_sudo_credentials
bootstrap_venv
echo -e "${GREEN}Installing systemd service...${RESET}"
sudo tee "$SERVICE_FILE" >/dev/null <<EOF
[Unit]
Description=MeshRunner BBS Service
After=network.target
[Service]
Type=simple
WorkingDirectory=${ROOT_DIR}
ExecStart=${VENV_DIR}/bin/python ${MAIN_SCRIPT}
Restart=always
User=$(whoami)
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable meshrunner
sudo systemctl restart meshrunner
echo -e "${GREEN}MeshRunner service installed and started.${RESET}"
}
remove_service() {
cache_sudo_credentials
echo -e "${RED}Removing MeshRunner service...${RESET}"
sudo systemctl stop meshrunner 2>/dev/null || true
sudo systemctl disable meshrunner 2>/dev/null || true
sudo rm -f "$SERVICE_FILE"
sudo systemctl daemon-reload
}
start_service() {
cache_sudo_credentials
sudo systemctl start meshrunner
}
stop_service() {
cache_sudo_credentials
sudo systemctl stop meshrunner
}
restart_service() {
cache_sudo_credentials
sudo systemctl restart meshrunner
}
check_logs() {
local log_file="${ROOT_DIR}/logs/meshrunner.log"
if [ -f "$log_file" ]; then
tail -n 50 "$log_file"
else
journalctl -u meshrunner -n 50 --no-pager -o cat
fi
}
show_logs_dialog() {
clear
if command -v less >/dev/null 2>&1; then
check_logs | less
else
check_logs
echo
read -r -p "Press Enter to return..." _
fi
clear
}
update_meshrunner() {
require_checkout
ensure_update_prerequisites
cache_sudo_credentials
if [ ! -d "${ROOT_DIR}/.git" ]; then
echo -e "${RED}Error: ${ROOT_DIR} is not a git checkout.${RESET}"
exit 1
fi
echo -e "${RED}Warning: update will run git pull --ff-only against this checkout.${RESET}"
echo -e "${GREEN}Updating MeshRunner checkout...${RESET}"
git -C "$ROOT_DIR" pull --ff-only
bootstrap_venv
if systemctl is-active --quiet meshrunner; then
restart_service
fi
}
show_menu() {
if ! command -v whiptail >/dev/null 2>&1; then
echo -e "${RED}whiptail is not installed. Run with --help for CLI usage.${RESET}"
exit 1
fi
while true; do
CHOICE=$(whiptail --title "MeshRunner Setup & Manager" --menu "Choose an option" 16 60 8 \
"1" "Install" \
"2" "Start service" \
"3" "Stop service" \
"4" "Restart service" \
"5" "Remove service" \
"6" "Show logs" \
"7" "Update (warning: destructive)" \
"8" "Exit" 3>&1 1>&2 2>&3)
case "$CHOICE" in
1) install_meshrunner ;;
2) start_service ;;
3) stop_service ;;
4) restart_service ;;
5) remove_service ;;
6) show_logs_dialog ;;
7) update_meshrunner ;;
8) exit 0 ;;
*) exit 1 ;;
esac
done
}
show_help() {
cat <<EOF
MeshRunner Setup & Manager
Usage: ${SCRIPT_NAME} [option]
This script assumes the repository has already been cloned locally.
Options:
--install Create/update the local .venv, install the systemd service, and start it
--remove-service Stop and remove the systemd service
--start Start the service
--stop Stop the service
--restart Restart the service
--logs Show recent service logs
--update Warning: destructive. Pull the current checkout from GitHub and refresh the .venv
--menu Open the interactive menu
--help Show this help message
EOF
}
case "${1:---menu}" in
--install) install_meshrunner ;;
--remove-service) remove_service ;;
--start) start_service ;;
--stop) stop_service ;;
--restart) restart_service ;;
--logs) check_logs ;;
--update) update_meshrunner ;;
--menu) show_menu ;;
--help) show_help ;;
*)
echo -e "${RED}Unknown option: $1${RESET}"
show_help
exit 1
;;
esac