dw 16 KB

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