netclient-install.sh 6.3 KB

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