netmaker/scripts/netclient-install.sh

290 lines
6.8 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'
2021-11-17 03:18:41 +08:00
elif [ "${OS}" = "FreeBSD" ]; then
2021-11-22 20:54:01 +08:00
dependencies="wireguard"
update_cmd='pkg update'
2021-11-16 22:51:37 +08:00
install_cmd='pkg install -y'
2021-11-22 20:54:01 +08:00
elif [ -f /etc/openwrt_release ]; then
dependencies="wireguard-tools"
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
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
2021-11-22 20:54:01 +08:00
if [ "${OS}" = "OpenWRT" ]; then
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
2021-11-22 20:54:01 +08:00
if [ "${OS}" = "OpenWRT" ]; then
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"
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-arm7
;;
2021-10-26 22:58:20 +08:00
arm*)
dist=netclient-$CPU_ARCH
2021-11-17 23:23:29 +08:00
;;
2021-11-17 23:21:35 +08:00
mipsle)
dist=netclient-mipsle
;;
2021-10-26 22:58:20 +08:00
*)
fatal "$CPU_ARCH : cpu architecture not supported"
esac
;;
darwin)
dist=netclient-darwin
;;
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)
2021-11-23 21:04:37 +08:00
dist=netclient-freebsd-arm7
2021-11-16 22:51:37 +08:00
;;
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"
2021-11-22 20:54:01 +08:00
curl_opts='-nv'
if [ "${OS}" = "OpenWRT" ]; then
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"
2021-11-22 20:54:01 +08:00
wget $curl_opts -O netclient https://github.com/gravitl/netmaker/releases/download/latest/$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=""
2021-11-22 20:54:01 +08:00
if [ "${OS}" = "FreeBSD" ] || [ "${OS}" = "OpenWRT" ]; then
2021-11-17 09:56:22 +08:00
EXTRA_ARGS="--daemon=off"
2021-11-16 22:51:37 +08:00
fi
2021-11-05 21:59:42 +08:00
if [ -z "${NAME}" ]; then
2021-11-17 09:56:22 +08:00
./netclient join -t $KEY $EXTRA_ARGS
2021-11-05 21:59:42 +08:00
else
2021-11-17 09:56:22 +08:00
./netclient join -t $KEY --name $NAME $EXTRA_ARGS
2021-11-05 21:59:42 +08:00
fi
2021-11-17 03:18:41 +08:00
if [ "${OS}" = "FreeBSD" ]; then
2021-11-17 09:56:22 +08:00
mv ./netclient /etc/netclient/netclient
cat << 'END_OF_FILE' > ./netclient.service.tmp
2021-11-16 22:51:37 +08:00
#!/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"
2021-11-17 09:56:22 +08:00
END_OF_FILE
sudo mv ./netclient.service.tmp /usr/local/etc/rc.d/netclient
sudo chmod +x /usr/local/etc/rc.d/netclient
sudo /usr/local/etc/rc.d/netclient enable
sudo /usr/local/etc/rc.d/netclient start
2021-11-22 20:54:01 +08:00
elif [ "${OS}" = "OpenWRT" ]; then
mv ./netclient /etc/netclient/netclient
cat << 'END_OF_FILE' > ./netclient.service.tmp
#!/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 checkin -n all"|grep -v grep|awk '{print $1}')
if [ "${PID}" ];then
echo "service is running"
return
fi
bash -c "while [ 1 ]; do /etc/netclient/netclient checkin -n all >> ${LOG_FILE} 2>&1;sleep 15;\
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 checkin -n all"|grep -v grep|awk '{print $1}')
for i in "${pids[@]}"
do
if [ "${i}" ];then
kill "${i}"
fi
done
echo "stop"
}
status() {
local PID=$(ps|grep "netclient checkin -n all"|grep -v grep|awk '{print $1}')
if [ "${PID}" ];then
echo -e "netclient[${PID}] is running \n"
else
echo -e "netclient is not running \n"
fi
}
END_OF_FILE
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
2021-11-17 03:18:41 +08:00
fi
2021-11-22 20:54:01 +08:00