-
Notifications
You must be signed in to change notification settings - Fork 2
/
do.sh
executable file
·249 lines (209 loc) · 6.6 KB
/
do.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
#!/bin/bash
_CURRENT_FILE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_CURRENT_RUNNING_DIR="$( cd "$( dirname "." )" && pwd )"
. $_CURRENT_FILE_DIR/stella-link.sh include
CONDA_ENV_NAME=$STELLA_APP_NAME
FLASK_APP_NAME=$STELLA_APP_NAME
DEFAULT_REDIS_PORT=6379
DEFAULT_REDIS_DATABASE=0
DEFAULT_REDIS_INSTANCE=auto
DEFAULT_FRONT_PORT=8080
usage() {
echo "USAGE :"
echo "----------------"
echo "o-- general management :"
echo "L install <id-sample> : deploy a sample"
echo "o"
echo "L start-front <id-sample> [--port=<port>] [--redis=auto[:<port>]|docker[:<port>]|<host[:port]>] : start flask frontend. --port is the frontend port, default is $DEFAULT_FRONT_PORT. --redis is the instance used, default is $DEFAULT_REDIS_INSTANCE"
echo "L stop-front <id-sample> : stop flask frontend."
echo "o"
echo "L start-back <id-sample> [--redis=auto[:<port>]|docker[:<port>]|<host[:port]>] : start celery backend. --redis is the instance used, default is $DEFAULT_REDIS_INSTANCE"
echo "L stop-back <id-sample> : stop celery backend."
echo "o-- celery monitoring :"
echo "L start-flower <id-sample> [--port=<port>] : start Flower web ui for monitoring celery. --port is Flower port, default is $DEFAULT_FRONT_PORT"
echo "L stop-flower <id-sample>"
echo "o-- redis management :"
echo "L install redis [--redis=auto|docker] : install an 'auto' or 'docker' redis instance. Default redis instance is $DEFAULT_REDIS_INSTANCE"
echo "L start redis [--redis=auto[:<port>]|docker[:<port>]] : start an 'auto' or 'docker' redis instance. Default port is $DEFAULT_REDIS_PORT"
echo "L stop redis [--redis=auto[:<port>]|docker] : stop an 'auto' or 'docker' redis instance. Default port is $DEFAULT_REDIS_PORT"
}
# COMMAND LINE -----------------------------------------------------------------------------------
PARAMETERS="
ACTION= 'action' a 'install start stop start-front start-back stop-front stop-back start-flower stop-flower'
ID= 'id' a 'redis sample1 sample2'
"
OPTIONS="
FORCE='' 'f' '' b 0 '1' Force.
REDIS='$DEFAULT_REDIS_INSTANCE' 'r' 'string' s 0 '' a redis instance. '<host[:port]>' or 'auto[:<port>]' or 'docker[:<port>]'.
PORT='$DEFAULT_FRONT_PORT' '' 'string' s 0 '' front for frontend api and celery UI listening port.
"
$STELLA_API argparse "$0" "$OPTIONS" "$PARAMETERS" "$STELLA_APP_NAME" "$(usage)" "APPARG" "$@"
#-------------------------------------------------------------------------------------------
# COMPUTE PARAMS
# redis instance
$STELLA_API uri_parse $REDIS
REDIS_HOST=$__stella_uri_host
REDIS_PORT=$__stella_uri_port
REDIS_INSTANCE=$__stella_uri_host
[ "$REDIS_PORT" = "" ] && REDIS_PORT=$DEFAULT_REDIS_PORT
case $REDIS_HOST in
docker )
echo "* Using docker redis"
docker pull redis
if [ ! "$DOCKER_MACHINE_NAME" = "" ]; then
REDIS_HOST="$(docker-machine ip $DOCKER_MACHINE_NAME)"
echo "* with docker-machine $DOCKER_MACHINE_NAME $REDIS_HOST"
else
REDIS_HOST=localhost
fi
;;
auto )
echo "* Using auto deployed redis"
REDIS_HOST=localhost
;;
* )
echo "* Using already deployed redis"
;;
esac
# sample
case $ID in
sample1 )
SAMPLE_ID=1
;;
sample2 )
SAMPLE_ID=2
;;
* )
;;
esac
SAMPLE_PATH="$STELLA_APP_ROOT/$ID"
REDIS_DATABASE=${SAMPLE_ID}
CONDA_ENV_NAME=${CONDA_ENV_NAME}-${SAMPLE_ID}
FLASK_APP_NAME=${FLASK_APP_NAME}-${SAMPLE_ID}
FLOWER_PORT="$PORT"
# var used in python env
export CELERY_BROKER_URL="redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DATABASE"
export CELERY_RESULT_BACKEND="redis://$REDIS_HOST:$REDIS_PORT/$REDIS_DATABASE"
#export CELERY_RESULT_BACKEND="db+sqlite:///results.sqlite"
export FRONTEND_PORT="$PORT"
export FLASK_APP_NAME="$FLASK_APP_NAME"
# --------------- INSTALL ----------------------------
if [ "$ACTION" = "install" ]; then
if [ "$ID" = "redis" ]; then
echo "** Install redis $REDIS_INSTANCE"
case $REDIS_INSTANCE in
docker )
docker pull redis
;;
auto )
$STELLA_API get_feature redis
;;
* )
;;
esac
else
echo "** Install sample $ID"
$STELLA_API get_feature miniconda
# create python conda env
conda create -y -n $CONDA_ENV_NAME python=3.6
# activation env
set -h
source activate $CONDA_ENV_NAME
# install python dependencies
pip install -r $SAMPLE_PATH/requirements.txt
source deactivate $CONDA_ENV_NAME
set +h
fi
fi
# --------------- START ----------------------------
if [ "$ACTION" = "start-front" ]; then
echo "** Start frontend"
cd "$STELLA_APP_ROOT"
set -h
source activate $CONDA_ENV_NAME
# launch API server
export PYTHONPATH=.:$PYTHONPATH
python $SAMPLE_PATH/front.py
source deactivate $CONDA_ENV_NAME
set +h
fi
if [ "$ACTION" = "start-back" ]; then
echo "** Start backend"
cd "$STELLA_APP_ROOT"
set -h
source activate $CONDA_ENV_NAME
# launch celery
celery -A ${ID}.back.backapp worker --loglevel=info
source deactivate $CONDA_ENV_NAME
set +h
fi
if [ "$ACTION" = "start-flower" ]; then
echo "** Start Flower UI"
cd "$STELLA_APP_ROOT"
set -h
source activate $CONDA_ENV_NAME
# launch celery flower UI
celery -A ${ID}.back.backapp flower --port=$FLOWER_PORT --logging=none
source deactivate $CONDA_ENV_NAME
set +h
fi
if [ "$ACTION" = "start" ]; then
if [ "$ID" = "redis" ]; then
echo "** Start redis $REDIS_INSTANCE"
case $REDIS_INSTANCE in
docker )
docker run -d -p $REDIS_PORT:6379 --name id-redis redis
;;
auto )
redis-server --port $REDIS_PORT
#$STELLA_API feature_info redis REDIS
#export REDIS_HOME=$REDIS_FEAT_INSTALL_ROOT
#nohup -- $REDIS_HOME/redis-server --port $REDIS_PORT 1>/dev/null 2>&1 &
;;
* )
;;
esac
fi
fi
# --------------- STOP ----------------------------
if [ "$ACTION" = "stop-front" ]; then
echo " ** Stop front"
# stop api
kill $(ps aux | grep "$SAMPLE_PATH/front.py" | tr -s " " | cut -d" " -f 2)
fi
if [ "$ACTION" = "stop-back" ]; then
echo " ** Stop back"
cd "$STELLA_APP_ROOT"
set -h
source activate $CONDA_ENV_NAME
# stop celery
celery -A ${ID}.back.backapp control shutdown
source deactivate $CONDA_ENV_NAME
set +h
fi
if [ "$ACTION" = "stop-flower" ]; then
echo " ** Stop Flower UI"
cd "$STELLA_APP_ROOT"
set -h
source activate $CONDA_ENV_NAME
# stop flower
kill $(ps aux | grep "celery -A ${ID}.back.backapp flower" | tr -s " " | cut -d" " -f 2)
source deactivate $CONDA_ENV_NAME
set +h
fi
if [ "$ACTION" = "stop" ]; then
if [ "$ID" = "redis" ]; then
echo " ** Stop redis $REDIS_INSTANCE"
# stop redis
case $REDIS_INSTANCE in
docker )
docker stop id-redis
;;
auto )
redis-cli -p $REDIS_PORT shutdown
;;
* )
;;
esac
fi
fi