-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathdw
More file actions
executable file
·564 lines (503 loc) · 18 KB
/
Copy pathdw
File metadata and controls
executable file
·564 lines (503 loc) · 18 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/bin/bash
#
# Dune Weaver CLI
#
# Usage: dw <command>
#
# Commands:
# install Run initial setup
# start Start Dune Weaver
# stop Stop Dune Weaver
# restart Restart Dune Weaver
# update Pull latest changes and restart
# logs View live logs (Ctrl+C to exit)
# status Show service status
# shell Open a shell with the venv activated
# checkout Switch to a branch and rebuild
# touch Manage touch screen app
# wifi Manage WiFi and hotspot
# help Show this help message
#
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Find dune-weaver directory
find_install_dir() {
# Check common locations
if [[ -f "$HOME/dune-weaver/main.py" ]]; then
echo "$HOME/dune-weaver"
elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
echo "/home/pi/dune-weaver"
elif [[ -f "./main.py" ]]; then
pwd
else
echo ""
fi
}
INSTALL_DIR=$(find_install_dir)
# Run a command as the real (non-root) user when invoked via sudo.
# For dw commands that are not typically run with sudo, this is a no-op.
run_as_user() {
if [[ $EUID -eq 0 && -n "$SUDO_USER" ]]; then
sudo -u "$SUDO_USER" -- "$@"
else
"$@"
fi
}
# Ensure repo files are owned by the real user (not root).
fix_repo_ownership() {
if [[ -n "$INSTALL_DIR" && $EUID -eq 0 && -n "$SUDO_USER" ]]; then
chown -R "$SUDO_USER:$SUDO_USER" "$INSTALL_DIR"
fi
}
# Check if installed
check_installed() {
if [[ -z "$INSTALL_DIR" ]]; then
echo -e "${RED}Error: Dune Weaver not found${NC}"
echo ""
echo "Please install first:"
echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
echo " cd dune-weaver"
echo " dw install"
exit 1
fi
}
# Commands
cmd_install() {
if [[ -z "$INSTALL_DIR" ]]; then
# Not installed, check if we're in the right directory
if [[ -f "./main.py" ]] && [[ -f "./requirements.txt" ]]; then
INSTALL_DIR=$(pwd)
else
echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
echo ""
echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
echo " cd dune-weaver"
echo " dw install"
exit 1
fi
fi
cd "$INSTALL_DIR"
bash setup-pi.sh "$@"
}
cmd_start() {
check_installed
echo -e "${BLUE}Starting Dune Weaver...${NC}"
sudo systemctl start dune-weaver
echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}')"
}
cmd_stop() {
check_installed
echo -e "${BLUE}Stopping Dune Weaver...${NC}"
sudo systemctl stop dune-weaver
echo -e "${GREEN}Stopped${NC}"
}
cmd_restart() {
check_installed
echo -e "${BLUE}Restarting Dune Weaver...${NC}"
sudo systemctl restart dune-weaver
echo -e "${GREEN}Restarted${NC}"
}
cmd_update() {
check_installed
echo -e "${BLUE}Updating Dune Weaver...${NC}"
cd "$INSTALL_DIR"
# Check if we should skip the pull phase (called after re-exec)
if [[ "$1" != "--continue" ]]; then
echo "Pulling latest code..."
run_as_user git config --global --add safe.directory "$INSTALL_DIR" 2>/dev/null || true
local branch
branch=$(git rev-parse --abbrev-ref HEAD)
run_as_user git fetch origin "$branch"
run_as_user git reset --hard "origin/$branch"
fix_repo_ownership
# Update dw CLI
echo "Updating dw command..."
sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
sudo chmod +x /usr/local/bin/dw
# Re-exec with the new script to ensure new code runs
echo "Restarting with updated CLI..."
exec /usr/local/bin/dw update --continue
fi
# Detect Docker-to-native migration: no venv means first native run
if [[ ! -d "$INSTALL_DIR/.venv" ]]; then
echo ""
echo -e "${YELLOW}Migrating to native deployment (first time setup)...${NC}"
echo "This may take a few minutes."
echo ""
# Install native system dependencies (nginx, python3-venv, gcc, etc.)
echo "Installing system dependencies..."
sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
python3-venv python3-pip python3-dev \
gcc g++ make swig unzip wget \
libjpeg-dev zlib1g-dev \
libgpiod-dev gpiod \
nginx
# Create Python venv as real user
echo "Creating Python virtual environment..."
run_as_user python3 -m venv .venv
# Stop and remove Docker containers/service if running
if command -v docker &> /dev/null; then
echo "Stopping Docker containers..."
sudo docker compose down 2>/dev/null || true
fi
fi
# Fix venv ownership if created by sudo (legacy installs)
if [[ -d ".venv" ]] && [[ "$(stat -c '%U' .venv 2>/dev/null)" == "root" ]]; then
echo "Fixing .venv ownership..."
local real_user="${SUDO_USER:-$USER}"
sudo chown -R "$real_user:$real_user" .venv
fi
echo "Updating Python dependencies..."
source .venv/bin/activate
run_as_user .venv/bin/pip install -r requirements.txt
# Update nginx config
echo "Updating nginx config..."
sudo cp "$INSTALL_DIR/nginx/dune-weaver.conf" /etc/nginx/sites-available/dune-weaver.conf
sudo sed -i "s|INSTALL_DIR_PLACEHOLDER|$INSTALL_DIR|g" /etc/nginx/sites-available/dune-weaver.conf
sudo ln -sf /etc/nginx/sites-available/dune-weaver.conf /etc/nginx/sites-enabled/dune-weaver.conf
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl restart nginx
# Update systemd service
echo "Updating systemd service..."
sudo cp "$INSTALL_DIR/dune-weaver.service" /etc/systemd/system/dune-weaver.service
sudo sed -i "s|INSTALL_DIR_PLACEHOLDER|$INSTALL_DIR|g" /etc/systemd/system/dune-weaver.service
sudo systemctl daemon-reload
sudo systemctl enable dune-weaver
echo "Restarting service..."
sudo systemctl restart dune-weaver
# Install/update WiFi host scripts if not present
if [[ -f "$INSTALL_DIR/wifi/setup-wifi.sh" ]]; then
if [[ ! -f /usr/local/bin/autohotspot ]]; then
echo ""
echo -e "${BLUE}Installing autohotspot WiFi scripts...${NC}"
sudo bash "$INSTALL_DIR/wifi/setup-wifi.sh"
else
# Always update the script and service files to latest
echo "Updating WiFi host scripts..."
sudo cp "$INSTALL_DIR/wifi/autohotspot" /usr/local/bin/autohotspot
sudo chmod +x /usr/local/bin/autohotspot
# Update service/timer files
sudo cp "$INSTALL_DIR/wifi/autohotspot.service" /etc/systemd/system/autohotspot.service
sudo cp "$INSTALL_DIR/wifi/autohotspot-check.service" /etc/systemd/system/autohotspot-check.service 2>/dev/null || true
sudo cp "$INSTALL_DIR/wifi/autohotspot-check.timer" /etc/systemd/system/autohotspot-check.timer 2>/dev/null || true
sudo systemctl daemon-reload
sudo systemctl enable autohotspot-check.timer 2>/dev/null || true
fi
fi
# Update touch app if installed (check both directory and service exist)
local touch_dir="$INSTALL_DIR/dune-weaver-touch"
if [[ -d "$touch_dir" ]] && systemctl list-unit-files dune-weaver-touch.service 2>/dev/null | grep -q dune-weaver-touch; then
echo ""
echo -e "${BLUE}Updating touch app...${NC}"
cd "$touch_dir"
# Update Python dependencies (code already pulled with main repo)
if [[ -f "requirements.txt" ]] && [[ -d ".venv" ]]; then
echo "Updating touch app dependencies..."
source .venv/bin/activate
pip install -q -r requirements.txt
deactivate
fi
# Restart to apply changes
sudo systemctl restart dune-weaver-touch
echo -e "${GREEN}Touch app updated!${NC}"
fi
echo -e "${GREEN}Update complete!${NC}"
echo ""
echo -e "${YELLOW}Please reload the web page in your browser to see the changes.${NC}"
}
cmd_logs() {
check_installed
# Parse optional line count (e.g., dw logs 100)
local lines="${1:-}"
if [[ -n "$lines" ]]; then
sudo journalctl -u dune-weaver -n "$lines"
else
sudo journalctl -u dune-weaver -f
fi
}
cmd_status() {
check_installed
sudo systemctl status dune-weaver
}
cmd_shell() {
check_installed
cd "$INSTALL_DIR"
echo -e "${BLUE}Activating venv...${NC}"
exec bash --init-file <(echo "source '$INSTALL_DIR/.venv/bin/activate'; cd '$INSTALL_DIR'; echo 'Dune Weaver venv activated. Type \"exit\" to leave.'")
}
# Touch app commands (systemd service)
cmd_touch() {
local subcmd="${1:-help}"
case "$subcmd" in
logs)
local lines="${2:-}"
if [[ -n "$lines" ]]; then
sudo journalctl -u dune-weaver-touch -n "$lines"
else
sudo journalctl -u dune-weaver-touch -f
fi
;;
restart)
echo -e "${BLUE}Restarting touch app...${NC}"
sudo systemctl restart dune-weaver-touch
echo -e "${GREEN}Touch app restarted${NC}"
;;
stop)
echo -e "${BLUE}Stopping touch app...${NC}"
sudo systemctl stop dune-weaver-touch
echo -e "${GREEN}Touch app stopped${NC}"
;;
start)
echo -e "${BLUE}Starting touch app...${NC}"
sudo systemctl start dune-weaver-touch
echo -e "${GREEN}Touch app started${NC}"
;;
status)
sudo systemctl status dune-weaver-touch
;;
install)
check_installed
local touch_dir="$INSTALL_DIR/dune-weaver-touch"
if [[ ! -f "$touch_dir/install.sh" ]]; then
echo -e "${RED}Touch app installer not found at $touch_dir/install.sh${NC}"
echo "Make sure you have the dune-weaver-touch directory in your dune-weaver folder."
exit 1
fi
echo -e "${BLUE}Running touch app installer...${NC}"
cd "$touch_dir"
sudo bash install.sh
;;
help|*)
echo -e "${GREEN}Touch App Commands${NC}"
echo ""
echo "Usage: dw touch <command>"
echo ""
echo "Commands:"
echo " install Install touch app (clone, setup, enable service)"
echo " logs [N] View logs (N lines or follow if omitted)"
echo " restart Restart touch app"
echo " stop Stop touch app"
echo " start Start touch app"
echo " status Show touch app status"
echo " help Show this help message"
;;
esac
}
cmd_checkout() {
check_installed
local branch="$1"
if [[ -z "$branch" ]]; then
echo -e "${RED}Usage: dw checkout <branch>${NC}"
echo ""
echo "Examples:"
echo " dw checkout main"
echo " dw checkout feature/security-settings-split"
echo ""
echo "Current branch: $(git -C "$INSTALL_DIR" rev-parse --abbrev-ref HEAD)"
exit 1
fi
cd "$INSTALL_DIR"
# Repo may be cloned with --single-branch; fetch the target branch explicitly
echo -e "${BLUE}Fetching branch ${branch}...${NC}"
if ! run_as_user git fetch origin "$branch" 2>/dev/null; then
echo -e "${RED}Branch '${branch}' not found on remote${NC}"
exit 1
fi
# Checkout: create local tracking branch if it doesn't exist
if git show-ref --verify --quiet "refs/heads/$branch"; then
run_as_user git checkout "$branch"
run_as_user git reset --hard "origin/$branch"
else
run_as_user git checkout -b "$branch" "origin/$branch"
fi
fix_repo_ownership
echo -e "Switched to branch: ${GREEN}${branch}${NC}"
# Update Python dependencies
echo "Updating Python dependencies..."
source .venv/bin/activate
run_as_user .venv/bin/pip install -r requirements.txt
# Update dw CLI from the new branch
sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
sudo chmod +x /usr/local/bin/dw
# Restart service
sudo systemctl restart dune-weaver
# Install WiFi scripts if available on this branch but not yet installed
if [[ -f "$INSTALL_DIR/wifi/setup-wifi.sh" ]] && [[ ! -f /usr/local/bin/autohotspot ]]; then
echo ""
echo -e "${BLUE}Installing autohotspot WiFi scripts...${NC}"
sudo bash "$INSTALL_DIR/wifi/setup-wifi.sh"
fi
echo -e "${GREEN}Checkout complete!${NC}"
}
# WiFi / Hotspot commands
cmd_wifi() {
local subcmd="${1:-help}"
case "$subcmd" in
status)
check_installed
echo -e "${BLUE}WiFi Status${NC}"
echo ""
# Show current mode
local mode="unknown"
if [[ -f /tmp/dw-wifi-mode ]]; then
mode=$(cat /tmp/dw-wifi-mode)
fi
echo -e "Mode: ${GREEN}${mode}${NC}"
# Show connected network
local ssid
ssid=$(nmcli -t -f GENERAL.CONNECTION dev show wlan0 2>/dev/null | head -1 | cut -d: -f2)
if [[ -n "$ssid" && "$ssid" != "--" ]]; then
echo -e "Network: ${GREEN}${ssid}${NC}"
fi
# Show IP
local ip
ip=$(nmcli -t -f IP4.ADDRESS dev show wlan0 2>/dev/null | head -1 | cut -d: -f2 | cut -d/ -f1)
if [[ -n "$ip" ]]; then
echo -e "IP: ${GREEN}${ip}${NC}"
fi
;;
scan)
echo -e "${BLUE}Scanning for WiFi networks...${NC}"
nmcli dev wifi rescan 2>/dev/null || true
sleep 2
nmcli dev wifi list
;;
connect)
echo -e "${BLUE}Available WiFi networks:${NC}"
nmcli dev wifi rescan 2>/dev/null || true
sleep 2
nmcli dev wifi list
echo ""
read -p "Enter SSID to connect to: " ssid
if [[ -z "$ssid" ]]; then
echo -e "${RED}No SSID provided${NC}"
exit 1
fi
read -sp "Enter password (leave empty for open networks): " password
echo ""
# Delete any stale connection profile for this SSID
sudo nmcli con delete "$ssid" 2>/dev/null || true
if [[ -n "$password" ]]; then
# Create connection with explicit security settings
# (nmcli dev wifi connect can fail on Trixie without key-mgmt)
sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
ssid "$ssid" \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "$password"
else
sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
ssid "$ssid"
fi
echo -e "${BLUE}Connecting to '$ssid'...${NC}"
if sudo nmcli con up "$ssid"; then
echo -e "${GREEN}Connected!${NC}"
else
echo -e "${RED}Failed to connect. Check password and try again.${NC}"
sudo nmcli con delete "$ssid" 2>/dev/null || true
exit 1
fi
;;
hotspot)
echo -e "${BLUE}Switching to hotspot mode...${NC}"
sudo nmcli dev disconnect wlan0 2>/dev/null || true
sudo nmcli con up DuneWeaver-Hotspot 2>/dev/null
echo "hotspot" | sudo tee /tmp/dw-wifi-mode > /dev/null
echo -e "${GREEN}Hotspot active!${NC} Connect to the 'Dune Weaver' WiFi network."
;;
setup)
check_installed
cd "$INSTALL_DIR"
sudo bash wifi/setup-wifi.sh
;;
help|*)
echo -e "${GREEN}WiFi Commands${NC}"
echo ""
echo "Usage: dw wifi <command>"
echo ""
echo "Commands:"
echo " status Show current WiFi mode and connection"
echo " scan Scan for available networks"
echo " connect Interactive: scan + select + connect"
echo " hotspot Force hotspot mode"
echo " setup Run initial autohotspot setup"
echo " help Show this help message"
;;
esac
}
cmd_help() {
echo -e "${GREEN}Dune Weaver CLI${NC}"
echo ""
echo "Usage: dw <command>"
echo ""
echo "Commands:"
echo " install Run initial setup"
echo " start Start Dune Weaver"
echo " stop Stop Dune Weaver"
echo " restart Restart Dune Weaver"
echo " update Pull latest changes and restart"
echo " checkout Switch to a branch and rebuild"
echo " logs [N] View logs (N=number of lines, or follow if omitted)"
echo " status Show service status"
echo " shell Open a shell with the venv activated"
echo " touch Manage touch screen app (run 'dw touch help')"
echo " wifi Manage WiFi and hotspot (run 'dw wifi help')"
echo " help Show this help message"
echo ""
if [[ -n "$INSTALL_DIR" ]]; then
echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
fi
}
# Main
case "${1:-help}" in
install)
shift
cmd_install "$@"
;;
start)
cmd_start
;;
stop)
cmd_stop
;;
restart)
cmd_restart
;;
update)
cmd_update "$2"
;;
checkout)
cmd_checkout "$2"
;;
logs)
cmd_logs "$2"
;;
status)
cmd_status
;;
shell)
cmd_shell
;;
touch)
shift
cmd_touch "$@"
;;
wifi)
shift
cmd_wifi "$@"
;;
help|--help|-h)
cmd_help
;;
*)
echo -e "${RED}Unknown command: $1${NC}"
echo ""
cmd_help
exit 1
;;
esac