dw 16 KB

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