-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.sh
executable file
·232 lines (199 loc) · 6.38 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
#!/usr/bin/env bash
set +e
set -o noglob
# Set Colors
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
# Headers and Logging
underline() {
printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() {
printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() {
printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() {
printf "${white}%s${reset}\n" "$@"
}
info() {
printf "${white}➜ %s${reset}\n" "$@"
}
success() {
printf "${green}✔ %s${reset}\n" "$@"
}
error() {
printf "${red}✖ %s${reset}\n" "$@"
}
warn() {
printf "${tan}➜ %s${reset}\n" "$@"
}
bold() {
printf "${bold}%s${reset}\n" "$@"
}
note() {
printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
CURRENT_PATH=$(
cd $(dirname $0)
pwd
)
PYTHON_PATH=/opt/miniconda3
item=0
function help() {
echo "Usage: bash $0 8099 /opt/packages.tar.gz"
}
if [[ $# -gt 3 ]]; then
help
exit 127
elif [[ $# -lt 2 ]]; then
help
exit 127
elif [[ $# -eq 2 ]]; then
APP_PORT=${1}
RESOURCE_PATH=${2}
else
APP_PORT=8099
fi
# notary is not enabled by default
with_notary=$false
# clair is not enabled by default
with_clair=$false
function stop() {
source ${CURRENT_PATH}/scripts/shutdown.sh >> ${CURRENT_PATH}/logs/run.log 2>&1 &
spid=$!
echo "Shutdown app pid=${spid}"
wait ${spid}
}
function unzip_resource() {
if [[ ! -f ${RESOURCE_PATH} ]]; then
help
exit 127
echo "${RESOURCE_PATH} Not Found!"
else
tar zxf ${RESOURCE_PATH} -C ${CURRENT_PATH}/apps && chown -R root.root ${CURRENT_PATH}
if [[ $? -ne 0 ]]; then
exit 127
fi
fi
}
function redis() {
if [[ ! -f ${REDIS_CONF} ]]; then
source ${CURRENT_PATH}/scripts/install-redis.sh >> ${CURRENT_PATH}/logs/run.log 2>&1 &
cpid=$!
echo "Install redis pid=${cpid}"
sleep 3
wait ${cpid}
# main
fi
}
function set_python() {
USER_ENV="/${USER}/.bashrc"
if [[ -d "${PYTHON_PATH}" ]]; then
rm -rf ${PYTHON_PATH}
tar -zxf ${CURRENT_PATH}/libs/miniconda3.tar.gz -C /opt >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
info "${PYTHON_PATH} reinstall success!"
fi
else
tar -zxf ${CURRENT_PATH}/libs/miniconda3.tar.gz -C /opt >/dev/null 2>&1
info "${PYTHON_PATH} install success!"
fi
if [[ -f "${USER_ENV}" ]]; then
sed -i "/LANG.*UTF-8$/d" ${USER_ENV}
sed -i "/PATH.*python/d" ${USER_ENV}
sed -i "/PATH.*miniconda3/d" ${USER_ENV}
sed -i "/PATH.*HISTTIMEFORMAT/d" ${USER_ENV}
sed -i "/PATH.*HISTFILESIZE/d" ${USER_ENV}
echo -e "export HISTTIMEFORMAT=\"%Y-%m-%d %H:%M:%S \"" >>${USER_ENV}
echo -e "export HISTFILESIZE=10000" >>${USER_ENV}
echo -e "export LANG=en_US.UTF-8\nexport PATH=${PYTHON_PATH}/bin:\$PATH" >>${USER_ENV}
export PATH=${PYTHON_PATH}/bin:$PATH
source ${USER_ENV}
fi
export PATH=$PATH:${PYTHON_PATH}/bin
${PYTHON_PATH}/bin/python -V
}
function start_app() {
if [[ -d "${CURRENT_PATH}/logs" ]]; then
rm -fr "${CURRENT_PATH}/logs"
mkdir "${CURRENT_PATH}/logs"
else
mkdir "${CURRENT_PATH}/logs"
fi
cd ${CURRENT_PATH} && rm -f celerybeat-schedule*
[[ -d "${CURRENT_PATH}/logs" ]] && rm -fr "${CURRENT_PATH}/logs" && mkdir "${CURRENT_PATH}/logs"
[[ -f "${CURRENT_PATH}/db.sqlite3" ]] && rm -f "${CURRENT_PATH}/db.sqlite3"
[[ -d "${CURRENT_PATH}/apps/__pycache__" ]] && rm -fr "${CURRENT_PATH}/apps/__pychche__"
[[ -d "${CURRENT_PATH}/apps/account/migrations" ]] && rm -fr "${CURRENT_PATH}/apps/account/migrations"
[[ -d "${CURRENT_PATH}/apps/account/__pycache__" ]] && rm -fr "${CURRENT_PATH}/apps/account/__pycache__"
[[ -d "${CURRENT_PATH}/apps/deploy/migrations" ]] && rm -fr "${CURRENT_PATH}/apps/deploy/migrations"
[[ -d "${CURRENT_PATH}/apps/deploy/__pycache__" ]] && rm -fr "${CURRENT_PATH}/apps/deploy/__pycache__"
[[ -d "${CURRENT_PATH}/apps/host/migrations" ]] && rm -fr "${CURRENT_PATH}/apps/host/migrations"
[[ -d "${CURRENT_PATH}/apps/host/__pycache__" ]] && rm -fr "${CURRENT_PATH}/apps/host/__pycache__"
${PYTHON_PATH}/bin/python manage.py makemigrations deploy >> ${CURRENT_PATH}/logs/db.log 2>&1 &
sleep 3
${PYTHON_PATH}/bin/python manage.py makemigrations host >> ${CURRENT_PATH}/logs/db.log 2>&1 &
sleep 3
${PYTHON_PATH}/bin/python manage.py makemigrations account >> ${CURRENT_PATH}/logs/db.log 2>&1 &
sleep 3
${PYTHON_PATH}/bin/python manage.py migrate >> ${CURRENT_PATH}/logs/db.log 2>&1 &
sleep 3
nohup ${PYTHON_PATH}/bin/python manage.py runserver 0.0.0.0:${APP_PORT} >> ${CURRENT_PATH}/logs/run.log 2>&1 &
# nohup ${PYTHON_PATH}/bin/flask run --host=0.0.0.0 -p ${APP_PORT} >${CURRENT_PATH}/logs/app.log 2>&1 &
sleep 5
port=$(ss -ntlp | grep ${APP_PORT} | awk '{print $4}' | awk -F: '{print $2}')
if [[ -n ${port} ]]; then
echo -e "\\033[1;32m[INFO] App already started and port is: ${port}\\033[0;39m"
else
echo -e "\\033[1;31m[ERROR] App not started!\\033[0;39m"
fi
}
function start_celery() {
nohup ${PYTHON_PATH}/bin/celery -B -A Door worker --concurrency=3 -l info >${CURRENT_PATH}/logs/celery.log 2>&1 &
pids=$(ps -ef | grep "celery" | grep -v grep | awk '{print $2}')
if [ "x${pids}" != "x" ]; then
echo -e "\\033[1;32m[INFO] celery start success!\\033[0;39m"
fi
}
function status() {
port=$(ss -ntlp | grep ${APP_PORT} | awk '{print $4}' | awk -F: '{print $2}')
if [[ -n ${port} ]]; then
echo -e "\\033[1;32m[INFO] URL: http://0.0.0.0:${APP_PORT}\\033[0;39m"
else
echo -e "\\033[1;31m[Failed]\\033[0;39m"
fi
}
function init_deploy() {
h1 "Deploy System V0.1"
export LANG=en_US.UTF-8
# export FLASK_APP=app
# export FLASK_ENV=development
# export FLASK_RUN_PORT=${APP_PORT}
unzip_resource
stop
sleep 3
h2 "[Step $item]: Install redis ..."
let item+=1
redis
h2 "[Step $item]: Set python runtime ..."
let item+=1
set_python
h2 "[Step $item]: Start App Service ..."
let item+=1
start_app
h2 "[Step $item]: Start Celery Service ..."
let item+=1
start_celery
h2 "[Step $item]: App Info ..."
let item+=1
status
}
init_deploy