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