watcher.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. source "${BASH_SOURCE%/*}/common.sh"
  3. ROOT_FOLDER="${PWD}"
  4. WATCH_FILE="${ROOT_FOLDER}/state/events"
  5. function clean_events() {
  6. echo "Cleaning events..."
  7. echo "" >"$WATCH_FILE"
  8. }
  9. function set_status() {
  10. local id=$1
  11. local status=$2
  12. write_log "Setting status for ${id} to ${status}"
  13. # Update the status of the event
  14. sed -i "s/${id}.*$/$(echo "${id} ${status}" | sed 's/\//\\\//g')/" "$WATCH_FILE"
  15. }
  16. function run_command() {
  17. local command_path="${1}"
  18. local id=$2
  19. shift 2
  20. set_status "$id" "running"
  21. $command_path "$@" >>"${ROOT_FOLDER}/logs/${id}.log" 2>&1
  22. local result=$?
  23. echo "Command ${command_path} exited with code ${result}"
  24. if [[ $result -eq 0 ]]; then
  25. set_status "$id" "success"
  26. else
  27. set_status "$id" "error"
  28. fi
  29. }
  30. function select_command() {
  31. # Example command:
  32. # clone_repo id waiting "args"
  33. local command=$(echo "$1" | cut -d ' ' -f 1)
  34. local id=$(echo "$1" | cut -d ' ' -f 2)
  35. local status=$(echo "$1" | cut -d ' ' -f 3)
  36. local args=$(echo "$1" | cut -d ' ' -f 4-)
  37. if [[ "$status" != "waiting" ]]; then
  38. return 0
  39. fi
  40. write_log "Executing command ${command}"
  41. if [ -z "$command" ]; then
  42. return 0
  43. fi
  44. if [ "$command" = "clone_repo" ]; then
  45. run_command "${ROOT_FOLDER}/scripts/git.sh" "$id" "clone" "$args"
  46. return 0
  47. fi
  48. if [ "$command" = "update_repo" ]; then
  49. run_command "${ROOT_FOLDER}/scripts/git.sh" "$id" "update" "$args"
  50. return 0
  51. fi
  52. if [ "$command" = "app" ]; then
  53. local arg1=$(echo "$args" | cut -d ' ' -f 1)
  54. local arg2=$(echo "$args" | cut -d ' ' -f 2)
  55. # Args example: start filebrowser
  56. run_command "${ROOT_FOLDER}/scripts/app.sh" "$id" "$arg1" "$arg2"
  57. return 0
  58. fi
  59. if [ "$command" = "system_info" ]; then
  60. run_command "${ROOT_FOLDER}/scripts/system-info.sh" "$id"
  61. return 0
  62. fi
  63. if [ "$command" = "update" ]; then
  64. run_command "${ROOT_FOLDER}/scripts/system.sh" "$id" "update"
  65. return 0
  66. fi
  67. if [ "$command" = "restart" ]; then
  68. run_command "${ROOT_FOLDER}/scripts/system.sh" "$id" "restart"
  69. return 0
  70. fi
  71. echo "Unknown command ${command}"
  72. return 0
  73. }
  74. write_log "Listening for events in ${WATCH_FILE}..."
  75. clean_events
  76. # Listen in for changes in the WATCH_FILE
  77. fswatch -0 "${WATCH_FILE}" | while read -d ""; do
  78. # Read the command from the last line of the file
  79. command=$(tail -n 1 "${WATCH_FILE}")
  80. status=$(echo "$command" | cut -d ' ' -f 3)
  81. if [ -z "$command" ] || [ "$status" != "waiting" ]; then
  82. continue
  83. else
  84. select_command "$command"
  85. fi
  86. done