netclient-install.sh 6.1 KB

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