From bbb7fb82404b61d877ed23f237b51e4ac199db90 Mon Sep 17 00:00:00 2001 From: Hien To Date: Tue, 14 Nov 2023 16:23:14 +0700 Subject: [PATCH 1/5] Add bashscript install nitro for linux and macos --- install.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 install.sh diff --git a/install.sh b/install.sh new file mode 100755 index 000000000..590c064dc --- /dev/null +++ b/install.sh @@ -0,0 +1,102 @@ +#!/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 + +# 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="" + +# 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." From e2f1de1a669f37db917d543e6138222b0fd9e469 Mon Sep 17 00:00:00 2001 From: Hien To Date: Tue, 14 Nov 2023 17:05:54 +0700 Subject: [PATCH 2/5] Add suggest install necessary dependencies --- install.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/install.sh b/install.sh index 590c064dc..21c9651c2 100755 --- a/install.sh +++ b/install.sh @@ -6,6 +6,35 @@ if [ "$(id -u)" != "0" ]; then exit 1 fi +# Check and suggest installing jq and unzip if not present +check_install_jq_unzip() { + if ! command -v jq &> /dev/null; then + echo "jq could not be found, installing..." + if [[ "$OS" == "Linux" ]]; then + echo "run command below to install jq" + echo "sudo apt-get install jq" + exit 1 + elif [[ "$OS" == "Darwin" ]]; then + echo "run command below to install jq" + echo "brew install jq" + exit 1 + fi + fi + + if ! command -v unzip &> /dev/null; then + echo "unzip could not be found, installing..." + if [[ "$OS" == "Linux" ]]; then + echo "run command below to install unzip" + echo "sudo apt-get install unzip" + exit 1 + elif [[ "$OS" == "Darwin" ]]; then + echo "run command below to install unzip" + echo "brew install unzip" + exit 1 + fi + fi +} + # Function to download and install nitro install_nitro() { rm -rf /tmp/nitro @@ -44,6 +73,8 @@ ARCH=$(uname -m) VERSION="latest" GPU="" +check_install_jq_unzip + # Parse arguments for arg in "$@" do From 0b7529d5afabe536fe0a7063d759afd14cb546a8 Mon Sep 17 00:00:00 2001 From: Hien To Date: Tue, 14 Nov 2023 17:13:41 +0700 Subject: [PATCH 3/5] Add color for suggestion command install jq and unzip --- install.sh | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/install.sh b/install.sh index 21c9651c2..f42403c8b 100755 --- a/install.sh +++ b/install.sh @@ -8,28 +8,32 @@ 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 "jq could not be found, installing..." + echo -e "${RED}jq could not be found ...${NC}" if [[ "$OS" == "Linux" ]]; then - echo "run command below to install jq" - echo "sudo apt-get install jq" + 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 "run command below to install jq" - echo "brew install jq" + 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 "unzip could not be found, installing..." + echo -e "${RED}unzip could not be found ...${NC}" if [[ "$OS" == "Linux" ]]; then - echo "run command below to install unzip" - echo "sudo apt-get install unzip" + 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 "run command below to install unzip" - echo "brew install unzip" + echo -e "${GREEN}Please run the command below to install unzip then rerun this script${NC}" + echo "$ brew install unzip" exit 1 fi fi From 88ae6e814a2432d4caf6e9e569a06a9fb969c5b1 Mon Sep 17 00:00:00 2001 From: Hien To Date: Tue, 14 Nov 2023 19:15:01 +0700 Subject: [PATCH 4/5] add install.bat for windows --- install.bat | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 install.bat diff --git a/install.bat b/install.bat new file mode 100644 index 000000000..f3314cb75 --- /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 uninstall.bat +echo @echo off > "%APPDATA%\nitro\uninstall.bat" +echo setx PATH "%PATH:;%APPDATA%\nitro=;%"" >> "%APPDATA%\nitro\uninstall.bat" +echo rmdir /S /Q "%APPDATA%\nitro" >> "%APPDATA%\nitro\uninstall.bat" + +:: Clean up +del %TEMP%\nitro.zip + +endlocal \ No newline at end of file From 050ed54e35b92848d625f279166ef3c0d6bb226f Mon Sep 17 00:00:00 2001 From: Hien To Date: Tue, 14 Nov 2023 19:42:38 +0700 Subject: [PATCH 5/5] rename uninstall.bat to uninstallnitro.bat --- install.bat | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/install.bat b/install.bat index f3314cb75..9c1b4bf7c 100644 --- a/install.bat +++ b/install.bat @@ -53,10 +53,10 @@ powershell -Command "Expand-Archive -Path '%TEMP%\nitro.zip' -DestinationPath '% :: Add nitro to the PATH setx PATH "%APPDATA%\nitro;%PATH%" -:: Create uninstall.bat -echo @echo off > "%APPDATA%\nitro\uninstall.bat" -echo setx PATH "%PATH:;%APPDATA%\nitro=;%"" >> "%APPDATA%\nitro\uninstall.bat" -echo rmdir /S /Q "%APPDATA%\nitro" >> "%APPDATA%\nitro\uninstall.bat" +:: 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