setup.sh.jinja 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. {% macro setup_script(values, settings) -%}
  2. {%- set config_dir = "%s/config"|format(values.consts.home_path) %}
  3. {%- set cli = "syncthing cli --home %s config"|format(config_dir) %}
  4. #!/bin/sh
  5. set -e
  6. trap cleanup EXIT TERM
  7. cleanup() {
  8. echo "Gracefully stopping Syncthing..."
  9. if kill -0 $SYNCTHING_PID > /dev/null 2>&1; then
  10. kill -SIGTERM $SYNCTHING_PID
  11. wait $SYNCTHING_PID
  12. fi
  13. echo "Syncthing stopped."
  14. }
  15. try_for() {
  16. local max_tries=$1
  17. local sleep_time=$2
  18. local cmd=$3
  19. tries=0
  20. until eval "$cmd"; do
  21. [ $tries -ge $max_tries ] && return 1
  22. tries=$((tries+1))
  23. sleep $sleep_time
  24. done
  25. }
  26. echo "Starting Syncthing in the background"
  27. /bin/entrypoint.sh /bin/syncthing &
  28. SYNCTHING_PID=$!
  29. echo "Syncthing started with PID [$SYNCTHING_PID]"
  30. echo "Waiting for Syncthing to be ready..."
  31. try_for 15 2 "[ -f '{{ config_dir }}/config.xml' ]" || { echo "Syncthing did not become ready in time. Exiting..."; exit 1; }
  32. try_for 15 2 "curl --silent --output /dev/null http://127.0.0.1:{{ values.network.web_port.port_number }}/rest/noauth/health" || { echo "Syncthing did not become ready in time. Exiting..."; exit 1; }
  33. echo "Syncthing is ready."
  34. {%- for cfg in settings %}
  35. echo 'Using subcommand [{{ cfg.cmd }}] to set value [{{ '\"%s\"' | format(cfg.value) if cfg.quote else cfg.value }}]'
  36. {{ cli }} {{ cfg.cmd }} set -- {{ '"%s"' | format(cfg.value) if cfg.quote else cfg.value }} || { echo "Failed to apply. Exiting..."; exit 1; }
  37. {%- endfor %}
  38. echo "Gracefully stopping Syncthing..."
  39. kill -SIGTERM $SYNCTHING_PID
  40. wait $SYNCTHING_PID
  41. echo "Syncthing stopped."
  42. {%- endmacro %}