dw 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. echo "Pulling latest code..."
  113. git pull
  114. # Update dw CLI
  115. echo "Updating dw command..."
  116. sudo cp "$INSTALL_DIR/dw" /usr/local/bin/dw
  117. sudo chmod +x /usr/local/bin/dw
  118. if is_docker_mode; then
  119. echo "Pulling latest Docker image..."
  120. sudo docker compose pull
  121. echo "Restarting with new version..."
  122. sudo docker compose up -d --quiet-pull
  123. sudo docker compose restart
  124. else
  125. echo "Updating Python dependencies..."
  126. source .venv/bin/activate
  127. pip install -r requirements.txt
  128. echo "Restarting service..."
  129. sudo systemctl restart dune-weaver
  130. fi
  131. # Update touch app if installed (check both directory and service exist)
  132. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  133. if [[ -d "$touch_dir" ]] && systemctl list-unit-files dune-weaver-touch.service 2>/dev/null | grep -q dune-weaver-touch; then
  134. echo ""
  135. echo -e "${BLUE}Updating touch app...${NC}"
  136. cd "$touch_dir"
  137. git pull
  138. sudo systemctl restart dune-weaver-touch
  139. echo -e "${GREEN}Touch app updated!${NC}"
  140. fi
  141. echo -e "${GREEN}Update complete!${NC}"
  142. }
  143. cmd_logs() {
  144. check_installed
  145. cd "$INSTALL_DIR"
  146. # Parse optional line count (e.g., dw logs 100)
  147. local lines="${1:-}"
  148. local follow="-f"
  149. if [[ -n "$lines" ]]; then
  150. follow="--tail $lines"
  151. fi
  152. if is_docker_mode; then
  153. sudo docker compose logs $follow
  154. else
  155. if [[ -n "$lines" ]]; then
  156. sudo journalctl -u dune-weaver -n "$lines"
  157. else
  158. sudo journalctl -u dune-weaver -f
  159. fi
  160. fi
  161. }
  162. cmd_status() {
  163. check_installed
  164. cd "$INSTALL_DIR"
  165. if is_docker_mode; then
  166. sudo docker compose ps
  167. else
  168. sudo systemctl status dune-weaver
  169. fi
  170. }
  171. cmd_shell() {
  172. check_installed
  173. cd "$INSTALL_DIR"
  174. if is_docker_mode; then
  175. sudo docker compose exec dune-weaver /bin/bash
  176. else
  177. echo -e "${YELLOW}Shell not available in systemd mode${NC}"
  178. echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
  179. fi
  180. }
  181. # Touch app commands (systemd service)
  182. cmd_touch() {
  183. local subcmd="${1:-help}"
  184. case "$subcmd" in
  185. logs)
  186. local lines="${2:-}"
  187. if [[ -n "$lines" ]]; then
  188. sudo journalctl -u dune-weaver-touch -n "$lines"
  189. else
  190. sudo journalctl -u dune-weaver-touch -f
  191. fi
  192. ;;
  193. restart)
  194. echo -e "${BLUE}Restarting touch app...${NC}"
  195. sudo systemctl restart dune-weaver-touch
  196. echo -e "${GREEN}Touch app restarted${NC}"
  197. ;;
  198. stop)
  199. echo -e "${BLUE}Stopping touch app...${NC}"
  200. sudo systemctl stop dune-weaver-touch
  201. echo -e "${GREEN}Touch app stopped${NC}"
  202. ;;
  203. start)
  204. echo -e "${BLUE}Starting touch app...${NC}"
  205. sudo systemctl start dune-weaver-touch
  206. echo -e "${GREEN}Touch app started${NC}"
  207. ;;
  208. status)
  209. sudo systemctl status dune-weaver-touch
  210. ;;
  211. install)
  212. check_installed
  213. local touch_dir="$INSTALL_DIR/dune-weaver-touch"
  214. if [[ ! -f "$touch_dir/install.sh" ]]; then
  215. echo -e "${RED}Touch app installer not found at $touch_dir/install.sh${NC}"
  216. echo "Make sure you have the dune-weaver-touch directory in your dune-weaver folder."
  217. exit 1
  218. fi
  219. echo -e "${BLUE}Running touch app installer...${NC}"
  220. cd "$touch_dir"
  221. sudo bash install.sh
  222. ;;
  223. help|*)
  224. echo -e "${GREEN}Touch App Commands${NC}"
  225. echo ""
  226. echo "Usage: dw touch <command>"
  227. echo ""
  228. echo "Commands:"
  229. echo " install Install touch app (clone, setup, enable service)"
  230. echo " logs [N] View logs (N lines or follow if omitted)"
  231. echo " restart Restart touch app"
  232. echo " stop Stop touch app"
  233. echo " start Start touch app"
  234. echo " status Show touch app status"
  235. echo " help Show this help message"
  236. ;;
  237. esac
  238. }
  239. cmd_help() {
  240. echo -e "${GREEN}Dune Weaver CLI${NC}"
  241. echo ""
  242. echo "Usage: dw <command>"
  243. echo ""
  244. echo "Commands:"
  245. echo " install Run initial setup (Docker + WiFi fix)"
  246. echo " start Start Dune Weaver"
  247. echo " stop Stop Dune Weaver"
  248. echo " restart Restart Dune Weaver"
  249. echo " update Pull latest changes and restart"
  250. echo " logs [N] View logs (N=number of lines, or follow if omitted)"
  251. echo " status Show container status"
  252. echo " shell Open a shell in the container"
  253. echo " touch Manage touch screen app (run 'dw touch help')"
  254. echo " help Show this help message"
  255. echo ""
  256. if [[ -n "$INSTALL_DIR" ]]; then
  257. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  258. fi
  259. }
  260. # Main
  261. case "${1:-help}" in
  262. install)
  263. shift
  264. cmd_install "$@"
  265. ;;
  266. start)
  267. cmd_start
  268. ;;
  269. stop)
  270. cmd_stop
  271. ;;
  272. restart)
  273. cmd_restart
  274. ;;
  275. update)
  276. cmd_update
  277. ;;
  278. logs)
  279. cmd_logs "$2"
  280. ;;
  281. status)
  282. cmd_status
  283. ;;
  284. shell)
  285. cmd_shell
  286. ;;
  287. touch)
  288. shift
  289. cmd_touch "$@"
  290. ;;
  291. help|--help|-h)
  292. cmd_help
  293. ;;
  294. *)
  295. echo -e "${RED}Unknown command: $1${NC}"
  296. echo ""
  297. cmd_help
  298. exit 1
  299. ;;
  300. esac