Skip to content

Commit

Permalink
bat, sh files are made as method driven
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagarjun Sanji committed Jul 9, 2024
1 parent c362b8d commit d85123a
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 147 deletions.
170 changes: 100 additions & 70 deletions resources/debricked-cli/install-debricked.bat
Original file line number Diff line number Diff line change
@@ -1,91 +1,121 @@
@echo off
setlocal enabledelayedexpansion

:: Check for admin rights
net session >nul 2>&1
if %errorLevel% neq 0 (
echo This script needs to be run as an administrator.
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c %~dpnx0'"
exit /b
)
:: Set log file path
set "logFile=%TEMP%\debricked_install.log"

:: Define release version
set "releaseVersion=v2.0.3"
:: Start logging
echo Debricked CLI Installation started at %date% %time% > "%logFile%"

:: Determine the architecture
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set "arch=x86_64"
) else (
set "arch=i386"
)
:: Check for admin rights
call :check_admin || exit /b

:: Define variables
set "releaseVersion=v2.0.3"
call :set_architecture
set "downloadUrl=https://github.com/debricked/cli/releases/download/%releaseVersion%/cli_windows_%arch%.tar.gz"
set "destinationPath=%~dp0debricked-cli.tar.gz"
set "extractPath=%~dp0cli"
set "installPath=C:\Program Files\debricked"

:: Download the file
echo Downloading Debricked CLI from %downloadUrl%
powershell -Command "Invoke-WebRequest -Uri '%downloadUrl%' -OutFile '%destinationPath%' -UseBasicParsing; if ($?) { exit 0 } else { exit 1 }"
if %errorLevel% neq 0 (
echo Failed to download Debricked CLI
pause
)
:: Main installation process
call :download_cli || exit /b
call :extract_cli || exit /b
call :install_cli || exit /b
call :update_path || exit /b
call :cleanup || exit /b

:: Create the extract path if it doesn't exist
if not exist "%extractPath%" (
mkdir "%extractPath%" || (
echo Failed to create extract path
pause
)
)

:: Extract the tar.gz file
echo Extracting Debricked CLI to %extractPath% ...
tar -xzf "%destinationPath%" -C "%extractPath%" || (
echo Failed to extract Debricked CLI
pause
)
echo Debricked(%releaseVersion%) CLI installation completed successfully.
echo See %logFile% for details.
pause
exit /b

:: Remove the tar.gz file
del "%destinationPath%" || (
echo Failed to delete tar.gz file
pause
)
:: Functions
:check_admin
net session >nul 2>&1
if %errorLevel% neq 0 (
echo This script needs to be run as an administrator.
echo ERROR: Script not run as administrator. >> "%logFile%"
powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c %~dpnx0'"
exit /b 1
)
echo Admin rights confirmed. >> "%logFile%"
exit /b 0

:: Create the install path if it doesn't exist
if not exist "%installPath%" (
mkdir "%installPath%" || (
echo Failed to create install path
pause
:set_architecture
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set "arch=x86_64"
) else (
set "arch=i386"
)
)
echo Architecture set to %arch%. >> "%logFile%"
exit /b 0

:: Copy the extracted debricked.exe to the install path
echo Installing Debricked CLI to %installPath% ...
copy "%extractPath%\debricked.exe" "%installPath%\debricked.exe" /Y || (
echo Failed to copy Debricked CLI to install path
pause
)
:download_cli
echo Downloading Debricked CLI from %downloadUrl%
echo Downloading from %downloadUrl% >> "%logFile%"
powershell -Command "Invoke-WebRequest -Uri '%downloadUrl%' -OutFile '%destinationPath%' -UseBasicParsing; if ($?) { exit 0 } else { exit 1 }"
if %errorLevel% neq 0 (
echo Failed to download Debricked CLI
echo ERROR: Download failed. >> "%logFile%"
exit /b 1
)
echo Download successful. >> "%logFile%"
exit /b 0

:: Add the install path to the system PATH if not already present
echo Adding Debricked CLI to system PATH...
for /f "tokens=2*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "currentPath=%%B"
echo %currentPath% | find /i "%installPath%" > nul
if errorlevel 1 (
setx /M PATH "%currentPath%;%installPath%" || (
echo Failed to add Debricked CLI to system PATH
pause
:extract_cli
if not exist "%extractPath%" mkdir "%extractPath%" || (
echo ERROR: Failed to create extract path. >> "%logFile%"
exit /b 1
)
echo Extracting Debricked CLI to %extractPath% ...
echo Extracting to %extractPath% >> "%logFile%"
tar -xzf "%destinationPath%" -C "%extractPath%" || (
echo ERROR: Extraction failed. >> "%logFile%"
exit /b 1
)
del "%destinationPath%" || (
echo ERROR: Failed to delete downloaded file. >> "%logFile%"
exit /b 1
)
)
echo Extraction successful. >> "%logFile%"
exit /b 0

:: Clean up the extraction path
rmdir /s /q "%extractPath%" || (
echo Failed to clean up extraction path
pause
)
:install_cli
if not exist "%installPath%" mkdir "%installPath%" || (
echo ERROR: Failed to create install path. >> "%logFile%"
exit /b 1
)
echo Installing Debricked CLI to %installPath% ...
echo Installing to %installPath% >> "%logFile%"
copy "%extractPath%\debricked.exe" "%installPath%\debricked.exe" /Y || (
echo ERROR: Failed to copy executable. >> "%logFile%"
exit /b 1
)
echo Installation successful. >> "%logFile%"
exit /b 0

echo Debricked(%releaseVersion%) CLI installation completed successfully.
:update_path
echo Adding Debricked CLI to system PATH...
echo Updating system PATH >> "%logFile%"
for /f "tokens=2*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "currentPath=%%B"
echo %currentPath% | find /i "%installPath%" > nul
if errorlevel 1 (
setx /M PATH "%currentPath%;%installPath%" || (
echo ERROR: Failed to update PATH. >> "%logFile%"
exit /b 1
)
echo PATH updated successfully. >> "%logFile%"
) else (
echo PATH already contains install directory. >> "%logFile%"
)
exit /b 0

:: Exit the terminal
pause
:cleanup
rmdir /s /q "%extractPath%" || (
echo ERROR: Failed to clean up extract path. >> "%logFile%"
exit /b 1
)
echo Cleanup successful. >> "%logFile%"
echo Debricked(%releaseVersion%) CLI installation completed successfully. >> "%logFile%"
exit /b 0
136 changes: 100 additions & 36 deletions resources/debricked-cli/install-debricked.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,113 @@
#!/bin/bash

# Set log file path
logFile="/tmp/debricked_install.log"

# Start logging
echo "Debricked CLI Installation started at $(date)" > "$logFile"

# Define release version
releaseVersion="v2.0.3"

# Determine the OS and architecture
os=""
arch=""
# Function to check for root privileges
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "This script needs to be run as root."
echo "ERROR: Script not run as root." >> "$logFile"
sudo "$0" "$@"
exit $?
fi
echo "Root privileges confirmed." >> "$logFile"
}

# Function to determine OS and architecture
set_os_and_arch() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="macOS"
else
echo "Unsupported OS"
echo "ERROR: Unsupported OS: $OSTYPE" >> "$logFile"
exit 1
fi

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
arch=$(uname -m)
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="macOS"
arch=$(uname -m)
else
echo "Unsupported OS"
exit 1
fi

case $arch in
"x86_64")
arch="x86_64"
;;
"aarch64" | "arm64")
arch="arm64"
;;
"i686" | "i386")
arch="i386"
;;
*)
echo "Unsupported architecture: $arch"
case $arch in
"x86_64")
arch="x86_64"
;;
"aarch64" | "arm64")
arch="arm64"
;;
"i686" | "i386")
arch="i386"
;;
*)
echo "Unsupported architecture: $arch"
echo "ERROR: Unsupported architecture: $arch" >> "$logFile"
exit 1
;;
esac

echo "OS set to $os, architecture set to $arch." >> "$logFile"
}

# Function to download CLI
download_cli() {
downloadUrl="https://github.com/debricked/cli/releases/download/$releaseVersion/cli_${os}_${arch}.tar.gz"
echo "Downloading Debricked CLI from $downloadUrl"
echo "Downloading from $downloadUrl" >> "$logFile"
if ! curl -L $downloadUrl -o /tmp/debricked-cli.tar.gz; then
echo "Failed to download Debricked CLI"
echo "ERROR: Download failed." >> "$logFile"
exit 1
;;
esac
fi
echo "Download successful." >> "$logFile"
}

downloadUrl="https://github.com/debricked/cli/releases/download/$releaseVersion/cli_${os}_${arch}.tar.gz"
installPath="/usr/local/bin/debricked"
# Function to extract CLI
extract_cli() {
echo "Extracting Debricked CLI..."
echo "Extracting CLI" >> "$logFile"
if ! tar -xzf /tmp/debricked-cli.tar.gz -C /tmp; then
echo "Failed to extract Debricked CLI"
echo "ERROR: Extraction failed." >> "$logFile"
exit 1
fi
echo "Extraction successful." >> "$logFile"
}

# Function to install CLI
install_cli() {
installPath="/usr/local/bin/debricked"
echo "Installing Debricked CLI to $installPath ..."
echo "Installing to $installPath" >> "$logFile"
if ! mv /tmp/debricked $installPath; then
echo "Failed to install Debricked CLI"
echo "ERROR: Installation failed." >> "$logFile"
exit 1
fi
chmod +x $installPath
echo "Installation successful." >> "$logFile"
}

# Download and extract the file directly
echo "Downloading and extracting Debricked CLI from $downloadUrl"
curl -L $downloadUrl | tar -xz debricked
# Function to clean up
cleanup() {
echo "Cleaning up..."
echo "Cleaning up" >> "$logFile"
rm /tmp/debricked-cli.tar.gz
echo "Cleanup successful." >> "$logFile"
}

# Move the extracted debricked executable to the install path
echo "Installing Debricked CLI to $installPath ..."
sudo mv debricked $installPath
sudo chmod +x $installPath
# Main installation process
check_root
set_os_and_arch
download_cli
extract_cli
install_cli
cleanup

echo "Debricked($releaseVersion) CLI installation completed successfully."
echo "Debricked($releaseVersion) CLI installation completed successfully." >> "$logFile"
echo "See $logFile for details."
Loading

0 comments on commit d85123a

Please sign in to comment.