mdserver-web/scripts/init.d/mw.tpl

336 lines
9.3 KiB
Smarty
Raw Normal View History

2018-12-12 05:06:02 -05:00
#!/bin/bash
2019-03-15 02:33:33 -04:00
# chkconfig: 2345 55 25
# description: MW Cloud Service
### BEGIN INIT INFO
2022-08-07 08:44:20 -04:00
# Provides: Midoks
2019-03-15 02:33:33 -04:00
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts mw
# Description: starts the mw
### END INIT INFO
2021-11-11 04:29:41 -05:00
PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
2022-11-21 12:05:20 -05:00
export LANG=en_US.UTF-8
2018-12-12 05:06:02 -05:00
mw_path={$SERVER_PATH}
2021-10-31 04:09:41 -04:00
PATH=$PATH:$mw_path/bin
2021-11-02 06:20:35 -04:00
2022-06-14 13:01:23 -04:00
2021-11-02 06:20:35 -04:00
if [ -f $mw_path/bin/activate ];then
source $mw_path/bin/activate
fi
2022-06-26 11:25:27 -04:00
mw_start_panel()
{
isStart=`ps -ef|grep 'gunicorn -c setting.py app:app' |grep -v grep|awk '{print $2}'`
if [ "$isStart" == '' ];then
2022-11-14 04:25:21 -05:00
echo -e "starting mw-panel... \c"
2022-06-26 11:25:27 -04:00
cd $mw_path && gunicorn -c setting.py app:app
port=$(cat ${mw_path}/data/port.pl)
isStart=""
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 15 ];then
break;
2018-12-12 05:06:02 -05:00
fi
2022-06-26 11:25:27 -04:00
done
if [ "$isStart" == '' ];then
2022-11-14 04:25:21 -05:00
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 ${mw_path}/logs/error.log
echo '------------------------------------------------------'
echo -e "\033[31mError: mw-panel service startup failed.\033[0m"
return;
2022-06-26 11:25:27 -04:00
fi
echo -e "\033[32mdone\033[0m"
2018-12-12 05:06:02 -05:00
else
2022-11-14 06:20:11 -05:00
echo "starting mw-panel... mw(pid $(echo $isStart)) already running"
2018-12-12 05:06:02 -05:00
fi
2022-06-26 11:25:27 -04:00
}
2018-12-12 05:45:33 -05:00
2022-06-26 11:25:27 -04:00
mw_start_task()
{
2018-12-12 05:45:33 -05:00
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" == '' ];then
2022-11-14 06:20:11 -05:00
echo -e "starting mw-tasks... \c"
2022-06-26 11:25:27 -04:00
cd $mw_path && python3 task.py >> ${mw_path}/logs/task.log 2>&1 &
sleep 0.3
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" == '' ];then
2022-11-14 04:25:21 -05:00
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 $mw_path/logs/task.log
echo '------------------------------------------------------'
echo -e "\033[31mError: mw-tasks service startup failed.\033[0m"
return;
2022-06-26 11:25:27 -04:00
fi
echo -e "\033[32mdone\033[0m"
2018-12-12 05:45:33 -05:00
else
2022-11-14 05:12:09 -05:00
echo "starting mw-tasks... mw-tasks (pid $(echo $isStart)) already running"
2018-12-12 05:45:33 -05:00
fi
2018-12-12 05:06:02 -05:00
}
2022-06-26 11:25:27 -04:00
mw_start()
{
mw_start_task
2022-07-04 11:22:31 -04:00
mw_start_panel
2022-06-26 11:25:27 -04:00
}
2018-12-12 05:06:02 -05:00
2022-07-11 01:56:00 -04:00
# /www/server/mdserver-web/tmp/panelTask.pl && service mw restart_task
2022-06-26 11:25:27 -04:00
mw_stop_task()
2018-12-12 06:21:44 -05:00
{
if [ -f $mw_path/tmp/panelTask.pl ];then
2022-11-14 05:12:09 -05:00
echo -e "\033[32mthe task is running and cannot be stopped\033[0m"
2022-07-04 11:06:50 -04:00
exit 0
fi
2022-11-14 05:12:09 -05:00
echo -e "stopping mw-tasks... \c";
2018-12-12 06:21:44 -05:00
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"
2022-06-26 11:25:27 -04:00
}
2018-12-12 05:06:02 -05:00
2022-06-26 11:25:27 -04:00
mw_stop_panel()
{
2022-11-14 05:12:09 -05:00
echo -e "stopping mw-panel... \c";
2018-12-12 06:21:44 -05:00
arr=`ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}'`
2022-06-26 11:25:27 -04:00
for p in ${arr[@]}
2018-12-12 06:21:44 -05:00
do
2022-11-14 04:25:21 -05:00
kill -9 $p &>/dev/null
2018-12-12 06:21:44 -05:00
done
2022-10-05 21:32:46 -04:00
pidfile=${mw_path}/logs/mw.pid
2018-12-12 06:21:44 -05:00
if [ -f $pidfile ];then
2022-06-26 11:25:27 -04:00
rm -f $pidfile
2018-12-12 06:21:44 -05:00
fi
echo -e "\033[32mdone\033[0m"
}
2022-06-26 11:25:27 -04:00
mw_stop()
{
mw_stop_task
2022-07-04 11:22:31 -04:00
mw_stop_panel
2022-06-26 11:25:27 -04:00
}
2018-12-12 06:21:44 -05:00
mw_status()
{
2022-11-14 04:26:08 -05:00
isStart=$(ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}')
if [ "$isStart" != '' ];then
echo -e "\033[32mmw (pid $(echo $isStart)) already running\033[0m"
else
echo -e "\033[31mmw not running\033[0m"
fi
isStart=$(ps aux |grep 'task.py'|grep -v grep|awk '{print $2}')
if [ "$isStart" != '' ];then
echo -e "\033[32mmw-task (pid $isStart) already running\033[0m"
else
echo -e "\033[31mmw-task not running\033[0m"
fi
2018-12-12 06:21:44 -05:00
}
mw_reload()
{
isStart=$(ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}')
if [ "$isStart" != '' ];then
2022-11-14 05:12:09 -05:00
echo -e "reload mw... \c";
2018-12-12 06:21:44 -05:00
arr=`ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}'`
for p in ${arr[@]}
do
kill -9 $p
done
2018-12-20 21:07:24 -05:00
cd $mw_path && gunicorn -c setting.py app:app
2018-12-12 06:21:44 -05:00
isStart=`ps aux|grep 'gunicorn -c setting.py app:app'|grep -v grep|awk '{print $2}'`
if [ "$isStart" == '' ];then
2022-11-14 04:26:08 -05:00
echo -e "\033[31mfailed\033[0m"
echo '------------------------------------------------------'
tail -n 20 $mw_path/logs/error.log
echo '------------------------------------------------------'
echo -e "\033[31mError: mw service startup failed.\033[0m"
return;
2018-12-12 06:21:44 -05:00
fi
echo -e "\033[32mdone\033[0m"
else
echo -e "\033[31mmw not running\033[0m"
2018-12-12 05:06:02 -05:00
mw_start
2018-12-12 06:21:44 -05:00
fi
}
2022-11-27 22:50:55 -05:00
mw_close(){
echo 'True' > $mw_path/data/close.pl
}
mw_open()
{
if [ -f $mw_path/data/close.pl ];then
rm -rf $mw_path/data/close.pl
fi
}
2018-12-12 06:21:44 -05:00
error_logs()
{
tail -n 100 $mw_path/logs/error.log
}
2022-10-08 12:30:03 -04:00
mw_update()
{
2022-11-18 08:15:26 -05:00
cn=$(curl -fsSL -m 10 http://ipinfo.io/json | grep "\"country\": \"CN\"")
if [ ! -z "$cn" ];then
curl -fsSL https://cdn.jsdelivr.net/gh/midoks/mdserver-web@latest/scripts/update.sh | bash
else
curl -fsSL https://raw.githubusercontent.com/midoks/mdserver-web/master/scripts/update.sh | bash
fi
2022-10-08 12:30:03 -04:00
}
mw_update_dev()
{
2022-11-18 08:15:26 -05:00
cn=$(curl -fsSL -m 10 http://ipinfo.io/json | grep "\"country\": \"CN\"")
if [ ! -z "$cn" ];then
2022-11-18 08:40:06 -05:00
curl -fsSL https://gitee.com/midoks/mdserver-web/raw/dev/scripts/update_dev.sh | bash
2022-11-18 08:15:26 -05:00
else
curl -fsSL https://raw.githubusercontent.com/midoks/mdserver-web/dev/scripts/update_dev.sh | bash
fi
2022-11-12 09:24:55 -05:00
cd /www/server/mdserver-web
2022-10-08 12:30:03 -04:00
}
2022-10-10 07:54:23 -04:00
mw_install_app()
{
bash $mw_path/scripts/quick/app.sh
}
2022-11-12 03:11:06 -05:00
mw_close_admin_path(){
if [ -f $mw_path/data/admin_path.pl ]; then
rm -rf $mw_path/data/admin_path.pl
fi
}
2022-11-13 03:14:39 -05:00
mw_force_kill()
{
PLIST=`ps -ef|grep app:app |grep -v grep|awk '{print $2}'`
for i in $PLIST
do
kill -9 $i
done
pids=`ps -ef|grep task.py | grep -v grep |awk '{print $2}'`
arr=($pids)
for p in ${arr[@]}
do
kill -9 $p
done
}
2022-11-13 02:54:12 -05:00
mw_debug(){
2022-11-13 02:55:28 -05:00
mw_stop
2022-11-13 03:14:39 -05:00
mw_force_kill
2022-11-13 02:54:12 -05:00
port=7200
if [ -f $mw_path/data/port.pl ];then
port=$(cat $mw_path/data/port.pl)
fi
2022-11-13 02:55:28 -05:00
2022-11-14 06:56:31 -05:00
if [ -d /www/server/mdserver-web ];then
cd /www/server/mdserver-web
fi
2022-11-13 02:54:12 -05:00
gunicorn -b :$port -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 app:app
}
2018-12-12 06:21:44 -05:00
case "$1" in
'start') mw_start;;
'stop') mw_stop;;
'reload') mw_reload;;
2018-12-12 08:35:04 -05:00
'restart')
mw_stop
mw_start;;
2022-06-26 11:25:27 -04:00
'restart_panel')
mw_stop_panel
mw_start_panel;;
2022-06-28 06:24:59 -04:00
'restart_task')
mw_stop_task
mw_start_task;;
2018-12-12 06:21:44 -05:00
'status') mw_status;;
'logs') error_logs;;
2022-11-27 22:50:55 -05:00
'close') mw_close;;
'open') mw_open;;
2022-10-08 12:30:03 -04:00
'update') mw_update;;
'update_dev') mw_update_dev;;
2022-10-10 07:54:23 -04:00
'install_app') mw_install_app;;
2022-11-12 03:11:06 -05:00
'close_admin_path') mw_close_admin_path;;
2022-11-13 02:54:12 -05:00
'debug') mw_debug;;
2019-01-15 02:34:40 -05:00
'default')
cd $mw_path
2022-06-13 05:48:21 -04:00
port=7200
if [ -f $mw_path/data/port.pl ];then
port=$(cat $mw_path/data/port.pl)
fi
2022-06-14 13:01:23 -04:00
if [ ! -f $mw_path/data/default.pl ];then
echo -e "\033[33mInstall Failed\033[0m"
2022-06-23 00:55:57 -04:00
exit 1
2022-06-14 13:01:23 -04:00
fi
2019-01-15 02:34:40 -05:00
password=$(cat $mw_path/data/default.pl)
if [ -f $mw_path/data/domain.conf ];then
address=$(cat $mw_path/data/domain.conf)
fi
if [ -f $mw_path/data/admin_path.pl ];then
auth_path=$(cat $mw_path/data/admin_path.pl)
fi
2022-06-22 23:40:06 -04:00
if [ "$address" = "" ];then
2022-06-26 05:35:59 -04:00
v4=$(python3 $mw_path/tools.py getServerIp 4)
v6=$(python3 $mw_path/tools.py getServerIp 6)
2022-06-26 05:24:43 -04:00
2022-06-26 05:35:03 -04:00
if [ "$v4" != "" ] && [ "$v6" != "" ]; then
2022-11-04 13:01:13 -04:00
address="MW-Panel-Url-Ipv4: http://$v4:$port$auth_path \nMW-Panel-Url-Ipv6: http://[$v6]:$port$auth_path"
2022-06-26 05:35:03 -04:00
elif [ "$v4" != "" ]; then
2022-11-04 13:01:13 -04:00
address="MW-Panel-Url: http://$v4:$port$auth_path"
2022-06-26 05:35:03 -04:00
elif [ "$v6" != "" ]; then
2022-10-24 09:44:34 -04:00
if [ ! -f $mw_path/data/ipv6.pl ];then
2022-10-24 13:43:29 -04:00
# Need to restart ipv6 to take effect
2022-11-22 22:46:26 -05:00
echo 'True' > $mw_path/data/ipv6.pl
2022-10-24 09:44:34 -04:00
mw_stop
mw_start
fi
2022-11-04 13:01:13 -04:00
address="MW-Panel-Url: http://[$v6]:$port$auth_path"
2022-06-22 23:40:06 -04:00
else
2022-11-04 13:01:13 -04:00
address="MW-Panel-Url: http://you-network-ip:$port$auth_path"
2022-06-22 23:40:06 -04:00
fi
else
2022-11-04 13:01:13 -04:00
address="MW-Panel-Url: http://$address:$port$auth_path"
2022-06-21 08:17:55 -04:00
fi
2022-06-21 00:57:29 -04:00
2022-10-01 07:45:45 -04:00
show_panel_ip="$port|"
2019-01-15 02:34:40 -05:00
echo -e "=================================================================="
echo -e "\033[32mMW-Panel default info!\033[0m"
echo -e "=================================================================="
2022-06-22 23:40:06 -04:00
echo -e "$address"
2021-05-08 12:25:56 -04:00
echo -e `python3 $mw_path/tools.py username`
2019-01-15 02:34:40 -05:00
echo -e "password: $password"
echo -e "\033[33mWarning:\033[0m"
2022-10-01 07:49:00 -04:00
echo -e "\033[33mIf you cannot access the panel. \033[0m"
echo -e "\033[33mrelease the following port (${show_panel_ip}888|80|443|22) in the security group.\033[0m"
2019-01-15 02:34:40 -05:00
echo -e "=================================================================="
;;
2022-10-05 23:12:01 -04:00
*)
cd $mw_path && python3 $mw_path/tools.py cli $1
;;
2022-06-21 08:17:55 -04:00
esac