Bulk update to deployment
[dcaegen2/deployments.git] / k8s-bootstrap-container / bootstrap.sh
1 #!/bin/bash
2 # ================================================================================
3 # Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
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 # ============LICENSE_END=========================================================
17
18 # Install DCAE via Cloudify Manager
19 # Expects:
20 #   CM address (IP or DNS) in CMADDR environment variable
21 #   CM password in CMPASS environment variable (assumes user is "admin")
22 #   ONAP common Kubernetes namespace in ONAP_NAMESPACE environment variable
23 #   If DCAE components are deployed in a separate Kubernetes namespace, that namespace in DCAE_NAMESPACE variable.
24 #   Consul address with port in CONSUL variable
25 #   Plugin wagon files in /wagons
26 #       Blueprints for components to be installed in /blueprints
27 #   Input files for components to be installed in /inputs
28 #   Configuration JSON files that need to be loaded into Consul in /dcae-configs
29 #   Consul is installed in /opt/consul/bin/consul, with base config in /opt/consul/config/00consul.json
30
31 ### FUNCTION DEFINITIONS ###
32
33 # keep_running: Keep running after bootstrap finishes or after error
34 keep_running() {
35     echo $1
36     sleep infinity &
37     wait
38 }
39
40 # cm_hasany: Query Cloudify Manager and return 0 (true) if there are any entities matching the query
41 # Used to see if something is already present on CM
42 # $1 -- query fragment, for instance "plugins?archive_name=xyz.wgn" to get
43 #  the number of plugins that came from the archive file "xyz.wgn"
44 function cm_hasany {
45     # We use _include=id to limit the amount of data the CM sends back
46     # We rely on the "metadata.pagination.total" field in the response
47     # for the total number of matching entities
48     COUNT=$(curl -Ss -H "Tenant: default_tenant" --user admin:${CMPASS} "${CMADDR}/api/v3.1/$1&_include=id" \
49              | /bin/jq .metadata.pagination.total)
50     if (( $COUNT > 0 ))
51     then
52         return 0
53     else
54         return 1
55     fi
56 }
57
58 # deploy: Deploy components if they're not already deployed
59 # $1 -- name (for bp and deployment)
60 # $2 -- blueprint file name
61 # $3 -- inputs file name (optional)
62 function deploy {
63     # Don't crash the script on error
64     set +e
65
66     # Upload blueprint if it's not already there
67     if cm_hasany "blueprints?id=$1"
68     then
69         echo blueprint $1 is already installed on ${CMADDR}
70     else
71         cfy blueprints upload -b $1  /blueprints/$2
72     fi
73
74     # Create deployment if it doesn't already exist
75     if cm_hasany "deployments?id=$1"
76     then
77        echo deployment $1 has already been created on ${CMADDR}
78     else
79         INPUTS=
80         if [ -n "$3" ]
81         then
82             INPUTS="-i/inputs/$3"
83         fi
84         cfy deployments create -b $1 ${INPUTS} $1
85     fi
86
87     # Run the install workflow if it hasn't been run already
88     # We don't have a completely certain way of determining this.
89     # We check to see if the deployment has any node instances
90     # that are in the 'uninitialized' or 'deleted' states.  (Note that
91     # the & in the query acts as a logical OR for the multiple state values.)
92     # We'll try to install when a deployment has node instances in those states
93     if cm_hasany "node-instances?deployment_id=$1&state=uninitialized&state=deleted"
94     then
95         cfy executions start -d $1 install
96     else
97         echo deployment $1 appears to have had an install workflow executed already or is not ready for an install
98     fi
99 }
100
101 # Install plugin if it's not already installed
102 # $1 -- path to wagon file for plugin
103 function install_plugin {
104     ARCHIVE=$(basename $1)
105     # See if it's already installed
106     if cm_hasany "plugins?archive_name=$ARCHIVE"
107     then
108         echo plugin $1 already installed on ${CMADDR}
109     else
110         cfy plugin upload $1
111     fi
112 }
113
114 ### END FUNCTION DEFINTIONS ###
115
116 set -x
117
118 # Make sure we keep the container alive after an error
119 trap keep_running ERR
120
121 set -e
122
123 # Consul service registration data
124 CBS_REG='{"ID": "dcae-cbs0", "Name": "config_binding_service", "Address": "config-binding-service", "Port": 10000}'
125 CBS_REG1='{"ID": "dcae-cbs1", "Name": "config-binding-service", "Address": "config-binding-service", "Port": 10000}'
126 HE_REG='{"ID": "dcae-he0", "Name": "holmes-engine-mgmt", "Address": "holmes-engine-mgmt", "Port": 9102}'
127 HR_REG='{"ID": "dcae-hr0", "Name": "holmes-rule-mgmt", "Address": "holmes-rule-mgmt", "Port": 9101}'
128 PH_REG='{"ID": "dcae-ph0", "Name": "policy_handler", "Port": 25577, "Address": "policy-handler'
129 if [ ! -z "${DCAE_NAMESPACE}" ]
130 then
131         PH_REG="${PH_REG}.${DCAE_NAMESPACE}"
132 fi
133 PH_REG="${PH_REG}\"}"
134
135 # Set up profile to access Cloudify Manager
136 cfy profiles use -u admin -t default_tenant -p "${CMPASS}"  "${CMADDR}"
137
138 # Output status, for debugging purposes
139 cfy status
140
141 # Check Consul readiness
142 # The readiness container waits for a "consul-server" container to be ready,
143 # but this isn't always enough.  We need the Consul API to be up and for
144 # the cluster to be formed, otherwise our Consul accesses might fail.
145 # (Note in ONAP R2, we never saw a problem, but occasionally in R3 we
146 # have seen Consul not be fully ready, so we add these checks, originally
147 # used in the R1 HEAT-based deployment.)
148 # Wait for Consul API to come up
149 until curl http://${CONSUL}/v1/agent/services
150 do
151     echo Waiting for Consul API
152     sleep 60
153 done
154 # Wait for a leader to be elected
155 until [[ "$(curl -Ss http://{$CONSUL}/v1/status/leader)" != '""' ]]
156 do
157     echo Waiting for leader
158     sleep 30
159 done
160
161 # Load configurations into Consul KV store
162 for config in /dcae-configs/*.json
163 do
164     # The basename of the file is the Consul key
165     key=$(basename ${config} .json)
166     # Strip out comments, empty lines
167     egrep -v "^#|^$" ${config} > /tmp/dcae-upload
168     curl -v -X PUT -H "Content-Type: application/json" --data-binary @/tmp/dcae-upload ${CONSUL}/v1/kv/${key}
169 done
170
171 # Put service registrations into the local Consul configuration directory
172 for sr in CBS_REG CBS_REG1 HE_REG HR_REG PH_REG
173 do
174   echo '{"service" : ' ${!sr}  ' }'> /opt/consul/config/${sr}.json
175 done
176
177 # Start the local consul agent instance
178 /opt/consul/bin/consul agent --config-dir /opt/consul/config 2>&1 | tee /opt/consul/consul.log &
179
180 # Store the CM password into a Cloudify secret
181 cfy secret create -s ${CMPASS} cmpass
182
183 # Load plugins onto CM
184 for wagon in /wagons/*.wgn
185 do
186     install_plugin ${wagon}
187 done
188
189 # After this point, failures should not stop the script or block later commands
190 trap - ERR
191 set +e
192
193 # Deploy platform components
194 # Allow for some parallelism to speed up the process.  Probably could be somewhat more aggressive.
195 deploy pgaas_initdb k8s-pgaas-initdb.yaml k8s-pgaas-initdb-inputs.yaml &
196 deploy dashboard k8s-dashboard.yaml k8s-dashboard-inputs.yaml &
197 PG_PID=$!
198 wait ${PG_PID}
199
200 # Deploy service components
201 # tca, ves, prh, hv-ves, datafile-collector can be deployed simultaneously
202 deploy tca k8s-tca.yaml k8s-tca-inputs.yaml &
203 deploy ves k8s-ves.yaml k8s-ves-inputs.yaml &
204 deploy snmptrap k8s-snmptrap.yaml k8s-snmptrap-inputs.yaml &
205 deploy prh k8s-prh.yaml k8s-prh-inputs.yaml &
206 deploy hv-ves k8s-hv-ves.yaml k8s-hv_ves-inputs.yaml &
207 deploy datafile-collector k8s-datafile-collector.yaml k8s-datafile-collector-inputs.yaml &
208 # holmes_rules must be deployed before holmes_engine, but holmes_rules can go in parallel with other service components
209 deploy holmes_rules k8s-holmes-rules.yaml k8s-holmes_rules-inputs.yaml
210 deploy holmes_engine k8s-holmes-engine.yaml k8s-holmes_engine-inputs.yaml
211
212 # Display deployments, for debugging purposes
213 cfy deployments list
214
215 # Continue running
216 keep_running "Finished bootstrap steps."
217 echo "Exiting!"