forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
230 lines (214 loc) · 7.2 KB
/
init.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
#!/bin/bash
# chkconfig: 2345 55 25
# description: bt Cloud Service
### BEGIN INIT INFO
# Provides: bt
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts bt
# Description: starts the bt
### END INIT INFO
panel_path=/www/server/panel
pidfile=$panel_path/logs/panel.pid
cd $panel_path
py26=$(python -V 2>&1|grep '2.6.')
if [ "$py26" != "" ];then
pythonV=python3
fi
panel_start()
{
isStart=`ps aux|grep 'runserver:app'|grep -v grep|awk '{print $2}'`
if [ "$isStart" == '' ];then
echo -e "Starting Bt-Panel.\c"
rm -f $pidfile
gunicorn -c runconfig.py runserver:app
port=$(cat /www/server/panel/data/port.pl)
isStart=""
n=0
while [[ "$isStart" == "" ]];
do
echo -e ".\c"
sleep 0.5
isStart=$(lsof -n -P -i:$port|grep LISTEN|grep -v grep|awk '{print $2}'|xargs)
let n+=1
if [ $n -gt 8 ];then
break;
fi
done
if [ "$isStart" == '' ];then
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 $panel_path/logs/error.log
echo '------------------------------------------------------'
echo -e "\033[31mError: BT-Panel service startup failed.\033[0m"
fi
echo -e " \033[32mdone\033[0m"
else
echo "Starting Bt-Panel... Bt-Panel (pid $(echo $isStart)) already running"
fi
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" == '' ];then
echo -e "Starting Bt-Tasks... \c"
nohup python task.py >> /www/server/panel/logs/task.log 2>&1 &
sleep 0.2
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" == '' ];then
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 /www/server/panel/logs/task.log
echo '------------------------------------------------------'
echo -e "\033[31mError: BT-Task service startup failed.\033[0m"
return;
fi
echo -e " \033[32mdone\033[0m"
else
echo "Starting Bt-Tasks... Bt-Tasks (pid $isStart) already running"
fi
}
panel_stop()
{
echo -e "Stopping Bt-Tasks...\c";
pids=$(ps aux | grep 'task.py'|grep -v grep|awk '{print $2}')
arr=($pids)
for p in ${arr[@]}
do
kill -9 $p
done
echo -e " \033[32mdone\033[0m"
echo -e "Stopping Bt-Panel...\c";
arr=`ps aux|grep 'runserver:app'|grep -v grep|awk '{print $2}'`
for p in ${arr[@]}
do
kill -9 $p &>/dev/null
done
if [ -f $pidfile ];then
rm -f $pidfile
fi
echo -e " \033[32mdone\033[0m"
}
panel_status()
{
port=$(cat /www/server/panel/data/port.pl)
isStart=$(lsof -i:$port|grep LISTEN|grep -v grep|awk '{print $2}'|xargs)
if [ "$isStart" != '' ];then
echo -e "\033[32mBt-Panel (pid $(echo $isStart)) already running\033[0m"
else
echo -e "\033[31mBt-Panel not running\033[0m"
fi
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" != '' ];then
echo -e "\033[32mBt-Task (pid $isStart) already running\033[0m"
else
echo -e "\033[31mBt-Task not running\033[0m"
fi
}
panel_reload()
{
isStart=$(ps aux|grep 'runserver:app'|grep -v grep|awk '{print $2}')
if [ "$isStart" != '' ];then
echo -e "Reload Bt-Panel.\c";
arr=`ps aux|grep 'runserver:app'|grep -v grep|awk '{print $2}'`
for p in ${arr[@]}
do
kill -9 $p
done
rm -f $pidfile
gunicorn -c runconfig.py runserver:app
port=$(cat /www/server/panel/data/port.pl)
isStart=""
n=0
while [[ "$isStart" == "" ]];
do
echo -e ".\c"
sleep 0.5
isStart=$(lsof -n -P -i:$port|grep LISTEN|grep -v grep|awk '{print $2}'|xargs)
let n+=1
if [ $n -gt 8 ];then
break;
fi
done
if [ "$isStart" == '' ];then
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 $panel_path/logs/error.log
echo '------------------------------------------------------'
echo -e "\033[31mError: BT-Panel service startup failed.\033[0m"
return;
fi
echo -e " \033[32mdone\033[0m"
else
echo -e "\033[31mBt-Panel not running\033[0m"
panel_start
fi
}
install_used()
{
if [ ! -f $panel_path/aliyun.pl ];then
return;
fi
password=$(cat /dev/urandom | head -n 16 | md5sum | head -c 12)
username=$($pythonV $panel_path/tools.py panel $password)
echo "$password" > $panel_path/default.pl
rm -f $panel_path/aliyun.pl
}
error_logs()
{
tail -n 100 $panel_path/logs/error.log
}
case "$1" in
'start')
install_used
panel_start
;;
'stop')
panel_stop
;;
'restart')
panel_stop
panel_start
;;
'reload')
panel_reload
;;
'status')
panel_status
;;
'logs')
error_logs
;;
'panel')
python $panel_path/tools.py cli $2
;;
'default')
port=$(cat $panel_path/data/port.pl)
password=$(cat $panel_path/default.pl)
if [ -f $panel_path/data/domain.conf ];then
address=$(cat $panel_path/data/domain.conf)
fi
if [ -f $panel_path/data/admin_path.pl ];then
auth_path=$(cat $panel_path/data/admin_path.pl)
fi
if [ "$address" = "" ];then
address=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
fi
pool=http
if [ -f $panel_path/data/ssl.pl ];then
pool=https
fi
echo -e "=================================================================="
echo -e "\033[32mBT-Panel default info!\033[0m"
echo -e "=================================================================="
echo "Bt-Panel-URL: $pool://$address:$port$auth_path"
echo -e `python $panel_path/tools.py username`
echo -e "password: $password"
echo -e "\033[33mWarning:\033[0m"
echo -e "\033[33mIf you cannot access the panel, \033[0m"
echo -e "\033[33mrelease the following port (8888|888|80|443|20|21) in the security group\033[0m"
echo -e "=================================================================="
;;
*)
python $panel_path/tools.py cli $1
;;
esac