-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
admin.sh
executable file
·186 lines (165 loc) · 6.54 KB
/
admin.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
#########################################################################
# File APP: admin.sh
# Author: wushuiyong
# mail: [email protected]
# Created Time: 2018年11月03日 星期六 06时09分46秒
#########################################################################
# Update Time : 2019-03-05
# Author: alenx <[email protected]>
# -->> 新增ubuntu初始化,全面支持Ubuntu环境(16.x/18.x)
#########################################################################
#!/usr/bin/env bash
# ubuntu 高版本 sh 指向的是 dash 而非 bash。 dash 无法使用 function 关键字以及 source 等命令。
# 如果检测到 sh 指向的是 dash, 那么将使用 bash 重新执行脚本,然后在参数末尾加上一个 flag, 表示此次运行是修正过的, 避免陷入死循环。
fix_ubuntu_bash="fix-sh-in-ubuntu"
if [[ ! -n "$(echo $@ | grep ${fix_ubuntu_bash})" ]]; then
if [[ -n "$(ls -l /bin/sh | grep "dash")" ]]; then
bash $0 $@ ${fix_ubuntu_bash}
exit
fi
fi
APP="waller.py"
function init() {
echo "Initing walle"
echo "----------------"
SystemName
pip install virtualenv
if [ ! -d "venv" ]; then
virtualenv --no-site-packages venv # 注意:安装失败请指定python路径. mac 可能会有用anaconda的python. 请不要mac试用, 麻烦多多
fi
requirement
echo "************************************************"
echo -e "\033[32m init walle success \033[0m"
echo -e "\033[32m welcome to walle 2.0 \033[0m"
}
function requirement() {
source ./venv/bin/activate
pip install -r ./requirements/prod.txt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
}
function SystemName() {
source /etc/os-release
case $ID in
centos|fedora|rhel)
which pip
if [ $? != "0" ]; then
wget https://bootstrap.pypa.io/3.3/get-pip.py
python get-pip.py
fi
echo "安装/更新可能缺少的依赖: mysql-community-devel gcc gcc-c++ python-devel"
# 安装python-devel报错 yum install yum-utils ,yum clean all yum -y install python-devel
sudo yum install -y yum-utils mariadb-devel mysql-devel --skip-broken gcc gcc-c++ python-devel MySQL-python
;;
debian|ubuntu|devuan)
echo "安装/更新可能缺少的依赖: libmysqld-dev libmysqlclient-dev python-dev python-virtualenv python-pip"
sudo apt update -y
sudo apt install -y libmysqld-dev libmysqlclient-dev python-dev python-virtualenv python-pip
;;
raspbian)
echo "安装/更新可能缺少的依赖"
sudo apt update -y
sudo apt install -y gcc g++ python-dev virtualenv python-pip libpq-dev libffi-dev libssl-dev libmariadbd18 libmariadbd-dev
;;
*)
exit 1
;;
esac
}
function start() {
echo "Starting walle"
echo "----------------"
source ./venv/bin/activate
mkdir -p logs
nohup python ${APP} >> logs/runtime.log 2>&1 &
echo -e "Start walle: [\033[32m ok \033[0m]"
echo -e "runtime: \033[32m logs/runtime.log \033[0m"
echo -e "error: \033[32m logs/error.log \033[0m"
}
function stop() {
echo "Stoping walle"
echo "----------------"
# 获取进程 PID
PID=$(ps -ef | grep ${APP} | grep -v grep | awk '{print $2}')
# 杀死进程
kill -9 ${PID}
echo -e "Stop walle: [\033[32m ok \033[0m]"
}
function restart() {
stop
echo ""
start
}
function upgrade() {
echo "Upgrading walle"
echo "----------------"
cd $(dirname $0)
echo -e "建议先暂存本地修改\033[33m git stash\033[0m,更新后再弹出\033[33m git stash pop\033[0m,处理冲突。"
source ./venv/bin/activate
git pull
}
function walle_banner() {
echo " ";
echo " llllllllllllll ";
echo " l::::l l::::l ";
echo "wwwwwww wwwww wwwwww aaaaaaaaaaaaa l::::l l::::l eeeeeeeeeeee ";
echo " w:::::w w:::::w w:::::w a::::::::::::a l::::l l::::l ee::::::::::::ee ";
echo " w:::::w w:::::::w w:::::w aaaaaaaaa:::::a l::::l l::::l e::::::eeeee:::::ee";
echo " w:::::w w:::::::::w w:::::w a::::a l::::l l::::l e::::::e e:::::e";
echo " w:::::w w:::::w:::::w w:::::w aaaaaaa:::::a l::::l l::::l e:::::::eeeee::::::e";
echo " w:::::w w:::::w w:::::w w:::::w aa::::::::::::a l::::l l::::l e:::::::::::::::::e ";
echo " w:::::w:::::w w:::::w:::::w a::::aaaa::::::a l::::l l::::l e::::::eeeeeeeeeee ";
echo " w:::::::::w w:::::::::w a::::a a:::::a l::::l l::::l e:::::::e ";
echo " w:::::::w w:::::::w a::::a a:::::a l::::l l::::l e::::::::e ";
echo " w:::::w w:::::w a:::::aaaa::::::a l::::l l::::l e::::::::eeeeeeee ";
echo " w:::w w:::w a::::::::::aa::a l::::: l:::::l ee:::::::::::::e ";
echo " www www aaaaaaaaaa aaaa llllllllllllllll eeeeeeeeeeeeee ";
echo " ";
}
function migration() {
echo "Migration walle"
echo "----------------"
source ./venv/bin/activate
export FLASK_APP=waller.py
flask db upgrade
if [ $? == "0" ]; then
echo -e "Migration: [\033[32m ok \033[0m]"
else
echo -e "Migration: [\033[31m fail \033[0m]"
fi
}
case "$1" in
require)
requirement;;
init )
walle_banner
init
;;
start )
walle_banner
start
;;
stop )
walle_banner
stop
;;
restart )
walle_banner
restart
;;
upgrade )
walle_banner
upgrade
requirement
migration
echo -e "\033[32m walle 更新成功. \033[0m \033[33m 建议重启服务 sh admin.sh restart\033[0m"
;;
migration )
walle_banner
migration
;;
* )
walle_banner
echo "************************************************"
echo "Usage: sh admin {init|require|start|stop|restart|upgrade|migration}"
echo "************************************************"
;;
esac