Fix DCAE deployment in Heat
[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 docker network rm config_default
52
53 docker login -u "$NEXUS_USER" -p "$NEXUS_PASSWORD" "$NEXUS_DOCKER_REPO"
54
55 if [[ $DEPLOYMENT_PROFILE == R1* || $DEPLOYMENT_PROFILE == R2* ]]; then
56   echo "R1 and R2 deployment profiles are not supported in Casablanca Heat deployment"
57 elif [[ $DEPLOYMENT_PROFILE == R3* ]]; then
58   RELEASE_TAG='R3'
59
60   set +e
61   rm -rf /opt/app/inputs-templates
62   mkdir -p /opt/app/inputs-templates
63   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-1.yaml
64   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-2.yaml
65   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-3.yaml
66   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/docker-compose-4.yaml
67   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/register.sh
68   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/setup.sh
69   wget -P /opt/app/inputs-templates https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/heat/teardown.sh
70
71   pip install --upgrade jinja2
72   wget https://${URL_ROOT}/${REPO_DEPLOYMENTS}/${RELEASE_TAG}/scripts/detemplate-bpinputs.py \
73     && \
74     (python detemplate-bpinputs.py /opt/config /opt/app/inputs-templates /opt/app/config; \
75      rm detemplate-bpinputs.py)
76
77   if [ -e /opt/app/config/register.sh ]; then
78     chmod +x /opt/app/config/register.sh
79   fi
80   if [ -e /opt/app/config/setup.sh ]; then
81     chmod +x /opt/app/config/setup.sh
82   fi
83   if [ -e /opt/app/config/build-plugins.sh ]; then
84     chmod +x /opt/app/config/build-plugins.sh
85   fi
86   set -e
87
88   cd /opt/app/config
89   # deploy essentials
90   /opt/docker/docker-compose -f docker-compose-1.yaml up -d
91
92   # wait for essentials to become ready
93   echo "Waiting for Consul to come up ready"
94   while ! nc -z localhost 8500; do sleep 1; done
95   echo "Waiting for Postgres DB to come up ready"
96   while ! nc -z localhost 5432; do sleep 1; done
97   echo "Waiting for CBS to come up ready"
98   while ! nc -z localhost 10000; do sleep 1; done
99   echo "All dependencies are up, proceed to the next phase"
100   sleep 30
101
102   echo "Setup CloudifyManager and Registrator"
103   ./setup.sh
104   sleep 10
105
106   export http_proxy=""
107   export https_proxy=""
108
109   ./register.sh
110
111   echo "Bring up DCAE MIN service components for R2 use cases"
112   /opt/docker/docker-compose -f docker-compose-2.yaml up -d
113
114   if [[ "$DEPLOYMENT_PROFILE" == "R3" || "$DEPLOYMENT_PROFILE" == "R3PLUS" ]]; then
115     echo "Bring up DCAE platform components"
116     /opt/docker/docker-compose -f docker-compose-3.yaml up -d
117
118     if [[ "$DEPLOYMENT_PROFILE" == "R3PLUS" ]]; then
119       echo "Bring up additional (plus) DCAE service components"
120       /opt/docker/docker-compose -f docker-compose-4.yaml up -d
121     fi
122   fi
123
124   # start proxy for consul's health check
125   CONSULIP=$(cat /opt/config/dcae_ip_addr.txt)
126   echo "Consul is available at $CONSULIP"
127 fi
128
129 cat >./nginx.conf <<EOL
130 server {
131     listen 80;
132     server_name dcae.simpledemo.onap.org;
133     root /www/healthcheck;
134
135     location /healthcheck {
136         try_files /services.yaml =404;
137     }
138     location /R3MIN{
139         try_files /r3mvp_healthy.yaml =404;
140     }
141     location /R3 {
142         try_files /r3_healthy.yaml =404;
143     }
144     location /R3PLUS {
145         try_files /r3plus_healthy.yaml =404;
146     }
147 }
148 EOL
149
150 HEALTHPORT=8000
151 docker run -d \
152 --name dcae-health \
153 -p ${HEALTHPORT}:80 \
154 -v "$(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf" \
155 -v "/tmp/healthcheck:/www/healthcheck" \
156 --label "SERVICE_80_NAME=dcae-health" \
157 --label "SERVICE_80_CHECK_HTTP=/healthcheck" \
158 --label "SERVICE_80_CHECK_INTERVAL=15s" \
159 --label "SERVICE_80_CHECK_INITIAL_STATUS=passing" \
160  nginx
161
162 echo "Healthcheck API available at http://${MYFLOATIP}:${HEALTHPORT}/healthcheck"
163 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3"
164 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3MIN"
165 echo "                             http://${MYFLOATIP}:${HEALTHPORT}/R3PLUS"
166
167 # run forever for updating health status based on consul
168 set +e
169 while :
170 do
171   rm -rf /tmp/healthcheck/*
172   # all registered services
173   SERVICES=$(curl -s http://consul:8500/v1/agent/services |jq '. | to_entries[] | .value.Service')
174   # passing services
175   SERVICES=$(curl -s http://consul:8500/v1/health/state/passing | jq '.[] | .ServiceName')
176
177   # remove empty lines/entries
178   SERVICES=$(echo "$SERVICES" | sed '/^\s*\"\"\s*$/d' |sed '/^\s*$/d')
179
180   SERVICES_JSON=$(echo "$SERVICES" | sed 's/\"$/\",/g' | sed '$ s/.$//')
181
182   echo "$(date): running healthy services:"
183   echo ">>>  $SERVICES"
184   PLT_CONSUL=$(echo "$SERVICES" |grep "consul")
185   PLT_CBS=$(echo "$SERVICES" |grep "config_binding_service")
186   MVP_PG_HOLMES=$(echo "$SERVICES" |grep "pgHolmes")
187   MVP_VES=$(echo "$SERVICES" |grep "mvp.*ves")
188   MVP_TCA=$(echo "$SERVICES" |grep "mvp.*tca")
189   MVP_HR=$(echo "$SERVICES" |grep "mvp.*holmes-rule")
190   MVP_HE=$(echo "$SERVICES" |grep "mvp.*holmes-engine")
191
192   PLT_CM=$(echo "$SERVICES" |grep "cloudify.*manager")
193   PLT_DH=$(echo "$SERVICES" |grep "deployment.*handler")
194   PLT_PH=$(echo "$SERVICES" |grep "policy.*handler")
195   PLT_SCH=$(echo "$SERVICES" |grep "service.*change.*handler")
196   PLT_INV=$(echo "$SERVICES" |grep "inventory")
197   PLT_PG_INVENTORY=$(echo "$SERVICES" |grep "pgInventory")
198
199   PLUS_MHB=$(echo "$SERVICES" |grep "heartbeat")
200   PLUS_PRH=$(echo "$SERVICES" |grep "prh")
201   PLUS_MPR=$(echo "$SERVICES" |grep "mapper")
202   PLUS_TRAP=$(echo "$SERVICES" |grep "snmptrap")
203
204   DATA="{\"healthy\" : \"$(date)\", \"healthy_services\": [${SERVICES_JSON}]}"
205   if [[ -n "$PLT_CONSUL" && -n "$PLT_CBS" && -n "$MVP_PG_HOLMES" && -n "$MVP_VES" && \
206         -n "$MVP_TCA" ]]; then
207     echo "${DATA}" > /tmp/healthcheck/r3mvp_healthy.yaml
208     echo "${DATA}" > /tmp/healthcheck/services.yaml
209     echo ">>>>>> enough services satisfying R3MIN service deployment"
210   else
211     echo ">>>>>> not enough services satisfying R3MIN service deployment"
212   fi
213
214   if [[ -n "$PLT_CONSUL" && -n "$PLT_CBS" && -n "$PLT_CM" && -n "$PLT_DH" && \
215         -n "$PLT_PH" && -n "$PLT_SCH" && -n "$PLT_INV" && -n "$PLT_PG_INVENTORY" ]]; then
216     echo ">>>>>> enough services satisfying R3 platform deployment"
217     echo "${DATA}" > /tmp/healthcheck/r3_healthy.yaml
218
219     if [[ -n "$PLUS_MHB" && -n "$PLUS_PRH" && -n "$PLUS_MPR" && -n "$PLUS_TRAP"  && -n "$MVP_HR" && -n "$MVP_HE" ]]; then
220       echo ">>>>>> enough services satisfying R3PLUS deployment"
221       echo "${DATA}" > /tmp/healthcheck/r3plus_healthy.yaml
222     else
223       echo ">>>>>> not enough services satisfying R3PLUS service deployment"
224     fi
225   else
226     echo ">>>>>> not enough services satisfying R3 platform or R3PLUS service deployment"
227   fi
228   
229   sleep 60
230 done
231