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.
pull/481/head
iprunache 10 years ago
parent 17495fccf0
commit 97e3fa41f1

@ -0,0 +1,9 @@
{
"watches": [
{
"type": "keyprefix",
"prefix": "proxysql/",
"handler": "/home/ionut/workspace/proxysql/proxysql/integrations/consul/proxysql-consul.py update"
}
]
}

@ -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)

Loading…
Cancel
Save