This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
install.sh
265 lines (223 loc) · 6.3 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
#!/usr/bin/env bash
set -e
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Requirements:
# Debian 6+, Ubuntu 14.04+, CentOS 7+,
# better with systemd.
# Blog: blog.lvcshu.club
# Author: johnpoint
# Maintain: BennyThink
# Install Express Bot
# Requires root privilege
# This code is tested under Ubuntu 16.04/14.04, CentOS 7 and Debian 9.
# Publish under GNU General Public License v2
# USE AT YOUR OWN RISK!!!
#=================================================
sh_ver="4.3.2"
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Green_font_prefix}[注意]${Font_color_suffix}"
Separator_1="——————————————————————————————"
Get_Dist_Name()
{
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
DISTRO='CentOS'
PM='yum'
elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
DISTRO='RHEL'
PM='yum'
elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
DISTRO='Aliyun'
PM='yum'
elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
DISTRO='Fedora'
PM='yum'
elif grep -Eqi "Amazon Linux AMI" /etc/issue || grep -Eq "Amazon Linux AMI" /etc/*-release; then
DISTRO='Amazon'
PM='yum'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
DISTRO='Debian'
PM='apt'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
DISTRO='Ubuntu'
PM='apt'
elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
DISTRO='Raspbian'
PM='apt'
elif grep -Eqi "Deepin" /etc/issue || grep -Eq "Deepin" /etc/*-release; then
DISTRO='Deepin'
PM='apt'
else
DISTRO='unknow'
fi
}
# Install_all
Install_all(){
dep_prepare
Install_pip
Install_config
install_service
Start_service
}
dep_prepare(){
if [ "$PM" = "yum" ]; then
$PM install -y epel-release
$PM update
$PM install -y python-pip git ffmpeg flac
elif [ "$PM" = "apt" ]; then
$PM update
$PM install -y build-essential python-dev python-pip git ffmpeg flac
pip install setuptools
fi
}
Install_config(){
echo 'Input your Token (telegram bot)'
read p
TOKEN=$p
echo 'Input your Turing Key, space to disable'
read p
TURING_KEY=$p
echo "import os">/home/ExpressBot/expressbot/config.py
echo "TOKEN = '$TOKEN'">>/home/ExpressBot/expressbot/config.py
echo "TURING_KEY ='$TURING_KEY'">>/home/ExpressBot/expressbot/config.py
echo "DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bot.db')">>/home/ExpressBot/expressbot/config.py
echo "INTERVAL = 120">>/home/ExpressBot/expressbot/config.py
echo "LOGGER = False">>/home/ExpressBot/expressbot/config.py
}
# Install_main
Install_pip(){
cd /home
git clone https://github.com/BennyThink/ExpressBot
cd ExpressBot
if [ "$PM" = "yum" ]; then
echo 'CentOS:-)'
# sed -i '$d' requirements.txt
fi
pip install -r requirements.txt
}
check_systemd(){
systemctl --version>>/dev/null
if [ $? -ne 0 ];then
echo -e "${Tip} 非systemd"
exit 1
fi
}
install_service(){
check_systemd
if [ ! -f /lib/systemd/system/expressbot.service ];then
cp expressbot.service /lib/systemd/system/expressbot.service
fi
systemctl daemon-reload
systemctl enable expressbot.service
}
remove_service(){
check_systemd
systemctl stop expressbot.service
systemctl disable expressbot.service
rm /lib/systemd/system/expressbot.service
systemctl daemon-reload
}
# Start_service
Start_service(){
check_systemd
systemctl start expressbot.service>/dev/null
echo -e "${Info}服务已经启动"
}
# Stop_service
Stop_service(){
check_systemd
systemctl status expressbot.service>/dev/null
if [ $? -eq 0 ];then
echo -e "${Info}正在停止服务"
systemctl stop expressbot.service
else
echo -e "${Error}服务已经停止"
fi
systemctl status expressbot.service>/dev/null
if [ $? -eq 0 ];then
echo -e "${Error}服务停止失败"
else
echo -e "${Info}服务已停止"
fi
}
uninstall_all(){
pip uninstall -y -r /home/ExpressBot/requirements.txt
rm -rf /home/ExpressBot
remove_service
}
# Restart_service
Restart_service(){
check_systemd
Stop_service
Start_service
}
# Service_status
Service_status(){
check_systemd
systemctl status expressbot.service
}
menu(){
echo -e " Express Bot一键管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
---- 主程序:BennyThink | 脚本:johnpoint ----
**** 服务配置仅支持systemd系统 ****
——————————————————————
${Green_font_prefix}1.${Font_color_suffix} 一键 安装(配置文件)
——————————————————————
${Green_font_prefix}2.${Font_color_suffix} 一键 卸载
——————————————————————
${Green_font_prefix}3.${Font_color_suffix} 启动 服务(systemd)
${Green_font_prefix}4.${Font_color_suffix} 停止 服务(systemd)
${Green_font_prefix}5.${Font_color_suffix} 重启 服务(systemd)
${Green_font_prefix}6.${Font_color_suffix} 查看 服务状态(systemd)
——————————————————————
"
read -p "请输入数字 [1-6]:" num
case "$num" in
1)
Install_all
;;
2)
uninstall_all
;;
3)
Start_service
;;
4)
Stop_service
;;
5)
Restart_service
;;
6)
Service_status
;;
*)
echo -e "${Error} 请输入正确的数字 [1-6]"
;;
esac
}
# main goes here...
# Check if user is root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please switch to root."
exit 1
fi
Get_Dist_Name
# check distribution
if [ "${DISTRO}" = "unknow" ]; then
echo -e "${Error} 无法获取发行版名称,或者不支持当前发行版"
exit 1
fi
action=$1
if [[ ! -z $action ]]; then
if [[ $action = "start" ]]; then
Start_service
elif [[ $action = "stop" ]]; then
Stop_service
fi
else
menu
fi