entrypoint.sh.release 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. grepzt() {
  3. [ -f /var/lib/zerotier-one/zerotier-one.pid -a -n "$(cat /var/lib/zerotier-one/zerotier-one.pid)" -a -d "/proc/$(cat /var/lib/zerotier-one/zerotier-one.pid)" ]
  4. return $?
  5. }
  6. mkztfile() {
  7. file=$1
  8. mode=$2
  9. content=$3
  10. mkdir -p /var/lib/zerotier-one
  11. echo "$content" > "/var/lib/zerotier-one/$file"
  12. chmod "$mode" "/var/lib/zerotier-one/$file"
  13. }
  14. if [ "x$ZEROTIER_API_SECRET" != "x" ]
  15. then
  16. mkztfile authtoken.secret 0600 "$ZEROTIER_API_SECRET"
  17. fi
  18. if [ "x$ZEROTIER_IDENTITY_PUBLIC" != "x" ]
  19. then
  20. mkztfile identity.public 0644 "$ZEROTIER_IDENTITY_PUBLIC"
  21. fi
  22. if [ "x$ZEROTIER_IDENTITY_SECRET" != "x" ]
  23. then
  24. mkztfile identity.secret 0600 "$ZEROTIER_IDENTITY_SECRET"
  25. fi
  26. mkztfile zerotier-one.port 0600 "9993"
  27. killzerotier() {
  28. echo "Killing zerotier"
  29. kill $(cat /var/lib/zerotier-one/zerotier-one.pid)
  30. exit 0
  31. }
  32. trap killzerotier INT TERM
  33. echo "Configuring networks to join"
  34. mkdir -p /var/lib/zerotier-one/networks.d
  35. echo "joining networks: $@"
  36. for i in "$@"
  37. do
  38. echo "Configuring join for $i"
  39. touch "/var/lib/zerotier-one/networks.d/${i}.conf"
  40. done
  41. echo "starting zerotier"
  42. nohup /usr/sbin/zerotier-one &
  43. while ! grepzt
  44. do
  45. echo "zerotier hasn't started, waiting a second"
  46. if [ -f nohup.out ]
  47. then
  48. tail -n 10 nohup.out
  49. fi
  50. sleep 1
  51. done
  52. echo "Writing healthcheck for networks: $@"
  53. cat >/healthcheck.sh <<EOF
  54. #!/bin/bash
  55. for i in $@
  56. do
  57. [ "\$(zerotier-cli get \$i status)" = "OK" ] || exit 1
  58. done
  59. EOF
  60. chmod +x /healthcheck.sh
  61. echo "Sleeping infinitely"
  62. while true
  63. do
  64. sleep 1
  65. done