netclient-install.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. armv7l)
  120. dist=netclient-arm7
  121. ;;
  122. arm*)
  123. dist=netclient-$CPU_ARCH
  124. ;;
  125. mipsle)
  126. dist=netclient-mipsle
  127. ;;
  128. *)
  129. fatal "$CPU_ARCH : cpu architecture not supported"
  130. esac
  131. ;;
  132. darwin)
  133. dist=netclient-darwin
  134. ;;
  135. freebsd*)
  136. if [ -z "$CPU_ARCH" ]; then
  137. CPU_ARCH=$(uname -m)
  138. fi
  139. case $CPU_ARCH in
  140. amd64)
  141. dist=netclient-freebsd
  142. ;;
  143. x86_64)
  144. dist=netclient-freebsd
  145. ;;
  146. x86_32)
  147. dist=netclient-freebsd-32
  148. ;;
  149. arm64)
  150. dist=netclient-freebsd-arm64
  151. ;;
  152. aarch64)
  153. dist=netclient-freebsd-arm64
  154. ;;
  155. armv7l)
  156. dist=netclient-freebsd-arm7
  157. ;;
  158. arm*)
  159. dist=netclient-freebsd-$CPU_ARCH
  160. ;;
  161. *)
  162. fatal "$CPU_ARCH : cpu architecture not supported"
  163. esac
  164. ;;
  165. esac
  166. echo "Binary = $dist"
  167. url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
  168. curl_opts='-nv'
  169. if [ "${OS}" = "OpenWRT" ]; then
  170. curl_opts='-q'
  171. fi
  172. if curl --output /dev/null --silent --head --fail "$url"; then
  173. echo "Downloading $dist $VERSION"
  174. wget $curl_opts -O netclient $url
  175. else
  176. echo "Downloading $dist latest"
  177. wget $curl_opts -O netclient https://github.com/gravitl/netmaker/releases/download/latest/$dist
  178. fi
  179. chmod +x netclient
  180. EXTRA_ARGS=""
  181. if [ "${OS}" = "FreeBSD" ] || [ "${OS}" = "OpenWRT" ]; then
  182. EXTRA_ARGS="--daemon=off"
  183. fi
  184. if [ -z "${NAME}" ]; then
  185. ./netclient join -t $KEY $EXTRA_ARGS
  186. else
  187. ./netclient join -t $KEY --name $NAME $EXTRA_ARGS
  188. fi
  189. if [ "${OS}" = "FreeBSD" ]; then
  190. mv ./netclient /etc/netclient/netclient
  191. cat << 'END_OF_FILE' > ./netclient.service.tmp
  192. #!/bin/sh
  193. # PROVIDE: netclient
  194. # REQUIRE: LOGIN DAEMON NETWORKING SERVERS FILESYSTEM
  195. # BEFORE:
  196. # KEYWORD: shutdown
  197. . /etc/rc.subr
  198. name="netclient"
  199. rcvar=netclient_enable
  200. pidfile="/var/run/${name}.pid"
  201. command="/usr/sbin/daemon"
  202. command_args="-c -f -P ${pidfile} -R 10 -t "Netclient" -u root -o /etc/netclient/netclient.log /etc/netclient/netclient checkin -n all"
  203. load_rc_config $name
  204. run_rc_command "$1"
  205. END_OF_FILE
  206. sudo mv ./netclient.service.tmp /usr/local/etc/rc.d/netclient
  207. sudo chmod +x /usr/local/etc/rc.d/netclient
  208. sudo /usr/local/etc/rc.d/netclient enable
  209. sudo /usr/local/etc/rc.d/netclient start
  210. elif [ "${OS}" = "OpenWRT" ]; then
  211. mv ./netclient /etc/netclient/netclient
  212. cat << 'END_OF_FILE' > ./netclient.service.tmp
  213. #!/bin/sh /etc/rc.common
  214. EXTRA_COMMANDS="status"
  215. EXTRA_HELP=" status Check service is running"
  216. START=99
  217. LOG_FILE="/tmp/netclient.logs"
  218. start() {
  219. if [ ! -f "${LOG_FILE}" ];then
  220. touch "${LOG_FILE}"
  221. fi
  222. local PID=$(ps|grep "netclient checkin -n all"|grep -v grep|awk '{print $1}')
  223. if [ "${PID}" ];then
  224. echo "service is running"
  225. return
  226. fi
  227. bash -c "while [ 1 ]; do /etc/netclient/netclient checkin -n all >> ${LOG_FILE} 2>&1;sleep 15;\
  228. if [ $(ls -l ${LOG_FILE}|awk '{print $5}') -gt 10240000 ];then tar zcf "${LOG_FILE}.tar" -C / "tmp/netclient.logs" && > $LOG_FILE;fi;done &"
  229. echo "start"
  230. }
  231. stop() {
  232. pids=$(ps|grep "netclient checkin -n all"|grep -v grep|awk '{print $1}')
  233. for i in "${pids[@]}"
  234. do
  235. if [ "${i}" ];then
  236. kill "${i}"
  237. fi
  238. done
  239. echo "stop"
  240. }
  241. status() {
  242. local PID=$(ps|grep "netclient checkin -n all"|grep -v grep|awk '{print $1}')
  243. if [ "${PID}" ];then
  244. echo -e "netclient[${PID}] is running \n"
  245. else
  246. echo -e "netclient is not running \n"
  247. fi
  248. }
  249. END_OF_FILE
  250. mv ./netclient.service.tmp /etc/init.d/netclient
  251. chmod +x /etc/init.d/netclient
  252. /etc/init.d/netclient enable
  253. /etc/init.d/netclient start
  254. else
  255. rm -f netclient
  256. fi