Align INFO.yaml
[sdc/dcae-d/dt-be-main.git] / docker / scripts / docker_run.sh
1 #!/bin/bash
2
3 #
4 # Constants:
5 #
6
7 WORKSPACE="${WORKSPACE:-}"
8 SUCCESS=0
9 FAILURE=1
10
11 RELEASE=latest
12 LOCAL=false
13
14
15 # Java Options:
16 DCAE_BE_JAVA_OPTIONS="-XX:MaxPermSize=256m -Xmx1024m -Dconfig.home=config -Dlog.home=/var/lib/jetty/logs/ -Dlogging.config=config/dcae-be/logback-spring.xml"
17 DCAE_FE_JAVA_OPTIONS="-XX:MaxPermSize=256m -Xmx1024m -Dconfig.home=config -Dlog.home=/var/lib/jetty/logs/ -Dlogging.config=config/dcae-fe/logback-spring.xml"
18
19 #Define this as variable, so it can be excluded in run commands on Docker for OSX, as /etc/localtime cant be mounted there.
20 LOCAL_TIME_MOUNT_CMD="--volume /etc/localtime:/etc/localtime:ro"
21
22 # If os is OSX, unset this, so /etc/localtime is not mounted, otherwise leave it be
23 if [[ "$OSTYPE" == "darwin"* ]]; then
24     LOCAL_TIME_MOUNT_CMD=""
25 fi
26
27
28 #
29 # Functions:
30 #
31
32 function usage {
33     echo "usage: docker_run.sh [ -r|--release <RELEASE-NAME> ]  [ -e|--environment <ENV-NAME> ] [ -p|--port <Docker-hub-port>] [ -l|--local <Run-without-pull>] [ -h|--help ]"
34     echo "example: sudo bash docker_run.sh -e AUTO -r 1.2-STAGING-latest"
35 }
36 #
37
38
39 function cleanup {
40     echo "Performing old dockers cleanup"
41
42     if [ "$1" == "all" ] ; then
43         docker_ids=`docker ps -a | egrep "dcae" | awk '{print $1}'`
44         for X in ${docker_ids}
45         do
46            docker rm -f ${X}
47          done
48     else
49         echo "performing $1 docker cleanup"
50         tmp=`docker ps -a -q --filter="name=$1"`
51         if [[ ! -z "$tmp" ]]; then
52             docker rm -f ${tmp}
53         fi
54     fi
55 }
56 #
57
58
59 function dir_perms {
60     mkdir -p ${WORKSPACE}/data/logs/DCAE-BE/DCAE
61     mkdir -p ${WORKSPACE}/data/logs/DCAE-FE/DCAE
62 }
63 #
64
65
66 function docker_logs {
67     docker logs $1 > ${WORKSPACE}/data/logs/docker_logs/$1_docker.log
68 }
69 #
70
71
72 #
73 # Readiness Prob
74 #
75
76 function ready_probe {
77     docker exec $1 /var/lib/ready-probe.sh > /dev/null 2>&1
78     rc=$?
79     if [[ ${rc} == 0 ]]; then
80         echo DOCKER $1 start finished in $2 seconds
81         return ${SUCCESS}
82     fi
83     return ${FAILURE}
84 }
85 #
86
87
88 function probe_docker {
89     MATCH=`docker logs --tail 30 $1 | grep "DOCKER STARTED"`
90     echo MATCH is -- ${MATCH}
91
92     if [ -n "$MATCH" ] ; then
93         echo DOCKER start finished in $2 seconds
94         return ${SUCCESS}
95     fi
96     return ${FAILURE}
97 }
98 #
99
100
101 function probe_dcae_be {
102     health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}' http://${IP}:8082/dcae/conf/composition)
103     if [[ "${health_check_http_code}" -eq 200 ]] ; then
104         echo DOCKER start finished in $1 seconds
105         return ${SUCCESS}
106     fi
107     return ${FAILURE}
108 }
109 #
110
111 function probe_dcae_fe {
112     health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}' http://${IP}:8183/dcaed/healthCheck)
113     if [[ "${health_check_http_code}" -eq 200 ]] ; then
114         echo DOCKER start finished in $1 seconds
115         return ${SUCCESS}
116     fi
117     return ${FAILURE}
118 }
119 #
120
121
122 # Not applicable for current release. Return Success in any case
123 function probe_dcae_tools {
124    health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}'  http://${IP}:8082/dcae/getResourcesByMonitoringTemplateCategory)
125     if [[ "${health_check_http_code}" -eq 200 ]] ; then
126         echo DOCKER start finished in $1 seconds
127         return ${SUCCESS}
128     fi
129     return ${SUCCESS}
130 }
131 #
132
133
134 function monitor_docker {
135     DOCKER_NAME=$1
136     echo "Monitor ${DOCKER_NAME} Docker"
137     sleep 5
138     TIME_OUT=900
139     INTERVAL=20
140     TIME=0
141
142     while [ "$TIME" -lt "$TIME_OUT" ]; do
143
144         case ${DOCKER_NAME} in
145
146             dcae-be)
147                 probe_dcae_be ${TIME} ;
148                 status=$? ;
149             ;;
150             dcae-fe)
151                 probe_dcae_fe ${TIME} ;
152                 status=$? ;
153             ;;
154             dcae-tools)
155                 probe_dcae_tools ;
156                 status=$? ;
157             ;;
158             *)
159                 probe_docker ${DOCKER_NAME} ${TIME};
160                 status=$? ;
161             ;;
162
163         esac
164
165         if [ ${status} == ${SUCCESS} ] ; then
166             break;
167         fi
168
169         echo "Sleep: ${INTERVAL} seconds before testing if ${DOCKER_NAME} DOCKER is up. Total wait time up now is: ${TIME} seconds. Timeout is: ${TIME_OUT} seconds"
170         sleep ${INTERVAL}
171         TIME=$(($TIME+$INTERVAL))
172     done
173
174     docker_logs ${DOCKER_NAME}
175
176     if [ "$TIME" -ge "$TIME_OUT" ]; then
177         echo -e "\e[1;31mTIME OUT: DOCKER was NOT fully started in $TIME_OUT seconds... Could cause problems ...\e[0m"
178     fi
179 }
180 #
181
182
183 function healthCheck {
184
185     echo "BE health-Check:"
186     curl --noproxy "*" http://${IP}:8080/sdc2/rest/healthCheck
187
188     echo ""
189     echo ""
190     echo "FE health-Check:"
191     curl --noproxy "*" http://${IP}:8181/sdc1/rest/healthCheck
192 }
193 #
194
195
196 function command_exit_status {
197     status=$1
198     docker=$2
199     if [ "${status}" != "0" ] ; then
200         echo "[  ERROR  ] Docker ${docker} run command exit with status [${status}]"
201         exit ${FAILURE}
202     fi
203 }
204 #
205
206
207 #
208 # Run Containers
209 #
210
211
212 # DCAE BackEnd
213 function dcae-be {
214     DOCKER_NAME="dcae-be"
215     echo "docker run ${DOCKER_NAME}..."
216     if [ ${LOCAL} = false ]; then
217         docker pull ${PREFIX}/${DOCKER_NAME}:${RELEASE}
218     fi
219     docker run --detach --name ${DOCKER_NAME} --env HOST_IP=${IP} --env ENVNAME="${DEP_ENV}" --env JAVA_OPTIONS="${DCAE_BE_JAVA_OPTIONS}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 ${LOCAL_TIME_MOUNT_CMD}  --volume ${WORKSPACE}/data/logs/DCAE-BE/:/var/lib/jetty/logs --volume ${WORKSPACE}/data/environments:/var/opt/dcae-be/chef-solo/environments --publish 8444:8444 --publish 8082:8082 ${PREFIX}/${DOCKER_NAME}:${RELEASE}
220     command_exit_status $? ${DOCKER_NAME}
221     echo "please wait while ${DOCKER_NAME^^} is starting....."
222     monitor_docker ${DOCKER_NAME}
223 }
224 #
225
226
227 # DCAE Configuration
228 function dcae-tools {
229     DOCKER_NAME="dcae-tools"
230     echo "docker run ${DOCKER_NAME}..."
231     if [ ${LOCAL} = false ]; then
232         docker pull ${PREFIX}/${DOCKER_NAME}:${RELEASE}
233     fi
234     docker run --detach --name ${DOCKER_NAME} --env HOST_IP=${IP} --env ENVNAME="${DEP_ENV}" ${LOCAL_TIME_MOUNT_CMD}  --volume ${WORKSPACE}/data/logs/BE/:/var/lib/jetty/logs --volume ${WORKSPACE}/data/environments:/var/opt/dcae-tools/chef-solo/environments  ${PREFIX}/${DOCKER_NAME}:${RELEASE}
235     command_exit_status $? ${DOCKER_NAME}
236     echo "please wait while ${DOCKER_NAME^^} is starting....."
237     monitor_docker ${DOCKER_NAME}
238
239 }
240 #
241
242
243 # DCAE FrontEnd
244 function dcae-fe {
245     DOCKER_NAME="dcae-fe"
246     echo "docker run ${DOCKER_NAME}..."
247     if [ ${LOCAL} = false ]; then
248         docker pull ${PREFIX}/${DOCKER_NAME}:${RELEASE}
249     fi
250     docker run --detach --name ${DOCKER_NAME} --env HOST_IP=${IP} --env ENVNAME="${DEP_ENV}" --env JAVA_OPTIONS="${DCAE_FE_JAVA_OPTIONS}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 ${LOCAL_TIME_MOUNT_CMD}  --volume ${WORKSPACE}/data/logs/DCAE-FE/:/var/lib/jetty/logs --volume ${WORKSPACE}/data/environments:/var/opt/dcae-fe/chef-solo/environments/ --publish 9444:9444 --publish 8183:8183 ${PREFIX}/${DOCKER_NAME}:${RELEASE}
251     command_exit_status $? ${DOCKER_NAME}
252     echo "please wait while ${DOCKER_NAME^^} is starting....."
253     monitor_docker ${DOCKER_NAME}
254
255 }
256 #
257
258
259
260 #
261 # Main
262 #
263
264 # Handle command line arguments
265
266 if [ $# -eq 0 ]; then
267     usage
268     exit ${FAILURE}
269 fi
270
271 while [ $# -gt 0 ]; do
272     case $1 in
273
274     # -r | --release - The specific docker version to pull and deploy
275     -r | --release )
276           shift 1 ;
277           RELEASE=$1;
278           shift 1;;
279
280     # -e | --environment - The environment name you want to deploy
281     -e | --environment )
282           shift 1;
283           DEP_ENV=$1;
284           shift 1 ;;
285
286     # -p | --port - The port from which to connect to the docker nexus
287     -p | --port )
288           shift 1 ;
289           PORT=$1;
290           shift 1 ;;
291
292     # -l | --local - Use this for deploying your local dockers without pulling them first
293     -l | --local )
294           LOCAL=true;
295           shift 1;;
296
297     # -d | --docker - The init specified docker
298     -d | --docker )
299           shift 1 ;
300           DOCKER=$1;
301           shift 1 ;;
302
303     # -h | --help - Display the help message with all the available run options
304     -h | --help )
305           usage;
306           exit  ${SUCCESS};;
307
308     * )
309           usage;
310           exit  ${FAILURE};;
311     esac
312 done
313
314
315 #Prefix those with WORKSPACE so it can be set to something other then /opt
316 [ -f ${WORKSPACE}/opt/config/env_name.txt ] && DEP_ENV=$(cat ${WORKSPACE}/opt/config/env_name.txt) || echo ${DEP_ENV}
317 [ -f ${WORKSPACE}/opt/config/nexus_username.txt ] && NEXUS_USERNAME=$(cat ${WORKSPACE}/opt/config/nexus_username.txt)    || NEXUS_USERNAME=release
318 [ -f ${WORKSPACE}/opt/config/nexus_password.txt ] && NEXUS_PASSWD=$(cat ${WORKSPACE}/opt/config/nexus_password.txt)      || NEXUS_PASSWD=sfWU3DFVdBr7GVxB85mTYgAW
319 [ -f ${WORKSPACE}/opt/config/nexus_docker_repo.txt ] && NEXUS_DOCKER_REPO=$(cat ${WORKSPACE}/opt/config/nexus_docker_repo.txt) || NEXUS_DOCKER_REPO=nexus3.onap.org:${PORT}
320 [ -f ${WORKSPACE}/opt/config/nexus_username.txt ] && docker login -u ${NEXUS_USERNAME} -p ${NEXUS_PASSWD} ${NEXUS_DOCKER_REPO}
321
322
323 export IP=`ip route get 8.8.8.8 | awk '/src/{ print $7 }'`
324 #If OSX, then use this to get IP
325 if [[ "$OSTYPE" == "darwin"* ]]; then
326     export IP=$(ipconfig getifaddr en0)
327 fi
328 export PREFIX=${NEXUS_DOCKER_REPO}'/onap'
329
330 if [ ${LOCAL} = true ]; then
331     PREFIX='onap'
332 fi
333
334 echo ""
335
336 if [ -z "${DOCKER}" ]; then
337     cleanup all
338     dir_perms
339     dcae-be
340     dcae-tools
341     dcae-fe
342     healthCheck
343 else
344     cleanup ${DOCKER}
345     dir_perms
346     ${DOCKER}
347     healthCheck
348 fi