dw 9.4 KB

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