|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Check for root privileges |
| 4 | +if [ "$(id -u)" != "0" ]; then |
| 5 | + echo "This script must be run as root. Please run again with sudo." |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +# Check and suggest installing jq and unzip if not present |
| 10 | +check_install_jq_unzip() { |
| 11 | + RED='\033[0;31m' |
| 12 | + GREEN='\033[0;32m' |
| 13 | + NC='\033[0m' # No Color |
| 14 | + |
| 15 | + if ! command -v jq &> /dev/null; then |
| 16 | + echo -e "${RED}jq could not be found ...${NC}" |
| 17 | + if [[ "$OS" == "Linux" ]]; then |
| 18 | + echo -e "${GREEN}Please run the command below to install jq then rerun this script${NC}" |
| 19 | + echo "$ sudo apt-get install jq" |
| 20 | + exit 1 |
| 21 | + elif [[ "$OS" == "Darwin" ]]; then |
| 22 | + echo -e "${GREEN}Please run the command below to install jq then rerun this script${NC}" |
| 23 | + echo "$ brew install jq" |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | + fi |
| 27 | + |
| 28 | + if ! command -v unzip &> /dev/null; then |
| 29 | + echo -e "${RED}unzip could not be found ...${NC}" |
| 30 | + if [[ "$OS" == "Linux" ]]; then |
| 31 | + echo -e "${GREEN}Please run the command below to install unzip then rerun this script${NC}" |
| 32 | + echo "$ sudo apt-get install unzip" |
| 33 | + exit 1 |
| 34 | + elif [[ "$OS" == "Darwin" ]]; then |
| 35 | + echo -e "${GREEN}Please run the command below to install unzip then rerun this script${NC}" |
| 36 | + echo "$ brew install unzip" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Function to download and install nitro |
| 43 | +install_nitro() { |
| 44 | + rm -rf /tmp/nitro |
| 45 | + rm /tmp/nitro.zip |
| 46 | + echo "Downloading Nitro version $VERSION... from $1" |
| 47 | + curl -sL "$1" -o /tmp/nitro.zip |
| 48 | + unzip /tmp/nitro.zip -d /tmp |
| 49 | + ls /tmp/nitro |
| 50 | + |
| 51 | + # Copying files to /usr/local/bin |
| 52 | + for file in /tmp/nitro/*; do |
| 53 | + chmod +x "$file" |
| 54 | + cp "$file" /usr/local/bin/ |
| 55 | + done |
| 56 | +} |
| 57 | + |
| 58 | +# Function to create uninstall script |
| 59 | +create_uninstall_script() { |
| 60 | + echo '#!/bin/bash' > /tmp/uninstall_nitro.sh |
| 61 | + echo 'if [ "$(id -u)" != "0" ]; then' >> /tmp/uninstall_nitro.sh |
| 62 | + echo ' echo "This script must be run as root. Please run again with sudo."' >> /tmp/uninstall_nitro.sh |
| 63 | + echo ' exit 1' >> /tmp/uninstall_nitro.sh |
| 64 | + echo 'fi' >> /tmp/uninstall_nitro.sh |
| 65 | + for file in /tmp/nitro/*; do |
| 66 | + echo "rm /usr/local/bin/$(basename "$file")" >> /tmp/uninstall_nitro.sh |
| 67 | + done |
| 68 | + echo "rm /usr/local/bin/uninstall_nitro.sh" >> /tmp/uninstall_nitro.sh |
| 69 | + echo 'echo "Nitro remove successfully."' >> /tmp/uninstall_nitro.sh |
| 70 | + chmod +x /tmp/uninstall_nitro.sh |
| 71 | + mv /tmp/uninstall_nitro.sh /usr/local/bin/ |
| 72 | +} |
| 73 | + |
| 74 | +# Determine OS and architecture |
| 75 | +OS=$(uname -s) |
| 76 | +ARCH=$(uname -m) |
| 77 | +VERSION="latest" |
| 78 | +GPU="" |
| 79 | + |
| 80 | +check_install_jq_unzip |
| 81 | + |
| 82 | +# Parse arguments |
| 83 | +for arg in "$@" |
| 84 | +do |
| 85 | + case $arg in |
| 86 | + --gpu) |
| 87 | + GPU="-cuda" |
| 88 | + shift |
| 89 | + ;; |
| 90 | + --version) |
| 91 | + VERSION="$2" |
| 92 | + shift |
| 93 | + shift |
| 94 | + ;; |
| 95 | + esac |
| 96 | +done |
| 97 | + |
| 98 | +# Notify if GPU option is not supported |
| 99 | +if [ "$GPU" == "-cuda" ] && [ "$OS" == "Darwin" ]; then |
| 100 | + echo "GPU option is only supported on Linux or Windows." |
| 101 | + exit 1 |
| 102 | +fi |
| 103 | + |
| 104 | +# Construct GitHub API URL and get latest version if not specified |
| 105 | +if [ "$VERSION" == "latest" ]; then |
| 106 | + API_URL="https://api.github.com/repos/janhq/nitro/releases/latest" |
| 107 | + VERSION=$(curl -s $API_URL | jq -r ".tag_name" | sed 's/^v//') |
| 108 | +fi |
| 109 | + |
| 110 | +# Check if version is empty |
| 111 | +if [ -z "$VERSION" ]; then |
| 112 | + echo "Failed to fetch latest version." |
| 113 | + exit 1 |
| 114 | +fi |
| 115 | + |
| 116 | +# Construct download URL based on OS, ARCH, GPU and VERSION |
| 117 | +case $OS in |
| 118 | + Linux) |
| 119 | + FILE_NAME="nitro-${VERSION}-linux-amd64${GPU}.zip" |
| 120 | + ;; |
| 121 | + Darwin) |
| 122 | + ARCH_FORMAT=$( [[ "$ARCH" == "arm64" ]] && echo "mac-arm64" || echo "mac-amd64") |
| 123 | + FILE_NAME="nitro-${VERSION}-${ARCH_FORMAT}.zip" |
| 124 | + ;; |
| 125 | + *) |
| 126 | + echo "Unsupported OS." |
| 127 | + exit 1 |
| 128 | + ;; |
| 129 | +esac |
| 130 | + |
| 131 | +DOWNLOAD_URL="https://github.com/janhq/nitro/releases/download/v${VERSION}/${FILE_NAME}" |
| 132 | + |
| 133 | +# Download, install, and create uninstall script |
| 134 | +install_nitro "$DOWNLOAD_URL" |
| 135 | +create_uninstall_script |
| 136 | + |
| 137 | +echo "Nitro installed successfully." |
0 commit comments