Jack Lucas | 5caf165 | 2019-02-08 14:21:58 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ================================================================================ |
| 3 | # Copyright (c) 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 | # Push service registrations and key-value pairs to consul |
| 19 | # |
| 20 | # Environment variables control the consul address used: |
| 21 | # -- CONSUL_PROTO: The protocol (http or https) used to access consul. DEFAULT: http |
| 22 | # -- CONSUL_HOST: The Consul host address. DEFAULT: consul |
| 23 | # -- CONSUL_PORT: The Consul API port. DEFAULT: 8500 |
| 24 | # |
| 25 | # Command line options |
| 26 | # --service name|address|port : Register a service with name 'name', address 'address', and port 'port'. |
| 27 | # --key keyname|filepath: Register a key-value pair with key 'keyname' and the contents of a file at 'filepath' as its value |
| 28 | # A command can include multiple instances of each option. |
| 29 | |
| 30 | CONSUL_ADDR=${CONSUL_PROTO:-http}://${CONSUL_HOST:-consul}:${CONSUL_PORT:-8500} |
| 31 | KV_URL=${CONSUL_ADDR}/v1/kv |
| 32 | REG_URL=${CONSUL_ADDR}/v1/catalog/register |
| 33 | |
| 34 | # Register a service into Consul so that it can be discovered via the Consul service discovery API |
| 35 | # $1: Name under which service is registered |
| 36 | # $2: Address (typically DNS name, but can be IP) of the service |
| 37 | # $3: Port used by the service |
| 38 | function register_service { |
| 39 | service="{\"Node\": \"dcae\", \"Address\": \"$2\", \"Service\": {\"Service\": \"$1\", \"Address\": \"$2\", \"Port\": $3}}" |
| 40 | echo $service |
| 41 | curl -v -X PUT --data-binary "${service}" -H 'Content-Type: application/json' $REG_URL |
| 42 | } |
| 43 | |
| 44 | # Store the contents of a file into Consul KV store |
| 45 | # $1: Key under which content is stored |
| 46 | # $2: Path to file whose content will be the value associated with the key |
| 47 | function put_key { |
| 48 | curl -v -X PUT --data-binary @$2 -H 'Content-Type: application/json' ${KV_URL}/$1 |
| 49 | } |
| 50 | |
| 51 | set -x |
| 52 | |
| 53 | # Check Consul readiness |
| 54 | # The readiness container waits for a "consul-server" container to be ready, |
| 55 | # but this isn't always enough. We need the Consul API to be up and for |
| 56 | # the cluster to be formed, otherwise our Consul accesses might fail. |
| 57 | # Wait for Consul API to come up |
| 58 | until curl ${CONSUL_ADDR}/v1/agent/services |
| 59 | do |
| 60 | echo Waiting for Consul API |
| 61 | sleep 60 |
| 62 | done |
| 63 | # Wait for a leader to be elected |
| 64 | until [[ "$(curl -Ss {$CONSUL_ADDR}/v1/status/leader)" != '""' ]] |
| 65 | do |
| 66 | echo Waiting for leader |
| 67 | sleep 30 |
| 68 | done |
| 69 | |
| 70 | while (( "$#" )) |
| 71 | do |
| 72 | case $1 in |
| 73 | |
| 74 | "--service") |
| 75 | # ${2//|/ } turns all of the | characters in argument 2 into spaces |
| 76 | # () uses the space delimited string to initialize an array |
| 77 | # this turns an argument like inventory-api|inventory.onap|8080 into |
| 78 | # a three-element array with elements "inventory-api", "inventory.onap", and "8080" |
| 79 | s=(${2//|/ }) |
| 80 | register_service ${s[@]} |
| 81 | shift 2; |
| 82 | ;; |
| 83 | "--key") |
| 84 | # See above for explanation of (${2//|/ }) |
| 85 | kv=(${2//|/ }) |
| 86 | put_key ${kv[@]} |
| 87 | shift 2; |
| 88 | ;; |
| 89 | *) |
| 90 | echo "ignoring $1" |
| 91 | shift |
| 92 | ;; |
| 93 | esac |
| 94 | done |