dw 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #!/bin/bash
  2. #
  3. # Dune Weaver CLI
  4. #
  5. # Usage: dw <command>
  6. #
  7. # Commands:
  8. # install Run initial setup
  9. # start Start Dune Weaver
  10. # stop Stop Dune Weaver
  11. # restart Restart Dune Weaver
  12. # update Pull latest changes and restart
  13. # logs View live logs (Ctrl+C to exit)
  14. # status Show service status
  15. # shell Open a shell with the venv activated
  16. # checkout Switch to a branch and rebuild
  17. # touch Manage touch screen app
  18. # wifi Manage WiFi and hotspot
  19. # help Show this help message
  20. #
  21. set -e
  22. # Colors
  23. RED='\033[0;31m'
  24. GREEN='\033[0;32m'
  25. YELLOW='\033[1;33m'
  26. BLUE='\033[0;34m'
  27. NC='\033[0m'
  28. # Find dune-weaver directory
  29. find_install_dir() {
  30. # Check common locations
  31. if [[ -f "$HOME/dune-weaver/main.py" ]]; then
  32. echo "$HOME/dune-weaver"
  33. elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
  34. echo "/home/pi/dune-weaver"
  35. elif [[ -f "./main.py" ]]; then
  36. pwd
  37. else
  38. echo ""
  39. fi
  40. }
  41. INSTALL_DIR=$(find_install_dir)
  42. # Check if installed
  43. check_installed() {
  44. if [[ -z "$INSTALL_DIR" ]]; then
  45. echo -e "${RED}Error: Dune Weaver not found${NC}"
  46. echo ""
  47. echo "Please install first:"
  48. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  49. echo " cd dune-weaver"
  50. echo " dw install"
  51. exit 1
  52. fi
  53. }
  54. # Commands
  55. cmd_install() {
  56. if [[ -z "$INSTALL_DIR" ]]; then
  57. # Not installed, check if we're in the right directory
  58. if [[ -f "./main.py" ]] && [[ -f "./requirements.txt" ]]; then
  59. INSTALL_DIR=$(pwd)
  60. else
  61. echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
  62. echo ""
  63. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  64. echo " cd dune-weaver"
  65. echo " dw install"
  66. exit 1
  67. fi
  68. fi
  69. cd "$INSTALL_DIR"
  70. bash setup-pi.sh "$@"
  71. }
  72. cmd_start() {
  73. check_installed
  74. echo -e "${BLUE}Starting Dune Weaver...${NC}"
  75. sudo systemctl start dune-weaver
  76. echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}')"
  77. }
  78. cmd_stop() {
  79. check_installed
  80. echo -e "${BLUE}Stopping Dune Weaver...${NC}"
  81. sudo systemctl stop dune-weaver
  82. echo -e "${GREEN}Stopped${NC}"
  83. }
  84. cmd_restart() {
  85. check_installed
  86. echo -e "${BLUE}Restarting Dune Weaver...${NC}"
  87. sudo systemctl restart dune-weaver
  88. echo -e "${GREEN}Restarted${NC}"
  89. }
  90. cmd_update() {
  91. check_installed
  92. echo -e "${BLUE}Updating Dune Weaver...${NC}"
  93. cd "$INSTALL_DIR"
  94. # Check if we should skip the pull phase (called after re-exec)
  95. if [[ "$1" != "--continue" ]]; then
  96. echo "Pulling latest code..."
  97. git config --global --add safe.directory "$INSTALL_DIR" 2>/dev/null || true
  98. local branch
  99. branch=$(git rev-parse --abbrev-ref HEAD)
  100. git fetch origin "$branch"
  101. git reset --hard "origin/$branch"
  102. # Update dw CLI
  103. echo "Updating dw command..."
  104. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  105. sudo chmod +x /usr/local/bin/dw
  106. # Re-exec with the new script to ensure new code runs
  107. echo "Restarting with updated CLI..."
  108. exec /usr/local/bin/dw update --continue
  109. fi
  110. # Detect Docker-to-native migration: no venv means first native run
  111. if [[ ! -d "$INSTALL_DIR/.venv" ]]; then
  112. echo ""
  113. echo -e "${YELLOW}Migrating to native deployment (first time setup)...${NC}"
  114. echo "This may take a few minutes."
  115. echo ""
  116. # Install native system dependencies (nginx, python3-venv, gcc, etc.)
  117. echo "Installing system dependencies..."
  118. sudo apt update
  119. sudo DEBIAN_FRONTEND=noninteractive apt install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
  120. python3-venv python3-pip python3-dev \
  121. gcc g++ make swig unzip wget \
  122. libjpeg-dev zlib1g-dev \
  123. libgpiod-dev gpiod \
  124. nginx
  125. # Create Python venv
  126. echo "Creating Python virtual environment..."
  127. python3 -m venv .venv
  128. # Stop and remove Docker containers/service if running
  129. if command -v docker &> /dev/null; then
  130. echo "Stopping Docker containers..."
  131. sudo docker compose down 2>/dev/null || true
  132. fi
  133. fi
  134. # Fix venv ownership if created by sudo (legacy installs)
  135. if [[ -d ".venv" ]] && [[ "$(stat -c '%U' .venv 2>/dev/null)" == "root" ]]; then
  136. echo "Fixing .venv ownership..."
  137. sudo chown -R "$USER:$USER" .venv
  138. fi
  139. echo "Updating Python dependencies..."
  140. source .venv/bin/activate
  141. pip install -r requirements.txt
  142. # Update nginx config
  143. echo "Updating nginx config..."
  144. sudo cp "$INSTALL_DIR/nginx/dune-weaver.conf" /etc/nginx/sites-available/dune-weaver.conf
  145. sudo sed -i "s|INSTALL_DIR_PLACEHOLDER|$INSTALL_DIR|g" /etc/nginx/sites-available/dune-weaver.conf
  146. sudo ln -sf /etc/nginx/sites-available/dune-weaver.conf /etc/nginx/sites-enabled/dune-weaver.conf
  147. sudo rm -f /etc/nginx/sites-enabled/default
  148. sudo nginx -t && sudo systemctl restart nginx
  149. # Update systemd service
  150. echo "Updating systemd service..."
  151. sudo cp "$INSTALL_DIR/dune-weaver.service" /etc/systemd/system/dune-weaver.service
  152. sudo sed -i "s|USER_PLACEHOLDER|$USER|g" /etc/systemd/system/dune-weaver.service
  153. sudo sed -i "s|INSTALL_DIR_PLACEHOLDER|$INSTALL_DIR|g" /etc/systemd/system/dune-weaver.service
  154. sudo systemctl daemon-reload
  155. sudo systemctl enable dune-weaver
  156. echo "Restarting service..."
  157. sudo systemctl restart dune-weaver
  158. # Install/update WiFi host scripts if not present
  159. if [[ -f "$INSTALL_DIR/wifi/setup-wifi.sh" ]]; then
  160. if [[ ! -f /usr/local/bin/autohotspot ]]; then
  161. echo ""
  162. echo -e "${BLUE}Installing autohotspot WiFi scripts...${NC}"
  163. sudo bash "$INSTALL_DIR/wifi/setup-wifi.sh"
  164. else
  165. # Always update the script and service files to latest
  166. echo "Updating WiFi host scripts..."
  167. sudo cp "$INSTALL_DIR/wifi/autohotspot" /usr/local/bin/autohotspot
  168. sudo chmod +x /usr/local/bin/autohotspot
  169. # Update service/timer files
  170. sudo cp "$INSTALL_DIR/wifi/autohotspot.service" /etc/systemd/system/autohotspot.service
  171. sudo cp "$INSTALL_DIR/wifi/autohotspot-check.service" /etc/systemd/system/autohotspot-check.service 2>/dev/null || true
  172. sudo cp "$INSTALL_DIR/wifi/autohotspot-check.timer" /etc/systemd/system/autohotspot-check.timer 2>/dev/null || true
  173. sudo systemctl daemon-reload
  174. sudo systemctl enable autohotspot-check.timer 2>/dev/null || true
  175. fi
  176. fi
  177. # Update touch app if installed (check both directory and service exist)
  178. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  179. if [[ -d "$touch_dir" ]] && systemctl list-unit-files dune-weaver-touch.service 2>/dev/null | grep -q dune-weaver-touch; then
  180. echo ""
  181. echo -e "${BLUE}Updating touch app...${NC}"
  182. cd "$touch_dir"
  183. # Update Python dependencies (code already pulled with main repo)
  184. if [[ -f "requirements.txt" ]] && [[ -d ".venv" ]]; then
  185. echo "Updating touch app dependencies..."
  186. source .venv/bin/activate
  187. pip install -q -r requirements.txt
  188. deactivate
  189. fi
  190. # Restart to apply changes
  191. sudo systemctl restart dune-weaver-touch
  192. echo -e "${GREEN}Touch app updated!${NC}"
  193. fi
  194. echo -e "${GREEN}Update complete!${NC}"
  195. echo ""
  196. echo -e "${YELLOW}Please reload the web page in your browser to see the changes.${NC}"
  197. }
  198. cmd_logs() {
  199. check_installed
  200. # Parse optional line count (e.g., dw logs 100)
  201. local lines="${1:-}"
  202. if [[ -n "$lines" ]]; then
  203. sudo journalctl -u dune-weaver -n "$lines"
  204. else
  205. sudo journalctl -u dune-weaver -f
  206. fi
  207. }
  208. cmd_status() {
  209. check_installed
  210. sudo systemctl status dune-weaver
  211. }
  212. cmd_shell() {
  213. check_installed
  214. cd "$INSTALL_DIR"
  215. echo -e "${BLUE}Activating venv...${NC}"
  216. exec bash --init-file <(echo "source '$INSTALL_DIR/.venv/bin/activate'; cd '$INSTALL_DIR'; echo 'Dune Weaver venv activated. Type \"exit\" to leave.'")
  217. }
  218. # Touch app commands (systemd service)
  219. cmd_touch() {
  220. local subcmd="${1:-help}"
  221. case "$subcmd" in
  222. logs)
  223. local lines="${2:-}"
  224. if [[ -n "$lines" ]]; then
  225. sudo journalctl -u dune-weaver-touch -n "$lines"
  226. else
  227. sudo journalctl -u dune-weaver-touch -f
  228. fi
  229. ;;
  230. restart)
  231. echo -e "${BLUE}Restarting touch app...${NC}"
  232. sudo systemctl restart dune-weaver-touch
  233. echo -e "${GREEN}Touch app restarted${NC}"
  234. ;;
  235. stop)
  236. echo -e "${BLUE}Stopping touch app...${NC}"
  237. sudo systemctl stop dune-weaver-touch
  238. echo -e "${GREEN}Touch app stopped${NC}"
  239. ;;
  240. start)
  241. echo -e "${BLUE}Starting touch app...${NC}"
  242. sudo systemctl start dune-weaver-touch
  243. echo -e "${GREEN}Touch app started${NC}"
  244. ;;
  245. status)
  246. sudo systemctl status dune-weaver-touch
  247. ;;
  248. install)
  249. check_installed
  250. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  251. if [[ ! -f "$touch_dir/install.sh" ]]; then
  252. echo -e "${RED}Touch app installer not found at $touch_dir/install.sh${NC}"
  253. echo "Make sure you have the dune-weaver-touch directory in your dune-weaver folder."
  254. exit 1
  255. fi
  256. echo -e "${BLUE}Running touch app installer...${NC}"
  257. cd "$touch_dir"
  258. sudo bash install.sh
  259. ;;
  260. help|*)
  261. echo -e "${GREEN}Touch App Commands${NC}"
  262. echo ""
  263. echo "Usage: dw touch <command>"
  264. echo ""
  265. echo "Commands:"
  266. echo " install Install touch app (clone, setup, enable service)"
  267. echo " logs [N] View logs (N lines or follow if omitted)"
  268. echo " restart Restart touch app"
  269. echo " stop Stop touch app"
  270. echo " start Start touch app"
  271. echo " status Show touch app status"
  272. echo " help Show this help message"
  273. ;;
  274. esac
  275. }
  276. cmd_checkout() {
  277. check_installed
  278. local branch="$1"
  279. if [[ -z "$branch" ]]; then
  280. echo -e "${RED}Usage: dw checkout <branch>${NC}"
  281. echo ""
  282. echo "Examples:"
  283. echo " dw checkout main"
  284. echo " dw checkout feature/security-settings-split"
  285. echo ""
  286. echo "Current branch: $(git -C "$INSTALL_DIR" rev-parse --abbrev-ref HEAD)"
  287. exit 1
  288. fi
  289. cd "$INSTALL_DIR"
  290. # Repo may be cloned with --single-branch; fetch the target branch explicitly
  291. echo -e "${BLUE}Fetching branch ${branch}...${NC}"
  292. if ! git fetch origin "$branch" 2>/dev/null; then
  293. echo -e "${RED}Branch '${branch}' not found on remote${NC}"
  294. exit 1
  295. fi
  296. # Checkout: create local tracking branch if it doesn't exist
  297. if git show-ref --verify --quiet "refs/heads/$branch"; then
  298. git checkout "$branch"
  299. git reset --hard "origin/$branch"
  300. else
  301. git checkout -b "$branch" "origin/$branch"
  302. fi
  303. echo -e "Switched to branch: ${GREEN}${branch}${NC}"
  304. # Update Python dependencies
  305. echo "Updating Python dependencies..."
  306. source .venv/bin/activate
  307. pip install -r requirements.txt
  308. # Update dw CLI from the new branch
  309. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  310. sudo chmod +x /usr/local/bin/dw
  311. # Restart service
  312. sudo systemctl restart dune-weaver
  313. # Install WiFi scripts if available on this branch but not yet installed
  314. if [[ -f "$INSTALL_DIR/wifi/setup-wifi.sh" ]] && [[ ! -f /usr/local/bin/autohotspot ]]; then
  315. echo ""
  316. echo -e "${BLUE}Installing autohotspot WiFi scripts...${NC}"
  317. sudo bash "$INSTALL_DIR/wifi/setup-wifi.sh"
  318. fi
  319. echo -e "${GREEN}Checkout complete!${NC}"
  320. }
  321. # WiFi / Hotspot commands
  322. cmd_wifi() {
  323. local subcmd="${1:-help}"
  324. case "$subcmd" in
  325. status)
  326. check_installed
  327. echo -e "${BLUE}WiFi Status${NC}"
  328. echo ""
  329. # Show current mode
  330. local mode="unknown"
  331. if [[ -f /tmp/dw-wifi-mode ]]; then
  332. mode=$(cat /tmp/dw-wifi-mode)
  333. fi
  334. echo -e "Mode: ${GREEN}${mode}${NC}"
  335. # Show connected network
  336. local ssid
  337. ssid=$(nmcli -t -f GENERAL.CONNECTION dev show wlan0 2>/dev/null | head -1 | cut -d: -f2)
  338. if [[ -n "$ssid" && "$ssid" != "--" ]]; then
  339. echo -e "Network: ${GREEN}${ssid}${NC}"
  340. fi
  341. # Show IP
  342. local ip
  343. ip=$(nmcli -t -f IP4.ADDRESS dev show wlan0 2>/dev/null | head -1 | cut -d: -f2 | cut -d/ -f1)
  344. if [[ -n "$ip" ]]; then
  345. echo -e "IP: ${GREEN}${ip}${NC}"
  346. fi
  347. ;;
  348. scan)
  349. echo -e "${BLUE}Scanning for WiFi networks...${NC}"
  350. nmcli dev wifi rescan 2>/dev/null || true
  351. sleep 2
  352. nmcli dev wifi list
  353. ;;
  354. connect)
  355. echo -e "${BLUE}Available WiFi networks:${NC}"
  356. nmcli dev wifi rescan 2>/dev/null || true
  357. sleep 2
  358. nmcli dev wifi list
  359. echo ""
  360. read -p "Enter SSID to connect to: " ssid
  361. if [[ -z "$ssid" ]]; then
  362. echo -e "${RED}No SSID provided${NC}"
  363. exit 1
  364. fi
  365. read -sp "Enter password (leave empty for open networks): " password
  366. echo ""
  367. # Delete any stale connection profile for this SSID
  368. sudo nmcli con delete "$ssid" 2>/dev/null || true
  369. if [[ -n "$password" ]]; then
  370. # Create connection with explicit security settings
  371. # (nmcli dev wifi connect can fail on Trixie without key-mgmt)
  372. sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
  373. ssid "$ssid" \
  374. wifi-sec.key-mgmt wpa-psk \
  375. wifi-sec.psk "$password"
  376. else
  377. sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
  378. ssid "$ssid"
  379. fi
  380. echo -e "${BLUE}Connecting to '$ssid'...${NC}"
  381. if sudo nmcli con up "$ssid"; then
  382. echo -e "${GREEN}Connected!${NC}"
  383. else
  384. echo -e "${RED}Failed to connect. Check password and try again.${NC}"
  385. sudo nmcli con delete "$ssid" 2>/dev/null || true
  386. exit 1
  387. fi
  388. ;;
  389. hotspot)
  390. echo -e "${BLUE}Switching to hotspot mode...${NC}"
  391. sudo nmcli dev disconnect wlan0 2>/dev/null || true
  392. sudo nmcli con up DuneWeaver-Hotspot 2>/dev/null
  393. echo "hotspot" | sudo tee /tmp/dw-wifi-mode > /dev/null
  394. echo -e "${GREEN}Hotspot active!${NC} Connect to the 'Dune Weaver' WiFi network."
  395. ;;
  396. setup)
  397. check_installed
  398. cd "$INSTALL_DIR"
  399. sudo bash wifi/setup-wifi.sh
  400. ;;
  401. help|*)
  402. echo -e "${GREEN}WiFi Commands${NC}"
  403. echo ""
  404. echo "Usage: dw wifi <command>"
  405. echo ""
  406. echo "Commands:"
  407. echo " status Show current WiFi mode and connection"
  408. echo " scan Scan for available networks"
  409. echo " connect Interactive: scan + select + connect"
  410. echo " hotspot Force hotspot mode"
  411. echo " setup Run initial autohotspot setup"
  412. echo " help Show this help message"
  413. ;;
  414. esac
  415. }
  416. cmd_help() {
  417. echo -e "${GREEN}Dune Weaver CLI${NC}"
  418. echo ""
  419. echo "Usage: dw <command>"
  420. echo ""
  421. echo "Commands:"
  422. echo " install Run initial setup"
  423. echo " start Start Dune Weaver"
  424. echo " stop Stop Dune Weaver"
  425. echo " restart Restart Dune Weaver"
  426. echo " update Pull latest changes and restart"
  427. echo " checkout Switch to a branch and rebuild"
  428. echo " logs [N] View logs (N=number of lines, or follow if omitted)"
  429. echo " status Show service status"
  430. echo " shell Open a shell with the venv activated"
  431. echo " touch Manage touch screen app (run 'dw touch help')"
  432. echo " wifi Manage WiFi and hotspot (run 'dw wifi help')"
  433. echo " help Show this help message"
  434. echo ""
  435. if [[ -n "$INSTALL_DIR" ]]; then
  436. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  437. fi
  438. }
  439. # Main
  440. case "${1:-help}" in
  441. install)
  442. shift
  443. cmd_install "$@"
  444. ;;
  445. start)
  446. cmd_start
  447. ;;
  448. stop)
  449. cmd_stop
  450. ;;
  451. restart)
  452. cmd_restart
  453. ;;
  454. update)
  455. cmd_update "$2"
  456. ;;
  457. checkout)
  458. cmd_checkout "$2"
  459. ;;
  460. logs)
  461. cmd_logs "$2"
  462. ;;
  463. status)
  464. cmd_status
  465. ;;
  466. shell)
  467. cmd_shell
  468. ;;
  469. touch)
  470. shift
  471. cmd_touch "$@"
  472. ;;
  473. wifi)
  474. shift
  475. cmd_wifi "$@"
  476. ;;
  477. help|--help|-h)
  478. cmd_help
  479. ;;
  480. *)
  481. echo -e "${RED}Unknown command: $1${NC}"
  482. echo ""
  483. cmd_help
  484. exit 1
  485. ;;
  486. esac