-
-
Save byteshiva/8f22acbfbaf27497c3bbc97f4fd03a4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python | |
| # Sanity check that consul-template is writing the services in Consul to the | |
| # nginx config file. | |
| import requests | |
| HOST = '10.1.1.13' | |
| CONSUL_PORT = 8500 | |
| NGINX_CONFIG_PATH = '/etc/nginx/sites-enabled/consul_template.conf' | |
| def read_file_lines(path): | |
| with open(path) as f: | |
| return f.read().split('\n') | |
| def find_in_file_lines(lines, keyword): | |
| for line in lines: | |
| if keyword in line: | |
| return line | |
| return None | |
| def is_consular_service(tags): | |
| return any([tag.startswith('consular-') for tag in tags]) | |
| def get_consul_services(host, port): | |
| response = requests.get('http://%s:%s/v1/catalog/services' % (host, port)) | |
| services = response.json() | |
| return [name for name, tags in services.items() | |
| if is_consular_service(tags)] | |
| lines = read_file_lines(NGINX_CONFIG_PATH) | |
| print('Read nginx config file with %d lines' % len(lines)) | |
| print('Fetching Consul services...') | |
| consul = get_consul_services(HOST, CONSUL_PORT) | |
| print('Found %d Consul services started by Consular' % len(consul)) | |
| missing = 0 | |
| skipped = 0 | |
| for service in consul: | |
| line = find_in_file_lines(lines, service) | |
| if line is None: | |
| missing += 1 | |
| print('Service "%s" was not found in the nginx config' % service) | |
| elif line.startswith('# Skipped'): | |
| skipped += 1 | |
| print('Service "%s" was skipped in the nginx config' % service) | |
| print('%d services in Consul not found in the nginx config' % missing) | |
| print('%d services in Consul skipped in the nginx config' % skipped) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment