From c81b83b3468034d2d757ee889cc693acc6ea3b7a Mon Sep 17 00:00:00 2001 From: Bethuel Mmbaga Date: Fri, 29 Sep 2023 00:58:04 +0300 Subject: [PATCH] Enhance compatibility of install.sh for systems without sudo (#1176) This commit modifies the install.sh script to improve compatibility with systems lacking the sudo command. A conditional check is added at the beginning of the script to see if the sudo command exists. If it does, operations in the script that previously required sudo would proceed as normal, using the sudo command. If the system does not have sudo, the shell would execute these operations without it. This change enhances the usability of this script in restricted environments where sudo is not installed or available to users. --- release_files/install.sh | 67 +++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/release_files/install.sh b/release_files/install.sh index 2ba0495fd..c553cc28a 100755 --- a/release_files/install.sh +++ b/release_files/install.sh @@ -1,4 +1,3 @@ -#!/bin/sh # This code is based on the netbird-installer contribution by physk on GitHub. # Source: https://github.com/physk/netbird-installer set -e @@ -17,6 +16,12 @@ OS_TYPE="" ARCH="$(uname -m)" PACKAGE_MANAGER="bin" INSTALL_DIR="" +SUDO="" + + +if command -v sudo > /dev/null && [ "$(id -u)" -ne 0 ]; then + SUDO="sudo" +fi get_latest_release() { if [ -n "$GITHUB_TOKEN" ]; then @@ -65,18 +70,18 @@ download_release_binary() { unzip -q -o "$BINARY_NAME" mv "netbird_ui_${OS_TYPE}_${ARCH}" "$INSTALL_DIR" else - sudo mkdir -p "$INSTALL_DIR" + ${SUDO} mkdir -p "$INSTALL_DIR" tar -xzvf "$BINARY_NAME" - sudo mv "${1%_"${BINARY_BASE_NAME}"}" "$INSTALL_DIR/" + ${SUDO} mv "${1%_"${BINARY_BASE_NAME}"}" "$INSTALL_DIR/" fi } add_apt_repo() { - sudo apt-get update - sudo apt-get install ca-certificates curl gnupg -y + ${SUDO} apt-get update + ${SUDO} apt-get install ca-certificates curl gnupg -y # Remove old keys and repo source files - sudo rm -f \ + ${SUDO} rm -f \ /etc/apt/sources.list.d/netbird.list \ /etc/apt/sources.list.d/wiretrustee.list \ /etc/apt/trusted.gpg.d/wiretrustee.gpg \ @@ -84,16 +89,16 @@ add_apt_repo() { /usr/share/keyrings/wiretrustee-archive-keyring.gpg curl -sSL https://pkgs.netbird.io/debian/public.key \ - | sudo gpg --dearmor -o /usr/share/keyrings/netbird-archive-keyring.gpg + | ${SUDO} gpg --dearmor -o /usr/share/keyrings/netbird-archive-keyring.gpg echo 'deb [signed-by=/usr/share/keyrings/netbird-archive-keyring.gpg] https://pkgs.netbird.io/debian stable main' \ - | sudo tee /etc/apt/sources.list.d/netbird.list + | ${SUDO} tee /etc/apt/sources.list.d/netbird.list - sudo apt-get update + ${SUDO} apt-get update } add_rpm_repo() { -cat <<-EOF | sudo tee /etc/yum.repos.d/netbird.repo +cat <<-EOF | ${SUDO} tee /etc/yum.repos.d/netbird.repo [NetBird] name=NetBird baseurl=https://pkgs.netbird.io/yum/ @@ -112,7 +117,7 @@ add_aur_repo() { for PKG in $INSTALL_PKGS; do if ! pacman -Q "$PKG" > /dev/null 2>&1; then # Install missing package(s) - sudo pacman -S "$PKG" --noconfirm + ${SUDO} pacman -S "$PKG" --noconfirm # Add installed package for clean up later REMOVE_PKGS="$REMOVE_PKGS $PKG" @@ -129,7 +134,7 @@ add_aur_repo() { fi # Clean up the installed packages - sudo pacman -Rs "$REMOVE_PKGS" --noconfirm + ${SUDO} pacman -Rs "$REMOVE_PKGS" --noconfirm } install_native_binaries() { @@ -194,31 +199,31 @@ install_netbird() { case "$PACKAGE_MANAGER" in apt) add_apt_repo - sudo apt-get install netbird -y + ${SUDO} apt-get install netbird -y if ! $SKIP_UI_APP; then - sudo apt-get install netbird-ui -y + ${SUDO} apt-get install netbird-ui -y fi ;; yum) add_rpm_repo - sudo yum -y install netbird + ${SUDO} yum -y install netbird if ! $SKIP_UI_APP; then - sudo yum -y install netbird-ui + ${SUDO} yum -y install netbird-ui fi ;; dnf) add_rpm_repo - sudo dnf -y install dnf-plugin-config-manager - sudo dnf config-manager --add-repo /etc/yum.repos.d/netbird.repo - sudo dnf -y install netbird + ${SUDO} dnf -y install dnf-plugin-config-manager + ${SUDO} dnf config-manager --add-repo /etc/yum.repos.d/netbird.repo + ${SUDO} dnf -y install netbird if ! $SKIP_UI_APP; then - sudo dnf -y install netbird-ui + ${SUDO} dnf -y install netbird-ui fi ;; pacman) - sudo pacman -Syy + ${SUDO} pacman -Syy add_aur_repo ;; brew) @@ -251,7 +256,7 @@ install_netbird() { echo "Build and apply new configuration:" echo "" - echo "sudo nixos-rebuild switch" + echo "${SUDO} nixos-rebuild switch" exit 0 fi @@ -260,14 +265,14 @@ install_netbird() { esac # Add package manager to config - sudo mkdir -p "$CONFIG_FOLDER" - echo "package_manager=$PACKAGE_MANAGER" | sudo tee "$CONFIG_FILE" > /dev/null + ${SUDO} mkdir -p "$CONFIG_FOLDER" + echo "package_manager=$PACKAGE_MANAGER" | ${SUDO} tee "$CONFIG_FILE" > /dev/null # 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" fi - if ! sudo netbird service start 2>&1; then + if ! ${SUDO} netbird service start 2>&1; then echo "NetBird service has already been started" fi @@ -282,7 +287,7 @@ version_greater_equal() { } is_bin_package_manager() { - if sudo test -f "$1" && sudo grep -q "package_manager=bin" "$1" ; then + if ${SUDO} test -f "$1" && ${SUDO} grep -q "package_manager=bin" "$1" ; then return 0 else return 1 @@ -305,12 +310,12 @@ update_netbird() { 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 + ${SUDO} netbird service stop + ${SUDO} netbird service uninstall install_native_binaries - sudo netbird service install - sudo netbird service start + ${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"