dw 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #!/bin/bash
  2. #
  3. # Dune Weaver CLI
  4. #
  5. # Usage: dw <command>
  6. #
  7. # Commands:
  8. # install Run initial setup (Docker + WiFi fix)
  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 container status
  15. # shell Open a shell in the container
  16. # checkout Switch to a branch and pull its Docker images
  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. # Check if using Docker or systemd
  42. is_docker_mode() {
  43. # Docker mode if docker-compose.yml exists and docker is available
  44. [[ -f "$INSTALL_DIR/docker-compose.yml" ]] && command -v docker &> /dev/null
  45. }
  46. # Set IMAGE_TAG from current git branch (e.g. feature/foo -> feature-foo)
  47. set_image_tag() {
  48. local branch
  49. branch=$(git -C "$INSTALL_DIR" rev-parse --abbrev-ref HEAD)
  50. export IMAGE_TAG="${branch//\//-}"
  51. }
  52. INSTALL_DIR=$(find_install_dir)
  53. # Check if installed
  54. check_installed() {
  55. if [[ -z "$INSTALL_DIR" ]]; then
  56. echo -e "${RED}Error: Dune Weaver not found${NC}"
  57. echo ""
  58. echo "Please install first:"
  59. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  60. echo " cd dune-weaver"
  61. echo " dw install"
  62. exit 1
  63. fi
  64. }
  65. # Commands
  66. cmd_install() {
  67. if [[ -z "$INSTALL_DIR" ]]; then
  68. # Not installed, check if we're in the right directory
  69. if [[ -f "./docker-compose.yml" ]] && [[ -f "./main.py" ]]; then
  70. INSTALL_DIR=$(pwd)
  71. else
  72. echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
  73. echo ""
  74. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  75. echo " cd dune-weaver"
  76. echo " dw install"
  77. exit 1
  78. fi
  79. fi
  80. cd "$INSTALL_DIR"
  81. bash setup-pi.sh "$@"
  82. }
  83. cmd_start() {
  84. check_installed
  85. echo -e "${BLUE}Starting Dune Weaver...${NC}"
  86. cd "$INSTALL_DIR"
  87. if is_docker_mode; then
  88. set_image_tag
  89. sudo -E docker compose up -d
  90. else
  91. sudo systemctl start dune-weaver
  92. fi
  93. local fe_port="${FRONTEND_PORT:-80}"
  94. local port_suffix=""
  95. [[ "$fe_port" != "80" ]] && port_suffix=":$fe_port"
  96. echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}')${port_suffix}"
  97. }
  98. cmd_stop() {
  99. check_installed
  100. echo -e "${BLUE}Stopping Dune Weaver...${NC}"
  101. cd "$INSTALL_DIR"
  102. if is_docker_mode; then
  103. sudo docker compose down
  104. else
  105. sudo systemctl stop dune-weaver
  106. fi
  107. echo -e "${GREEN}Stopped${NC}"
  108. }
  109. cmd_restart() {
  110. check_installed
  111. echo -e "${BLUE}Restarting Dune Weaver...${NC}"
  112. cd "$INSTALL_DIR"
  113. if is_docker_mode; then
  114. set_image_tag
  115. sudo -E docker compose restart
  116. else
  117. sudo systemctl restart dune-weaver
  118. fi
  119. echo -e "${GREEN}Restarted${NC}"
  120. }
  121. cmd_update() {
  122. check_installed
  123. echo -e "${BLUE}Updating Dune Weaver...${NC}"
  124. cd "$INSTALL_DIR"
  125. # Check if we should skip the pull phase (called after re-exec)
  126. if [[ "$1" != "--continue" ]]; then
  127. echo "Pulling latest code..."
  128. git pull
  129. # Update dw CLI
  130. echo "Updating dw command..."
  131. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  132. sudo chmod +x /usr/local/bin/dw
  133. # Re-exec with the new script to ensure new code runs
  134. echo "Restarting with updated CLI..."
  135. exec /usr/local/bin/dw update --continue
  136. fi
  137. if is_docker_mode; then
  138. set_image_tag
  139. echo -e "Image tag: ${BLUE}${IMAGE_TAG}${NC}"
  140. echo "Pulling latest Docker image..."
  141. sudo -E docker compose pull
  142. echo "Stopping current container..."
  143. sudo docker compose down
  144. echo "Starting with new version..."
  145. sudo -E docker compose up -d --remove-orphans
  146. echo "Cleaning up unused Docker resources..."
  147. sudo docker image prune -f
  148. sudo docker container prune -f
  149. else
  150. echo "Updating Python dependencies..."
  151. source .venv/bin/activate
  152. pip install -r requirements.txt
  153. echo "Restarting service..."
  154. sudo systemctl restart dune-weaver
  155. fi
  156. # Update touch app if installed (check both directory and service exist)
  157. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  158. if [[ -d "$touch_dir" ]] && systemctl list-unit-files dune-weaver-touch.service 2>/dev/null | grep -q dune-weaver-touch; then
  159. echo ""
  160. echo -e "${BLUE}Updating touch app...${NC}"
  161. cd "$touch_dir"
  162. # Update Python dependencies (code already pulled with main repo)
  163. if [[ -f "requirements.txt" ]] && [[ -d ".venv" ]]; then
  164. echo "Updating touch app dependencies..."
  165. source .venv/bin/activate
  166. pip install -q -r requirements.txt
  167. deactivate
  168. fi
  169. # Restart to apply changes
  170. sudo systemctl restart dune-weaver-touch
  171. echo -e "${GREEN}Touch app updated!${NC}"
  172. fi
  173. echo -e "${GREEN}Update complete!${NC}"
  174. }
  175. cmd_logs() {
  176. check_installed
  177. cd "$INSTALL_DIR"
  178. # Parse optional line count (e.g., dw logs 100)
  179. local lines="${1:-}"
  180. local follow="-f"
  181. if [[ -n "$lines" ]]; then
  182. follow="--tail $lines"
  183. fi
  184. if is_docker_mode; then
  185. sudo docker compose logs $follow
  186. else
  187. if [[ -n "$lines" ]]; then
  188. sudo journalctl -u dune-weaver -n "$lines"
  189. else
  190. sudo journalctl -u dune-weaver -f
  191. fi
  192. fi
  193. }
  194. cmd_status() {
  195. check_installed
  196. cd "$INSTALL_DIR"
  197. if is_docker_mode; then
  198. sudo docker compose ps
  199. else
  200. sudo systemctl status dune-weaver
  201. fi
  202. }
  203. cmd_shell() {
  204. check_installed
  205. cd "$INSTALL_DIR"
  206. if is_docker_mode; then
  207. sudo docker compose exec dune-weaver /bin/bash
  208. else
  209. echo -e "${YELLOW}Shell not available in systemd mode${NC}"
  210. echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
  211. fi
  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 pull
  295. else
  296. git checkout -b "$branch" "origin/$branch"
  297. fi
  298. echo -e "Switched to branch: ${GREEN}${branch}${NC}"
  299. # Pull and restart Docker images for the new branch
  300. if is_docker_mode; then
  301. set_image_tag
  302. echo -e "Image tag: ${BLUE}${IMAGE_TAG}${NC}"
  303. echo "Pulling Docker images..."
  304. sudo -E docker compose pull
  305. echo "Restarting with new images..."
  306. sudo docker compose down
  307. sudo -E docker compose up -d --remove-orphans
  308. echo "Cleaning up old images..."
  309. sudo docker image prune -f
  310. fi
  311. # Update dw CLI from the new branch
  312. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  313. sudo chmod +x /usr/local/bin/dw
  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. if [[ -n "$password" ]]; then
  363. sudo nmcli dev wifi connect "$ssid" password "$password" ifname wlan0
  364. else
  365. sudo nmcli dev wifi connect "$ssid" ifname wlan0
  366. fi
  367. ;;
  368. hotspot)
  369. echo -e "${BLUE}Switching to hotspot mode...${NC}"
  370. sudo nmcli dev disconnect wlan0 2>/dev/null || true
  371. sudo nmcli con up DuneWeaver-Hotspot 2>/dev/null
  372. echo "hotspot" | sudo tee /tmp/dw-wifi-mode > /dev/null
  373. echo -e "${GREEN}Hotspot active!${NC} Connect to the 'Dune Weaver' WiFi network."
  374. ;;
  375. setup)
  376. check_installed
  377. cd "$INSTALL_DIR"
  378. sudo bash wifi/setup-wifi.sh
  379. ;;
  380. help|*)
  381. echo -e "${GREEN}WiFi Commands${NC}"
  382. echo ""
  383. echo "Usage: dw wifi <command>"
  384. echo ""
  385. echo "Commands:"
  386. echo " status Show current WiFi mode and connection"
  387. echo " scan Scan for available networks"
  388. echo " connect Interactive: scan + select + connect"
  389. echo " hotspot Force hotspot mode"
  390. echo " setup Run initial autohotspot setup"
  391. echo " help Show this help message"
  392. ;;
  393. esac
  394. }
  395. cmd_help() {
  396. echo -e "${GREEN}Dune Weaver CLI${NC}"
  397. echo ""
  398. echo "Usage: dw <command>"
  399. echo ""
  400. echo "Commands:"
  401. echo " install Run initial setup (Docker + WiFi fix)"
  402. echo " start Start Dune Weaver"
  403. echo " stop Stop Dune Weaver"
  404. echo " restart Restart Dune Weaver"
  405. echo " update Pull latest changes and restart"
  406. echo " checkout Switch to a branch and pull its Docker images"
  407. echo " logs [N] View logs (N=number of lines, or follow if omitted)"
  408. echo " status Show container status"
  409. echo " shell Open a shell in the container"
  410. echo " touch Manage touch screen app (run 'dw touch help')"
  411. echo " wifi Manage WiFi and hotspot (run 'dw wifi help')"
  412. echo " help Show this help message"
  413. echo ""
  414. if [[ -n "$INSTALL_DIR" ]]; then
  415. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  416. fi
  417. }
  418. # Main
  419. case "${1:-help}" in
  420. install)
  421. shift
  422. cmd_install "$@"
  423. ;;
  424. start)
  425. cmd_start
  426. ;;
  427. stop)
  428. cmd_stop
  429. ;;
  430. restart)
  431. cmd_restart
  432. ;;
  433. update)
  434. cmd_update "$2"
  435. ;;
  436. checkout)
  437. cmd_checkout "$2"
  438. ;;
  439. logs)
  440. cmd_logs "$2"
  441. ;;
  442. status)
  443. cmd_status
  444. ;;
  445. shell)
  446. cmd_shell
  447. ;;
  448. touch)
  449. shift
  450. cmd_touch "$@"
  451. ;;
  452. wifi)
  453. shift
  454. cmd_wifi "$@"
  455. ;;
  456. help|--help|-h)
  457. cmd_help
  458. ;;
  459. *)
  460. echo -e "${RED}Unknown command: $1${NC}"
  461. echo ""
  462. cmd_help
  463. exit 1
  464. ;;
  465. esac