watcher.sh 2.8 KB

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