Release healthcheck-container 2.4.1
[dcaegen2/deployments.git] / consul-loader-container / consul_store.sh
1 #!/bin/bash
2 # ================================================================================
3 # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 # Copyright (c) 2021 J. F. Lucas. 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 #
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
19 # Push service registrations and key-value pairs to consul
20 #
21 # Environment variables control the consul address used:
22 #   -- CONSUL_PROTO:  The protocol (http or https) used to access consul.  DEFAULT: http
23 #   -- CONSUL_HOST: The Consul host address.  DEFAULT: consul
24 #   -- CONSUL_PORT: The Consul API port.  DEFAULT: 8500
25 #
26 # Command line options
27 # --service name|address|port :  Register a service with name 'name', address 'address', and port 'port'.
28 # --key keyname|filepath:  Register a key-value pair with key 'keyname' and the contents of a file at 'filepath' as its value
29 # --key-yaml keyname|filepath: Register a key-value pair with name 'keyname', converting the YAML content of the file at 'filepath'
30 #   to JSON, and storing the JSON result as the value.  This is used for Helm deployment of DCAE microservices, where the initial
31 #   application configuration is stored in a Helm values.yaml file in YAML form.  --key-yaml converts the YAML configuration into
32 #   JSON, which is the format that microservices expect.
33 # -- delete-key
34 # A command can include multiple instances of each option.
35
36 CONSUL_ADDR=${CONSUL_PROTO:-http}://${CONSUL_HOST:-consul}:${CONSUL_PORT:-8500}
37 KV_URL=${CONSUL_ADDR}/v1/kv
38 REG_URL=${CONSUL_ADDR}/v1/catalog/register
39
40 # Register a service into Consul so that it can be discovered via the Consul service discovery API
41 #  $1: Name under which service is registered
42 #  $2: Address (typically DNS name, but can be IP) of the service
43 #  $3: Port used by the service
44 function register_service {
45   service="{\"Node\": \"dcae\", \"Address\": \"$2\", \"Service\": {\"Service\": \"$1\", \"Address\": \"$2\", \"Port\": $3}}"
46   echo $service
47   curl -v -X PUT --data-binary "${service}" -H 'Content-Type: application/json' $REG_URL
48 }
49
50 # Store the contents of a file into Consul KV store
51 #  $1: Key under which content is stored
52 #  $2: Path to file whose content will be the value associated with the key
53 function put_key {
54   curl -v -X PUT --data-binary @$2 -H 'Content-Type: application/json' ${KV_URL}/$1
55 }
56
57 # Delete a key from the Consul KV store
58 # $1: Key to be deleted
59 function delete_key {
60   curl -v -X DELETE ${KV_URL}/$1
61 }
62
63 set -x
64
65 # Check Consul readiness
66 # The readiness container waits for a "consul-server" container to be ready,
67 # but this isn't always enough.  We need the Consul API to be up and for
68 # the cluster to be formed, otherwise our Consul accesses might fail.
69 # Wait for Consul API to come up
70 until curl ${CONSUL_ADDR}/v1/agent/services
71 do
72     echo Waiting for Consul API
73     sleep 60
74 done
75 # Wait for a leader to be elected
76 until [[ "$(curl -Ss {$CONSUL_ADDR}/v1/status/leader)" != '""' ]]
77 do
78     echo Waiting for leader
79     sleep 30
80 done
81
82 while (( "$#" ))
83 do
84   case $1 in
85
86   "--service")
87      # ${2//|/ } turns all of the | characters in argument 2 into spaces
88      # () uses the space delimited string to initialize an array
89      # this turns an argument like inventory-api|inventory.onap|8080 into
90      # a three-element array with elements "inventory-api", "inventory.onap", and "8080"
91      s=(${2//|/ })
92      register_service ${s[@]}
93      shift 2;
94      ;;
95   "--key")
96     # See above for explanation of (${2//|/ })
97     kv=(${2//|/ })
98     put_key ${kv[@]}
99     shift 2;
100     ;;
101   "--key-yaml")
102     # See above for explanation of (${2//|/ })
103     kv=(${2//|/ })
104     cat ${kv[1]} | /opt/app/yaml2json.py | put_key ${kv[0]} -
105     shift 2;
106     ;;
107   "--delete-key")
108     delete_key $2
109     shift 2;
110     ;;
111   *)
112     echo "ignoring $1"
113     shift
114     ;;
115   esac
116 done