install.tmpl.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. export PATH=/bin:/usr/bin:/sbin:/usr/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 'Extracting files...'
  57. if [ $dryRun -gt 0 ]; then
  58. echo ">> tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -"
  59. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -t -f - | sed 's/^/>> /'
  60. else
  61. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f -
  62. fi
  63. if [ $dryRun -eq 0 -a ! -d "/var/lib/zerotier-one" ]; then
  64. echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".'
  65. exit 2
  66. fi
  67. echo 'Installing zerotier-cli command line utility...'
  68. rm -f /usr/bin/zerotier-cli /usr/bin/zerotier-idtool
  69. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli
  70. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-idtool
  71. echo 'Installing and (re-)starting zerotier-one daemon...'
  72. # Note: ensure that service restarts are the last thing this script actually
  73. # does, since these may kill the script itself. Also note the & to allow
  74. # them to finish independently.
  75. if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then
  76. # If this was updated or upgraded from an init.d based system, clean up the old
  77. # init.d stuff before installing directly via systemd.
  78. if [ -f /etc/init.d/zerotier-one ]; then
  79. if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then
  80. chkconfig zerotier-one off
  81. fi
  82. rm -f /etc/init.d/zerotier-one
  83. fi
  84. cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service"
  85. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  86. systemctl enable zerotier-one
  87. systemctl restart zerotier-one &
  88. else
  89. cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one
  90. chmod 0755 /etc/init.d/zerotier-one
  91. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  92. if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /usr/bin/chkconfig -o -f /bin/chkconfig ]; then
  93. chkconfig zerotier-one on
  94. else
  95. if [ -d /etc/rc0.d ]; then
  96. rm -f /etc/rc0.d/???zerotier-one
  97. ln -sf /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one
  98. fi
  99. if [ -d /etc/rc1.d ]; then
  100. rm -f /etc/rc1.d/???zerotier-one
  101. ln -sf /etc/init.d/zerotier-one /etc/rc1.d/K89zerotier-one
  102. fi
  103. if [ -d /etc/rc2.d ]; then
  104. rm -f /etc/rc2.d/???zerotier-one
  105. ln -sf /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one
  106. fi
  107. if [ -d /etc/rc3.d ]; then
  108. rm -f /etc/rc3.d/???zerotier-one
  109. ln -sf /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one
  110. fi
  111. if [ -d /etc/rc4.d ]; then
  112. rm -f /etc/rc4.d/???zerotier-one
  113. ln -sf /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one
  114. fi
  115. if [ -d /etc/rc5.d ]; then
  116. rm -f /etc/rc5.d/???zerotier-one
  117. ln -sf /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one
  118. fi
  119. if [ -d /etc/rc6.d ]; then
  120. rm -f /etc/rc6.d/???zerotier-one
  121. ln -sf /etc/init.d/zerotier-one /etc/rc6.d/K89zerotier-one
  122. fi
  123. fi
  124. service zerotier-one restart &
  125. fi
  126. sleep 1
  127. exit 0
  128. # Do not remove the last line or add a carriage return to it! The installer
  129. # looks for an unterminated line beginning with 16 #'s in itself to find
  130. # the binary blob data, which is appended after it.
  131. ################