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