netclient-install.sh 6.9 KB

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