dw 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. # help Show this help message
  17. #
  18. set -e
  19. # Colors
  20. RED='\033[0;31m'
  21. GREEN='\033[0;32m'
  22. YELLOW='\033[1;33m'
  23. BLUE='\033[0;34m'
  24. NC='\033[0m'
  25. # Find dune-weaver directory
  26. find_install_dir() {
  27. # Check common locations
  28. if [[ -f "$HOME/dune-weaver/main.py" ]]; then
  29. echo "$HOME/dune-weaver"
  30. elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
  31. echo "/home/pi/dune-weaver"
  32. elif [[ -f "./main.py" ]]; then
  33. pwd
  34. else
  35. echo ""
  36. fi
  37. }
  38. # Check if using Docker or systemd
  39. is_docker_mode() {
  40. # Docker mode if docker-compose.yml exists and docker is available
  41. [[ -f "$INSTALL_DIR/docker-compose.yml" ]] && command -v docker &> /dev/null
  42. }
  43. INSTALL_DIR=$(find_install_dir)
  44. # Check if installed
  45. check_installed() {
  46. if [[ -z "$INSTALL_DIR" ]]; then
  47. echo -e "${RED}Error: Dune Weaver not found${NC}"
  48. echo ""
  49. echo "Please install first:"
  50. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  51. echo " cd dune-weaver"
  52. echo " dw install"
  53. exit 1
  54. fi
  55. }
  56. # Commands
  57. cmd_install() {
  58. if [[ -z "$INSTALL_DIR" ]]; then
  59. # Not installed, check if we're in the right directory
  60. if [[ -f "./docker-compose.yml" ]] && [[ -f "./main.py" ]]; then
  61. INSTALL_DIR=$(pwd)
  62. else
  63. echo -e "${RED}Error: Run this from the dune-weaver directory${NC}"
  64. echo ""
  65. echo " git clone https://github.com/tuanchris/dune-weaver --single-branch"
  66. echo " cd dune-weaver"
  67. echo " dw install"
  68. exit 1
  69. fi
  70. fi
  71. cd "$INSTALL_DIR"
  72. bash setup-pi.sh "$@"
  73. }
  74. cmd_start() {
  75. check_installed
  76. echo -e "${BLUE}Starting Dune Weaver...${NC}"
  77. cd "$INSTALL_DIR"
  78. if is_docker_mode; then
  79. sudo docker compose up -d
  80. else
  81. sudo systemctl start dune-weaver
  82. fi
  83. echo -e "${GREEN}Started!${NC} Access at http://$(hostname -I | awk '{print $1}'):8080"
  84. }
  85. cmd_stop() {
  86. check_installed
  87. echo -e "${BLUE}Stopping Dune Weaver...${NC}"
  88. cd "$INSTALL_DIR"
  89. if is_docker_mode; then
  90. sudo docker compose down
  91. else
  92. sudo systemctl stop dune-weaver
  93. fi
  94. echo -e "${GREEN}Stopped${NC}"
  95. }
  96. cmd_restart() {
  97. check_installed
  98. echo -e "${BLUE}Restarting Dune Weaver...${NC}"
  99. cd "$INSTALL_DIR"
  100. if is_docker_mode; then
  101. sudo docker compose restart
  102. else
  103. sudo systemctl restart dune-weaver
  104. fi
  105. echo -e "${GREEN}Restarted${NC}"
  106. }
  107. cmd_update() {
  108. check_installed
  109. echo -e "${BLUE}Updating Dune Weaver...${NC}"
  110. cd "$INSTALL_DIR"
  111. echo "Pulling latest code..."
  112. git pull
  113. if is_docker_mode; then
  114. echo "Pulling latest Docker image..."
  115. sudo docker compose pull
  116. echo "Restarting with new version..."
  117. sudo docker compose up -d
  118. else
  119. echo "Updating Python dependencies..."
  120. source .venv/bin/activate
  121. pip install -r requirements.txt
  122. echo "Restarting service..."
  123. sudo systemctl restart dune-weaver
  124. fi
  125. echo -e "${GREEN}Updated!${NC}"
  126. }
  127. cmd_logs() {
  128. check_installed
  129. cd "$INSTALL_DIR"
  130. if is_docker_mode; then
  131. sudo docker compose logs -f
  132. else
  133. sudo journalctl -u dune-weaver -f
  134. fi
  135. }
  136. cmd_status() {
  137. check_installed
  138. cd "$INSTALL_DIR"
  139. if is_docker_mode; then
  140. sudo docker compose ps
  141. else
  142. sudo systemctl status dune-weaver
  143. fi
  144. }
  145. cmd_shell() {
  146. check_installed
  147. cd "$INSTALL_DIR"
  148. if is_docker_mode; then
  149. sudo docker compose exec dune-weaver /bin/bash
  150. else
  151. echo -e "${YELLOW}Shell not available in systemd mode${NC}"
  152. echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
  153. fi
  154. }
  155. cmd_help() {
  156. echo -e "${GREEN}Dune Weaver CLI${NC}"
  157. echo ""
  158. echo "Usage: dw <command>"
  159. echo ""
  160. echo "Commands:"
  161. echo " install Run initial setup (Docker + WiFi fix)"
  162. echo " start Start Dune Weaver"
  163. echo " stop Stop Dune Weaver"
  164. echo " restart Restart Dune Weaver"
  165. echo " update Pull latest changes and restart"
  166. echo " logs View live logs (Ctrl+C to exit)"
  167. echo " status Show container status"
  168. echo " shell Open a shell in the container"
  169. echo " help Show this help message"
  170. echo ""
  171. if [[ -n "$INSTALL_DIR" ]]; then
  172. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  173. fi
  174. }
  175. # Main
  176. case "${1:-help}" in
  177. install)
  178. shift
  179. cmd_install "$@"
  180. ;;
  181. start)
  182. cmd_start
  183. ;;
  184. stop)
  185. cmd_stop
  186. ;;
  187. restart)
  188. cmd_restart
  189. ;;
  190. update)
  191. cmd_update
  192. ;;
  193. logs)
  194. cmd_logs
  195. ;;
  196. status)
  197. cmd_status
  198. ;;
  199. shell)
  200. cmd_shell
  201. ;;
  202. help|--help|-h)
  203. cmd_help
  204. ;;
  205. *)
  206. echo -e "${RED}Unknown command: $1${NC}"
  207. echo ""
  208. cmd_help
  209. exit 1
  210. ;;
  211. esac