This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
control.sh
executable file
·250 lines (223 loc) · 5.92 KB
/
control.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
#!/bin/bash
# System:
# 1. Darwin or Linux
# Python:
# 1. version: 2.7.3+/3.1.2+ is recommended
# 2. other:
# * python should belong to current user, otherwise virtualenv is required
# * pip is required
# Node.js:
# 1. version: 4.7.0+ is recommended
# 2. other:
# * npm is required
set -u
set -e
cd "$(dirname "$0")"
readonly G_ROOT_DIR=`pwd`
readonly G_WEB_DIR="${G_ROOT_DIR}/web"
readonly G_API_DIR="${G_ROOT_DIR}/api"
readonly G_VENV_DIR="${G_ROOT_DIR}/venv"
G_VERSION='none'
if [ -e .git ]; then
G_VERSION=`git rev-parse HEAD`
fi
G_CONDA=''
PS1='$'
cutoff() {
echo "============================================================="
}
help() {
echo "${0} <start|stop|reload|terminate|version>"
exit 1
}
version() {
if [ ${G_VERSION}x != 'x' ]; then
cutoff
echo "local Curve version: ${G_VERSION}"
cutoff
fi
}
check_web() {
readonly BUILD_PATH="${G_WEB_DIR}/build"
readonly BUILD_VERSION_FILE="${BUILD_PATH}/version"
BUILD_VERSION=''
if [ -e ${BUILD_VERSION_FILE} ]; then
BUILD_VERSION=`cat ${BUILD_VERSION_FILE}`
fi
readonly DEPLOY_PATH="${G_API_DIR}/curve/web"
readonly DEPLOY_VERSION_FILE="${DEPLOY_PATH}/version"
DEPLOY_VERSION=''
if [ -e ${DEPLOY_VERSION_FILE} ]; then
DEPLOY_VERSION=`cat ${DEPLOY_VERSION_FILE}`
fi
if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${DEPLOY_VERSION}x ]; then
return
fi
cutoff
echo "build web..."
if [ ${G_VERSION}x == 'x' -o ${G_VERSION}x != ${BUILD_VERSION}x ]; then
cd ${G_WEB_DIR}
npm install
npm run build
echo ${G_VERSION} > ${BUILD_VERSION_FILE}
fi
if [ -e ${DEPLOY_PATH} ]; then
rm -rf ${DEPLOY_PATH}
fi
mv ${BUILD_PATH} ${DEPLOY_PATH}
echo "web built."
cutoff
}
check_py() {
readonly VENV_VERSION_FILE="${G_VENV_DIR}/version"
VENV_VERSION=''
if [ -e ${VENV_VERSION_FILE} ]; then
VENV_VERSION=`cat ${VENV_VERSION_FILE}`
fi
if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${VENV_VERSION}x ]; then
return
fi
cutoff
echo "deploy venv..."
if [ ! -e "${G_ROOT_DIR}/venv" ]; then
# pip install --upgrade pip
# pip install virtualenv
virtualenv --no-site-packages ${G_VENV_DIR}
fi
source ${G_VENV_DIR}/bin/activate
pip install -r ${G_API_DIR}/requirements.txt
if [ ! -e ${G_VENV_DIR}/bin/curve_uwsgi ]; then
cd ${G_VENV_DIR}/bin
ln -s uwsgi curve_uwsgi
cd -
fi
echo ${G_VERSION} > ${VENV_VERSION_FILE}
deactivate
echo "venv deployed."
cutoff
}
check_api() {
readonly SWAGGER_UI_DIR="${G_API_DIR}/curve/web/swagger-ui"
readonly SWAGGER_UI_VERSION_FILE="${SWAGGER_UI_DIR}/version"
SWAGGER_UI_VERSION=''
if [ -e ${SWAGGER_UI_VERSION_FILE} ]; then
SWAGGER_UI_VERSION=`cat ${SWAGGER_UI_VERSION_FILE}`
fi
if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${SWAGGER_UI_VERSION}x ]; then
return
fi
cutoff
echo "deploy api..."
cd ${G_ROOT_DIR}
source ${G_VENV_DIR}/bin/activate
pip install swagger-py-codegen==0.2.9
swagger_py_codegen --ui --spec -s doc/web_api.yaml api -p curve
deactivate
if [ -e ${G_API_DIR}/curve/web/swagger-ui ]; then
rm -rf ${G_API_DIR}/curve/web/swagger-ui
fi
mv ${G_API_DIR}/curve/static/swagger-ui ${G_API_DIR}/curve/web/
if [ -e ${G_API_DIR}/curve/web/static/v1 ]; then
rm -rf ${G_API_DIR}/curve/web/static/v1
fi
mv ${G_API_DIR}/curve/static/v1 ${G_API_DIR}/curve/web/static/
rm -rf ${G_API_DIR}/curve/static
patch ${G_API_DIR}/curve/web/swagger-ui/index.html ${G_ROOT_DIR}/opt/swagger-ui/index.html.patch
echo ${G_VERSION} > ${SWAGGER_UI_VERSION_FILE}
echo "api deployed."
cutoff
}
check_path() {
mkdir -p ${G_API_DIR}/log
}
check() {
check_web
check_py
check_api
check_path
}
start() {
if [ -e ${G_API_DIR}/uwsgi.pid ]; then
PID=`cat ${G_API_DIR}/uwsgi.pid`
if [ `ps -ef | fgrep curve_uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then
echo "Curve is running."
return
fi
fi
check
if [ `ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
fi
echo "start Curve..."
source ${G_VENV_DIR}/bin/activate
cd ${G_API_DIR}
${G_VENV_DIR}/bin/curve_uwsgi uwsgi.ini
echo "Curve started."
}
stop() {
echo "stop Curve..."
cd ${G_API_DIR}
source ${G_VENV_DIR}/bin/activate
[ -e uwsgi.pid ] && ${G_VENV_DIR}/bin/curve_uwsgi --stop uwsgi.pid
echo "Curve stopped."
}
reload() {
check
if [ -e ${G_API_DIR}/uwsgi.pid ]; then
PID=`cat ${G_API_DIR}/uwsgi.pid`
if [ `ps -ef | fgrep curve_uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then
echo "reload Curve..."
source ${G_VENV_DIR}/bin/activate
cd ${G_API_DIR}
${G_VENV_DIR}/bin/curve_uwsgi --reload uwsgi.pid
echo "Curve reloaded."
return
fi
fi
echo "clean Curve..."
if [ `ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
fi
echo "start Curve..."
source ${G_VENV_DIR}/bin/activate
cd ${G_API_DIR}
${G_VENV_DIR}/bin/curve_uwsgi uwsgi.ini
echo "Curve reloaded."
}
terminate() {
echo "terminate Curve..."
if [ `ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then
ps -ef | fgrep curve_uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9
echo "Curve terminated."
fi
echo "Curve is not running."
}
case "${1}" in
check)
version
check
;;
start)
version
start
;;
stop)
stop
;;
reload)
version
reload
;;
terminate)
terminate
;;
help)
help
;;
version)
version
;;
*)
help
;;
esac