netclient-install.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. if [ $(id -u) -ne 0 ]; then
  3. echo "This script must be run as root"
  4. exit 1
  5. fi
  6. echo "checking dependencies..."
  7. OS=$(uname)
  8. if [ -f /etc/debian_version ]; then
  9. install_cmd='apt-get install -y'
  10. elif [ -f /etc/alpine-release ]; then
  11. install_cmd='apk --update add'
  12. elif [ -f /etc/centos-release ]; then
  13. install_cmd='yum install -y'
  14. elif [ -f /etc/fedora-release ]; then
  15. install_cmd='dnf install -y'
  16. elif [ "${OS}" = "FreeBSD" ]; then
  17. install_cmd='pkg install -y'
  18. else
  19. install_cmd=''
  20. fi
  21. if [ -z "${install_cmd}" ]; then
  22. echo "OS unsupported for automatic dependency install"
  23. exit 1
  24. fi
  25. dependencies="wireguard"
  26. set -- $dependencies
  27. while [ -n "$1" ]; do
  28. echo $1
  29. if [ "${OS}" = "FreeBSD" ]; then
  30. is_installed=$(pkg check -d $1 | grep '100%')
  31. if [ "${is_installed}" = '100%' ]; then
  32. echo " " $1 is installed
  33. else
  34. echo " " $1 is not installed. Attempting install.
  35. ${install_cmd} $1
  36. sleep 5
  37. is_installed=$(pkg check -d $1 | grep '100%')
  38. if [ "${is_installed}" = '100%' ]; then
  39. echo " " $1 is installed
  40. elif [ -x "$(command -v $1)" ]; then
  41. echo " " $1 is installed
  42. else
  43. echo " " FAILED TO INSTALL $1
  44. echo " " This may break functionality.
  45. fi
  46. fi
  47. else
  48. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  49. if [ "${is_installed}" = "install ok installed" ]; then
  50. echo " " $1 is installed
  51. else
  52. echo " " $1 is not installed. Attempting install.
  53. ${install_cmd} $1
  54. sleep 5
  55. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  56. if [ "${is_installed}" = "install ok installed" ]; then
  57. echo " " $1 is installed
  58. elif [ -x "$(command -v $1)" ]; then
  59. echo " " $1 is installed
  60. else
  61. echo " " FAILED TO INSTALL $1
  62. echo " " This may break functionality.
  63. fi
  64. fi
  65. fi
  66. shift
  67. done
  68. set -e
  69. [ -z "$KEY" ] && KEY=nokey;
  70. [ -z "$VERSION" ] && echo "no \$VERSION provided, fallback to latest" && VERSION=latest;
  71. [ "latest" != "$VERSION" ] && [ "v" != `echo $VERSION | cut -c1` ] && VERSION="v$VERSION"
  72. [ -z "$NAME" ] && NAME="";
  73. dist=netclient
  74. echo "OS Version = $(uname)"
  75. echo "Netclient Version = $VERSION"
  76. case $(uname | tr '[:upper:]' '[:lower:]') in
  77. linux*)
  78. if [ -z "$CPU_ARCH" ]; then
  79. CPU_ARCH=$(uname -m)
  80. fi
  81. case $CPU_ARCH in
  82. amd64)
  83. dist=netclient
  84. ;;
  85. x86_64)
  86. dist=netclient
  87. ;;
  88. x86_32)
  89. dist=netclient-32
  90. ;;
  91. arm64)
  92. dist=netclient-arm64
  93. ;;
  94. aarch64)
  95. dist=netclient-arm64
  96. ;;
  97. armv7l)
  98. dist=netclient-armv7
  99. ;;
  100. arm*)
  101. dist=netclient-$CPU_ARCH
  102. ;;
  103. *)
  104. fatal "$CPU_ARCH : cpu architecture not supported"
  105. esac
  106. ;;
  107. darwin)
  108. dist=netclient-darwin
  109. ;;
  110. freebsd*)
  111. if [ -z "$CPU_ARCH" ]; then
  112. CPU_ARCH=$(uname -m)
  113. fi
  114. case $CPU_ARCH in
  115. amd64)
  116. dist=netclient-freebsd
  117. ;;
  118. x86_64)
  119. dist=netclient-freebsd
  120. ;;
  121. x86_32)
  122. dist=netclient-freebsd-32
  123. ;;
  124. arm64)
  125. dist=netclient-freebsd-arm64
  126. ;;
  127. aarch64)
  128. dist=netclient-freebsd-arm64
  129. ;;
  130. armv7l)
  131. dist=netclient-freebsd-armv7
  132. ;;
  133. arm*)
  134. dist=netclient-freebsd-$CPU_ARCH
  135. ;;
  136. *)
  137. fatal "$CPU_ARCH : cpu architecture not supported"
  138. esac
  139. ;;
  140. esac
  141. echo "Binary = $dist"
  142. url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
  143. if curl --output /dev/null --silent --head --fail "$url"; then
  144. echo "Downloading $dist $VERSION"
  145. wget -nv -O netclient $url
  146. else
  147. echo "Downloading $dist latest"
  148. wget -nv -O netclient https://github.com/gravitl/netmaker/releases/download/latest/$dist
  149. fi
  150. chmod +x netclient
  151. EXTRA_ARGS=""
  152. if [ "${OS}" = "FreeBSD" ]; then
  153. EXTRA_ARGS = "--daemon=off"
  154. fi
  155. if [ -z "${NAME}" ]; then
  156. ./netclient join -t $KEY $EXTRA_ARGS
  157. else
  158. ./netclient join -t $KEY --name $NAME $EXTRA_ARGS
  159. fi
  160. rm -f netclient
  161. if [ "${OS}" = "FreeBSD" ]; then
  162. tee /usr/local/etc/rc.d/netclient <<'EOF' >/dev/null
  163. #!/bin/sh
  164. # PROVIDE: netclient
  165. # REQUIRE: LOGIN DAEMON NETWORKING SERVERS FILESYSTEM
  166. # BEFORE:
  167. # KEYWORD: shutdown
  168. . /etc/rc.subr
  169. name="netclient"
  170. rcvar=netclient_enable
  171. pidfile="/var/run/${name}.pid"
  172. command="/usr/sbin/daemon"
  173. command_args="-c -f -P ${pidfile} -R 10 -t "Netclient" -u root -o /etc/netclient/netclient.log /etc/netclient/netclient checkin -n all"
  174. load_rc_config $name
  175. run_rc_command "$1"
  176. EOF
  177. /usr/local/etc/rc.d/netclient enable
  178. /usr/local/etc/rc.d/netclient start
  179. fi