Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ================================================================================ |
vv770d | ba21cba | 2021-03-29 19:30:30 +0000 | [diff] [blame] | 3 | # Copyright (c) 2018-2021 AT&T Intellectual Property. All rights reserved. |
Jack Lucas | ec99060 | 2021-01-13 12:50:10 -0500 | [diff] [blame] | 4 | # Copyright (c) 2021 J. F. Lucas. All rights reserved. |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 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 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END========================================================= |
| 18 | |
Jack Lucas | fa2da72 | 2021-06-03 12:55:52 -0400 | [diff] [blame] | 19 | # Prior to the "H" release, this bootstrap script was used to deploy DCAE components |
| 20 | # during the initial DCAE installation process using Cloudify Manager with Cloudify |
| 21 | # blueprints. Over the course of several releases, we have migrated these components to use Helm |
| 22 | # deployment, starting with the DCAE "platform components" and, in the "H" release, the 4 |
| 23 | # DCAE microservices that are always launched when DCAE is deployed. |
| 24 | # |
| 25 | # For the "I" release, we are expecting to migrate all DCAE microservices to Helm deployment, |
| 26 | # including the microservices that are launched on demand after initial DCAE installation. |
| 27 | # We are continuing to provide support for Cloudify deployments during the "I" release. This |
| 28 | # bootstrap script will deploy the Cloudify blueprint that initializes the Cloudify-based |
| 29 | # DCAE postgres instance and will upload all of the DCAE Cloudify blueprints to the DCAE |
| 30 | # inventory component. The bootstrap container will continue to run after its deployment and |
| 31 | # upload work is complete. User can "exec" into the bootstrap container and use the Cloudify |
| 32 | # "cfy" command to debug any issues related to Cloudify deployments. |
| 33 | # |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 34 | # Expects: |
| 35 | # CM address (IP or DNS) in CMADDR environment variable |
| 36 | # CM password in CMPASS environment variable (assumes user is "admin") |
Jack Lucas | ec99060 | 2021-01-13 12:50:10 -0500 | [diff] [blame] | 37 | # Blueprints for components to be installed in /blueprints |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 38 | # Input files for components to be installed in /inputs |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 39 | # Optionally, allows: |
| 40 | # CM protocol in CMPROTO environment variable (defaults to HTTP) |
| 41 | # CM port in CMPORT environment variable (defaults to 80) |
| 42 | # If CMPROTO is set to "https", bootstrap will use HTTPS to communicate with CM. Otherwise, |
| 43 | # it will use HTTP. |
| 44 | # If CMPROTO is set to "https", the script assumes the CA cert needed to verify the cert |
| 45 | # presented by CM is mounted at /certs/cacert.pem. |
| 46 | |
| 47 | # Set defaults for CM protocol and port |
| 48 | CMPROTO=${CMPROTO:-http} |
| 49 | CMPORT=${CMPORT:-80} |
| 50 | |
| 51 | # Set up additional parameters for using HTTPS |
Jack Lucas | a25c923 | 2020-03-02 11:07:31 -0500 | [diff] [blame] | 52 | CACERT="/certs/cacert.pem" |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 53 | CFYTLS="" |
| 54 | CURLTLS="" |
| 55 | if [ $CMPROTO = "https" ] |
| 56 | then |
Jack Lucas | a25c923 | 2020-03-02 11:07:31 -0500 | [diff] [blame] | 57 | CFYTLS="--rest-certificate $CACERT --ssl" |
| 58 | CURLTLS="--cacert $CACERT" |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 59 | fi |
| 60 | |
| 61 | ### FUNCTION DEFINITIONS ### |
| 62 | |
| 63 | # keep_running: Keep running after bootstrap finishes or after error |
| 64 | keep_running() { |
| 65 | echo $1 |
| 66 | sleep infinity & |
| 67 | wait |
| 68 | } |
| 69 | |
| 70 | # cm_hasany: Query Cloudify Manager and return 0 (true) if there are any entities matching the query |
| 71 | # Used to see if something is already present on CM |
| 72 | # $1 -- query fragment, for instance "plugins?archive_name=xyz.wgn" to get |
| 73 | # the number of plugins that came from the archive file "xyz.wgn" |
| 74 | function cm_hasany { |
| 75 | # We use _include=id to limit the amount of data the CM sends back |
| 76 | # We rely on the "metadata.pagination.total" field in the response |
| 77 | # for the total number of matching entities |
| 78 | COUNT=$(curl -Ss -H "Tenant: default_tenant" --user admin:${CMPASS} ${CURLTLS} "${CMPROTO}://${CMADDR}:${CMPORT}/api/v3.1/$1&_include=id" \ |
| 79 | | /bin/jq .metadata.pagination.total) |
| 80 | if (( $COUNT > 0 )) |
| 81 | then |
| 82 | return 0 |
| 83 | else |
| 84 | return 1 |
| 85 | fi |
| 86 | } |
| 87 | |
| 88 | # deploy: Deploy components if they're not already deployed |
| 89 | # $1 -- name (for bp and deployment) |
| 90 | # $2 -- blueprint file name |
| 91 | # $3 -- inputs file name (optional) |
| 92 | function deploy { |
| 93 | # Don't crash the script on error |
| 94 | set +e |
| 95 | |
| 96 | # Upload blueprint if it's not already there |
| 97 | if cm_hasany "blueprints?id=$1" |
| 98 | then |
| 99 | echo blueprint $1 is already installed on ${CMADDR} |
| 100 | else |
| 101 | cfy blueprints upload -b $1 /blueprints/$2 |
| 102 | fi |
| 103 | |
| 104 | # Create deployment if it doesn't already exist |
| 105 | if cm_hasany "deployments?id=$1" |
| 106 | then |
| 107 | echo deployment $1 has already been created on ${CMADDR} |
| 108 | else |
| 109 | INPUTS= |
| 110 | if [ -n "$3" ] |
| 111 | then |
| 112 | INPUTS="-i/inputs/$3" |
| 113 | fi |
| 114 | cfy deployments create -b $1 ${INPUTS} $1 |
| 115 | fi |
| 116 | |
| 117 | # Run the install workflow if it hasn't been run already |
| 118 | # We don't have a completely certain way of determining this. |
| 119 | # We check to see if the deployment has any node instances |
| 120 | # that are in the 'uninitialized' or 'deleted' states. (Note that |
| 121 | # the & in the query acts as a logical OR for the multiple state values.) |
| 122 | # We'll try to install when a deployment has node instances in those states |
| 123 | if cm_hasany "node-instances?deployment_id=$1&state=uninitialized&state=deleted" |
| 124 | then |
| 125 | cfy executions start -d $1 install |
| 126 | else |
| 127 | echo deployment $1 appears to have had an install workflow executed already or is not ready for an install |
| 128 | fi |
| 129 | } |
| 130 | |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 131 | ### END FUNCTION DEFINTIONS ### |
| 132 | |
| 133 | set -x |
| 134 | |
| 135 | # Make sure we keep the container alive after an error |
| 136 | trap keep_running ERR |
| 137 | |
| 138 | set -e |
| 139 | |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 140 | # Set up profile to access Cloudify Manager |
| 141 | cfy profiles use -u admin -t default_tenant -p "${CMPASS}" ${CFYTLS} "${CMADDR}" |
| 142 | |
| 143 | # Output status, for debugging purposes |
| 144 | cfy status |
| 145 | |
Jack Lucas | 01d6019 | 2020-06-17 17:11:17 -0400 | [diff] [blame] | 146 | # Store the CM password into a Cloudify secret |
| 147 | cfy secret create -s ${CMPASS} cmpass |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 148 | |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 149 | # After this point, failures should not stop the script or block later commands |
| 150 | trap - ERR |
| 151 | set +e |
| 152 | |
| 153 | # Initialize the DCAE postgres instance |
| 154 | deploy pgaas_initdb k8s-pgaas-initdb.yaml k8s-pgaas-initdb-inputs.yaml |
| 155 | |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 156 | # Display deployments, for debugging purposes |
| 157 | cfy deployments list |
| 158 | |
Jack Lucas | a25c923 | 2020-03-02 11:07:31 -0500 | [diff] [blame] | 159 | # Load blueprints into DCAE inventory as |
| 160 | # DCAE service types |
| 161 | . /scripts/inventory.sh |
| 162 | for BP in /blueprints/*.yaml |
| 163 | do |
| 164 | upload_service_type $BP $CACERT |
| 165 | done |
| 166 | |
Jack Lucas | bad7720 | 2020-02-03 18:21:29 -0500 | [diff] [blame] | 167 | # Continue running |
| 168 | keep_running "Finished bootstrap steps." |
| 169 | echo "Exiting!" |