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 |
| 24 | |
| 25 | log = get_logger() |
| 26 | |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 27 | |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 28 | def connect_to_consult(consul_endpoint): |
| 29 | log.debug('getting-service-endpoint', consul=consul_endpoint) |
Khen Nursimulu | 90fc35d | 2017-01-09 08:42:04 -0500 | [diff] [blame] | 30 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 31 | host = consul_endpoint.split(':')[0].strip() |
| 32 | port = int(consul_endpoint.split(':')[1].strip()) |
| 33 | |
alshabib | 16c0da7 | 2017-01-19 12:26:02 -0600 | [diff] [blame] | 34 | return Consul(host=host, port=port) |
| 35 | |
| 36 | |
| 37 | def verify_all_services_healthy(consul_endpoint, service_name=None, |
| 38 | number_of_expected_services=None): |
| 39 | """ |
| 40 | Verify in consul if any service is healthy |
| 41 | :param consul_endpoint: a <host>:<port> string |
| 42 | :param service_name: name of service to check, optional |
| 43 | :param number_of_expected_services number of services to check for, optional |
| 44 | :return: true if healthy, false otherwise |
| 45 | """ |
| 46 | |
| 47 | def check_health(service): |
| 48 | _, serv_health = consul.health.service(service, passing=True) |
| 49 | return not serv_health == [] |
| 50 | |
| 51 | consul = connect_to_consult(consul_endpoint) |
| 52 | |
| 53 | if service_name is not None: |
| 54 | return check_health(service_name) |
| 55 | |
| 56 | services = get_all_services(consul_endpoint) |
| 57 | |
| 58 | items = services.keys() |
| 59 | |
| 60 | if number_of_expected_services is not None and \ |
| 61 | len(items) != number_of_expected_services: |
| 62 | return False |
| 63 | |
| 64 | for item in items: |
| 65 | if not check_health(item): |
| 66 | return False |
| 67 | |
| 68 | return True |
| 69 | |
| 70 | |
| 71 | def get_all_services(consul_endpoint): |
| 72 | |
| 73 | log.debug('getting-service-verify-health') |
| 74 | |
| 75 | consul = connect_to_consult(consul_endpoint) |
| 76 | _, services = consul.catalog.services() |
| 77 | |
| 78 | return services |
| 79 | |
| 80 | |
| 81 | def get_endpoint_from_consul(consul_endpoint, service_name): |
| 82 | """Look up, from consul, the service name specified by service-name |
| 83 | """ |
| 84 | log.debug('getting-service-info', service=service_name) |
| 85 | |
| 86 | consul = connect_to_consult(consul_endpoint) |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 87 | _, services = consul.catalog.service(service_name) |
| 88 | |
| 89 | if len(services) == 0: |
Khen Nursimulu | 441dedd | 2016-10-05 14:44:26 -0700 | [diff] [blame] | 90 | raise Exception( |
| 91 | 'Cannot find service {} in consul'.format(service_name)) |
alshabib | b634d90 | 2017-01-16 13:10:17 -0600 | [diff] [blame] | 92 | os.exit(1) |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 93 | |
alshabib | 37494a7 | 2017-01-26 11:59:52 -0800 | [diff] [blame] | 94 | # pick local addresses when resolving a service via consul |
| 95 | # see CORD-818 (https://jira.opencord.org/browse/CORD-818) |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 96 | |
| 97 | service = services[randint(0, len(services) - 1)] |
| 98 | endpoint = '{}:{}'.format(service['ServiceAddress'], |
| 99 | service['ServicePort']) |
| 100 | |
khen | b95fe9a | 2016-10-05 11:15:25 -0700 | [diff] [blame] | 101 | return endpoint |
| 102 | |
| 103 | |
| 104 | if __name__ == '__main__': |
Khen Nursimulu | 9b9f1ad | 2017-01-10 15:43:32 -0500 | [diff] [blame] | 105 | print get_endpoint_from_consul('10.100.198.220:8500', 'kafka') |