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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 # Push service registrations and key-value pairs to consul
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
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.
34 # A command can include multiple instances of each option.
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
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}}"
47 curl -v -X PUT --data-binary "${service}" -H 'Content-Type: application/json' $REG_URL
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
54 curl -v -X PUT --data-binary @$2 -H 'Content-Type: application/json' ${KV_URL}/$1
57 # Delete a key from the Consul KV store
58 # $1: Key to be deleted
60 curl -v -X DELETE ${KV_URL}/$1
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
72 echo Waiting for Consul API
75 # Wait for a leader to be elected
76 until [[ "$(curl -Ss {$CONSUL_ADDR}/v1/status/leader)" != '""' ]]
78 echo Waiting for leader
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"
92 register_service ${s[@]}
96 # See above for explanation of (${2//|/ })
102 # See above for explanation of (${2//|/ })
104 cat ${kv[1]} | /opt/app/yaml2json.py | put_key ${kv[0]} -