blob: 3247772ba5cfbe248a50f78d79b37986e4a50f7a [file] [log] [blame]
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +03001#!/bin/bash
2
3#
4# Constants:
5#
6
7WORKSPACE="${WORKSPACE:-}"
8SUCCESS=0
9FAILURE=1
10
11RELEASE=latest
12LOCAL=false
13
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030014DEP_ENV="AUTO"
15
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030016
17# Java Options:
18DCAE_BE_JAVA_OPTIONS="-XX:MaxPermSize=256m -Xmx1024m -Dconfig.home=config -Dlog.home=/var/lib/jetty/logs/ -Dlogging.config=config/dcae-be/logback-spring.xml"
19DCAE_FE_JAVA_OPTIONS="-XX:MaxPermSize=256m -Xmx1024m -Dconfig.home=config -Dlog.home=/var/lib/jetty/logs/ -Dlogging.config=config/dcae-fe/logback-spring.xml"
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030020DCAE_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
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030022
23#Define this as variable, so it can be excluded in run commands on Docker for OSX, as /etc/localtime cant be mounted there.
24LOCAL_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
27if [[ "$OSTYPE" == "darwin"* ]]; then
28 LOCAL_TIME_MOUNT_CMD=""
29fi
30
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030031# Docker running Mode
32DOCKER_RUN_MODE_BG="--detach"
33DOCKER_RUN_MODE_FG="-dti"
34
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030035
36#
37# Functions:
38#
39
40function 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
47function cleanup {
48 echo "Performing old dockers cleanup"
49
50 if [ "$1" == "all" ] ; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030051 docker_ids=$(docker ps -a | egrep "dcae" | awk '{print $1}')
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030052 for X in ${docker_ids}
53 do
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030054 docker rm -f "${X}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030055 done
56 else
57 echo "performing $1 docker cleanup"
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030058 tmp=$(docker ps -a -q --filter="name=$1")
59 if [[ -n "$tmp" ]]; then
60 docker rm -f "${tmp}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030061 fi
62 fi
63}
64#
65
66
67function dir_perms {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030068 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"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030071}
72#
73
74
75function docker_logs {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030076 docker logs "$1" > "${WORKSPACE}/data/logs/docker_logs/$1_docker.log"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030077}
78#
79
80
81#
82# Readiness Prob
83#
84
85function ready_probe {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030086 docker exec "$1" /var/lib/ready-probe.sh > /dev/null 2>&1
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030087 rc=$?
88 if [[ ${rc} == 0 ]]; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030089 echo "DOCKER $1 start finished in $2 seconds"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +030090 return ${SUCCESS}
91 fi
92 return ${FAILURE}
93}
94#
95
96
97function probe_docker {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +030098 MATCH=$(docker logs --tail 30 "$1" | grep "DOCKER STARTED")
99 echo MATCH is -- "${MATCH}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300100
101 if [ -n "$MATCH" ] ; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300102 echo "DOCKER start finished in $2 seconds"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300103 return ${SUCCESS}
104 fi
105 return ${FAILURE}
106}
107#
108
109
110function probe_dcae_be {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300111 health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}' "http://${IP}:8082/dcae/conf/composition")
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300112 if [[ "${health_check_http_code}" -eq 200 ]] ; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300113 echo "DOCKER start finished in $1 seconds"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300114 return ${SUCCESS}
115 fi
116 return ${FAILURE}
117}
118#
119
120function probe_dcae_fe {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300121 health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}' "http://${IP}:8183/dcaed/healthCheck")
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300122 if [[ "${health_check_http_code}" -eq 200 ]] ; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300123 echo "DOCKER start finished in $1 seconds"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300124 return ${SUCCESS}
125 fi
126 return ${FAILURE}
127}
128#
129
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300130function 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#
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300139
140# Not applicable for current release. Return Success in any case
141function probe_dcae_tools {
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300142 health_check_http_code=$(curl -i -o /dev/null -w '%{http_code}' "http://${IP}:8082/dcae/getResourcesByMonitoringTemplateCategory")
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300143 if [[ "${health_check_http_code}" -eq 200 ]] ; then
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300144 echo "DOCKER start finished in $1 seconds"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300145 return ${SUCCESS}
146 fi
147 return ${SUCCESS}
148}
149#
150
151
152function 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 ;;
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300172 dcae-dt)
173 probe_dcae_dt ${TIME} ;
174 status=$? ;
175 ;;
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300176 dcae-tools)
177 probe_dcae_tools ;
178 status=$? ;
179 ;;
180 *)
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300181 probe_docker "${DOCKER_NAME}" ${TIME};
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300182 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
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300196 docker_logs "${DOCKER_NAME}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300197
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
205function healthCheck {
206
207 echo "BE health-Check:"
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300208 curl --noproxy "*" "http://${IP}:8080/sdc2/rest/healthCheck"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300209
210 echo ""
211 echo ""
212 echo "FE health-Check:"
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300213 curl --noproxy "*" "http://${IP}:8181/sdc1/rest/healthCheck"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300214}
215#
216
217
218function 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
235function dcae-be {
236 DOCKER_NAME="dcae-be"
237 echo "docker run ${DOCKER_NAME}..."
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300238 if [ ${LOCAL} == false ]; then
239 docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300240 fi
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300241 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
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300242 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
250function dcae-tools {
251 DOCKER_NAME="dcae-tools"
252 echo "docker run ${DOCKER_NAME}..."
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300253 if [ ${LOCAL} == false ]; then
254 docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300255 fi
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300256 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}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300257 command_exit_status $? ${DOCKER_NAME}
258 echo "please wait while ${DOCKER_NAME^^} is starting....."
259 monitor_docker ${DOCKER_NAME}
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300260}
261#
262
263
264# DCAE FrontEnd
265function dcae-fe {
266 DOCKER_NAME="dcae-fe"
267 echo "docker run ${DOCKER_NAME}..."
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300268 if [ ${LOCAL} == false ]; then
269 docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300270 fi
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300271 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
279function 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
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300286 command_exit_status $? ${DOCKER_NAME}
287 echo "please wait while ${DOCKER_NAME^^} is starting....."
288 monitor_docker ${DOCKER_NAME}
289
290}
291#
292
293
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300294#
295# Main
296#
297
298# Handle command line arguments
299
300if [ $# -eq 0 ]; then
301 usage
302 exit ${FAILURE}
303fi
304
305while [ $# -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
346done
347
348
349#Prefix those with WORKSPACE so it can be set to something other then /opt
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300350[ -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
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300356
357
358export IP=`ip route get 8.8.8.8 | awk '/src/{ print $7 }'`
359#If OSX, then use this to get IP
360if [[ "$OSTYPE" == "darwin"* ]]; then
361 export IP=$(ipconfig getifaddr en0)
362fi
363export PREFIX=${NEXUS_DOCKER_REPO}'/onap'
364
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300365if [ ${LOCAL} == true ]; then
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300366 PREFIX='onap'
367fi
368
369echo ""
370
371if [ -z "${DOCKER}" ]; then
372 cleanup all
373 dir_perms
374 dcae-be
375 dcae-tools
376 dcae-fe
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300377 dcae-dt
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300378 healthCheck
379else
Areli, Fuss (af732p)dd6eb9b2018-08-30 17:18:20 +0300380 cleanup "${DOCKER}"
Areli, Fuss (af732p)735b5812018-08-02 15:38:04 +0300381 dir_perms
382 ${DOCKER}
383 healthCheck
384fi