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