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