dw 9.7 KB

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