update-watcher.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash
  2. #
  3. # Update Watcher for Dune Weaver
  4. #
  5. # This script runs on the host machine and watches for update triggers
  6. # from the Docker container. When a trigger is detected, it runs 'dw update'.
  7. #
  8. # The container signals an update by creating .update-trigger file in the
  9. # mounted volume, which the host can see and act upon.
  10. #
  11. set -e
  12. # Configuration
  13. TRIGGER_FILE=""
  14. INSTALL_DIR=""
  15. LOG_PREFIX="[update-watcher]"
  16. # Find dune-weaver directory (same logic as dw script)
  17. find_install_dir() {
  18. if [[ -f "$HOME/dune-weaver/main.py" ]]; then
  19. echo "$HOME/dune-weaver"
  20. elif [[ -f "/home/pi/dune-weaver/main.py" ]]; then
  21. echo "/home/pi/dune-weaver"
  22. else
  23. echo ""
  24. fi
  25. }
  26. log() {
  27. echo "$LOG_PREFIX $(date '+%Y-%m-%d %H:%M:%S') $1"
  28. }
  29. # Initialize
  30. INSTALL_DIR=$(find_install_dir)
  31. if [[ -z "$INSTALL_DIR" ]]; then
  32. log "ERROR: Dune Weaver installation not found"
  33. exit 1
  34. fi
  35. TRIGGER_FILE="$INSTALL_DIR/.update-trigger"
  36. log "Watching for update triggers at: $TRIGGER_FILE"
  37. log "Install directory: $INSTALL_DIR"
  38. # Main watch loop
  39. while true; do
  40. if [[ -f "$TRIGGER_FILE" ]]; then
  41. log "Update trigger detected!"
  42. # Read any message from trigger file (optional metadata)
  43. if [[ -s "$TRIGGER_FILE" ]]; then
  44. log "Trigger message: $(cat "$TRIGGER_FILE")"
  45. fi
  46. # Remove trigger file before update to prevent re-triggering
  47. rm -f "$TRIGGER_FILE"
  48. # Run the update
  49. log "Starting update process..."
  50. cd "$INSTALL_DIR"
  51. if /usr/local/bin/dw update 2>&1 | while read -r line; do log "$line"; done; then
  52. log "Update completed successfully"
  53. else
  54. log "Update completed with errors (exit code: $?)"
  55. fi
  56. log "Resuming watch..."
  57. fi
  58. # Poll every 2 seconds
  59. sleep 2
  60. done