unit-entrypoint.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env bash
  2. set -e
  3. echo "Using entrypoint patch"
  4. echo "***************************************************************"
  5. curl_put()
  6. {
  7. RET=`/usr/bin/curl -s -w '%{http_code}' -X PUT --data-binary @$1 --unix-socket /var/run/control.unit.sock http://localhost/$2`
  8. RET_BODY=${RET::-3}
  9. RET_STATUS=$(echo $RET | /usr/bin/tail -c 4)
  10. if [ "$RET_STATUS" -ne "200" ]; then
  11. echo "$0: Error: HTTP response status code is '$RET_STATUS'"
  12. echo "$RET_BODY"
  13. return 1
  14. else
  15. echo "$0: OK: HTTP response status code is '$RET_STATUS'"
  16. echo "$RET_BODY"
  17. fi
  18. return 0
  19. }
  20. if [ "$1" = "unitd" -o "$1" = "unitd-debug" ]; then
  21. if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
  22. echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
  23. else
  24. if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
  25. echo "$0: /docker-entrypoint.d/ is not empty, launching Unit daemon to perform initial configuration..."
  26. /usr/sbin/$1 --control unix:/var/run/control.unit.sock
  27. while [ ! -S /var/run/control.unit.sock ]; do echo "$0: Waiting for control socket to be created..."; /bin/sleep 0.1; done
  28. # even when the control socket exists, it does not mean unit has finished initialisation
  29. # this curl call will get a reply once unit is fully launched
  30. /usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
  31. echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
  32. for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
  33. echo "$0: Uploading certificates bundle: $f"
  34. curl_put $f "certificates/$(basename $f .pem)"
  35. done
  36. echo "$0: Looking for configuration snippets in /docker-entrypoint.d/..."
  37. for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.json"); do
  38. echo "$0: Applying configuration $f";
  39. curl_put $f "config"
  40. done
  41. echo "$0: Looking for shell scripts in /docker-entrypoint.d/..."
  42. for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.sh"); do
  43. echo "$0: Launching $f";
  44. "$f"
  45. done
  46. # warn on filetypes we don't know what to do with
  47. for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem"); do
  48. echo "$0: Ignoring $f";
  49. done
  50. echo "$0: Stopping Unit daemon after initial configuration..."
  51. kill -TERM `/bin/cat /var/run/unit.pid`
  52. for i in {1..5}; do
  53. if [[ -S /var/run/control.unit.sock ]]
  54. then
  55. echo "$0 Waiting for control socket to be removed..."
  56. /bin/sleep 1.0
  57. else
  58. break
  59. fi
  60. done
  61. if [ -S /var/run/control.unit.sock ]; then
  62. kill -SIGTERM `/bin/cat /var/run/unit.pid` && rm -f /var/run/control.unit.sock
  63. fi
  64. echo
  65. echo "$0: Unit initial configuration complete; ready for start up..."
  66. echo
  67. else
  68. echo "$0: /docker-entrypoint.d/ is empty, skipping initial configuration..."
  69. fi
  70. fi
  71. fi
  72. exec "$@"