khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [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 | # |
| 16 | |
| 17 | """ |
| 18 | Some consul related convenience functions |
| 19 | """ |
| 20 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 21 | from structlog import get_logger |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 22 | from consul import Consul |
| 23 | from random import randint |
ggowdru | 85150c2 | 2017-04-26 07:24:40 -0700 | [diff] [blame] | 24 | from common.utils.nethelpers import get_my_primary_local_ipv4 |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 25 | |
| 26 | log = get_logger() |
| 27 | |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 28 | |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 29 | def connect_to_consult(consul_endpoint): |
| 30 | log.debug('getting-service-endpoint', consul=consul_endpoint) |
Khen Nursimulu | 90fc35d | 2017-01-09 08:42:04 -0500 | [diff] [blame] | 31 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 32 | host = consul_endpoint.split(':')[0].strip() |
| 33 | port = int(consul_endpoint.split(':')[1].strip()) |
| 34 | |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 35 | return Consul(host=host, port=port) |
| 36 | |
| 37 | |
| 38 | def verify_all_services_healthy(consul_endpoint, service_name=None, |
| 39 | number_of_expected_services=None): |
| 40 | """ |
| 41 | Verify in consul if any service is healthy |
| 42 | :param consul_endpoint: a <host>:<port> string |
| 43 | :param service_name: name of service to check, optional |
| 44 | :param number_of_expected_services number of services to check for, optional |
| 45 | :return: true if healthy, false otherwise |
| 46 | """ |
| 47 | |
| 48 | def check_health(service): |
| 49 | _, serv_health = consul.health.service(service, passing=True) |
| 50 | return not serv_health == [] |
| 51 | |
| 52 | consul = connect_to_consult(consul_endpoint) |
| 53 | |
| 54 | if service_name is not None: |
| 55 | return check_health(service_name) |
| 56 | |
| 57 | services = get_all_services(consul_endpoint) |
| 58 | |
| 59 | items = services.keys() |
| 60 | |
| 61 | if number_of_expected_services is not None and \ |
| 62 | len(items) != number_of_expected_services: |
| 63 | return False |
| 64 | |
| 65 | for item in items: |
| 66 | if not check_health(item): |
| 67 | return False |
| 68 | |
| 69 | return True |
| 70 | |
| 71 | |
| 72 | def get_all_services(consul_endpoint): |
| 73 | |
| 74 | log.debug('getting-service-verify-health') |
| 75 | |
| 76 | consul = connect_to_consult(consul_endpoint) |
| 77 | _, services = consul.catalog.services() |
| 78 | |
| 79 | return services |
| 80 | |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 81 | def get_endpoint_from_consul(consul_endpoint, service_name): |
ggowdru | 85150c2 | 2017-04-26 07:24:40 -0700 | [diff] [blame] | 82 | """ |
| 83 | Get endpoint of service_name from consul. |
| 84 | :param consul_endpoint: a <host>:<port> string |
| 85 | :param service_name: name of service for which endpoint |
| 86 | needs to be found. |
| 87 | :return: service endpoint if available, else exit. |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 88 | """ |
| 89 | log.debug('getting-service-info', service=service_name) |
| 90 | |
| 91 | consul = connect_to_consult(consul_endpoint) |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 92 | _, services = consul.catalog.service(service_name) |
| 93 | |
| 94 | if len(services) == 0: |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 95 | raise Exception( |
| 96 | 'Cannot find service {} in consul'.format(service_name)) |
alshabib | b634d90 | 2017-01-16 13:10:17 -0600 | [diff] [blame] | 97 | os.exit(1) |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 98 | |
ggowdru | 85150c2 | 2017-04-26 07:24:40 -0700 | [diff] [blame] | 99 | """ Get host IPV4 address |
| 100 | """ |
| 101 | local_ipv4 = get_my_primary_local_ipv4() |
| 102 | """ If host IP address from where the request came in matches |
| 103 | the IP address of the requested service's host IP address, |
| 104 | pick the endpoint |
| 105 | """ |
| 106 | for i in range(len(services)): |
| 107 | service = services[i] |
| 108 | if service['ServiceAddress'] == local_ipv4: |
| 109 | log.debug("picking address locally") |
| 110 | endpoint = '{}:{}'.format(service['ServiceAddress'], |
| 111 | service['ServicePort']) |
| 112 | return endpoint |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 113 | |
ggowdru | 85150c2 | 2017-04-26 07:24:40 -0700 | [diff] [blame] | 114 | """ If service is not available locally, picak a random |
| 115 | endpoint for the service from the list |
| 116 | """ |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 117 | service = services[randint(0, len(services) - 1)] |
| 118 | endpoint = '{}:{}'.format(service['ServiceAddress'], |
| 119 | service['ServicePort']) |
| 120 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 121 | return endpoint |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
Khen Nursimulu | 9b9f1ad | 2017-01-10 15:43:32 -0500 | [diff] [blame] | 125 | print get_endpoint_from_consul('10.100.198.220:8500', 'kafka') |