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