install.tmpl.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  3. shopt -s expand_aliases
  4. dryRun=0
  5. echo "*** ZeroTier One install/update ***"
  6. if [ "$UID" -ne 0 ]; then
  7. echo "Not running as root so doing dry run (no modifications to system)..."
  8. dryRun=1
  9. fi
  10. # Detect systemd vs. regular init
  11. SYSTEMDUNITDIR=
  12. if [ -e /bin/systemctl -o -e /usr/bin/systemctl -o -e /usr/local/bin/systemctl -o -e /sbin/systemctl -o -e /usr/sbin/systemctl ]; then
  13. if [ -e /usr/bin/pkg-config ]; then
  14. SYSTEMDUNITDIR=`/usr/bin/pkg-config systemd --variable=systemdsystemunitdir`
  15. fi
  16. if [ -z "$SYSTEMDUNITDIR" -o ! -d "$SYSTEMDUNITDIR" ]; then
  17. if [ -d /usr/lib/systemd/system ]; then
  18. SYSTEMDUNITDIR=/usr/lib/systemd/system
  19. fi
  20. if [ -d /etc/systemd/system ]; then
  21. SYSTEMDUNITDIR=/etc/systemd/system
  22. fi
  23. fi
  24. fi
  25. if [ $dryRun -gt 0 ]; then
  26. alias ln="echo '>> ln'"
  27. alias rm="echo '>> rm'"
  28. alias mv="echo '>> mv'"
  29. alias cp="echo '>> cp'"
  30. alias chown="echo '>> chown'"
  31. alias chgrp="echo '>> chgrp'"
  32. alias chmod="echo '>> chmod'"
  33. alias chkconfig="echo '>> chkconfig'"
  34. alias zerotier-cli="echo '>> zerotier-cli'"
  35. alias service="echo '>> service'"
  36. alias systemctl="echo '>> systemctl'"
  37. fi
  38. scriptPath="`dirname "$0"`/`basename "$0"`"
  39. if [ ! -r "$scriptPath" ]; then
  40. scriptPath="$0"
  41. if [ ! -r "$scriptPath" ]; then
  42. echo "Installer cannot determine its own path; $scriptPath is not readable."
  43. exit 2
  44. fi
  45. fi
  46. endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
  47. if [ "$endMarkerIndex" -le 100 ]; then
  48. echo 'Internal error: unable to find end of script / start of binary data marker.'
  49. exit 2
  50. fi
  51. blobStart=`expr $endMarkerIndex + 17`
  52. if [ "$blobStart" -le "$endMarkerIndex" ]; then
  53. echo 'Internal error: unable to find end of script / start of binary data marker.'
  54. exit 2
  55. fi
  56. echo -n 'Getting version of existing install... '
  57. origVersion=NONE
  58. if [ -x /var/lib/zerotier-one/zerotier-one ]; then
  59. origVersion=`/var/lib/zerotier-one/zerotier-one -v`
  60. fi
  61. echo $origVersion
  62. echo 'Extracting files...'
  63. if [ $dryRun -gt 0 ]; then
  64. echo ">> tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -"
  65. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -t -f - | sed 's/^/>> /'
  66. else
  67. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f -
  68. fi
  69. if [ $dryRun -eq 0 -a ! -x "/var/lib/zerotier-one/zerotier-one" ]; then
  70. echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".'
  71. exit 2
  72. fi
  73. echo -n 'Getting version of new install... '
  74. newVersion=`/var/lib/zerotier-one/zerotier-one -v`
  75. echo $newVersion
  76. echo 'Installing zerotier-cli command line utility...'
  77. rm -f /usr/bin/zerotier-cli /usr/bin/zerotier-idtool
  78. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli
  79. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-idtool
  80. echo 'Installing zerotier-one service...'
  81. # Note: ensure that service restarts are the last thing this script actually
  82. # does, since these may kill the script itself. Also note the & to allow
  83. # them to finish independently.
  84. if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then
  85. # If this was updated or upgraded from an init.d based system, clean up the old
  86. # init.d stuff before installing directly via systemd.
  87. if [ -f /etc/init.d/zerotier-one ]; then
  88. if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then
  89. chkconfig zerotier-one off
  90. fi
  91. rm -f /etc/init.d/zerotier-one
  92. fi
  93. cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service"
  94. chown 0 "$SYSTEMDUNITDIR/zerotier-one.service"
  95. chgrp 0 "$SYSTEMDUNITDIR/zerotier-one.service"
  96. chmod 0755 "$SYSTEMDUNITDIR/zerotier-one.service"
  97. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  98. systemctl enable zerotier-one.service
  99. if [ "$origVersion" != "$newVersion" ]; then
  100. echo 'Version has changed, starting...'
  101. systemctl restart zerotier-one.service
  102. fi
  103. else
  104. cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one
  105. chmod 0755 /etc/init.d/zerotier-one
  106. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  107. if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /usr/bin/chkconfig -o -f /bin/chkconfig ]; then
  108. chkconfig zerotier-one on
  109. else
  110. if [ -d /etc/rc0.d ]; then
  111. rm -f /etc/rc0.d/???zerotier-one
  112. ln -sf /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one
  113. fi
  114. if [ -d /etc/rc1.d ]; then
  115. rm -f /etc/rc1.d/???zerotier-one
  116. ln -sf /etc/init.d/zerotier-one /etc/rc1.d/K89zerotier-one
  117. fi
  118. if [ -d /etc/rc2.d ]; then
  119. rm -f /etc/rc2.d/???zerotier-one
  120. ln -sf /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one
  121. fi
  122. if [ -d /etc/rc3.d ]; then
  123. rm -f /etc/rc3.d/???zerotier-one
  124. ln -sf /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one
  125. fi
  126. if [ -d /etc/rc4.d ]; then
  127. rm -f /etc/rc4.d/???zerotier-one
  128. ln -sf /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one
  129. fi
  130. if [ -d /etc/rc5.d ]; then
  131. rm -f /etc/rc5.d/???zerotier-one
  132. ln -sf /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one
  133. fi
  134. if [ -d /etc/rc6.d ]; then
  135. rm -f /etc/rc6.d/???zerotier-one
  136. ln -sf /etc/init.d/zerotier-one /etc/rc6.d/K89zerotier-one
  137. fi
  138. fi
  139. if [ "$origVersion" != "$newVersion" ]; then
  140. echo 'Version has changed, starting...'
  141. if [ -f /sbin/service -o -f /usr/sbin/service ]; then
  142. service zerotier-one restart
  143. else
  144. /etc/init.d/zerotier-one restart
  145. fi
  146. fi
  147. fi
  148. exit 0
  149. # Do not remove the last line or add a carriage return to it! The installer
  150. # looks for an unterminated line beginning with 16 #'s in itself to find
  151. # the binary blob data, which is appended after it.
  152. ################