install.tmpl.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 '>> dry run: ln'"
  27. alias rm="echo '>> dry run: rm'"
  28. alias mv="echo '>> dry run: mv'"
  29. alias chown="echo '>> dry run: chown'"
  30. alias chgrp="echo '>> dry run: chgrp'"
  31. alias chkconfig="echo '>> dry run: chkconfig'"
  32. alias zerotier-cli="echo '>> dry run: zerotier-cli'"
  33. alias service="echo '>> dry run: service'"
  34. alias systemctl="echo '>> dry run: systemctl'"
  35. fi
  36. scriptPath="`dirname "$0"`/`basename "$0"`"
  37. if [ ! -r "$scriptPath" ]; then
  38. scriptPath="$0"
  39. if [ ! -r "$scriptPath" ]; then
  40. echo "Installer cannot determine its own path; $scriptPath is not readable."
  41. exit 2
  42. fi
  43. fi
  44. endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
  45. if [ "$endMarkerIndex" -le 100 ]; then
  46. echo 'Internal error: unable to find end of script / start of binary data marker.'
  47. exit 2
  48. fi
  49. blobStart=`expr $endMarkerIndex + 17`
  50. if [ "$blobStart" -le "$endMarkerIndex" ]; then
  51. echo 'Internal error: unable to find end of script / start of binary data marker.'
  52. exit 2
  53. fi
  54. echo 'Extracting files...'
  55. if [ $dryRun -gt 0 ]; then
  56. echo ">> dry run: tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -"
  57. else
  58. tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f -
  59. fi
  60. if [ $dryRun -eq 0 -a ! -d "/var/lib/zerotier-one" ]; then
  61. echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".'
  62. exit 2
  63. fi
  64. echo 'Installing zerotier-cli command line utility...'
  65. rm -f /usr/bin/zerotier-cli
  66. ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli
  67. echo 'Installing and (re-)starting zerotier-one daemon...'
  68. # Note: ensure that service restarts are the last thing this script actually
  69. # does, since these may kill the script itself. Also note the & to allow
  70. # them to finish independently.
  71. if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then
  72. # If this was updated or upgraded from an init.d based system, clean up the old
  73. # init.d stuff before installing directly via systemd.
  74. if [ -f /etc/init.d/zerotier-one ]; then
  75. if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then
  76. chkconfig zerotier-one off
  77. fi
  78. rm -f /etc/init.d/zerotier-one
  79. fi
  80. cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service"
  81. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  82. systemctl enable zerotier-one
  83. systemctl restart zerotier-one &
  84. else
  85. cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one
  86. chmod 0755 /etc/init.d/zerotier-one
  87. rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one
  88. chkconfig zerotier-one on
  89. service zerotier-one restart &
  90. fi
  91. sleep 1
  92. exit 0
  93. # Do not remove the last line or add a carriage return to it! The installer
  94. # looks for an unterminated line beginning with 16 #'s in itself to find
  95. # the binary blob data, which is appended after it.
  96. ################