dw 8.8 KB

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