install.tmpl.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if [ $dryRun -gt 0 ]; then
  11. alias ln="echo '>> ln'"
  12. alias rm="echo '>> rm'"
  13. alias mv="echo '>> mv'"
  14. alias cp="echo '>> cp'"
  15. alias chown="echo '>> chown'"
  16. alias chgrp="echo '>> chgrp'"
  17. alias chmod="echo '>> chmod'"
  18. alias chkconfig="echo '>> chkconfig'"
  19. alias zerotier-cli="echo '>> zerotier-cli'"
  20. alias service="echo '>> service'"
  21. alias systemctl="echo '>> systemctl'"
  22. fi
  23. scriptPath="`dirname "$0"`/`basename "$0"`"
  24. if [ ! -r "$scriptPath" ]; then
  25. scriptPath="$0"
  26. if [ ! -r "$scriptPath" ]; then
  27. echo "Installer cannot determine its own path; $scriptPath is not readable."
  28. exit 2
  29. fi
  30. fi
  31. # Check for systemd vs. old school SysV init
  32. SYSTEMDUNITDIR=
  33. 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
  34. # Second check: test if systemd appears to actually be running. Apparently Ubuntu
  35. # thought it was a good idea to ship with systemd installed but not used. Issue #133
  36. if [ -d /var/run/systemd/system -o -d /run/systemd/system ]; then
  37. if [ -e /usr/bin/pkg-config ]; then
  38. SYSTEMDUNITDIR=`/usr/bin/pkg-config systemd --variable=systemdsystemunitdir`
  39. fi
  40. if [ -z "$SYSTEMDUNITDIR" -o ! -d "$SYSTEMDUNITDIR" ]; then
  41. if [ -d /usr/lib/systemd/system ]; then
  42. SYSTEMDUNITDIR=/usr/lib/systemd/system
  43. fi
  44. if [ -d /etc/systemd/system ]; then
  45. SYSTEMDUNITDIR=/etc/systemd/system
  46. fi
  47. fi
  48. fi
  49. fi
  50. # Find the end of this script, which is where we have appended binary data.
  51. endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
  52. if [ "$endMarkerIndex" -le 100 ]; then
  53. echo 'Internal error: unable to find end of script / start of binary data marker.'
  54. exit 2
  55. fi
  56. blobStart=`expr $endMarkerIndex + 17`
  57. if [ "$blobStart" -le "$endMarkerIndex" ]; then
  58. echo 'Internal error: unable to find end of script / start of binary data marker.'
  59. exit 2
  60. fi
  61. echo -n 'Getting version of existing install... '
  62. origVersion=NONE
  63. if [ -x /var/lib/zerotier-one/zerotier-one ]; then
  64. origVersion=`/var/lib/zerotier-one/zerotier-one -v`
  65. fi
  66. echo $origVersion
  67. echo 'Extracting files...'
  68. if [ $dryRun -gt 0 ]; then
  69. echo ">> tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -"
  70. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -t -f - | sed 's/^/>> /'
  71. else
  72. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f -
  73. fi
  74. if [ $dryRun -eq 0 -a ! -x "/var/lib/zerotier-one/zerotier-one" ]; then
  75. echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".'
  76. exit 2
  77. fi
  78. echo -n 'Getting version of new install... '
  79. newVersion=`/var/lib/zerotier-one/zerotier-one -v`
  80. echo $newVersion
  81. echo 'Creating symlinks...'
  82. rm -f /usr/bin/zerotier-cli /usr/bin/zerotier-idtool
  83. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli
  84. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-idtool
  85. echo 'Installing zerotier-one service...'
  86. if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then
  87. # SYSTEMD
  88. # If this was updated or upgraded from an init.d based system, clean up the old
  89. # init.d stuff before installing directly via systemd.
  90. if [ -f /etc/init.d/zerotier-one ]; then
  91. if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then
  92. chkconfig zerotier-one off
  93. fi
  94. rm -f /etc/init.d/zerotier-one
  95. fi
  96. cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service"
  97. chown 0 "$SYSTEMDUNITDIR/zerotier-one.service"
  98. chgrp 0 "$SYSTEMDUNITDIR/zerotier-one.service"
  99. chmod 0755 "$SYSTEMDUNITDIR/zerotier-one.service"
  100. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  101. systemctl enable zerotier-one.service
  102. #if [ "$origVersion" != "$newVersion" ]; then
  103. # echo 'Version has changed, starting...'
  104. # systemctl restart zerotier-one.service
  105. #fi
  106. else
  107. # SYSV INIT -- also covers upstart which supports SysVinit backward compatibility
  108. cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one
  109. chmod 0755 /etc/init.d/zerotier-one
  110. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  111. if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /usr/bin/chkconfig -o -f /bin/chkconfig ]; then
  112. chkconfig zerotier-one on
  113. else
  114. # Yes Virginia, some systems lack chkconfig.
  115. if [ -d /etc/rc0.d ]; then
  116. rm -f /etc/rc0.d/???zerotier-one
  117. ln -sf /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one
  118. fi
  119. if [ -d /etc/rc1.d ]; then
  120. rm -f /etc/rc1.d/???zerotier-one
  121. ln -sf /etc/init.d/zerotier-one /etc/rc1.d/K89zerotier-one
  122. fi
  123. if [ -d /etc/rc2.d ]; then
  124. rm -f /etc/rc2.d/???zerotier-one
  125. ln -sf /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one
  126. fi
  127. if [ -d /etc/rc3.d ]; then
  128. rm -f /etc/rc3.d/???zerotier-one
  129. ln -sf /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one
  130. fi
  131. if [ -d /etc/rc4.d ]; then
  132. rm -f /etc/rc4.d/???zerotier-one
  133. ln -sf /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one
  134. fi
  135. if [ -d /etc/rc5.d ]; then
  136. rm -f /etc/rc5.d/???zerotier-one
  137. ln -sf /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one
  138. fi
  139. if [ -d /etc/rc6.d ]; then
  140. rm -f /etc/rc6.d/???zerotier-one
  141. ln -sf /etc/init.d/zerotier-one /etc/rc6.d/K89zerotier-one
  142. fi
  143. fi
  144. #if [ "$origVersion" != "$newVersion" ]; then
  145. # echo 'Version has changed, starting...'
  146. # if [ -f /sbin/service -o -f /usr/sbin/service ]; then
  147. # service zerotier-one restart
  148. # else
  149. # /etc/init.d/zerotier-one restart
  150. # fi
  151. #fi
  152. fi
  153. exit 0
  154. # Do not remove the last line or add a carriage return to it! The installer
  155. # looks for an unterminated line beginning with 16 #'s in itself to find
  156. # the binary blob data, which is appended after it.
  157. ################