fix docker_run script
[sdc.git] / sdc-os-chef / scripts / sanity_run.sh
1 #!/bin/bash
2
3 #Define this as variable, so it can be excluded in run commands on Docker for OSX, as /etc/localtime cant be mounted there.
4 LOCAL_TIME_MOUNT_CMD="--volume /etc/localtime:/etc/localtime:ro"
5 # If os is OSX, unset this, so /etc/localtime is not mounted, otherwise leave it be
6 if [[ "$OSTYPE" == "darwin"* ]]; then
7   LOCAL_TIME_MOUNT_CMD=""
8 fi
9
10 function usage {
11     echo "usage: sanity_run.sh [ -r|--release <RELEASE-NAME> ]  [ -e|--environment <ENV-NAME> ] [ -p|--port <Docker-hub-port>] [ -l|--local <Run-without-pull>] [ -t|--runTests <Run-with-sanityDocker>] [ -h|--help ]"
12 }
13
14 function cleanup {
15     echo "performing old dockers cleanup"
16     docker_ids=`docker ps -a | egrep -v "onap/sdc-simulator" | egrep "ecomp-nexus:${PORT}/sdc|sdc-sanity|Exit" | awk '{print $1}'`
17     for X in ${docker_ids}
18     do
19         docker rm -f ${X}
20     done
21 }
22
23
24 function dir_perms {
25     mkdir -p ${WORKSPACE}/data/logs/BE/SDC/SDC-BE
26     mkdir -p ${WORKSPACE}/data/logs/FE/SDC/SDC-FE
27     chmod -R 777 ${WORKSPACE}/data/logs
28 }
29
30 function monitor_docker {
31
32     echo monitor $1 Docker
33     sleep 5
34     TIME_OUT=900
35     INTERVAL=20
36     TIME=0
37     while [ "$TIME" -lt "$TIME_OUT" ]; do
38
39         MATCH=`docker logs --tail 30 $1 | grep "DOCKER STARTED"`
40         echo MATCH is -- $MATCH
41
42         if [ -n "$MATCH" ]; then
43             echo DOCKER start finished in $TIME seconds
44             break
45         fi
46
47         echo Sleep: $INTERVAL seconds before testing if $1 DOCKER is up. Total wait time up now is: $TIME seconds. Timeout is: $TIME_OUT seconds
48         sleep $INTERVAL
49         TIME=$(($TIME+$INTERVAL))
50     done
51
52     if [ "$TIME" -ge "$TIME_OUT" ]; then
53         echo -e "\e[1;31mTIME OUT: DOCKER was NOT fully started in $TIME_OUT seconds... Could cause problems ...\e[0m"
54     fi
55
56 }
57
58 function healthCheck {
59         curl localhost:9200/_cluster/health?pretty=true
60
61         echo "BE health-Check:"
62         curl http://localhost:8080/sdc2/rest/healthCheck
63
64         echo ""
65         echo ""
66         echo "FE health-Check:"
67         curl http://localhost:8181/sdc1/rest/healthCheck
68
69
70         echo ""
71         echo ""
72         healthCheck_http_code=$(curl -o /dev/null -w '%{http_code}' -H "Accept: application/json" -H "Content-Type: application/json" -H "USER_ID: jh0003" http://localhost:8080/sdc2/rest/v1/user/demo;)
73         if [[ ${healthCheck_http_code} != 200 ]]
74         then
75                 echo "Error [${healthCheck_http_code}] while user existance check"
76                 return ${healthCheck_http_code}
77         fi
78         echo "check user existance: OK"
79         return ${healthCheck_http_code}
80 }
81
82 function elasticHealthCheck {
83         echo "Elastic Health-Check:"
84         
85         COUNTER=0
86     while [  $COUNTER -lt 20 ]; do
87         echo "Waiting ES docker to start"
88                 health_Check_http_code=$(curl -o /dev/null -w '%{http_code}' http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=120s)
89                 if [[ "$health_Check_http_code" -eq 200 ]]
90                 then
91                         break
92                 fi
93                 let COUNTER=COUNTER+1 
94                 sleep 4
95         done
96         
97         healthCheck_http_code=$(curl -o /dev/null -w '%{http_code}' http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=120s)
98         if [[ "$health_Check_http_code" != 200 ]]
99         then
100                 echo "Error [${healthCheck_http_code}] ES NOT started correctly"
101                 exit ${healthCheck_http_code}
102         fi
103         echo "ES started correctly"
104         curl localhost:9200/_cluster/health?pretty=true
105         return ${healthCheck_http_code}
106 }
107
108 RELEASE=latest
109 LOCAL=false
110 RUNTESTS=true
111 DEBUG_PORT="--publish 4000:4000"
112
113 while [ $# -gt 0 ]; do
114     case $1 in
115         # -r | --release - The specific docker version to pull and deploy
116     -r | --release )
117           shift 1 ;
118           RELEASE=$1;
119           shift 1;;
120         # -e | --environment - The environment name you want to deploy
121     -e | --environment )
122           shift 1;
123           DEP_ENV=$1;
124           shift 1 ;;
125         # -p | --port - The port from which to connect to the docker nexus
126     -p | --port )
127           shift 1 ;
128           PORT=$1;
129           shift 1 ;;
130         # -l | --local - Use this for deploying your local dockers without pulling them first
131     -l | --local )
132           LOCAL=true;
133           shift 1;;
134         # -t | --runTests - Use this for running the sanity tests docker after all other dockers have been deployed
135     -t | --runTests )
136           RUNTESTS=true;
137           shift 1 ;;
138         # -h | --help - Display the help message with all the available run options
139     -h | --help )
140           usage;
141           exit  0;;
142          * )
143           usage;
144           exit  1;;
145     esac
146 done
147
148
149 [ -f ${WORKSPACE}/opt/config/env_name.txt ] && DEP_ENV=$(cat ${WORKSPACE}/opt/config/env_name.txt) || DEP_ENV=__ENV-NAME__
150 [ -f ${WORKSPACE}/opt/config/nexus_username.txt ] && NEXUS_USERNAME=$(cat ${WORKSPACE}/opt/config/nexus_username.txt)    || NEXUS_USERNAME=release
151 [ -f ${WORKSPACE}/opt/config/nexus_password.txt ] && NEXUS_PASSWD=$(cat ${WORKSPACE}/opt/config/nexus_password.txt)      || NEXUS_PASSWD=sfWU3DFVdBr7GVxB85mTYgAW
152 [ -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}
153 [ -f ${WORKSPACE}/opt/config/nexus_username.txt ] && docker login -u $NEXUS_USERNAME -p $NEXUS_PASSWD $NEXUS_DOCKER_REPO
154
155
156 cleanup
157
158
159 export IP=`ip route get 8.8.8.8 | awk '/src/{ print $7 }'`
160 #If OSX, then use this to get IP
161 if [[ "$OSTYPE" == "darwin"* ]]; then
162     export IP=$(ipconfig getifaddr en0)
163 fi
164 export PREFIX=${NEXUS_DOCKER_REPO}'/onap'
165
166 if [ ${LOCAL} = true ]; then
167         PREFIX='onap'
168 fi
169
170 echo ""
171
172 healthCheck
173
174 # sanityDocker
175 if [[ (${RUNTESTS} = true) && (${healthCheck_http_code} == 200) ]]; then
176     echo "docker run sdc-sanity..."
177     echo "Triger sanity docker, please wait..."
178         
179     if [ ${LOCAL} = false ]; then
180         docker pull ${PREFIX}/sdc-sanity:${RELEASE}
181     fi
182
183 docker run --detach --name sdc-sanity --env HOST_IP=${IP} --env ENVNAME="${DEP_ENV}" --env http_proxy=${http_proxy} --env https_proxy=${https_proxy} --env no_proxy=${no_proxy} --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --ulimit memlock=-1:-1 --memory 1500m --ulimit nofile=4096:100000 $LOCAL_TIME_MOUNT_CMD --volume ${WORKSPACE}/data/logs/sdc-sanity/target:/var/lib/tests/target --volume ${WORKSPACE}/data/logs/sdc-sanity/ExtentReport:/var/lib/tests/ExtentReport --volume ${WORKSPACE}/data/logs/sdc-sanity/outputCsar:/var/lib/tests/outputCsar --volume ${WORKSPACE}/data/environments:/root/chef-solo/environments --publish 9560:9560 ${PREFIX}/sdc-sanity:${RELEASE}
184 echo "please wait while SANITY is starting....."
185 monitor_docker sdc-sanity
186
187 fi