-
Notifications
You must be signed in to change notification settings - Fork 89
/
install.sh
executable file
路186 lines (165 loc) 路 4.97 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash -e
# 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 tar if not present
check_install_jq_tar() {
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 tar &> /dev/null; then
echo -e "${RED}tar could not be found ...${NC}"
if [[ "$OS" == "Linux" ]]; then
echo -e "${GREEN}Please run the command below to install tar then rerun this script${NC}"
echo "$ sudo apt install tar gzip"
exit 1
elif [[ "$OS" == "Darwin" ]]; then
echo -e "${GREEN}Please run the command below to install tar then rerun this script${NC}"
echo "$ brew install gnu-tar"
exit 1
fi
fi
}
determine_avx_support() {
if grep -q avx512 /proc/cpuinfo; then
echo "-avx512"
elif grep -q avx2 /proc/cpuinfo; then
echo "-avx2"
elif grep -q avx /proc/cpuinfo; then
echo "-avx"
else
echo ""
fi
}
# Function to download and install nitro
install_nitro() {
rm -rf /tmp/nitro
rm /tmp/nitro.tar.gz
echo "Downloading Nitro version $VERSION... from $1"
curl -sL "$1" -o /tmp/nitro.tar.gz
tar -xzvf /tmp/nitro.tar.gz -C /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=""
AVX=""
check_install_jq_tar
# Parse arguments
for arg in "$@"
do
case $arg in
--gpu)
GPU="-cuda"
shift
;;
--version)
VERSION="$2"
shift
shift
;;
--avx)
AVX="-avx"
shift
;;
--avx2)
AVX="-avx2"
shift
;;
--avx512)
AVX="-avx512"
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
# There are only two supported CUDA version. Let it fail if unsupported.
if [ "$GPU" == "-cuda" ]; then
export PATH="/usr/local/cuda/bin:$PATH"
CUDA_MAJ=$(nvcc --version | grep -oP "(?<=release )\d+") || 0
if [[ $CUDA_MAJ == 12 ]]; then
CUDA_VERSION="-12-0";
elif [[ $CUDA_MAJ == 11 ]]; then
CUDA_VERSION="-11-7"
fi
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)
if [ -z "$AVX" ]; then
AVX=$(determine_avx_support)
fi
FILE_NAME="nitro-${VERSION}-linux-amd64${AVX}${GPU}${CUDA_VERSION}.tar.gz"
;;
Darwin)
ARCH_FORMAT="mac-universal"
FILE_NAME="nitro-${VERSION}-${ARCH_FORMAT}.tar.gz"
;;
*)
echo "Unsupported OS."
exit 1
;;
esac
DOWNLOAD_URL="https://github.com/janhq/nitro/releases/download/v${VERSION}/${FILE_NAME}"
# Check AVX support
if [ -z "$AVX" ] && [ "$OS" == "Linux" ]; then
echo "AVX is not supported on this system."
exit 1
fi
# Remove existing Nitro installation
echo "Removing existing Nitro installation..."
rm -rf /usr/local/bin/nitro
# Download, install, and create uninstall script
install_nitro "$DOWNLOAD_URL"
create_uninstall_script
echo "Nitro installed successfully."