summaryrefslogtreecommitdiffstats
path: root/docker/scripts/docker_run.sh
blob: ceba2e437951ceaeb2b4d432440c3d569c1ce28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/bin/bash

#
# Constants:
#

WORKSPACE="${WORKSPACE:-}"
SUCCESS=0
FAILURE=1

RELEASE=latest
LOCAL=false

DEP_ENV="AUTO"


# Java Options:
JAVA_OPTIONS="-XX:MaxPermSize=256m -Xmx1024m"


#Define this as variable, so it can be excluded in run commands on Docker for OSX, as /etc/localtime cant be mounted there.
LOCAL_TIME_MOUNT_CMD="--volume /etc/localtime:/etc/localtime:ro"

# If os is OSX, unset this, so /etc/localtime is not mounted, otherwise leave it be
if [[ "$OSTYPE" == "darwin"* ]]; then
    LOCAL_TIME_MOUNT_CMD=""
fi

# Docker running Mode
DOCKER_RUN_MODE_BG="--detach"
DOCKER_RUN_MODE_FG="-dti"


#
# Functions:
#

function usage {
    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 ]"
    echo "example: sudo bash dcae_docker_run.sh -e AUTO -r 1.2-STAGING-latest"
}
#


function cleanup {
    echo "Performing old dockers cleanup"

    if [ "$1" == "all" ] ; then
        docker_ids=$(docker ps -a | egrep "dcae" | awk '{print $1}')
        for X in ${docker_ids}
        do
           docker rm -f "${X}"
         done
    else
        echo "performing $1 docker cleanup"
        tmp=$(docker ps -a -q --filter="name=$1")
        if [[ -n "$tmp" ]]; then
            docker rm -f "${tmp}"
        fi
    fi
}
#


function dir_perms {
    mkdir -p "${WORKSPACE}/data/logs/DCAE-BE/DCAE"
    mkdir -p "${WORKSPACE}/data/logs/DCAE-FE/DCAE"
    mkdir -p "${WORKSPACE}/data/logs/DCAE-DT/DCAE"
    mkdir -p "${WORKSPACE}/data/logs/DCAE-TOSCA/DCAE"

    chmod -R 777 "${WORKSPACE}/data/logs"
}
#


function docker_logs {
    docker logs "$1" > "${WORKSPACE}/data/logs/docker_logs/$1_docker.log"
}
#


#
# Readiness Prob
#

function ready_probe {
    docker exec "$1" /var/lib/ready-probe.sh > /dev/null 2>&1
    rc=$?
    if [[ ${rc} == 0 ]]; then
        echo "DOCKER $1 start finished in $2 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#


function probe_docker {
    MATCH=$(docker logs --tail 30 "$1" | grep "DOCKER STARTED")
    echo MATCH is -- "${MATCH}"

    if [ -n "$MATCH" ] ; then
        echo "DOCKER start finished in $2 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#

function probe_dcae_tosca {
    health_check_http_code=$(curl --noproxy "*" -k -i -o /dev/null -w '%{http_code}' "http://${IP}:8085/healthcheck")
    if [[ "${health_check_http_code}" -eq 200 ]] ; then
        echo "DOCKER start finished in $1 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#

# probe script used the secure connection to do health check (https is always turn on)
function probe_dcae_be {
    health_check_http_code=$(curl --noproxy "*" -k -i -o /dev/null -w '%{http_code}' "https://${IP}:8444/dcae/conf/composition")
    if [[ "${health_check_http_code}" -eq 200 ]] ; then
        echo "DOCKER start finished in $1 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#

function probe_dcae_fe {
    health_check_http_code=$(curl --noproxy "*" -k -i -o /dev/null -w '%{http_code}' "https://${IP}:9444/dcaed/healthCheck")
    if [[ "${health_check_http_code}" -eq 200 ]] ; then
        echo "DOCKER start finished in $1 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#

function probe_dcae_dt {
    health_check_http_code=$(curl --noproxy "*" -k -i -o /dev/null -w '%{http_code}' "https://${IP}:9446/dcae/healthCheckOld")
    if [[ "${health_check_http_code}" -eq 200 ]] ; then
        echo "DOCKER start finished in $1 seconds"
        return ${SUCCESS}
    fi
    return ${FAILURE}
}
#

# Not applicable for current release. Return Success in any case
function probe_dcae_tools {
   health_check_http_code=$(curl --noproxy "*" -k -i -o /dev/null -w '%{http_code}'  "https://${IP}:8444/dcae/getResourcesByMonitoringTemplateCategory")
    if [[ "${health_check_http_code}" -eq 200 ]] ; then
        echo "DOCKER start finished in $1 seconds"
        return ${SUCCESS}
    fi
    return ${SUCCESS}
}
#


function monitor_docker {
    DOCKER_NAME=$1
    echo "Monitor ${DOCKER_NAME} Docker"
    sleep 5
    TIME_OUT=900
    INTERVAL=20
    TIME=0

    while [ "$TIME" -lt "$TIME_OUT" ]; do

        case ${DOCKER_NAME} in

            dcae-tosca-app)
                probe_dcae_tosca ${TIME} ;
                status=$? ;
            ;;
            dcae-be)
                probe_dcae_be ${TIME} ;
                status=$? ;
            ;;
            dcae-fe)
                probe_dcae_fe ${TIME} ;
                status=$? ;
            ;;
            dcae-dt)
                probe_dcae_dt ${TIME} ;
                status=$? ;
            ;;
            dcae-tools)
                probe_dcae_tools ;
                status=$? ;
            ;;
            *)
                probe_docker "${DOCKER_NAME}" ${TIME};
                status=$? ;
            ;;

        esac

        if [ ${status} == ${SUCCESS} ] ; then
            break;
        fi

        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"
        sleep ${INTERVAL}
        TIME=$(($TIME+$INTERVAL))
    done

    docker_logs "${DOCKER_NAME}"

    if [ "$TIME" -ge "$TIME_OUT" ]; then
        echo -e "\e[1;31mTIME OUT: DOCKER was NOT fully started in $TIME_OUT seconds... Could cause problems ...\e[0m"
    fi
}
#

# healthCheck script used the secure connection to send request (https is always turn on)
function healthCheck {

    echo "BE health-Check:"
    curl -k --noproxy "*" "https://${IP}:8443/sdc2/rest/healthCheck"

    echo ""
    echo ""
    echo "FE health-Check:"
    curl -k --noproxy "*" "https://${IP}:9443/sdc1/rest/healthCheck"
}
#


function command_exit_status {
    status=$1
    docker=$2
    if [ "${status}" != "0" ] ; then
        echo "[  ERROR  ] Docker ${docker} run command exit with status [${status}]"
        exit ${FAILURE}
    fi
}
#


#
# Run Containers
#

# DCAE TOSCA
function dcae-tosca {
    DOCKER_NAME="dcae-tosca-app"
    echo "docker run ${DOCKER_NAME}..."
    if [ ${LOCAL} == false ]; then
        docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    fi
    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}"
    command_exit_status $? ${DOCKER_NAME}
    echo "please wait while ${DOCKER_NAME^^} is starting....."
    monitor_docker ${DOCKER_NAME}
}
#


# DCAE BackEnd
function dcae-be {
    DOCKER_NAME="dcae-be"
    echo "docker run ${DOCKER_NAME}..."
    if [ ${LOCAL} == false ]; then
        docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    fi
    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:/root/chef-solo/environments" --publish 8444:8444 --publish 8082:8082 "${PREFIX}/${DOCKER_NAME}:${RELEASE}" /bin/sh
    command_exit_status $? ${DOCKER_NAME}
    echo "please wait while ${DOCKER_NAME^^} is starting....."
    monitor_docker ${DOCKER_NAME}
}
#


# DCAE Configuration
function dcae-tools {
    DOCKER_NAME="dcae-tools"
    echo "docker run ${DOCKER_NAME}..."
    if [ ${LOCAL} == false ]; then
        docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    fi
    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:/root/chef-solo/environments"  "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    command_exit_status $? ${DOCKER_NAME}
    echo "please wait while ${DOCKER_NAME^^} is starting....."
    monitor_docker ${DOCKER_NAME}
}
#


# DCAE FrontEnd
function dcae-fe {
    DOCKER_NAME="dcae-fe"
    echo "docker run ${DOCKER_NAME}..."
    if [ ${LOCAL} == false ]; then
        docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    fi
    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:/root/chef-solo/environments" --publish 9444:9444 --publish 8183:8183 "${PREFIX}/${DOCKER_NAME}:${RELEASE}" /bin/sh
    command_exit_status $? ${DOCKER_NAME}
    echo "please wait while ${DOCKER_NAME^^} is starting....."
    monitor_docker ${DOCKER_NAME}
}
#

# DCAE DT
function dcae-dt {
    DOCKER_NAME="dcae-dt"
    echo "docker run ${DOCKER_NAME}..."
    if [ ${LOCAL} == false ]; then
        docker pull "${PREFIX}/${DOCKER_NAME}:${RELEASE}"
    fi
    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:/root/chef-solo/environments/" --publish 9446:9446 --publish 8186:8186 "${PREFIX}/${DOCKER_NAME}:${RELEASE}" /bin/sh
    command_exit_status $? ${DOCKER_NAME}
    echo "please wait while ${DOCKER_NAME^^} is starting....."
    monitor_docker ${DOCKER_NAME}

}
#


#
# Main
#

# Handle command line arguments

if [ $# -eq 0 ]; then
    usage
    exit ${FAILURE}
fi

while [ $# -gt 0 ]; do
    case $1 in

    # -r | --release - The specific docker version to pull and deploy
    -r | --release )
          shift 1 ;
          RELEASE=$1;
          shift 1;;

    # -e | --environment - The environment name you want to deploy
    -e | --environment )
          shift 1;
          DEP_ENV=$1;
          shift 1 ;;

    # -p | --port - The port from which to connect to the docker nexus
    -p | --port )
          shift 1 ;
          PORT=$1;
          shift 1 ;;

    # -l | --local - Use this for deploying your local dockers without pulling them first
    -l | --local )
          LOCAL=true;
          shift 1;;

    # -d | --docker - The init specified docker
    -d | --docker )
          shift 1 ;
          DOCKER=$1;
          shift 1 ;;

    # -h | --help - Display the help message with all the available run options
    -h | --help )
          usage;
          exit  ${SUCCESS};;

    * )
          usage;
          exit  ${FAILURE};;
    esac
done


#Prefix those with WORKSPACE so it can be set to something other then /opt
[ -f "${WORKSPACE}/opt/config/env_name.txt" ] && DEP_ENV=$(cat "${WORKSPACE}/opt/config/env_name.txt") || echo "${DEP_ENV}"
[ -f "${WORKSPACE}/opt/config/nexus_username.txt" ] && NEXUS_USERNAME=$(cat "${WORKSPACE}/opt/config/nexus_username.txt")    || NEXUS_USERNAME="release"
[ -f "${WORKSPACE}/opt/config/nexus_password.txt" ] && NEXUS_PASSWD=$(cat "${WORKSPACE}/opt/config/nexus_password.txt")      || NEXUS_PASSWD="sfWU3DFVdBr7GVxB85mTYgAW"
[ -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}"
[ -f "${WORKSPACE}/opt/config/nexus_username.txt" ] && docker login -u ${NEXUS_USERNAME} -p ${NEXUS_PASSWD} ${NEXUS_DOCKER_REPO}



export IP=`ip route get 8.8.8.8 | awk '/src/{ print $7 }'`
#If OSX, then use this to get IP
if [[ "$OSTYPE" == "darwin"* ]]; then
    export IP=$(ipconfig getifaddr en0)
fi
export PREFIX=${NEXUS_DOCKER_REPO}'/onap'

if [ ${LOCAL} == true ]; then
    PREFIX='onap'
fi

echo ""

if [ -z "${DOCKER}" ]; then
    cleanup all
    dir_perms
    dcae-tosca
    dcae-be
    dcae-tools
    dcae-fe
    dcae-dt
    healthCheck
else
    cleanup "${DOCKER}"
    dir_perms
    ${DOCKER}
    healthCheck
fi