entrypoint.sh.release 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/sh
  2. grepzt() {
  3. [ -f /var/lib/zerotier-one/zerotier-one.pid -a -n "$(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)" -a -d "/proc/$(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)" ]
  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. log "Killing zerotier"
  29. kill $(cat /var/lib/zerotier-one/zerotier-one.pid 2>/dev/null)
  30. exit 0
  31. }
  32. log_header() {
  33. echo -n "\r=>"
  34. }
  35. log_detail_header() {
  36. echo -n "\r===>"
  37. }
  38. log() {
  39. echo "$(log_header)" "$@"
  40. }
  41. log_params() {
  42. title=$1
  43. shift
  44. log "$title" "[$@]"
  45. }
  46. log_detail() {
  47. echo "$(log_detail_header)" "$@"
  48. }
  49. log_detail_params() {
  50. title=$1
  51. shift
  52. log_detail "$title" "[$@]"
  53. }
  54. trap killzerotier INT TERM
  55. log "Configuring networks to join"
  56. mkdir -p /var/lib/zerotier-one/networks.d
  57. log_params "Joining networks from command line:" $@
  58. for i in "$@"
  59. do
  60. log_detail_params "Configuring join:" "$i"
  61. touch "/var/lib/zerotier-one/networks.d/${i}.conf"
  62. done
  63. if [ "x$ZEROTIER_JOIN_NETWORKS" != "x" ]
  64. then
  65. log_params "Joining networks from environment:" $ZEROTIER_JOIN_NETWORKS
  66. for i in $ZEROTIER_JOIN_NETWORKS
  67. do
  68. log_detail_params "Configuring join:" "$i"
  69. touch "/var/lib/zerotier-one/networks.d/${i}.conf"
  70. done
  71. fi
  72. log "Starting ZeroTier"
  73. nohup /usr/sbin/zerotier-one &
  74. while ! grepzt
  75. do
  76. log_detail "ZeroTier hasn't started, waiting a second"
  77. if [ -f nohup.out ]
  78. then
  79. tail -n 10 nohup.out
  80. fi
  81. sleep 1
  82. done
  83. log_params "Writing healthcheck for networks:" $@
  84. cat >/healthcheck.sh <<EOF
  85. #!/bin/bash
  86. for i in $@ $ZEROTIER_JOIN_NETWORKS
  87. do
  88. [ "\$(zerotier-cli get \$i status)" = "OK" ] || exit 1
  89. done
  90. EOF
  91. chmod +x /healthcheck.sh
  92. log_params "zerotier-cli info:" "$(zerotier-cli info)"
  93. log "Sleeping infinitely"
  94. while true
  95. do
  96. sleep 1
  97. done