netmaker/scripts/netclient-install.sh

307 lines
7.6 KiB
Bash
Raw Normal View History

2021-10-26 22:58:20 +08:00
#!/bin/sh
2021-03-30 06:12:30 +08:00
2021-10-26 22:58:20 +08:00
if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root"
2021-06-03 12:55:39 +08:00
exit 1
fi
2021-10-26 22:58:20 +08:00
echo "checking dependencies..."
2021-11-16 22:51:37 +08:00
OS=$(uname)
2021-10-26 22:58:20 +08:00
if [ -f /etc/debian_version ]; then
2021-11-22 20:54:01 +08:00
dependencies="wireguard wireguard-tools"
update_cmd='apt update'
2021-10-26 22:58:20 +08:00
install_cmd='apt-get install -y'
elif [ -f /etc/alpine-release ]; then
2021-11-22 20:54:01 +08:00
dependencies="wireguard"
update_cmd='apk update'
2021-10-26 22:58:20 +08:00
install_cmd='apk --update add'
elif [ -f /etc/centos-release ]; then
2021-11-22 20:54:01 +08:00
dependencies="wireguard"
update_cmd='yum update'
2021-10-26 22:58:20 +08:00
install_cmd='yum install -y'
elif [ -f /etc/fedora-release ]; then
2021-11-22 20:54:01 +08:00
dependencies="wireguard"
update_cmd='dnf update'
2021-10-26 22:58:20 +08:00
install_cmd='dnf install -y'
elif [ -f /etc/redhat-release ]; then
dependencies="wireguard"
update_cmd='yum update'
install_cmd='yum install -y'
2022-03-20 21:50:41 +08:00
elif [ -f /etc/arch-release ]; then
dependecies="wireguard-tools"
update_cmd='pacman -Sy'
install_cmd='pacman -S --noconfirm'
2021-11-17 03:18:41 +08:00
elif [ "${OS}" = "FreeBSD" ]; then
2022-06-22 23:52:01 +08:00
dependencies="wireguard wget"
2021-11-22 20:54:01 +08:00
update_cmd='pkg update'
2021-11-16 22:51:37 +08:00
install_cmd='pkg install -y'
2022-10-15 20:41:41 +08:00
elif [ -f /etc/turris-version ]; then
dependencies="wireguard-tools bash"
OS="TurrisOS"
update_cmd='opkg update'
install_cmd='opkg install'
2021-11-22 20:54:01 +08:00
elif [ -f /etc/openwrt_release ]; then
2022-07-14 01:45:13 +08:00
dependencies="wireguard-tools bash"
2021-11-22 20:54:01 +08:00
OS="OpenWRT"
update_cmd='opkg update'
install_cmd='opkg install'
2021-10-26 22:58:20 +08:00
else
install_cmd=''
fi
if [ -z "${install_cmd}" ]; then
echo "OS unsupported for automatic dependency install"
exit 1
fi
2021-11-22 20:54:01 +08:00
2022-07-08 16:53:21 +08:00
${update_cmd}
2021-10-26 22:58:20 +08:00
set -- $dependencies
while [ -n "$1" ]; do
echo $1
2021-11-17 03:18:41 +08:00
if [ "${OS}" = "FreeBSD" ]; then
2021-11-17 09:56:22 +08:00
is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
if [ "$is_installed" != "" ]; then
2021-11-16 22:51:37 +08:00
echo " " $1 is installed
else
echo " " $1 is not installed. Attempting install.
${install_cmd} $1
sleep 5
2021-11-17 09:56:22 +08:00
is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
if [ "$is_installed" != "" ]; then
2021-11-16 22:51:37 +08:00
echo " " $1 is installed
elif [ -x "$(command -v $1)" ]; then
echo " " $1 is installed
else
echo " " FAILED TO INSTALL $1
echo " " This may break functionality.
fi
fi
2021-10-26 22:58:20 +08:00
else
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
2021-11-22 20:54:01 +08:00
is_installed=$(opkg list-installed $1 | grep $1)
else
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
fi
if [ "${is_installed}" != "" ]; then
2021-10-26 22:58:20 +08:00
echo " " $1 is installed
else
2021-11-16 22:51:37 +08:00
echo " " $1 is not installed. Attempting install.
${install_cmd} $1
sleep 5
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
2021-11-22 20:54:01 +08:00
is_installed=$(opkg list-installed $1 | grep $1)
else
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
fi
if [ "${is_installed}" != "" ]; then
2021-11-16 22:51:37 +08:00
echo " " $1 is installed
elif [ -x "$(command -v $1)" ]; then
echo " " $1 is installed
else
echo " " FAILED TO INSTALL $1
echo " " This may break functionality.
fi
2021-10-26 22:58:20 +08:00
fi
fi
shift
done
set -e
2021-03-30 19:57:51 +08:00
[ -z "$KEY" ] && KEY=nokey;
2021-10-20 23:06:54 +08:00
[ -z "$VERSION" ] && echo "no \$VERSION provided, fallback to latest" && VERSION=latest;
[ "latest" != "$VERSION" ] && [ "v" != `echo $VERSION | cut -c1` ] && VERSION="v$VERSION"
[ -z "$NAME" ] && NAME="";
2021-03-30 06:12:30 +08:00
2021-10-20 23:06:54 +08:00
dist=netclient
2021-10-21 00:29:59 +08:00
echo "OS Version = $(uname)"
2021-10-20 23:06:54 +08:00
echo "Netclient Version = $VERSION"
case $(uname | tr A-Z a-z) in
2021-10-26 22:58:20 +08:00
linux*)
if [ -z "$CPU_ARCH" ]; then
CPU_ARCH=$(uname -m)
fi
case $CPU_ARCH in
amd64)
dist=netclient
;;
x86_64)
dist=netclient
;;
arm64)
dist=netclient-arm64
;;
aarch64)
2022-07-08 16:53:21 +08:00
dist=netclient-arm64
2021-10-26 22:58:20 +08:00
;;
armv6l)
2022-07-08 16:53:21 +08:00
dist=netclient-arm6
;;
armv7l)
2022-07-08 16:53:21 +08:00
dist=netclient-arm7
;;
2021-10-26 22:58:20 +08:00
arm*)
dist=netclient-$CPU_ARCH
2021-11-17 23:23:29 +08:00
;;
2022-10-23 15:36:16 +08:00
mipsle)
2022-07-08 16:53:21 +08:00
dist=netclient-mipsle
2022-10-23 15:36:16 +08:00
;;
2022-11-21 21:35:15 +08:00
mips)
#If binary in the below condition is not compatible with your hardware, retry with other netclient-mips* binaries.
if [[ `printf '\0\1' | hexdump -e '/2 "%04x"'` -eq 0100 ]]; then
#Little Endian, tested and confirmed in GL-MT1300 OS "OpenWrt 19.07.8"
dist=netclient-mipsle-softfloat
else
#Big Endian, tested and confirmed in DSL-2750U OS "OpenWrt 22.03.2"
dist=netclient-mips-softfloat
fi
2021-11-17 23:21:35 +08:00
;;
2021-10-26 22:58:20 +08:00
*)
fatal "$CPU_ARCH : cpu architecture not supported"
esac
;;
darwin)
2022-07-08 16:53:21 +08:00
dist=netclient-darwin
2021-10-26 22:58:20 +08:00
;;
Darwin)
2022-07-08 16:53:21 +08:00
dist=netclient-darwin
;;
2021-11-16 22:51:37 +08:00
freebsd*)
if [ -z "$CPU_ARCH" ]; then
CPU_ARCH=$(uname -m)
fi
case $CPU_ARCH in
amd64)
dist=netclient-freebsd
;;
x86_64)
dist=netclient-freebsd
;;
arm64)
dist=netclient-freebsd-arm64
;;
aarch64)
2022-07-08 16:53:21 +08:00
dist=netclient-freebsd-arm64
2021-11-16 22:51:37 +08:00
;;
armv7l)
2022-07-08 16:53:21 +08:00
dist=netclient-freebsd-arm7
2021-11-16 22:51:37 +08:00
;;
arm*)
dist=netclient-freebsd-$CPU_ARCH
2022-07-08 16:53:21 +08:00
;;
2021-11-16 22:51:37 +08:00
*)
fatal "$CPU_ARCH : cpu architecture not supported"
esac
;;
2021-10-26 22:58:20 +08:00
esac
2021-10-20 23:06:54 +08:00
echo "Binary = $dist"
url="https://github.com/gravitl/netmaker/releases/download/$VERSION/$dist"
2021-11-22 20:54:01 +08:00
curl_opts='-nv'
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
2021-11-22 20:54:01 +08:00
curl_opts='-q'
fi
if curl --output /dev/null --silent --head --fail "$url"; then
echo "Downloading $dist $VERSION"
2021-11-22 20:54:01 +08:00
wget $curl_opts -O netclient $url
else
echo "Downloading $dist latest"
wget $curl_opts -O netclient https://github.com/gravitl/netmaker/releases/latest/download/$dist
fi
2021-11-17 09:56:22 +08:00
2021-03-30 06:12:30 +08:00
chmod +x netclient
2021-11-05 21:59:42 +08:00
2021-11-17 03:18:41 +08:00
EXTRA_ARGS=""
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
2021-11-17 09:56:22 +08:00
EXTRA_ARGS="--daemon=off"
2021-11-16 22:51:37 +08:00
fi
2022-06-22 23:57:27 +08:00
if [ "${KEY}" != "nokey" ]; then
if [ -z "${NAME}" ]; then
./netclient join -t $KEY $EXTRA_ARGS
else
./netclient join -t $KEY --name $NAME $EXTRA_ARGS
fi
2021-11-05 21:59:42 +08:00
fi
if [ "${OS}" = "FreeBSD" ]; then
2022-06-23 01:03:04 +08:00
if ! [ -x /usr/sbin/netclient ]; then
echo "Moving netclient executable to \"/usr/sbin/netclient\""
mv netclient /usr/sbin
else
echo "Netclient already present."
fi
fi
2021-11-16 22:51:37 +08:00
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
mv ./netclient /sbin/netclient
2022-10-15 20:41:41 +08:00
if [ "${OS}" = "TurrisOS" ]; then
url="https://raw.githubusercontent.com/gravitl/netmaker/$VERSION/scripts/openwrt-daemon.sh"
if curl --output /dev/null --silent --head --fail $url; then
wget $curl_opts -O netclient.service.tmp $url
else
wget $curl_opts -O netclient.service.tmp https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/openwrt-daemon.sh
fi
2022-10-23 15:36:16 +08:00
elif [ "${OS}" = "OpenWRT" ] && [ "$CPU_ARCH" = "mips" ]; then
wget $curl_opts -O netclient.service.tmp https://raw.githubusercontent.com/gravitl/netmaker/master/scripts/openwrt-daemon.sh
2022-10-15 20:41:41 +08:00
else
cat << 'END_OF_FILE' > ./netclient.service.tmp
2021-11-22 20:54:01 +08:00
#!/bin/sh /etc/rc.common
EXTRA_COMMANDS="status"
EXTRA_HELP=" status Check service is running"
START=99
LOG_FILE="/tmp/netclient.logs"
start() {
if [ ! -f "${LOG_FILE}" ];then
touch "${LOG_FILE}"
fi
local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
2021-11-22 20:54:01 +08:00
if [ "${PID}" ];then
echo "service is running"
return
fi
bash -c "do /sbin/netclient daemon >> ${LOG_FILE} 2>&1;\
2021-11-22 20:54:01 +08:00
if [ $(ls -l ${LOG_FILE}|awk '{print $5}') -gt 10240000 ];then tar zcf "${LOG_FILE}.tar" -C / "tmp/netclient.logs" && > $LOG_FILE;fi;done &"
echo "start"
}
stop() {
pids=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
2021-11-22 20:54:01 +08:00
for i in "${pids[@]}"
do
if [ "${i}" ];then
kill "${i}"
fi
done
echo "stop"
}
status() {
local PID=$(ps|grep "netclient daemon"|grep -v grep|awk '{print $1}')
2021-11-22 20:54:01 +08:00
if [ "${PID}" ];then
echo -e "netclient[${PID}] is running \n"
else
echo -e "netclient is not running \n"
fi
}
END_OF_FILE
2022-10-15 20:41:41 +08:00
fi
2021-11-22 20:54:01 +08:00
mv ./netclient.service.tmp /etc/init.d/netclient
chmod +x /etc/init.d/netclient
/etc/init.d/netclient enable
/etc/init.d/netclient start
else
2021-11-17 09:56:22 +08:00
rm -f netclient
2022-10-23 15:36:16 +08:00
fi