install.tmpl.sh 4.7 KB

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