|
| 1 | +#!/bin/bash -e |
| 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 tar if not present |
| 10 | +check_install_jq_tar() { |
| 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 | + echo -e "${GREEN}Please install jq then rerun this script${NC}" |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | + |
| 21 | + if ! command -v tar &> /dev/null; then |
| 22 | + echo -e "${RED}tar could not be found ...${NC}" |
| 23 | + echo -e "${GREEN}Please install tar then rerun this script${NC}" |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# Function to fetch the latest version based on channel |
| 29 | +get_latest_version() { |
| 30 | + local channel=$1 |
| 31 | + local tag_name |
| 32 | + case $channel in |
| 33 | + stable) |
| 34 | + tag_name=$(curl -s "https://api.github.com/repos/janhq/cortex.cpp/releases/latest" | grep -oP '"tag_name": "\K(.*)(?=")') |
| 35 | + ;; |
| 36 | + beta) |
| 37 | + tag_name=$(curl -s "https://api.github.com/repos/janhq/cortex.cpp/releases" | jq -r '.[] | select(.prerelease) | .tag_name' | head -n 1) |
| 38 | + ;; |
| 39 | + nightly) |
| 40 | + tag_name=$(curl -s "https://delta.jan.ai/cortex/latest/version.json" | jq -r '.tag_name') |
| 41 | + ;; |
| 42 | + *) |
| 43 | + echo "Invalid channel specified." |
| 44 | + exit 1 |
| 45 | + ;; |
| 46 | + esac |
| 47 | + echo "${tag_name#v}" |
| 48 | +} |
| 49 | + |
| 50 | +# Determine the home directory based on the user |
| 51 | +USER_TO_RUN_AS=${SUDO_USER:-$(whoami)} |
| 52 | +if [ "$USER_TO_RUN_AS" = "root" ]; then |
| 53 | + USER_HOME="/root" |
| 54 | +else |
| 55 | + USER_HOME="/home/$USER_TO_RUN_AS" |
| 56 | +fi |
| 57 | + |
| 58 | +# Default values |
| 59 | +CHANNEL="stable" |
| 60 | +VERSION="" |
| 61 | +IS_UPDATE="false" |
| 62 | + |
| 63 | +# Function to parse command-line arguments |
| 64 | +parse_args() { |
| 65 | + while [[ "$#" -gt 0 ]]; do |
| 66 | + case $1 in |
| 67 | + --channel) |
| 68 | + CHANNEL="$2" |
| 69 | + shift 2 |
| 70 | + ;; |
| 71 | + --version) |
| 72 | + VERSION="$2" |
| 73 | + shift 2 |
| 74 | + ;; |
| 75 | + --is_update) |
| 76 | + IS_UPDATE="true" |
| 77 | + shift 1 |
| 78 | + ;; |
| 79 | + *) |
| 80 | + echo "Unknown option: $1" |
| 81 | + exit 1 |
| 82 | + ;; |
| 83 | + esac |
| 84 | + done |
| 85 | +} |
| 86 | + |
| 87 | +# Call parse_args function to handle options |
| 88 | +parse_args "$@" |
| 89 | + |
| 90 | +# Check if VERSION is empty and fetch latest if necessary |
| 91 | +if [ -z "$VERSION" ]; then |
| 92 | + VERSION=$(get_latest_version $CHANNEL) |
| 93 | +fi |
| 94 | + |
| 95 | +# Set paths based on channel |
| 96 | +case $CHANNEL in |
| 97 | + stable) |
| 98 | + CLI_BINARY_NAME="cortex" |
| 99 | + SERVER_BINARY_NAME="cortex-server" |
| 100 | + DATA_DIR="$USER_HOME/cortexcpp" |
| 101 | + UNINSTALL_SCRIPT="/usr/bin/cortex-uninstall.sh" |
| 102 | + UPDATER_SCRIPT="/usr/bin/cortex-updater.sh" |
| 103 | + CONFIGURATION_FILE="$USER_HOME/.cortexrc" |
| 104 | + ;; |
| 105 | + beta) |
| 106 | + CLI_BINARY_NAME="cortex-beta" |
| 107 | + SERVER_BINARY_NAME="cortex-server-beta" |
| 108 | + DATA_DIR="$USER_HOME/cortexcpp-beta" |
| 109 | + UNINSTALL_SCRIPT="/usr/bin/cortex-beta-uninstall.sh" |
| 110 | + UPDATER_SCRIPT="/usr/bin/cortex-beta-updater.sh" |
| 111 | + CONFIGURATION_FILE="$USER_HOME/.cortexrc-beta" |
| 112 | + ;; |
| 113 | + nightly) |
| 114 | + CLI_BINARY_NAME="cortex-nightly" |
| 115 | + SERVER_BINARY_NAME="cortex-server-nightly" |
| 116 | + DATA_DIR="$USER_HOME/cortexcpp-nightly" |
| 117 | + UNINSTALL_SCRIPT="/usr/bin/cortex-nightly-uninstall.sh" |
| 118 | + UPDATER_SCRIPT="/usr/bin/cortex-nightly-updater.sh" |
| 119 | + CONFIGURATION_FILE="$USER_HOME/.cortexrc-nightly" |
| 120 | + ;; |
| 121 | + *) |
| 122 | + echo "Invalid channel specified." |
| 123 | + exit 1 |
| 124 | + ;; |
| 125 | +esac |
| 126 | + |
| 127 | +INSTALL_DIR="/usr/bin" |
| 128 | + |
| 129 | +# Function to download and extract cortex |
| 130 | +install_cortex() { |
| 131 | + local channel=$1 |
| 132 | + local version=$2 |
| 133 | + local url="" |
| 134 | + |
| 135 | + case $channel in |
| 136 | + stable) |
| 137 | + url="https://github.com/janhq/cortex.cpp/releases/download/v${version}/cortex-${version}-linux-amd64.tar.gz" |
| 138 | + ;; |
| 139 | + beta) |
| 140 | + url="https://github.com/janhq/cortex.cpp/releases/download/v${version}/cortex-${version}-linux-amd64.tar.gz" |
| 141 | + ;; |
| 142 | + nightly) |
| 143 | + url="https://delta.jan.ai/cortex/v${version}/linux-amd64/cortex-nightly.tar.gz" |
| 144 | + ;; |
| 145 | + esac |
| 146 | + echo "Downloading cortex $channel version $version from $url" |
| 147 | + curl -L $url -o /tmp/cortex.tar.gz |
| 148 | + tar -xzvf /tmp/cortex.tar.gz -C /tmp |
| 149 | + chmod +x /tmp/cortex/* |
| 150 | + cp /tmp/cortex/* /usr/bin/ |
| 151 | + # Check is update or not |
| 152 | + if [ "$IS_UPDATE" = "false" ]; then |
| 153 | + su -c "$INSTALL_DIR/$CLI_BINARY_NAME engines install llama-cpp" $USER_TO_RUN_AS |
| 154 | + su -c "$INSTALL_DIR/$CLI_BINARY_NAME stop > /dev/null 2>&1" $USER_TO_RUN_AS |
| 155 | + fi |
| 156 | +} |
| 157 | + |
| 158 | +# Function to create uninstall script |
| 159 | +create_uninstall_script() { |
| 160 | + cat << EOF > $UNINSTALL_SCRIPT |
| 161 | +#!/bin/bash |
| 162 | +# Check for root privileges |
| 163 | +if [ "\$(id -u)" != "0" ]; then |
| 164 | + echo "This script must be run as root. Please run again with sudo." |
| 165 | + exit 1 |
| 166 | +fi |
| 167 | +
|
| 168 | +echo "Stopping cortex..." |
| 169 | +su -c "$INSTALL_DIR/$CLI_BINARY_NAME stop > /dev/null 2>&1" $USER_TO_RUN_AS |
| 170 | +rm -rf $INSTALL_DIR/$CLI_BINARY_NAME |
| 171 | +rm -rf $INSTALL_DIR/$SERVER_BINARY_NAME |
| 172 | +rm -f $UNINSTALL_SCRIPT |
| 173 | +rm -f $UPDATER_SCRIPT |
| 174 | +
|
| 175 | +echo "Do you want to delete the ~/$DATA_DIR data folder and file ~/$CONFIGURATION_FILE? (yes/no) [default: no]" |
| 176 | +read -r answer |
| 177 | +while true; do |
| 178 | + case "\$answer" in |
| 179 | + [yY][eE][sS]|[yY]) |
| 180 | + echo "Deleting cortex data folders..." |
| 181 | + if [ -d "$DATA_DIR" ]; then |
| 182 | + echo "Removing $DATA_DIR" |
| 183 | + rm -rf "$DATA_DIR" > /dev/null 2>&1 |
| 184 | + fi |
| 185 | + if [ -f "$CONFIGURATION_FILE" ]; then |
| 186 | + echo "Removing $CONFIGURATION_FILE" |
| 187 | + rm -f "$CONFIGURATION_FILE" > /dev/null 2>&1 |
| 188 | + fi |
| 189 | + break |
| 190 | + ;; |
| 191 | + [nN][oO]|[nN]|"") |
| 192 | + echo "Keeping the 'cortex' data folders." |
| 193 | + break |
| 194 | + ;; |
| 195 | + *) |
| 196 | + echo "Invalid response. Please type 'yes', 'no', 'y', or 'n' (case-insensitive)." |
| 197 | + read -r answer |
| 198 | + ;; |
| 199 | + esac |
| 200 | +done |
| 201 | +
|
| 202 | +EOF |
| 203 | + chmod +x $UNINSTALL_SCRIPT |
| 204 | + echo "Uninstall script created at $UNINSTALL_SCRIPT" |
| 205 | +} |
| 206 | + |
| 207 | +# Function to create updater script |
| 208 | +create_updater_script() { |
| 209 | + cat << EOF > $UPDATER_SCRIPT |
| 210 | +#!/bin/bash |
| 211 | +# Check for root privileges |
| 212 | +if [ "\$(id -u)" != "0" ]; then |
| 213 | + echo "This script must be run as root. Please run again with sudo." |
| 214 | + exit 1 |
| 215 | +fi |
| 216 | +echo "Stopping cortex for update..." |
| 217 | +su -c "$INSTALL_DIR/$CLI_BINARY_NAME stop > /dev/null 2>&1" $USER_TO_RUN_AS |
| 218 | +curl -s https://raw.githubusercontent.com/janhq/cortex/feat/linux-bash-install-script/engine/templates/linux/install.sh | bash -s -- --channel $CHANNEL --is_update |
| 219 | +EOF |
| 220 | + chmod +x $UPDATER_SCRIPT |
| 221 | + echo "Updater script created at $UPDATER_SCRIPT" |
| 222 | +} |
| 223 | + |
| 224 | +# Run installation |
| 225 | +check_install_jq_tar |
| 226 | +install_cortex $CHANNEL $VERSION |
| 227 | +create_uninstall_script |
| 228 | +create_updater_script |
| 229 | + |
| 230 | +echo "Installation complete. Run cortex-uninstall.sh to uninstall and cortex-updater.sh to update." |
0 commit comments