netclient-install.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. dependencies="wireguard wireguard-tools"
  10. update_cmd='apt update'
  11. install_cmd='apt-get install -y'
  12. elif [ -f /etc/alpine-release ]; then
  13. dependencies="wireguard"
  14. update_cmd='apk update'
  15. install_cmd='apk --update add'
  16. elif [ -f /etc/centos-release ]; then
  17. dependencies="wireguard"
  18. update_cmd='yum update'
  19. install_cmd='yum install -y'
  20. elif [ -f /etc/fedora-release ]; then
  21. dependencies="wireguard"
  22. update_cmd='dnf update'
  23. install_cmd='dnf install -y'
  24. elif [ -f /etc/arch-releae ]; then
  25. dependecies="wireguard-tools"
  26. update_cmd='pacman -Sy'
  27. install_cmd='pacman -S --noconfirm'
  28. elif [ "${OS}" = "FreeBSD" ]; then
  29. dependencies="wireguard"
  30. update_cmd='pkg update'
  31. install_cmd='pkg install -y'
  32. elif [ -f /etc/openwrt_release ]; then
  33. dependencies="wireguard-tools"
  34. OS="OpenWRT"
  35. update_cmd='opkg update'
  36. install_cmd='opkg install'
  37. else
  38. install_cmd=''
  39. fi
  40. if [ -z "${install_cmd}" ]; then
  41. echo "OS unsupported for automatic dependency install"
  42. exit 1
  43. fi
  44. set -- $dependencies
  45. while [ -n "$1" ]; do
  46. echo $1
  47. if [ "${OS}" = "FreeBSD" ]; then
  48. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  49. if [ "$is_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=$(pkg check -d $1 | grep "Checking" | grep "done")
  56. if [ "$is_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. else
  66. if [ "${OS}" = "OpenWRT" ]; then
  67. is_installed=$(opkg list-installed $1 | grep $1)
  68. else
  69. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  70. fi
  71. if [ "${is_installed}" != "" ]; then
  72. echo " " $1 is installed
  73. else
  74. echo " " $1 is not installed. Attempting install.
  75. ${install_cmd} $1
  76. sleep 5
  77. if [ "${OS}" = "OpenWRT" ]; then
  78. is_installed=$(opkg list-installed $1 | grep $1)
  79. else
  80. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  81. fi
  82. if [ "${is_installed}" != "" ]; then
  83. echo " " $1 is installed
  84. elif [ -x "$(command -v $1)" ]; then
  85. echo " " $1 is installed
  86. else
  87. echo " " FAILED TO INSTALL $1
  88. echo " " This may break functionality.
  89. fi
  90. fi
  91. fi
  92. shift
  93. done
  94. set -e
  95. [ -z "$KEY" ] && KEY=nokey;
  96. [ -z "$VERSION" ] && echo "no \$VERSION provided, fallback to latest" && VERSION=latest;
  97. [ "latest" != "$VERSION" ] && [ "v" != `echo $VERSION | cut -c1` ] && VERSION="v$VERSION"
  98. [ -z "$NAME" ] && NAME="";
  99. dist=netclient
  100. echo "OS Version = $(uname)"
  101. echo "Netclient Version = $VERSION"
  102. case $(uname | tr '[:upper:]' '[:lower:]') in
  103. linux*)
  104. if [ -z "$CPU_ARCH" ]; then
  105. CPU_ARCH=$(uname -m)
  106. fi
  107. case $CPU_ARCH in
  108. amd64)
  109. dist=netclient
  110. ;;
  111. x86_64)
  112. dist=netclient
  113. ;;
  114. arm64)
  115. dist=netclient-arm64
  116. ;;
  117. aarch64)
  118. dist=netclient-arm64
  119. ;;
  120. armv6l)
  121. dist=netclient-arm6
  122. ;;
  123. armv7l)
  124. dist=netclient-arm7
  125. ;;
  126. arm*)
  127. dist=netclient-$CPU_ARCH
  128. ;;
  129. mipsle)
  130. dist=netclient-mipsle
  131. ;;
  132. *)
  133. fatal "$CPU_ARCH : cpu architecture not supported"
  134. esac
  135. ;;
  136. darwin)
  137. dist=netclient-darwin
  138. ;;
  139. Darwin)
  140. dist=netclient-darwin
  141. ;;
  142. freebsd*)
  143. if [ -z "$CPU_ARCH" ]; then
  144. CPU_ARCH=$(uname -m)
  145. fi
  146. case $CPU_ARCH in
  147. amd64)
  148. dist=netclient-freebsd
  149. ;;
  150. x86_64)
  151. dist=netclient-freebsd
  152. ;;
  153. arm64)
  154. dist=netclient-freebsd-arm64
  155. ;;
  156. aarch64)
  157. dist=netclient-freebsd-arm64
  158. ;;
  159. armv7l)
  160. dist=netclient-freebsd-arm7
  161. ;;
  162. arm*)
  163. dist=netclient-freebsd-$CPU_ARCH
  164. ;;
  165. *)
  166. fatal "$CPU_ARCH : cpu architecture not supported"
  167. esac
  168. ;;
  169. esac
  170. echo "Binary = $dist"
  171. url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
  172. curl_opts='-nv'
  173. if [ "${OS}" = "OpenWRT" ]; then
  174. curl_opts='-q'
  175. fi
  176. if curl --output /dev/null --silent --head --fail "$url"; then
  177. echo "Downloading $dist $VERSION"
  178. wget $curl_opts -O netclient $url
  179. else
  180. echo "Downloading $dist latest"
  181. wget $curl_opts -O netclient https://github.com/gravitl/netmaker/releases/latest/download/$dist
  182. fi
  183. chmod +x netclient
  184. EXTRA_ARGS=""
  185. if [ "${OS}" = "OpenWRT" ]; then
  186. EXTRA_ARGS="--daemon=off"
  187. fi
  188. if [ -z "${NAME}" ]; then
  189. ./netclient join -t $KEY $EXTRA_ARGS
  190. else
  191. ./netclient join -t $KEY --name $NAME $EXTRA_ARGS
  192. fi
  193. if [ "${OS}" = "OpenWRT" ]; then
  194. mv ./netclient /sbin/netclient
  195. cat << 'END_OF_FILE' > ./netclient.service.tmp
  196. #!/bin/sh /etc/rc.common
  197. EXTRA_COMMANDS="status"
  198. EXTRA_HELP=" status Check service is running"
  199. START=99
  200. LOG_FILE="/tmp/netclient.logs"
  201. start() {
  202. if [ ! -f "${LOG_FILE}" ];then
  203. touch "${LOG_FILE}"
  204. fi
  205. local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
  206. if [ "${PID}" ];then
  207. echo "service is running"
  208. return
  209. fi
  210. bash -c "do /sbin/netclient daemon >> ${LOG_FILE} 2>&1;\
  211. if [ $(ls -l ${LOG_FILE}|awk '{print $5}') -gt 10240000 ];then tar zcf "${LOG_FILE}.tar" -C / "tmp/netclient.logs" && > $LOG_FILE;fi;done &"
  212. echo "start"
  213. }
  214. stop() {
  215. pids=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
  216. for i in "${pids[@]}"
  217. do
  218. if [ "${i}" ];then
  219. kill "${i}"
  220. fi
  221. done
  222. echo "stop"
  223. }
  224. status() {
  225. local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
  226. if [ "${PID}" ];then
  227. echo -e "netclient[${PID}] is running \n"
  228. else
  229. echo -e "netclient is not running \n"
  230. fi
  231. }
  232. END_OF_FILE
  233. mv ./netclient.service.tmp /etc/init.d/netclient
  234. chmod +x /etc/init.d/netclient
  235. /etc/init.d/netclient enable
  236. /etc/init.d/netclient start
  237. else
  238. rm -f netclient
  239. fi