netclient-install.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "Checking" | grep "done")
  31. if [ "$is_installed" != "" ]; 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 "Checking" | grep "done")
  38. if [ "$is_installed" != "" ]; 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. mipsle)
  104. dist=netclient-mipsle
  105. ;;
  106. *)
  107. fatal "$CPU_ARCH : cpu architecture not supported"
  108. esac
  109. ;;
  110. darwin)
  111. dist=netclient-darwin
  112. ;;
  113. freebsd*)
  114. if [ -z "$CPU_ARCH" ]; then
  115. CPU_ARCH=$(uname -m)
  116. fi
  117. case $CPU_ARCH in
  118. amd64)
  119. dist=netclient-freebsd
  120. ;;
  121. x86_64)
  122. dist=netclient-freebsd
  123. ;;
  124. x86_32)
  125. dist=netclient-freebsd-32
  126. ;;
  127. arm64)
  128. dist=netclient-freebsd-arm64
  129. ;;
  130. aarch64)
  131. dist=netclient-freebsd-arm64
  132. ;;
  133. armv7l)
  134. dist=netclient-freebsd-armv7
  135. ;;
  136. arm*)
  137. dist=netclient-freebsd-$CPU_ARCH
  138. ;;
  139. *)
  140. fatal "$CPU_ARCH : cpu architecture not supported"
  141. esac
  142. ;;
  143. esac
  144. echo "Binary = $dist"
  145. url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
  146. if curl --output /dev/null --silent --head --fail "$url"; then
  147. echo "Downloading $dist $VERSION"
  148. wget -nv -O netclient $url
  149. else
  150. echo "Downloading $dist latest"
  151. wget -nv -O netclient https://github.com/gravitl/netmaker/releases/download/latest/$dist
  152. fi
  153. chmod +x netclient
  154. EXTRA_ARGS=""
  155. if [ "${OS}" = "FreeBSD" ]; then
  156. EXTRA_ARGS="--daemon=off"
  157. fi
  158. if [ -z "${NAME}" ]; then
  159. ./netclient join -t $KEY $EXTRA_ARGS
  160. else
  161. ./netclient join -t $KEY --name $NAME $EXTRA_ARGS
  162. fi
  163. if [ "${OS}" = "FreeBSD" ]; then
  164. mv ./netclient /etc/netclient/netclient
  165. cat << 'END_OF_FILE' > ./netclient.service.tmp
  166. #!/bin/sh
  167. # PROVIDE: netclient
  168. # REQUIRE: LOGIN DAEMON NETWORKING SERVERS FILESYSTEM
  169. # BEFORE:
  170. # KEYWORD: shutdown
  171. . /etc/rc.subr
  172. name="netclient"
  173. rcvar=netclient_enable
  174. pidfile="/var/run/${name}.pid"
  175. command="/usr/sbin/daemon"
  176. command_args="-c -f -P ${pidfile} -R 10 -t "Netclient" -u root -o /etc/netclient/netclient.log /etc/netclient/netclient checkin -n all"
  177. load_rc_config $name
  178. run_rc_command "$1"
  179. END_OF_FILE
  180. sudo mv ./netclient.service.tmp /usr/local/etc/rc.d/netclient
  181. sudo chmod +x /usr/local/etc/rc.d/netclient
  182. sudo /usr/local/etc/rc.d/netclient enable
  183. sudo /usr/local/etc/rc.d/netclient start
  184. else
  185. rm -f netclient
  186. fi