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