1
0

dw 17 KB

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