dw 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "Pulling latest Docker image..."
  120. sudo docker compose pull
  121. echo "Restarting with new version..."
  122. sudo docker compose up -d --quiet-pull
  123. else
  124. echo "Updating Python dependencies..."
  125. source .venv/bin/activate
  126. pip install -r requirements.txt
  127. echo "Restarting service..."
  128. sudo systemctl restart dune-weaver
  129. fi
  130. # Update touch app if installed
  131. local touch_dir="/home/pi/dune-weaver-touch"
  132. if [[ -d "$touch_dir" ]]; then
  133. echo ""
  134. echo -e "${BLUE}Updating touch app...${NC}"
  135. cd "$touch_dir"
  136. git pull
  137. sudo systemctl restart dune-weaver-touch
  138. echo -e "${GREEN}Touch app updated!${NC}"
  139. fi
  140. echo -e "${GREEN}Update complete!${NC}"
  141. }
  142. cmd_logs() {
  143. check_installed
  144. cd "$INSTALL_DIR"
  145. # Parse optional line count (e.g., dw logs 100)
  146. local lines="${1:-}"
  147. local follow="-f"
  148. if [[ -n "$lines" ]]; then
  149. follow="--tail $lines"
  150. fi
  151. if is_docker_mode; then
  152. sudo docker compose logs $follow
  153. else
  154. if [[ -n "$lines" ]]; then
  155. sudo journalctl -u dune-weaver -n "$lines"
  156. else
  157. sudo journalctl -u dune-weaver -f
  158. fi
  159. fi
  160. }
  161. cmd_status() {
  162. check_installed
  163. cd "$INSTALL_DIR"
  164. if is_docker_mode; then
  165. sudo docker compose ps
  166. else
  167. sudo systemctl status dune-weaver
  168. fi
  169. }
  170. cmd_shell() {
  171. check_installed
  172. cd "$INSTALL_DIR"
  173. if is_docker_mode; then
  174. sudo docker compose exec dune-weaver /bin/bash
  175. else
  176. echo -e "${YELLOW}Shell not available in systemd mode${NC}"
  177. echo "Use: cd $INSTALL_DIR && source .venv/bin/activate"
  178. fi
  179. }
  180. # Touch app commands (systemd service)
  181. cmd_touch() {
  182. local subcmd="${1:-help}"
  183. case "$subcmd" in
  184. logs)
  185. local lines="${2:-}"
  186. if [[ -n "$lines" ]]; then
  187. sudo journalctl -u dune-weaver-touch -n "$lines"
  188. else
  189. sudo journalctl -u dune-weaver-touch -f
  190. fi
  191. ;;
  192. restart)
  193. echo -e "${BLUE}Restarting touch app...${NC}"
  194. sudo systemctl restart dune-weaver-touch
  195. echo -e "${GREEN}Touch app restarted${NC}"
  196. ;;
  197. stop)
  198. echo -e "${BLUE}Stopping touch app...${NC}"
  199. sudo systemctl stop dune-weaver-touch
  200. echo -e "${GREEN}Touch app stopped${NC}"
  201. ;;
  202. start)
  203. echo -e "${BLUE}Starting touch app...${NC}"
  204. sudo systemctl start dune-weaver-touch
  205. echo -e "${GREEN}Touch app started${NC}"
  206. ;;
  207. status)
  208. sudo systemctl status dune-weaver-touch
  209. ;;
  210. install)
  211. local touch_dir="/home/pi/dune-weaver-touch"
  212. local touch_repo="https://github.com/tuanchris/dune-weaver-touch"
  213. echo -e "${BLUE}Installing touch app...${NC}"
  214. # Install system dependencies for Qt/PySide6
  215. echo "Installing system dependencies..."
  216. sudo apt update
  217. sudo apt install -y python3-venv python3-pip \
  218. libxcb-cursor0 libxkbcommon0 libxcb-xinerama0 \
  219. libxcb-icccm4 libxcb-image0 libxcb-keysyms1 \
  220. libxcb-randr0 libxcb-render-util0 libxcb-shape0
  221. # Clone or update repo
  222. if [[ -d "$touch_dir" ]]; then
  223. echo "Updating existing installation..."
  224. cd "$touch_dir"
  225. git pull
  226. else
  227. echo "Cloning touch app repository..."
  228. git clone "$touch_repo" "$touch_dir"
  229. cd "$touch_dir"
  230. fi
  231. # Create venv and install dependencies
  232. echo "Setting up Python environment..."
  233. python3 -m venv venv
  234. source venv/bin/activate
  235. pip install --upgrade pip
  236. pip install -r requirements.txt
  237. deactivate
  238. # Create systemd service
  239. echo "Creating systemd service..."
  240. sudo tee /etc/systemd/system/dune-weaver-touch.service > /dev/null << EOF
  241. [Unit]
  242. Description=Dune Weaver Touch App
  243. After=network.target
  244. [Service]
  245. Type=simple
  246. User=pi
  247. WorkingDirectory=$touch_dir
  248. Environment=QT_QPA_PLATFORM=eglfs
  249. Environment=QT_QPA_EGLFS_INTEGRATION=eglfs_kms
  250. Environment=QT_QPA_EGLFS_KMS_CONFIG=$touch_dir/eglfs_config.json
  251. Environment=QML2_IMPORT_PATH=$touch_dir/venv/lib/python3.11/site-packages/PySide6/qml
  252. ExecStart=$touch_dir/venv/bin/python $touch_dir/main.py
  253. Restart=always
  254. RestartSec=5
  255. [Install]
  256. WantedBy=multi-user.target
  257. EOF
  258. # Enable and start service
  259. sudo systemctl daemon-reload
  260. sudo systemctl enable dune-weaver-touch
  261. sudo systemctl start dune-weaver-touch
  262. echo -e "${GREEN}Touch app installed!${NC}"
  263. echo "View logs with: dw touch logs"
  264. ;;
  265. help|*)
  266. echo -e "${GREEN}Touch App Commands${NC}"
  267. echo ""
  268. echo "Usage: dw touch <command>"
  269. echo ""
  270. echo "Commands:"
  271. echo " install Install touch app (clone, setup, enable service)"
  272. echo " logs [N] View logs (N lines or follow if omitted)"
  273. echo " restart Restart touch app"
  274. echo " stop Stop touch app"
  275. echo " start Start touch app"
  276. echo " status Show touch app status"
  277. echo " help Show this help message"
  278. ;;
  279. esac
  280. }
  281. cmd_help() {
  282. echo -e "${GREEN}Dune Weaver CLI${NC}"
  283. echo ""
  284. echo "Usage: dw <command>"
  285. echo ""
  286. echo "Commands:"
  287. echo " install Run initial setup (Docker + WiFi fix)"
  288. echo " start Start Dune Weaver"
  289. echo " stop Stop Dune Weaver"
  290. echo " restart Restart Dune Weaver"
  291. echo " update Pull latest changes and restart"
  292. echo " logs [N] View logs (N=number of lines, or follow if omitted)"
  293. echo " status Show container status"
  294. echo " shell Open a shell in the container"
  295. echo " touch Manage touch screen app (run 'dw touch help')"
  296. echo " help Show this help message"
  297. echo ""
  298. if [[ -n "$INSTALL_DIR" ]]; then
  299. echo -e "Install directory: ${BLUE}$INSTALL_DIR${NC}"
  300. fi
  301. }
  302. # Main
  303. case "${1:-help}" in
  304. install)
  305. shift
  306. cmd_install "$@"
  307. ;;
  308. start)
  309. cmd_start
  310. ;;
  311. stop)
  312. cmd_stop
  313. ;;
  314. restart)
  315. cmd_restart
  316. ;;
  317. update)
  318. cmd_update
  319. ;;
  320. logs)
  321. cmd_logs "$2"
  322. ;;
  323. status)
  324. cmd_status
  325. ;;
  326. shell)
  327. cmd_shell
  328. ;;
  329. touch)
  330. shift
  331. cmd_touch "$@"
  332. ;;
  333. help|--help|-h)
  334. cmd_help
  335. ;;
  336. *)
  337. echo -e "${RED}Unknown command: $1${NC}"
  338. echo ""
  339. cmd_help
  340. exit 1
  341. ;;
  342. esac