From 81df0ded66a1891f86ee3e77b32b705275f8a287 Mon Sep 17 00:00:00 2001 From: iprunache Date: Wed, 24 Feb 2016 16:21:12 +0200 Subject: [PATCH] T87 Fetch proxysql configs using admin interface given table name Use the admin iface to read config to minimize code changes to proxy. The config will be passed as a JSON of rows to Consul. --- integrations/consul/proxysql-consul.py | 44 +++++++++++++++++++------- lib/ProxySQL_Admin.cpp | 2 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/integrations/consul/proxysql-consul.py b/integrations/consul/proxysql-consul.py index 16d316a3a..47949cd7c 100755 --- a/integrations/consul/proxysql-consul.py +++ b/integrations/consul/proxysql-consul.py @@ -6,6 +6,7 @@ import os import requests import sys +import MySQLdb USAGE = """ proxysql-consul put config config_type @@ -14,10 +15,15 @@ proxysql-consul put config config_type CFG_PATH = '/etc/proxysql-consul.cfg' CFG_CONSUL_IFACE = 'consul_iface' CFG_CONSUL_PORT = 'consul_port' +CFG_PROXY_IFACE = 'proxysql_admin_iface' +CFG_PROXY_PORT = 'proxysql_admin_port' +CFG_PROXY_USERNAME = 'proxysql_admin_username' +CFG_PROXY_PASSWORD = 'proxysql_admin_password' # Proxysql config types to Consul key mapping -TYPE_TO_KEY = { - 'mysql_servers': 'proxysql/mysql_serverss' +TABLE_TO_KEY = { + 'mysql_servers': 'proxysql/mysql_servers', + 'global_variables': 'proxysql/global_variables' } config = {} @@ -32,17 +38,34 @@ def read_config(): # TODO validate config -def write_data(proxysql_cfg, proxysql_cfg_type): - if proxysql_cfg_type not in TYPE_TO_KEY: - print 'Unknown config type. Exiting.' +def fetch_proxysql_config(table): + admin_connection = MySQLdb.connect(config[CFG_PROXY_IFACE], + config[CFG_PROXY_USERNAME], + config[CFG_PROXY_PASSWORD], + port=config[CFG_PROXY_PORT], + db='main') + cursor = admin_connection.cursor() + cursor.execute('SELECT * FROM %s' % table) + rows = cursor.fetchall() + cursor.close() + admin_connection.close() + return rows + + +def put_config(table): + if table not in TABLE_TO_KEY: + print 'Unknown config table. Exiting.' exit(1) - key = TYPE_TO_KEY[proxysql_cfg_type] + rows = fetch_proxysql_config(table) + rows_json = json.dumps(rows) + + key = TABLE_TO_KEY[table] consul_iface = config[CFG_CONSUL_IFACE] consul_port = config[CFG_CONSUL_PORT] url = 'http://%s:%s/v1/kv/%s' % (consul_iface, consul_port, key) - r = requests.put(url, data=proxysql_cfg) + r = requests.put(url, data=rows_json) print r.status_code @@ -83,10 +106,9 @@ if __name__ == '__main__': if len(sys.argv) > 1: mode = sys.argv[1] - if mode == 'put' and len(sys.argv) > 3: - key = sys.argv[2] - value = sys.argv[3] - write_data(key, value) + if mode == 'put' and len(sys.argv) > 2: + table = sys.argv[2] + put_config(table) exit(0) elif mode == 'update': update_config() diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 376153e4c..a9edcac03 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -3295,7 +3295,7 @@ int ProxySQL_Admin::save_config_to_cluster(char *config, char *type) { if (pid == 0) { // child errno = 0; - int exec_status = execl("/usr/local/bin/proxysql-consul.py", "/usr/local/bin/proxysql-consul.py", "put", config, type, (char *) 0); + int exec_status = execl("/usr/local/bin/proxysql-consul.py", "/usr/local/bin/proxysql-consul.py", "put", "mysql_servers", (char *) 0); if (exec_status == -1) { proxy_error("Exec failed for config save script with errno: %d.\n", errno); }