netmaker/scripts/netclient-install.sh

200 lines
4.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
install_cmd='apt-get install -y'
elif [ -f /etc/alpine-release ]; then
install_cmd='apk --update add'
elif [ -f /etc/centos-release ]; then
install_cmd='yum install -y'
elif [ -f /etc/fedora-release ]; then
install_cmd='dnf install -y'
2021-11-16 22:51:37 +08:00
elif [ OS == "FreeBSD" ]; then
install_cmd='pkg install -y'
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-10-29 05:59:54 +08:00
dependencies="wireguard"
2021-10-26 22:58:20 +08:00
set -- $dependencies
while [ -n "$1" ]; do
echo $1
2021-11-16 22:51:37 +08:00
if [ OS == "FreeBSD" ]; then
is_installed=$(pkg check -d $1 | grep '100%')
if [ "${is_installed}" = '100%' ]; then
echo " " $1 is installed
else
echo " " $1 is not installed. Attempting install.
${install_cmd} $1
sleep 5
is_installed=$(pkg check -d $1 | grep '100%')
if [ "${is_installed}" = '100%' ]; then
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
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
2021-11-16 22:51:37 +08:00
if [ "${is_installed}" = "install ok 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
is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
if [ "${is_installed}" = "install ok installed" ]; then
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"
2021-10-26 22:58:20 +08:00
case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
if [ -z "$CPU_ARCH" ]; then
CPU_ARCH=$(uname -m)
fi
case $CPU_ARCH in
amd64)
dist=netclient
;;
x86_64)
dist=netclient
;;
x86_32)
dist=netclient-32
;;
arm64)
dist=netclient-arm64
;;
aarch64)
dist=netclient-arm64
;;
armv7l)
dist=netclient-armv7
;;
2021-10-26 22:58:20 +08:00
arm*)
dist=netclient-$CPU_ARCH
;;
*)
fatal "$CPU_ARCH : cpu architecture not supported"
esac
;;
darwin)
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
;;
x86_32)
dist=netclient-freebsd-32
;;
arm64)
dist=netclient-freebsd-arm64
;;
aarch64)
dist=netclient-freebsd-arm64
;;
armv7l)
dist=netclient-freebsd-armv7
;;
arm*)
dist=netclient-freebsd-$CPU_ARCH
;;
*)
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"
if curl --output /dev/null --silent --head --fail "$url"; then
echo "Downloading $dist $VERSION"
wget -nv -O netclient $url
else
echo "Downloading $dist latest"
wget -nv -O netclient https://github.com/gravitl/netmaker/releases/download/latest/$dist
fi
2021-03-30 06:12:30 +08:00
chmod +x netclient
2021-11-05 21:59:42 +08:00
2021-11-16 22:51:37 +08:00
EXTRA_ARGS = ""
if [ OS == "FreeBSD" ]; then
EXTRA_ARGS = "--daemon=off"
fi
2021-11-05 21:59:42 +08:00
if [ -z "${NAME}" ]; then
2021-11-16 22:51:37 +08:00
sudo ./netclient join -t $KEY EXTRA_ARGS
2021-11-05 21:59:42 +08:00
else
2021-11-16 22:51:37 +08:00
sudo ./netclient join -t $KEY --name $NAME EXTRA_ARGS
2021-11-05 21:59:42 +08:00
fi
2021-03-30 06:12:30 +08:00
rm -f netclient
2021-11-16 22:51:37 +08:00
if [ OS == "FreeBSD" ]; then
tee /usr/local/etc/rc.d/netclient <<'EOF' >/dev/null
#!/bin/sh
# PROVIDE: netclient
# REQUIRE: LOGIN DAEMON NETWORKING SERVERS FILESYSTEM
# BEFORE:
# KEYWORD: shutdown
. /etc/rc.subr
name="netclient"
rcvar=netclient_enable
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-c -f -P ${pidfile} -R 10 -t "Netclient" -u root -o /etc/netclient/netclient.log /etc/netclient/netclient checkin -n all"
load_rc_config $name
run_rc_command "$1"
EOF
/usr/local/etc/rc.d/netclient enable
/usr/local/etc/rc.d/netclient start
fi