|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# Copyright 2021 Michal Arbet |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import yaml |
| 20 | + |
| 21 | +# Default paths |
| 22 | +PROXYSQL_CONFIG_DIR = "/etc/proxysql" |
| 23 | +PROXYSQL_CONFIG = "/etc/proxysql.cnf" |
| 24 | + |
| 25 | +# Logging |
| 26 | +log_format = '%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s' |
| 27 | +logging.basicConfig(format=log_format, |
| 28 | + datefmt='%H:%M:%S', |
| 29 | + level=logging.DEBUG) |
| 30 | +LOG = logging.getLogger("proxysql_config_sync") |
| 31 | + |
| 32 | + |
| 33 | +class ProxySQLConfig: |
| 34 | + |
| 35 | + def __init__(self, conf_dir, conf_file): |
| 36 | + self.configs = dict() |
| 37 | + self.config = dict() |
| 38 | + self.configs['global'] = "{}/proxysql.yaml".format(conf_dir) |
| 39 | + self.configs['users'] = "{}/users".format(conf_dir) |
| 40 | + self.configs['rules'] = "{}/rules".format(conf_dir) |
| 41 | + self.conf_file = conf_file |
| 42 | + self._load_config() |
| 43 | + |
| 44 | + def _load_config(self): |
| 45 | + users = dict() |
| 46 | + rules = dict() |
| 47 | + |
| 48 | + for cfg, path in self.configs.items(): |
| 49 | + if not os.path.isdir(path): |
| 50 | + with open(path) as config_file: |
| 51 | + self.config.update( |
| 52 | + yaml.safe_load(config_file)) |
| 53 | + else: |
| 54 | + users['mysql_users'] = list() |
| 55 | + rules['mysql_query_rules'] = list() |
| 56 | + user_paths = [os.path.join(self.configs['users'], f) |
| 57 | + for f in os.listdir(self.configs['users'])] |
| 58 | + rule_paths = [os.path.join(self.configs['rules'], f) |
| 59 | + for f in os.listdir(self.configs['rules'])] |
| 60 | + |
| 61 | + for user_conf in user_paths: |
| 62 | + with open(user_conf) as config_file: |
| 63 | + tmp_users = yaml.safe_load(config_file) |
| 64 | + for i in tmp_users['mysql_users']: |
| 65 | + users['mysql_users'].append(i) |
| 66 | + self.config.update(users) |
| 67 | + for rule_conf in rule_paths: |
| 68 | + with open(rule_conf) as config_file: |
| 69 | + tmp_rules = yaml.safe_load(config_file) |
| 70 | + for i in tmp_rules['mysql_query_rules']: |
| 71 | + rules['mysql_query_rules'].append(i) |
| 72 | + self.config.update(rules) |
| 73 | + self._sanity() |
| 74 | + |
| 75 | + def _sanity(self): |
| 76 | + self._users_sanity() |
| 77 | + self._rules_sanity() |
| 78 | + |
| 79 | + def _users_sanity(self): |
| 80 | + users_added = list() |
| 81 | + users = list() |
| 82 | + for user in self.config['mysql_users']: |
| 83 | + if user['username'] not in users_added: |
| 84 | + users_added.append(user['username']) |
| 85 | + users.append(user) |
| 86 | + else: |
| 87 | + LOG.warning("User {} already exist, ignoring." |
| 88 | + .format(user['username'])) |
| 89 | + self.config['mysql_users'] = users |
| 90 | + |
| 91 | + def _rules_sanity(self): |
| 92 | + rules_added = list() |
| 93 | + rules = list() |
| 94 | + rule_id = 1 |
| 95 | + for rule in self.config['mysql_query_rules']: |
| 96 | + if rule['schemaname'] not in rules_added: |
| 97 | + rules_added.append(rule['schemaname']) |
| 98 | + rule['rule_id'] = rule_id |
| 99 | + rules.append(rule) |
| 100 | + rule_id += 1 |
| 101 | + else: |
| 102 | + LOG.warning("Rule witch schemaname {} already exist, ignoring." |
| 103 | + .format(rule['schemaname'])) |
| 104 | + self.config['mysql_query_rules'] = rules |
| 105 | + |
| 106 | + def _write_dict(self, key, value): |
| 107 | + if not isinstance(value, list): |
| 108 | + value = [value] |
| 109 | + with open(self.conf_file, "a+") as f: |
| 110 | + if key: |
| 111 | + f.write("{} =\n".format(key)) |
| 112 | + for i in range(len(value)): |
| 113 | + f.write(" {\n") |
| 114 | + for k, v in value[i].items(): |
| 115 | + if isinstance(v, str): |
| 116 | + v = '"{}"'.format(v) |
| 117 | + f.write(" {} = {}\n".format(k, v)) |
| 118 | + if i == len(value)-1: |
| 119 | + f.write(" }\n") |
| 120 | + else: |
| 121 | + f.write(" },\n") |
| 122 | + |
| 123 | + def _write_list(self, key, values): |
| 124 | + with open(self.conf_file, "a+") as f: |
| 125 | + f.write("{} =\n".format(key)) |
| 126 | + f.write("(\n") |
| 127 | + self._write_dict(key=None, value=values) |
| 128 | + with open(self.conf_file, "a+") as f: |
| 129 | + f.write(")\n") |
| 130 | + |
| 131 | + def _write(self, key, value): |
| 132 | + with open(self.conf_file, "a+") as f: |
| 133 | + if isinstance(value, str): |
| 134 | + value = '"{}"'.format(value) |
| 135 | + f.write("{} = {}\n".format(key, value)) |
| 136 | + |
| 137 | + def write_config(self): |
| 138 | + LOG.info("Writing config to {}".format(self.conf_file)) |
| 139 | + if os.path.exists(self.conf_file): |
| 140 | + os.remove(self.conf_file) |
| 141 | + for k, v in self.config.items(): |
| 142 | + if isinstance(v, dict): |
| 143 | + self._write_dict(k, v) |
| 144 | + elif isinstance(v, list): |
| 145 | + self._write_list(k, v) |
| 146 | + else: |
| 147 | + self._write(k, v) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + config = ProxySQLConfig(PROXYSQL_CONFIG_DIR, PROXYSQL_CONFIG) |
| 152 | + config.write_config() |
0 commit comments