diff --git a/install.bat b/install.bat new file mode 100644 index 000000000..9c1b4bf7c --- /dev/null +++ b/install.bat @@ -0,0 +1,64 @@ +@echo off +setlocal + +:: Remove existing nitro directory if it exists +if exist "%APPDATA%\nitro" ( + echo Removing existing Nitro installation... + rmdir /S /Q "%APPDATA%\nitro" +) + + +:: Parse arguments +set "VERSION=latest" +set "GPU=false" +:arg_loop +if "%~1"=="" goto arg_loop_end +if "%~1"=="--gpu" ( + set "GPU=true" + shift + goto arg_loop +) +if "%~1"=="--version" ( + set "VERSION=%~2" + shift + shift + goto arg_loop +) +shift +goto arg_loop +:arg_loop_end + +echo %VERSION% + +:: Get the release +if "%VERSION%"=="latest" ( + :: If the version is set to "latest", get the latest version number from the Nitro GitHub repository + for /f "delims=" %%i in ('powershell -Command "& {$version = Invoke-RestMethod -Uri 'https://api.github.com/repos/janhq/nitro/releases/latest'; return $version.tag_name.TrimStart('v')}"') do set "VERSION=%%i" +) + +:: Construct the download URL +set "URL=https://github.com/janhq/nitro/releases/download/v%VERSION%/nitro-%VERSION%-win-amd64" +if "%GPU%"=="true" ( + :: If --gpu option is provided, append -cuda to the URL + set "URL=%URL%-cuda" +) +set "URL=%URL%.zip" + +:: Download and extract nitro +echo Downloading Nitro from: %URL% +powershell -Command "Invoke-WebRequest -OutFile '%TEMP%\nitro.zip' '%URL%'" +echo Extracting Nitro... +powershell -Command "Expand-Archive -Path '%TEMP%\nitro.zip' -DestinationPath '%APPDATA%\nitro'" + +:: Add nitro to the PATH +setx PATH "%APPDATA%\nitro;%PATH%" + +:: Create uninstallnitro.bat +echo @echo off > "%APPDATA%\nitro\uninstallnitro.bat" +echo setx PATH "%PATH:;%APPDATA%\nitro=;%"" >> "%APPDATA%\nitro\uninstallnitro.bat" +echo rmdir /S /Q "%APPDATA%\nitro" >> "%APPDATA%\nitro\uninstallnitro.bat" + +:: Clean up +del %TEMP%\nitro.zip + +endlocal \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 000000000..f42403c8b --- /dev/null +++ b/install.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +# Check for root privileges +if [ "$(id -u)" != "0" ]; then + echo "This script must be run as root. Please run again with sudo." + exit 1 +fi + +# Check and suggest installing jq and unzip if not present +check_install_jq_unzip() { + RED='\033[0;31m' + GREEN='\033[0;32m' + NC='\033[0m' # No Color + + if ! command -v jq &> /dev/null; then + echo -e "${RED}jq could not be found ...${NC}" + if [[ "$OS" == "Linux" ]]; then + echo -e "${GREEN}Please run the command below to install jq then rerun this script${NC}" + echo "$ sudo apt-get install jq" + exit 1 + elif [[ "$OS" == "Darwin" ]]; then + echo -e "${GREEN}Please run the command below to install jq then rerun this script${NC}" + echo "$ brew install jq" + exit 1 + fi + fi + + if ! command -v unzip &> /dev/null; then + echo -e "${RED}unzip could not be found ...${NC}" + if [[ "$OS" == "Linux" ]]; then + echo -e "${GREEN}Please run the command below to install unzip then rerun this script${NC}" + echo "$ sudo apt-get install unzip" + exit 1 + elif [[ "$OS" == "Darwin" ]]; then + echo -e "${GREEN}Please run the command below to install unzip then rerun this script${NC}" + echo "$ brew install unzip" + exit 1 + fi + fi +} + +# Function to download and install nitro +install_nitro() { + rm -rf /tmp/nitro + rm /tmp/nitro.zip + echo "Downloading Nitro version $VERSION... from $1" + curl -sL "$1" -o /tmp/nitro.zip + unzip /tmp/nitro.zip -d /tmp + ls /tmp/nitro + + # Copying files to /usr/local/bin + for file in /tmp/nitro/*; do + chmod +x "$file" + cp "$file" /usr/local/bin/ + done +} + +# Function to create uninstall script +create_uninstall_script() { + echo '#!/bin/bash' > /tmp/uninstall_nitro.sh + echo 'if [ "$(id -u)" != "0" ]; then' >> /tmp/uninstall_nitro.sh + echo ' echo "This script must be run as root. Please run again with sudo."' >> /tmp/uninstall_nitro.sh + echo ' exit 1' >> /tmp/uninstall_nitro.sh + echo 'fi' >> /tmp/uninstall_nitro.sh + for file in /tmp/nitro/*; do + echo "rm /usr/local/bin/$(basename "$file")" >> /tmp/uninstall_nitro.sh + done + echo "rm /usr/local/bin/uninstall_nitro.sh" >> /tmp/uninstall_nitro.sh + echo 'echo "Nitro remove successfully."' >> /tmp/uninstall_nitro.sh + chmod +x /tmp/uninstall_nitro.sh + mv /tmp/uninstall_nitro.sh /usr/local/bin/ +} + +# Determine OS and architecture +OS=$(uname -s) +ARCH=$(uname -m) +VERSION="latest" +GPU="" + +check_install_jq_unzip + +# Parse arguments +for arg in "$@" +do + case $arg in + --gpu) + GPU="-cuda" + shift + ;; + --version) + VERSION="$2" + shift + shift + ;; + esac +done + +# Notify if GPU option is not supported +if [ "$GPU" == "-cuda" ] && [ "$OS" == "Darwin" ]; then + echo "GPU option is only supported on Linux or Windows." + exit 1 +fi + +# Construct GitHub API URL and get latest version if not specified +if [ "$VERSION" == "latest" ]; then + API_URL="https://api.github.com/repos/janhq/nitro/releases/latest" + VERSION=$(curl -s $API_URL | jq -r ".tag_name" | sed 's/^v//') +fi + +# Check if version is empty +if [ -z "$VERSION" ]; then + echo "Failed to fetch latest version." + exit 1 +fi + +# Construct download URL based on OS, ARCH, GPU and VERSION +case $OS in + Linux) + FILE_NAME="nitro-${VERSION}-linux-amd64${GPU}.zip" + ;; + Darwin) + ARCH_FORMAT=$( [[ "$ARCH" == "arm64" ]] && echo "mac-arm64" || echo "mac-amd64") + FILE_NAME="nitro-${VERSION}-${ARCH_FORMAT}.zip" + ;; + *) + echo "Unsupported OS." + exit 1 + ;; +esac + +DOWNLOAD_URL="https://github.com/janhq/nitro/releases/download/v${VERSION}/${FILE_NAME}" + +# Download, install, and create uninstall script +install_nitro "$DOWNLOAD_URL" +create_uninstall_script + +echo "Nitro installed successfully."