fix installation bail problem
[demo.git] / boot / dcae2_vm_init.sh
1 #!/bin/bash
2 #############################################################################
3 #
4 # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #        http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #############################################################################
18
19
20
21
22 ################################## start of vm_init #####################################
23
24 set -ex
25
26 URL_ROOT='nexus.onap.org/service/local/repositories/raw/content'
27 REPO_BLUEPRINTS='org.onap.dcaegen2.platform.blueprints'
28 REPO_DEPLOYMENTS='org.onap.dcaegen2.deployments'
29 if [ -e /opt/config/dcae_deployment_profile.txt ]; then
30   DEPLOYMENT_PROFILE=$(cat /opt/config/dcae_deployment_profile.txt)
31 fi
32 DEPLOYMENT_PROFILE=${DEPLOYMENT_PROFILE:-R3}
33
34 NEXUS_USER=$(cat /opt/config/nexus_username.txt)
35 NEXUS_PASSWORD=$(cat /opt/config/nexus_password.txt)
36 NEXUS_DOCKER_REPO=$(cat /opt/config/nexus_docker_repo.txt)
37 DOCKER_VERSION=$(cat /opt/config/docker_version.txt)
38
39 MYFLOATIP=$(cat /opt/config/dcae_float_ip.txt)
40 MYLOCALIP=$(cat /opt/config/dcae_ip_addr.txt)
41 HTTP_PROXY=$(cat /opt/config/http_proxy.txt)
42 HTTPS_PROXY=$(cat /opt/config/https_proxy.txt)
43
44 if [ $HTTP_PROXY != "no_proxy" ]
45 then
46     export http_proxy=$HTTP_PROXY
47     export https_proxy=$HTTPS_PROXY
48 fi
49
50 # clean up old network configuration in docker engine
51 set +e
52 if [ -n "$(docker ps -q -a)" ]; then
53   docker stop $(docker ps -q -a)
54   docker update --restart=no $(docker ps -a -q)
55   systemctl restart docker
56   docker rm $(docker ps -q -a)
57   if [ -n "$(docker network ls | grep 'config_default')" ]; then
58     docker network rm config_default
59   fi
60 fi
61 set -e
62
63
64 docker login -u "$NEXUS_USER" -p "$NEXUS_PASSWORD" "$NEXUS_DOCKER_REPO"
65
66 if [[ $DEPLOYMENT_PROFILE == R1* || $DEPLOYMENT_PROFILE == R2* ]]; then
67   echo "R1 and R2 deployment profiles are not supported in Casablanca Heat deployment"
68 elif [[ $DEPLOYMENT_PROFILE == R3* ]]; then
69   RELEASE_TAG='R3'
70
71   set +e
72   rm -rf /opt/app/inputs-templates
73   mkdir -p /opt/app/inputs-templates
74   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-1.yaml
75   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-2.yaml
76   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-3.yaml
77   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-4.yaml
78   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/register.sh
79   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/setup.sh
80   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/teardown.sh
81
82   pip install --upgrade jinja2
83   wget https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/scripts/detemplate-bpinputs.py \
84     && \
85     (python detemplate-bpinputs.py /opt/config /opt/app/inputs-templates /opt/app/config; \
86      rm detemplate-bpinputs.py)
87
88   if [ -e /opt/app/config/register.sh ]; then
89     chmod +x /opt/app/config/register.sh
90   fi
91   if [ -e /opt/app/config/setup.sh ]; then
92     chmod +x /opt/app/config/setup.sh
93   fi
94   if [ -e /opt/app/config/build-plugins.sh ]; then
95     chmod +x /opt/app/config/build-plugins.sh
96   fi
97   set -e
98
99   cd /opt/app/config
100   # deploy essentials
101   /opt/docker/docker-compose -f docker-compose-1.yaml up -d
102
103   # wait for essentials to become ready
104   echo "Waiting for Consul to come up ready"
105   while ! nc -z localhost 8500; do sleep 1; done
106   echo "Waiting for Postgres DB to come up ready"
107   while ! nc -z localhost 5432; do sleep 1; done
108   echo "Waiting for CBS to come up ready"
109   while ! nc -z localhost 10000; do sleep 1; done
110   echo "All dependencies are up, proceed to the next phase"
111   sleep 30
112
113   echo "Setup CloudifyManager and Registrator"
114   ./setup.sh
115   sleep 10
116
117   export http_proxy=""
118   export https_proxy=""
119
120   ./register.sh
121
122   echo "Bring up DCAE MIN service components for R2 use cases"
123   /opt/docker/docker-compose -f docker-compose-2.yaml up -d
124
125   if [[ "$DEPLOYMENT_PROFILE" == "R3" || "$DEPLOYMENT_PROFILE" == "R3PLUS" ]]; then
126     echo "Bring up DCAE platform components"
127     /opt/docker/docker-compose -f docker-compose-3.yaml up -d
128
129     if [[ "$DEPLOYMENT_PROFILE" == "R3PLUS" ]]; then
130       echo "Bring up additional (plus) DCAE service components"
131       /opt/docker/docker-compose -f docker-compose-4.yaml up -d
132     fi
133   fi
134
135   # start proxy for consul's health check
136   CONSULIP=$(cat /opt/config/dcae_ip_addr.txt)
137   echo "Consul is available at $CONSULIP"
138 fi
139
140 cat >./nginx.conf <<EOL
141 server {
142     listen 80;
143     server_name dcae.simpledemo.onap.org;
144     root /www/healthcheck;
145
146     location /healthcheck {
147         try_files /services.yaml =404;
148     }
149     location /R3MIN{
150         try_files /r3mvp_healthy.yaml =404;
151     }
152     location /R3 {
153         try_files /r3_healthy.yaml =404;
154     }
155     location /R3PLUS {
156         try_files /r3plus_healthy.yaml =404;
157     }
158 }
159 EOL
160
161 HEALTHPORT=8000
162 docker run -d \
163 --name dcae-health \
164 -p ${HEALTHPORT}:80 \
165 -v "$(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf" \
166 -v "/tmp/healthcheck:/www/healthcheck" \
167 --label "SERVICE_80_NAME=dcae-health" \
168 --label "SERVICE_80_CHECK_HTTP=/healthcheck" \
169 --label "SERVICE_80_CHECK_INTERVAL=15s" \
170 --label "SERVICE_80_CHECK_INITIAL_STATUS=passing" \
171  nginx
172
173 echo "Healthcheck API available at http://${MYFLOATIP}:${HEALTHPORT}/healthcheck"
174 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3"
175 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3MIN"
176 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3PLUS"
177
178 # run forever for updating health status based on consul
179 set +e
180 while :
181 do
182   rm -rf /tmp/healthcheck/*
183   # all registered services
184   SERVICES=$(curl -s http://consul:8500/v1/agent/services |jq '. | to_entries[] | .value.Service')
185   # passing services
186   SERVICES=$(curl -s http://consul:8500/v1/health/state/passing | jq '.[] | .ServiceName')
187
188   # remove empty lines/entries
189   SERVICES=$(echo "$SERVICES" | sed '/^\s*\"\"\s*$/d' |sed '/^\s*$/d')
190
191   SERVICES_JSON=$(echo "$SERVICES" | sed 's/\"$/\",/g' | sed '$ s/.$//')
192
193   echo "$(date): running healthy services:"
194   echo ">>>  $SERVICES"
195   PLT_CONSUL=$(echo "$SERVICES" |grep "consul")
196   PLT_CBS=$(echo "$SERVICES" |grep "config_binding_service")
197   MVP_PG_HOLMES=$(echo "$SERVICES" |grep "pgHolmes")
198   MVP_VES=$(echo "$SERVICES" |grep "mvp.*ves")
199   MVP_TCA=$(echo "$SERVICES" |grep "mvp.*tca")
200   MVP_HR=$(echo "$SERVICES" |grep "mvp.*holmes-rule")
201   MVP_HE=$(echo "$SERVICES" |grep "mvp.*holmes-engine")
202
203   PLT_CM=$(echo "$SERVICES" |grep "cloudify.*manager")
204   PLT_DH=$(echo "$SERVICES" |grep "deployment.*handler")
205   PLT_PH=$(echo "$SERVICES" |grep "policy.*handler")
206   PLT_SCH=$(echo "$SERVICES" |grep "service.*change.*handler")
207   PLT_INV=$(echo "$SERVICES" |grep "inventory")
208   PLT_PG_INVENTORY=$(echo "$SERVICES" |grep "pgInventory")
209
210   PLUS_MHB=$(echo "$SERVICES" |grep "heartbeat")
211   PLUS_PRH=$(echo "$SERVICES" |grep "prh")
212   PLUS_MPR=$(echo "$SERVICES" |grep "mapper")
213   PLUS_TRAP=$(echo "$SERVICES" |grep "snmptrap")
214
215   DATA="{\"healthy\" : \"$(date)\", \"healthy_services\": [${SERVICES_JSON}]}"
216   if [[ -n "$PLT_CONSUL" && -n "$PLT_CBS" && -n "$MVP_PG_HOLMES" && -n "$MVP_VES" && \
217         -n "$MVP_TCA" ]]; then
218     echo "${DATA}" > /tmp/healthcheck/r3mvp_healthy.yaml
219     echo "${DATA}" > /tmp/healthcheck/services.yaml
220     echo ">>>>>> enough services satisfying R3MIN service deployment"
221   else
222     echo ">>>>>> not enough services satisfying R3MIN service deployment"
223   fi
224
225   if [[ -n "$PLT_CONSUL" && -n "$PLT_CBS" && -n "$PLT_CM" && -n "$PLT_DH" && \
226         -n "$PLT_PH" && -n "$PLT_SCH" && -n "$PLT_INV" && -n "$PLT_PG_INVENTORY" ]]; then
227     echo ">>>>>> enough services satisfying R3 platform deployment"
228     echo "${DATA}" > /tmp/healthcheck/r3_healthy.yaml
229
230     if [[ -n "$PLUS_MHB" && -n "$PLUS_PRH" && -n "$PLUS_MPR" && -n "$PLUS_TRAP"  && -n "$MVP_HR" && -n "$MVP_HE" ]]; then
231       echo ">>>>>> enough services satisfying R3PLUS deployment"
232       echo "${DATA}" > /tmp/healthcheck/r3plus_healthy.yaml
233     else
234       echo ">>>>>> not enough services satisfying R3PLUS service deployment"
235     fi
236   else
237     echo ">>>>>> not enough services satisfying R3 platform or R3PLUS service deployment"
238   fi
239   
240   sleep 60
241 done
242