dw 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. # touch Manage touch screen app
  17. # help Show this help message
  18. #
  19. set -e
  20. # Colors
  21. RED='\033[0;31m'
  22. GREEN='\033[0;32m'
  23. YELLOW='\033[1;33m'
  24. BLUE='\033[0;34m'
  25. NC='\033[0m'
  26. # Find dune-weaver directory
  27. find_install_dir() {
  28. # Check common locations
  29. if [[ -f "$HOME/dune-weaver/main.py" ]]; then
  30. echo "$HOME/dune-weaver"
  31. elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
  32. echo "/home/pi/dune-weaver"
  33. elif [[ -f "./main.py" ]]; then
  34. pwd
  35. else
  36. echo ""
  37. fi
  38. }
  39. # Check if using Docker or systemd
  40. is_docker_mode() {
  41. # Docker mode if docker-compose.yml exists and docker is available
  42. [[ -f "$INSTALL_DIR/docker-compose.yml" ]] && command -v docker &> /dev/null
  43. }
  44. INSTALL_DIR=$(find_install_dir)
  45. # Check if installed
  46. check_installed() {
  47. if [[ -z "$INSTALL_DIR" ]]; then
  48. echo -e "${RED}Error: Dune Weaver not found${NC}"
  49. echo ""
  50. echo "Please install first:"
  51. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  52. echo " cd dune-weaver"
  53. echo " dw install"
  54. exit 1
  55. fi
  56. }
  57. # Commands
  58. cmd_install() {
  59. if [[ -z "$INSTALL_DIR" ]]; then
  60. # Not installed, check if we're in the right directory
  61. if [[ -f "./docker-compose.yml" ]] && [[ -f "./main.py" ]]; then
  62. INSTALL_DIR=$(pwd)
  63. else
  64. echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
  65. echo ""
  66. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  67. echo " cd dune-weaver"
  68. echo " dw install"
  69. exit 1
  70. fi
  71. fi
  72. cd "$INSTALL_DIR"
  73. bash setup-pi.sh "$@"
  74. }
  75. cmd_start() {
  76. check_installed
  77. echo -e "${BLUE}Starting Dune Weaver...${NC}"
  78. cd "$INSTALL_DIR"
  79. if is_docker_mode; then
  80. sudo docker compose up -d
  81. else
  82. sudo systemctl start dune-weaver
  83. fi
  84. echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}'):8080"
  85. }
  86. cmd_stop() {
  87. check_installed
  88. echo -e "${BLUE}Stopping Dune Weaver...${NC}"
  89. cd "$INSTALL_DIR"
  90. if is_docker_mode; then
  91. sudo docker compose down
  92. else
  93. sudo systemctl stop dune-weaver
  94. fi
  95. echo -e "${GREEN}Stopped${NC}"
  96. }
  97. cmd_restart() {
  98. check_installed
  99. echo -e "${BLUE}Restarting Dune Weaver...${NC}"
  100. cd "$INSTALL_DIR"
  101. if is_docker_mode; then
  102. sudo docker compose restart
  103. else
  104. sudo systemctl restart dune-weaver
  105. fi
  106. echo -e "${GREEN}Restarted${NC}"
  107. }
  108. cmd_update() {
  109. check_installed
  110. echo -e "${BLUE}Updating Dune Weaver...${NC}"
  111. cd "$INSTALL_DIR"
  112. # Check if we should skip the pull phase (called after re-exec)
  113. if [[ "$1" != "--continue" ]]; then
  114. echo "Pulling latest code..."
  115. git pull
  116. # Update dw CLI
  117. echo "Updating dw command..."
  118. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  119. sudo chmod +x /usr/local/bin/dw
  120. # Re-exec with the new script to ensure new code runs
  121. echo "Restarting with updated CLI..."
  122. exec /usr/local/bin/dw update --continue
  123. fi
  124. if is_docker_mode; then
  125. echo "Stopping current container..."
  126. sudo docker compose down
  127. echo "Pulling latest Docker image..."
  128. sudo docker compose pull
  129. echo "Starting with new version..."
  130. sudo docker compose up -d --remove-orphans
  131. echo "Cleaning up unused Docker resources..."
  132. sudo docker image prune -f
  133. sudo docker container prune -f
  134. else
  135. echo "Updating Python dependencies..."
  136. source .venv/bin/activate
  137. pip install -r requirements.txt
  138. echo "Restarting service..."
  139. sudo systemctl restart dune-weaver
  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. git pull
  148. sudo systemctl restart dune-weaver-touch
  149. echo -e "${GREEN}Touch app updated!${NC}"
  150. fi
  151. echo -e "${GREEN}Update complete!${NC}"
  152. }
  153. cmd_logs() {
  154. check_installed
  155. cd "$INSTALL_DIR"
  156. # Parse optional line count (e.g., dw logs 100)
  157. local lines="${1:-}"
  158. local follow="-f"
  159. if [[ -n "$lines" ]]; then
  160. follow="--tail $lines"
  161. fi
  162. if is_docker_mode; then
  163. sudo docker compose logs $follow
  164. else
  165. if [[ -n "$lines" ]]; then
  166. sudo journalctl -u dune-weaver -n "$lines"
  167. else
  168. sudo journalctl -u dune-weaver -f
  169. fi
  170. fi
  171. }
  172. cmd_status() {
  173. check_installed
  174. cd "$INSTALL_DIR"
  175. if is_docker_mode; then
  176. sudo docker compose ps
  177. else
  178. sudo systemctl status dune-weaver
  179. fi
  180. }
  181. cmd_shell() {
  182. check_installed
  183. cd "$INSTALL_DIR"
  184. if is_docker_mode; then
  185. sudo docker compose exec dune-weaver /bin/bash
  186. else
  187. echo -e "${YELLOW}Shell not available in systemd mode${NC}"
  188. echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
  189. fi
  190. }
  191. # Touch app commands (systemd service)
  192. cmd_touch() {
  193. local subcmd="${1:-help}"
  194. case "$subcmd" in
  195. logs)
  196. local lines="${2:-}"
  197. if [[ -n "$lines" ]]; then
  198. sudo journalctl -u dune-weaver-touch -n "$lines"
  199. else
  200. sudo journalctl -u dune-weaver-touch -f
  201. fi
  202. ;;
  203. restart)
  204. echo -e "${BLUE}Restarting touch app...${NC}"
  205. sudo systemctl restart dune-weaver-touch
  206. echo -e "${GREEN}Touch app restarted${NC}"
  207. ;;
  208. stop)
  209. echo -e "${BLUE}Stopping touch app...${NC}"
  210. sudo systemctl stop dune-weaver-touch
  211. echo -e "${GREEN}Touch app stopped${NC}"
  212. ;;
  213. start)
  214. echo -e "${BLUE}Starting touch app...${NC}"
  215. sudo systemctl start dune-weaver-touch
  216. echo -e "${GREEN}Touch app started${NC}"
  217. ;;
  218. status)
  219. sudo systemctl status dune-weaver-touch
  220. ;;
  221. install)
  222. check_installed
  223. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  224. if [[ ! -f "$touch_dir/install.sh" ]]; then
  225. echo -e "${RED}Touch app installer not found at $touch_dir/install.sh${NC}"
  226. echo "Make sure you have the dune-weaver-touch directory in your dune-weaver folder."
  227. exit 1
  228. fi
  229. echo -e "${BLUE}Running touch app installer...${NC}"
  230. cd "$touch_dir"
  231. sudo bash install.sh
  232. ;;
  233. help|*)
  234. echo -e "${GREEN}Touch App Commands${NC}"
  235. echo ""
  236. echo "Usage: dw touch <command>"
  237. echo ""
  238. echo "Commands:"
  239. echo " install Install touch app (clone, setup, enable service)"
  240. echo " logs [N] View logs (N lines or follow if omitted)"
  241. echo " restart Restart touch app"
  242. echo " stop Stop touch app"
  243. echo " start Start touch app"
  244. echo " status Show touch app status"
  245. echo " help Show this help message"
  246. ;;
  247. esac
  248. }
  249. cmd_help() {
  250. echo -e "${GREEN}Dune Weaver CLI${NC}"
  251. echo ""
  252. echo "Usage: dw <command>"
  253. echo ""
  254. echo "Commands:"
  255. echo " install Run initial setup (Docker + WiFi fix)"
  256. echo " start Start Dune Weaver"
  257. echo " stop Stop Dune Weaver"
  258. echo " restart Restart Dune Weaver"
  259. echo " update Pull latest changes and restart"
  260. echo " logs [N] View logs (N=number of lines, or follow if omitted)"
  261. echo " status Show container status"
  262. echo " shell Open a shell in the container"
  263. echo " touch Manage touch screen app (run 'dw touch help')"
  264. echo " help Show this help message"
  265. echo ""
  266. if [[ -n "$INSTALL_DIR" ]]; then
  267. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  268. fi
  269. }
  270. # Main
  271. case "${1:-help}" in
  272. install)
  273. shift
  274. cmd_install "$@"
  275. ;;
  276. start)
  277. cmd_start
  278. ;;
  279. stop)
  280. cmd_stop
  281. ;;
  282. restart)
  283. cmd_restart
  284. ;;
  285. update)
  286. cmd_update "$2"
  287. ;;
  288. logs)
  289. cmd_logs "$2"
  290. ;;
  291. status)
  292. cmd_status
  293. ;;
  294. shell)
  295. cmd_shell
  296. ;;
  297. touch)
  298. shift
  299. cmd_touch "$@"
  300. ;;
  301. help|--help|-h)
  302. cmd_help
  303. ;;
  304. *)
  305. echo -e "${RED}Unknown command: $1${NC}"
  306. echo ""
  307. cmd_help
  308. exit 1
  309. ;;
  310. esac