From 97e3fa41f10e6e47a3a9f28bcd1b2011f3867290 Mon Sep 17 00:00:00 2001 From: iprunache Date: Wed, 24 Feb 2016 15:05:37 +0200 Subject: [PATCH] T87 Made script to read input provided by Consul on watch trigger The script reads all input and the CONSUL_INDEX env var. It uses the latter to determine which of the values passed by consul was last modified and actually triggered the watch. --- integrations/consul/consul.cfg | 9 ++++++ integrations/consul/proxysql-consul.py | 41 +++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 integrations/consul/consul.cfg diff --git a/integrations/consul/consul.cfg b/integrations/consul/consul.cfg new file mode 100644 index 000000000..3cd5a1c86 --- /dev/null +++ b/integrations/consul/consul.cfg @@ -0,0 +1,9 @@ +{ + "watches": [ + { + "type": "keyprefix", + "prefix": "proxysql/", + "handler": "/home/ionut/workspace/proxysql/proxysql/integrations/consul/proxysql-consul.py update" + } + ] +} diff --git a/integrations/consul/proxysql-consul.py b/integrations/consul/proxysql-consul.py index 1406d7154..16d316a3a 100755 --- a/integrations/consul/proxysql-consul.py +++ b/integrations/consul/proxysql-consul.py @@ -1,6 +1,8 @@ #!/usr/bin/env python +import base64 import json +import os import requests import sys @@ -15,7 +17,7 @@ CFG_CONSUL_PORT = 'consul_port' # Proxysql config types to Consul key mapping TYPE_TO_KEY = { - 'mysql_servers': 'proxysql/mysql_servers' + 'mysql_servers': 'proxysql/mysql_serverss' } config = {} @@ -43,6 +45,39 @@ def write_data(proxysql_cfg, proxysql_cfg_type): r = requests.put(url, data=proxysql_cfg) print r.status_code + +def update_config(): + """ + Reads all input from stdin that is passed by Consul and extracts the config + that was modified. + + Consul is configured to watch all proxysql keys and returns all their values + when one key is modified. To determine the value that was actually modified + we use the CONSUL_INDEX env var written by Consul. This should match the + 'ModifyIndex' field of the value that triggered the watch. + + Values are returned as a JSON array. The actual value content is stored in + the 'Value' field, base64 encoded. + """ + if 'CONSUL_INDEX' not in os.environ: + print 'Missing consul index on update request' + exit(1) + consul_index = int(os.environ['CONSUL_INDEX']) + consul_data = sys.stdin.read() + + updated_value = None + values = json.loads(consul_data) + for value in values: + if 'ModifyIndex' in value and value['ModifyIndex'] == consul_index: + updated_value = value + break + if not updated_value: + print 'Failed to determine updated config from Consul data' + exit(1) + + proxysql_config = base64.b64decode(updated_value['Value']) + print proxysql_config + if __name__ == '__main__': read_config() @@ -53,5 +88,9 @@ if __name__ == '__main__': value = sys.argv[3] write_data(key, value) exit(0) + elif mode == 'update': + update_config() + exit(0) + print USAGE exit(1)