1
0

dw 17 KB

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