1
0

dw 12 KB

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