alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # ================================================================================ |
Lusheng Ji | 967cc9a | 2018-02-12 10:45:42 -0500 | [diff] [blame] | 2 | # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 3 | # ================================================================================ |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============LICENSE_END========================================================= |
| 16 | # |
| 17 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 18 | |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 19 | """client to talk to consul at the standard port 8500""" |
| 20 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 21 | import base64 |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 22 | import json |
| 23 | import logging |
| 24 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 25 | import requests |
| 26 | |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 27 | from .customize import CustomizerUser |
| 28 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 29 | class DiscoveryClient(object): |
| 30 | """talking to consul at http://consul:8500 |
| 31 | |
| 32 | relies on proper --add-host "consul:<consul-agent ip>" in |
| 33 | docker run command that runs along the consul-agent: |
| 34 | |
| 35 | docker run --name ${APPNAME} -d |
| 36 | -e HOSTNAME |
| 37 | --add-host "consul:<consul-agent ip>" |
| 38 | -v ${BASEDIR}/logs:${TARGETDIR}/logs |
| 39 | -v ${BASEDIR}/etc:${TARGETDIR}/etc |
| 40 | -p <outport>:<innerport> |
| 41 | ${APPNAME}:latest |
| 42 | """ |
| 43 | CONSUL_SERVICE_MASK = "http://consul:8500/v1/catalog/service/{0}" |
| 44 | CONSUL_KV_MASK = "http://consul:8500/v1/kv/{0}" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 45 | _logger = logging.getLogger("policy_handler.discovery") |
| 46 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 47 | @staticmethod |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 48 | def get_service_url(audit, service_name): |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 49 | """find the service record in consul""" |
| 50 | service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(service_name) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 51 | log_line = "discover {0}".format(service_path) |
| 52 | DiscoveryClient._logger.info(log_line) |
| 53 | audit.info(log_line) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 54 | response = requests.get(service_path) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 55 | |
| 56 | log_line = "response {0} for {1}: {2}".format( |
| 57 | response.status_code, service_path, response.text) |
| 58 | DiscoveryClient._logger.info(log_line) |
| 59 | audit.info(log_line) |
| 60 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 61 | response.raise_for_status() |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 62 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 63 | service = response.json() |
| 64 | if not service: |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 65 | log_line = "failed discover {0}".format(service_path) |
| 66 | DiscoveryClient._logger.error(log_line) |
| 67 | audit.error(log_line) |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 68 | return |
| 69 | service = service[0] |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame^] | 70 | |
| 71 | service_url = CustomizerUser.get_customizer().get_service_url(audit, service_name, service) |
| 72 | if not service_url: |
| 73 | log_line = "failed to get service_url for {0}".format(service_name) |
| 74 | DiscoveryClient._logger.error(log_line) |
| 75 | audit.error(log_line) |
| 76 | return |
| 77 | |
| 78 | log_line = "got service_url: {0} for {1}".format(service_url, service_name) |
| 79 | DiscoveryClient._logger.info(log_line) |
| 80 | audit.info(log_line) |
| 81 | return service_url |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 82 | |
| 83 | @staticmethod |
| 84 | def get_value(key): |
| 85 | """get the value for the key from consul-kv""" |
| 86 | response = requests.get(DiscoveryClient.CONSUL_KV_MASK.format(key)) |
| 87 | response.raise_for_status() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 88 | data = response.json() |
| 89 | if not data: |
| 90 | DiscoveryClient._logger.error("failed get_value %s", key) |
| 91 | return |
| 92 | value = base64.b64decode(data[0]["Value"]).decode("utf-8") |
| 93 | DiscoveryClient._logger.info("consul-kv key=%s value(%s) data=%s", |
| 94 | key, value, json.dumps(data)) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 95 | return json.loads(value) |