Add auto-update feature in netbird script for binary installation (#1106)

This pull request addresses the need to enhance the installer script by introducing a new parameter --update to trigger updates. The goal is to streamline the update process for binary installations and provide a better experience for users.
This commit is contained in:
Bethuel Mmbaga 2023-08-28 17:21:04 +03:00 committed by GitHub
parent ac0b7dc8cb
commit 80d9b5fca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

159
release_files/install.sh Normal file → Executable file
View File

@ -3,6 +3,9 @@
# Source: https://github.com/physk/netbird-installer # Source: https://github.com/physk/netbird-installer
set -e set -e
CONFIG_FOLDER="/etc/netbird"
CONFIG_FILE="$CONFIG_FOLDER/install.conf"
OWNER="netbirdio" OWNER="netbirdio"
REPO="netbird" REPO="netbird"
CLI_APP="netbird" CLI_APP="netbird"
@ -12,7 +15,7 @@ UI_APP="netbird-ui"
OS_NAME="" OS_NAME=""
OS_TYPE="" OS_TYPE=""
ARCH="$(uname -m)" ARCH="$(uname -m)"
PACKAGE_MANAGER="" PACKAGE_MANAGER="bin"
INSTALL_DIR="" INSTALL_DIR=""
get_latest_release() { get_latest_release() {
@ -25,7 +28,7 @@ download_release_binary() {
BASE_URL="https://github.com/${OWNER}/${REPO}/releases/download" BASE_URL="https://github.com/${OWNER}/${REPO}/releases/download"
BINARY_BASE_NAME="${VERSION#v}_${OS_TYPE}_${ARCH}.tar.gz" BINARY_BASE_NAME="${VERSION#v}_${OS_TYPE}_${ARCH}.tar.gz"
# for Darwin, download the signed Netbird-UI # for Darwin, download the signed NetBird-UI
if [ "$OS_TYPE" = "darwin" ] && [ "$1" = "$UI_APP" ]; then if [ "$OS_TYPE" = "darwin" ] && [ "$1" = "$UI_APP" ]; then
BINARY_BASE_NAME="${VERSION#v}_${OS_TYPE}_${ARCH}_signed.zip" BINARY_BASE_NAME="${VERSION#v}_${OS_TYPE}_${ARCH}_signed.zip"
fi fi
@ -42,12 +45,12 @@ download_release_binary() {
DOWNLOAD_URL="${BASE_URL}/${VERSION}/${BINARY_NAME}" DOWNLOAD_URL="${BASE_URL}/${VERSION}/${BINARY_NAME}"
echo "Installing $1 from $DOWNLOAD_URL" echo "Installing $1 from $DOWNLOAD_URL"
cd /tmp && curl -LO "$DOWNLOAD_URL" cd /tmp && curl -LO "$DOWNLOAD_URL"
if [ "$OS_TYPE" = "darwin" ] && [ "$1" = "$UI_APP" ]; then if [ "$OS_TYPE" = "darwin" ] && [ "$1" = "$UI_APP" ]; then
INSTALL_DIR="/Applications/NetBird UI.app" INSTALL_DIR="/Applications/NetBird UI.app"
# Unzip the app and move to INSTALL_DIR # Unzip the app and move to INSTALL_DIR
unzip -q -o "$BINARY_NAME" unzip -q -o "$BINARY_NAME"
mv "netbird_ui_${OS_TYPE}_${ARCH}" "$INSTALL_DIR" mv "netbird_ui_${OS_TYPE}_${ARCH}" "$INSTALL_DIR"
@ -61,7 +64,7 @@ download_release_binary() {
add_apt_repo() { add_apt_repo() {
sudo apt-get update sudo apt-get update
sudo apt-get install ca-certificates gnupg -y sudo apt-get install ca-certificates gnupg -y
curl -sSL https://pkgs.wiretrustee.com/debian/public.key \ curl -sSL https://pkgs.wiretrustee.com/debian/public.key \
| sudo gpg --dearmor --output /usr/share/keyrings/wiretrustee-archive-keyring.gpg | sudo gpg --dearmor --output /usr/share/keyrings/wiretrustee-archive-keyring.gpg
@ -73,15 +76,15 @@ add_apt_repo() {
add_rpm_repo() { add_rpm_repo() {
cat <<-EOF | sudo tee /etc/yum.repos.d/netbird.repo cat <<-EOF | sudo tee /etc/yum.repos.d/netbird.repo
[Netbird] [NetBird]
name=Netbird name=NetBird
baseurl=https://pkgs.netbird.io/yum/ baseurl=https://pkgs.netbird.io/yum/
enabled=1 enabled=1
gpgcheck=0 gpgcheck=0
gpgkey=https://pkgs.netbird.io/yum/repodata/repomd.xml.key gpgkey=https://pkgs.netbird.io/yum/repodata/repomd.xml.key
repo_gpgcheck=1 repo_gpgcheck=1
EOF EOF
} }
add_aur_repo() { add_aur_repo() {
INSTALL_PKGS="git base-devel go" INSTALL_PKGS="git base-devel go"
@ -99,10 +102,10 @@ add_aur_repo() {
done done
# Build package from AUR # Build package from AUR
cd /tmp && git clone https://aur.archlinux.org/netbird.git cd /tmp && git clone https://aur.archlinux.org/netbird.git
cd netbird && makepkg -sri --noconfirm cd netbird && makepkg -sri --noconfirm
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
cd /tmp && git clone https://aur.archlinux.org/netbird-ui.git cd /tmp && git clone https://aur.archlinux.org/netbird-ui.git
cd netbird-ui && makepkg -sri --noconfirm cd netbird-ui && makepkg -sri --noconfirm
fi fi
@ -131,9 +134,9 @@ install_native_binaries() {
# download and copy binaries to INSTALL_DIR # download and copy binaries to INSTALL_DIR
download_release_binary "$CLI_APP" download_release_binary "$CLI_APP"
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
download_release_binary "$UI_APP" download_release_binary "$UI_APP"
fi fi
} }
check_use_bin_variable() { check_use_bin_variable() {
@ -145,23 +148,26 @@ check_use_bin_variable() {
} }
install_netbird() { install_netbird() {
# Check if netbird CLI is installed
if [ -x "$(command -v netbird)" ]; then if [ -x "$(command -v netbird)" ]; then
if netbird status > /dev/null 2>&1; then status_output=$(netbird status)
echo "Netbird service is running, please stop it before proceeding" if echo "$status_output" | grep -q 'Management: Connected' && echo "$status_output" | grep -q 'Signal: Connected'; then
echo "NetBird service is running, please stop it before proceeding"
exit 1
fi fi
echo "Netbird seems to be installed already, please remove it before proceeding" if [ -n "$status_output" ]; then
exit 1 echo "NetBird seems to be installed already, please remove it before proceeding"
exit 1
fi
fi fi
# Checks if SKIP_UI_APP env is set # Checks if SKIP_UI_APP env is set
if [ -z "$SKIP_UI_APP" ]; then if [ -z "$SKIP_UI_APP" ]; then
SKIP_UI_APP=false SKIP_UI_APP=false
else else
if $SKIP_UI_APP; then if $SKIP_UI_APP; then
echo "SKIP_UI_APP has been set to true in the environment" echo "SKIP_UI_APP has been set to true in the environment"
echo "Netbird UI installation will be omitted based on your preference" echo "NetBird UI installation will be omitted based on your preference"
fi fi
fi fi
@ -169,21 +175,21 @@ install_netbird() {
if type uname >/dev/null 2>&1; then if type uname >/dev/null 2>&1; then
case "$(uname)" in case "$(uname)" in
Linux) Linux)
OS_NAME="$(. /etc/os-release && echo "$ID")" OS_NAME="$(. /etc/os-release && echo "$ID")"
OS_TYPE="linux" OS_TYPE="linux"
INSTALL_DIR="/usr/bin" INSTALL_DIR="/usr/bin"
# Allow netbird UI installation for x64 arch only # Allow netbird UI installation for x64 arch only
if [ "$ARCH" != "amd64" ] && [ "$ARCH" != "arm64" ] \ if [ "$ARCH" != "amd64" ] && [ "$ARCH" != "arm64" ] \
&& [ "$ARCH" != "x86_64" ];then && [ "$ARCH" != "x86_64" ];then
SKIP_UI_APP=true SKIP_UI_APP=true
echo "Netbird UI installation will be omitted as $ARCH is not a compactible architecture" echo "NetBird UI installation will be omitted as $ARCH is not a compactible architecture"
fi fi
# Allow netbird UI installation for linux running desktop enviroment # Allow netbird UI installation for linux running desktop enviroment
if [ -z "$XDG_CURRENT_DESKTOP" ];then if [ -z "$XDG_CURRENT_DESKTOP" ];then
SKIP_UI_APP=true SKIP_UI_APP=true
echo "Netbird UI installation will be omitted as Linux does not run desktop environment" echo "NetBird UI installation will be omitted as Linux does not run desktop environment"
fi fi
# Check the availability of a compatible package manager # Check the availability of a compatible package manager
@ -207,7 +213,7 @@ install_netbird() {
OS_NAME="macos" OS_NAME="macos"
OS_TYPE="darwin" OS_TYPE="darwin"
INSTALL_DIR="/usr/local/bin" INSTALL_DIR="/usr/local/bin"
# Check the availability of a compatible package manager # Check the availability of a compatible package manager
if check_use_bin_variable; then if check_use_bin_variable; then
PACKAGE_MANAGER="bin" PACKAGE_MANAGER="bin"
@ -225,15 +231,15 @@ install_netbird() {
apt) apt)
add_apt_repo add_apt_repo
sudo apt-get install netbird -y sudo apt-get install netbird -y
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
sudo apt-get install netbird-ui -y sudo apt-get install netbird-ui -y
fi fi
;; ;;
yum) yum)
add_rpm_repo add_rpm_repo
sudo yum -y install netbird sudo yum -y install netbird
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
sudo yum -y install netbird-ui sudo yum -y install netbird-ui
fi fi
;; ;;
@ -243,7 +249,7 @@ install_netbird() {
sudo dnf config-manager --add-repo /etc/yum.repos.d/netbird.repo sudo dnf config-manager --add-repo /etc/yum.repos.d/netbird.repo
sudo dnf -y install netbird sudo dnf -y install netbird
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
sudo dnf -y install netbird-ui sudo dnf -y install netbird-ui
fi fi
;; ;;
@ -255,46 +261,50 @@ install_netbird() {
# Remove Wiretrustee if it had been installed using Homebrew before # Remove Wiretrustee if it had been installed using Homebrew before
if brew ls --versions wiretrustee >/dev/null 2>&1; then if brew ls --versions wiretrustee >/dev/null 2>&1; then
echo "Removing existing wiretrustee client" echo "Removing existing wiretrustee client"
# Stop and uninstall daemon service: # Stop and uninstall daemon service:
wiretrustee service stop wiretrustee service stop
wiretrustee service uninstall wiretrustee service uninstall
# Unlik the app # Unlik the app
brew unlink wiretrustee brew unlink wiretrustee
fi fi
brew install netbirdio/tap/netbird brew install netbirdio/tap/netbird
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
brew install --cask netbirdio/tap/netbird-ui brew install --cask netbirdio/tap/netbird-ui
fi fi
;; ;;
*) *)
if [ "$OS_NAME" = "nixos" ];then if [ "$OS_NAME" = "nixos" ];then
echo "Please add Netbird to your NixOS configuration.nix directly:" echo "Please add NetBird to your NixOS configuration.nix directly:"
echo echo ""
echo "services.netbird.enable = true;" echo "services.netbird.enable = true;"
if ! $SKIP_UI_APP; then if ! $SKIP_UI_APP; then
echo "environment.systemPackages = [ pkgs.netbird-ui ];" echo "environment.systemPackages = [ pkgs.netbird-ui ];"
fi fi
echo "Build and apply new configuration:" echo "Build and apply new configuration:"
echo echo ""
echo "sudo nixos-rebuild switch" echo "sudo nixos-rebuild switch"
exit 0 exit 0
fi fi
install_native_binaries install_native_binaries
;; ;;
esac esac
# Add package manager to config
sudo mkdir -p "$CONFIG_FOLDER"
echo "package_manager=$PACKAGE_MANAGER" | sudo tee "$CONFIG_FILE" > /dev/null
# Load and start netbird service # Load and start netbird service
if ! sudo netbird service install 2>&1; then if ! sudo netbird service install 2>&1; then
echo "Netbird service has already been loaded" echo "NetBird service has already been loaded"
fi fi
if ! sudo netbird service start 2>&1; then if ! sudo netbird service start 2>&1; then
echo "Netbird service has already been started" echo "NetBird service has already been started"
fi fi
@ -303,4 +313,51 @@ install_netbird() {
echo "sudo netbird up" echo "sudo netbird up"
} }
install_netbird version_greater_equal() {
printf '%s\n%s\n' "$2" "$1" | sort -V -C
}
is_bin_package_manager() {
if sudo test -f "$1" && sudo grep -q "package_manager=bin" "$1" ; then
return 0
else
return 1
fi
}
update_netbird() {
if is_bin_package_manager "$CONFIG_FILE"; then
latest_release=$(get_latest_release)
latest_version=${latest_release#v}
installed_version=$(netbird version)
if [ "$latest_version" = "$installed_version" ]; then
echo "Installed netbird version ($installed_version) is up-to-date"
exit 0
fi
if version_greater_equal "$latest_version" "$installed_version"; then
echo "NetBird new version ($latest_version) available. Updating..."
echo ""
echo "Initiating NetBird update. This will stop the netbird service and restart it after the update"
sudo netbird service stop
sudo netbird service uninstall
install_native_binaries
sudo netbird service install
sudo netbird service start
fi
else
echo "NetBird installation was done using a package manager. Please use your system's package manager to update"
fi
}
case "$1" in
--update)
update_netbird
;;
*)
install_netbird
esac