dw 15 KB

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