forked from jonssonyan/h-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
317 lines (275 loc) · 8.09 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
init_var() {
ECHO_TYPE="echo -e"
package_manager=""
release=""
get_arch=""
HUI_DATA_DOCKER="/h-ui/"
HUI_DATA_SYSTEMD="/usr/local/h-ui/"
h_ui_port=8081
h_ui_time_zone=Asia/Shanghai
}
echo_content() {
case $1 in
"red")
${ECHO_TYPE} "\033[31m$2\033[0m"
;;
"green")
${ECHO_TYPE} "\033[32m$2\033[0m"
;;
"yellow")
${ECHO_TYPE} "\033[33m$2\033[0m"
;;
"blue")
${ECHO_TYPE} "\033[34m$2\033[0m"
;;
"purple")
${ECHO_TYPE} "\033[35m$2\033[0m"
;;
"skyBlue")
${ECHO_TYPE} "\033[36m$2\033[0m"
;;
"white")
${ECHO_TYPE} "\033[37m$2\033[0m"
;;
esac
}
can_connect() {
ping -c2 -i0.3 -W1 "$1" &>/dev/null
if [[ "$?" == "0" ]]; then
return 0
else
return 1
fi
}
check_sys() {
if [[ $(id -u) != "0" ]]; then
echo_content red "You must be root to run this script"
exit 1
fi
can_connect www.google.com
if [[ "$?" == "1" ]]; then
echo_content red "---> Network connection failed"
exit 1
fi
if [[ $(command -v yum) ]]; then
package_manager='yum'
elif [[ $(command -v dnf) ]]; then
package_manager='dnf'
elif [[ $(command -v apt-get) ]]; then
package_manager='apt-get'
elif [[ $(command -v apt) ]]; then
package_manager='apt'
fi
if [[ -z "${package_manager}" ]]; then
echo_content red "This system is not currently supported"
exit 1
fi
if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
release="centos"
elif grep </etc/issue -q -i "debian" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "debian" && [[ -f "/proc/version" ]]; then
release="debian"
elif grep </etc/issue -q -i "ubuntu" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "ubuntu" && [[ -f "/proc/version" ]]; then
release="ubuntu"
fi
if [[ -z "${release}" ]]; then
echo_content red "Only supports CentOS 7+/Ubuntu 18+/Debian 10+"
exit 1
fi
if [[ $(arch) =~ ("x86_64"|"amd64") ]]; then
get_arch="amd64"
elif [[ $(arch) =~ ("aarch64"|"arm64") ]]; then
get_arch="arm64"
fi
if [[ -z "${get_arch}" ]]; then
echo_content red "Only supports x86_64/amd64 arm64/aarch64"
exit 1
fi
}
install_depend() {
if [[ "${package_manager}" == 'apt-get' || "${package_manager}" == 'apt' ]]; then
${package_manager} update -y
fi
${package_manager} install -y \
curl \
wget
}
setup_docker() {
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<EOF
{
"log-driver":"json-file",
"log-opts":{
"max-size":"100m"
}
}
EOF
systemctl daemon-reload
}
install_docker() {
if [[ ! $(command -v docker) ]]; then
echo_content green "---> Install Docker"
bash <(curl -fsSL https://get.docker.com)
setup_docker
systemctl enable docker && systemctl restart docker
if [[ $(command -v docker) ]]; then
echo_content skyBlue "---> Docker install successful"
else
echo_content red "---> Docker install failed"
exit 1
fi
else
echo_content skyBlue "---> Docker is already installed"
fi
}
install_h_ui_docker() {
if [[ -n $(docker ps -a -q -f "name=^h-ui$") ]]; then
echo_content skyBlue "---> H UI is already installed"
exit 0
fi
mkdir -p ${HUI_DATA_DOCKER}
read -r -p "Please enter the port of H UI (default: 8081): " h_ui_port
[[ -z "${h_ui_port}" ]] && h_ui_port="8081"
read -r -p "Please enter the Time zone of H UI (default: Asia/Shanghai): " h_ui_time_zone
[[ -z "${h_ui_time_zone}" ]] && h_ui_time_zone="Asia/Shanghai"
docker pull jonssonyan/h-ui &&
docker run -d --name h-ui --restart always \
--network=host \
-e TZ=${h_ui_time_zone} \
-v /h-ui/bin:/h-ui/bin \
-v /h-ui/data:/h-ui/data \
-v /h-ui/export:/h-ui/export \
-v /h-ui/logs:/h-ui/logs \
jonssonyan/h-ui \
./h-ui -p ${h_ui_port}
echo_content skyBlue "---> H UI install successful"
}
upgrade_h_ui_docker() {
if [[ ! $(command -v docker) ]]; then
echo_content red "---> Docker not installed"
exit 0
fi
if [[ -z $(docker ps -a -q -f "name=^h-ui$") ]]; then
echo_content red "---> H UI not installed"
exit 0
fi
latest_version=$(curl -Ls "https://api.github.com/repos/jonssonyan/h-ui/releases/latest" | grep '"tag_name":' | sed 's/.*"tag_name": "\(.*\)",.*/\1/')
current_version=$(docker exec h-ui ./h-ui -v | sed -n 's/.*version \([^\ ]*\).*/\1/p')
if [[ "${latest_version}" == "${current_version}" ]]; then
echo_content skyBlue "---> H UI is already the latest version"
exit 0
fi
docker rm -f h-ui
docker rmi jonssonyan/h-ui
install_h_ui_docker
echo_content skyBlue "---> H UI upgrade successful"
}
uninstall_h_ui_docker() {
if [[ ! $(command -v docker) ]]; then
echo_content red "---> Docker not installed"
exit 0
fi
if [[ -z $(docker ps -a -q -f "name=^h-ui$") ]]; then
echo_content red "---> H UI not installed"
exit 0
fi
docker rm -f h-ui
docker rmi jonssonyan/h-ui
rm -rf /h-ui/
echo_content skyBlue "---> H UI uninstall successful"
}
install_h_ui_systemd() {
if systemctl status h-ui &> /dev/null; then
echo_content skyBlue "---> H UI is already installed"
exit 0
fi
mkdir -p ${HUI_DATA_SYSTEMD}
curl -fsSL https://github.com/jonssonyan/h-ui/releases/latest/download/h-ui-linux-${get_arch} -o /usr/local/h-ui/h-ui &&
chmod +x /usr/local/h-ui/h-ui &&
curl -fsSL https://raw.githubusercontent.com/jonssonyan/h-ui/main/h-ui.service -o /etc/systemd/system/h-ui.service &&
systemctl daemon-reload &&
systemctl enable h-ui &&
systemctl restart h-ui
echo_content skyBlue "---> H UI install successful"
}
upgrade_h_ui_systemd() {
if ! systemctl status h-ui &> /dev/null; then
echo_content red "---> H UI not installed"
exit 0
fi
latest_version=$(curl -Ls "https://api.github.com/repos/jonssonyan/h-ui/releases/latest" | grep '"tag_name":' | sed 's/.*"tag_name": "\(.*\)",.*/\1/')
current_version=$(/usr/local/h-ui/h-ui -v | sed -n 's/.*version \([^\ ]*\).*/\1/p')
if [[ "${latest_version}" == "${current_version}" ]]; then
echo_content skyBlue "---> H UI is already the latest version"
exit 0
fi
if [[ $(systemctl is-active h-ui) == "active" ]]; then
systemctl stop h-ui
fi
curl -fsSL https://github.com/jonssonyan/h-ui/releases/latest/download/h-ui-linux-${get_arch} -o /usr/local/h-ui/h-ui &&
chmod +x /usr/local/h-ui/h-ui &&
systemctl restart h-ui
echo_content skyBlue "---> H UI upgrade successful"
}
uninstall_h_ui_systemd() {
if ! systemctl status h-ui &> /dev/null; then
echo_content red "---> H UI not installed"
exit 0
fi
if [[ $(systemctl is-active h-ui) == "active" ]]; then
systemctl stop h-ui
fi
systemctl disable h-ui.service &&
rm -f /etc/systemd/system/h-ui.service &&
systemctl daemon-reload &&
rm -rf /usr/local/h-ui/
echo_content skyBlue "---> H UI uninstall successful"
}
main() {
cd "$HOME" || exit 0
init_var
check_sys
install_depend
clear
echo_content red "\n=============================================================="
echo_content skyBlue "Recommended OS: CentOS 8+/Ubuntu 20+/Debian 11+"
echo_content skyBlue "Description: Quick Installation of H UI"
echo_content skyBlue "Author: jonssonyan <https://jonssonyan.com>"
echo_content skyBlue "Github: https://github.com/jonssonyan/h-ui"
echo_content red "\n=============================================================="
echo_content yellow "1. Install H UI (Docker)"
echo_content yellow "2. Upgrade H UI (Docker)"
echo_content yellow "3. Uninstall H UI (Docker)"
echo_content red "\n=============================================================="
echo_content yellow "4. Install H UI (systemd)"
echo_content yellow "5. Upgrade H UI (systemd)"
echo_content yellow "6. Uninstall H UI (systemd)"
read -r -p "Please choose: " input_option
case ${input_option} in
1)
install_docker
install_h_ui_docker
;;
2)
upgrade_h_ui_docker
;;
3)
uninstall_h_ui_docker
;;
4)
install_h_ui_systemd
;;
5)
upgrade_h_ui_systemd
;;
6)
uninstall_h_ui_systemd
;;
*)
echo_content red "No such option"
;;
esac
}
main