Skip to content

Commit

Permalink
Merge pull request #1147 from HollowMan6/install
Browse files Browse the repository at this point in the history
Change installation path to ~/.kubescape/bin
  • Loading branch information
matthyx committed Mar 24, 2023
2 parents 8a40bab + d2dcd29 commit 5923ce5
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions install.sh
Expand Up @@ -5,10 +5,11 @@ while getopts v: option
do
case ${option} in
v) RELEASE="download/${OPTARG}";;
*) ;;
esac
done

if [ -z ${RELEASE} ]; then
if [ -z "${RELEASE}" ]; then
RELEASE="latest/download"
fi

Expand All @@ -17,7 +18,6 @@ echo

BASE_DIR=~/.kubescape
KUBESCAPE_EXEC=kubescape
KUBESCAPE_ZIP=kubescape.zip

osName=$(uname -s)
if [[ $osName == *"MINGW"* ]]; then
Expand All @@ -35,34 +35,66 @@ DOWNLOAD_URL="https://github.com/kubescape/kubescape/releases/${RELEASE}/kubesca

curl --progress-bar -L $DOWNLOAD_URL -o $OUTPUT

# Checking if SUDO needed/exists
# Find install dir
install_dir=/usr/local/bin # default if running as root
if [ "$(id -u)" -ne 0 ]; then
install_dir=$BASE_DIR/bin # if not running as root, install to user dir
export PATH=$PATH:$BASE_DIR/bin
fi

# Create install dir if it does not exist
if [ ! -d "$install_dir" ]; then
mkdir -p $install_dir
fi

chmod +x $OUTPUT 2>/dev/null

# cleaning up old install
SUDO=
if [ "$(id -u)" -ne 0 ] && [ -n "$(which sudo)" ]; then
if [ "$(id -u)" -ne 0 ] && [ -n "$(which sudo)" ] && [ "$KUBESCAPE_EXEC" != "" ] && [ -f /usr/local/bin/$KUBESCAPE_EXEC ]; then
SUDO=sudo
echo -e "\n\033[33mOld installation as root found, do you want to remove it? [\033[0my\033[33m/n]:"
read -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]] && [[ "$REPLY" != "" ]]; then
echo -e "\n\033[0mSkipping old installation as root removal."
else
echo -e "\n\033[0mWe will need the root access to uninstall the old kubescape CLI."
if $SUDO rm -f /usr/local/bin/$KUBESCAPE_EXEC 2>/dev/null; then
echo -e "\033[32mRemoved old installation as root at /usr/local/bin/$KUBESCAPE_EXEC"
else
echo -e "\033[31mFailed to remove old installation as root at /usr/local/bin/$KUBESCAPE_EXEC, please remove it manually."
fi
fi
fi

if [ "$KUBESCAPE_EXEC" != "" ]; then
if [ "${SUDO_USER:-$USER}" != "" ]; then
rm -f /home/"${SUDO_USER:-$USER}"/.kubescape/bin/$KUBESCAPE_EXEC 2>/dev/null || true
fi
if [ "$BASE_DIR" != "" ]; then
rm -f $BASE_DIR/bin/$KUBESCAPE_EXEC 2>/dev/null || true
fi
fi

# Find install dir
install_dir=/usr/local/bin #default
# Old install location, clean all those things up
for pdir in ${PATH//:/ }; do
edir="${pdir/#\~/$HOME}"
if [[ $edir == $HOME/* ]]; then
install_dir=$edir
mkdir -p $install_dir 2>/dev/null || true
SUDO=
break
if [[ $edir == $HOME/* ]] && [[ -f $edir/$KUBESCAPE_EXEC ]]; then
echo -e "\n\033[33mOld installation found at $edir/$KUBESCAPE_EXEC, do you want to remove it? [\033[0my\033[33m/n]:"
read -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]] && [[ "$REPLY" != "" ]]; then
continue
fi
if rm -f "$edir"/$KUBESCAPE_EXEC 2>/dev/null; then
echo -e "\n\033[32mRemoved old installation at $edir/$KUBESCAPE_EXEC"
else
echo -e "\n\033[31mFailed to remove old installation as root at $edir/$KUBESCAPE_EXEC, please remove it manually."
fi
fi
done

# Create install dir if it does not exist
if [ ! -d "$install_dir" ]; then
$SUDO mkdir -p $install_dir
fi

chmod +x $OUTPUT 2>/dev/null
$SUDO rm -f /usr/local/bin/$KUBESCAPE_EXEC 2>/dev/null || true # clearning up old install
$SUDO cp $OUTPUT $install_dir/$KUBESCAPE_EXEC
rm -rf $OUTPUT
cp $OUTPUT $install_dir/$KUBESCAPE_EXEC
rm -f $OUTPUT

echo
echo -e "\033[32mFinished Installation."
Expand All @@ -73,4 +105,9 @@ echo

echo -e "\033[35mUsage: $ $KUBESCAPE_EXEC scan --enable-host-scan"

if [ "$(id -u)" -ne 0 ]; then
echo -e "\nRemember to add the Kubescape CLI to your path with:"
echo -e " export PATH=\$PATH:$BASE_DIR/bin"
fi

echo -e "\033[0m"

0 comments on commit 5923ce5

Please sign in to comment.