| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: proxysql_backend_servers |
| 12 |
author: "Ben Mildren (@bmildren)" |
| 13 |
short_description: Adds or removes mysql hosts from proxysql admin interface |
| 14 |
description: |
| 15 |
- The M(community.proxysql.proxysql_backend_servers) module adds or removes mysql hosts using |
| 16 |
the proxysql admin interface. |
| 17 |
options: |
| 18 |
hostgroup_id: |
| 19 |
description: |
| 20 |
- The hostgroup in which this mysqld instance is included. An instance |
| 21 |
can be part of one or more hostgroups. |
| 22 |
type: int |
| 23 |
default: 0 |
| 24 |
hostname: |
| 25 |
description: |
| 26 |
- The ip address at which the mysqld instance can be contacted. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
port: |
| 30 |
description: |
| 31 |
- The port at which the mysqld instance can be contacted. |
| 32 |
type: int |
| 33 |
default: 3306 |
| 34 |
status: |
| 35 |
description: |
| 36 |
- ONLINE - Backend server is fully operational. |
| 37 |
OFFLINE_SOFT - When a server is put into C(OFFLINE_SOFT) mode, |
| 38 |
connections are kept in use until the current |
| 39 |
transaction is completed. This allows to gracefully |
| 40 |
detach a backend. |
| 41 |
OFFLINE_HARD - When a server is put into C(OFFLINE_HARD) mode, the |
| 42 |
existing connections are dropped, while new incoming |
| 43 |
connections aren't accepted either. |
| 44 |
|
| 45 |
If omitted the proxysql database default for I(status) is C(ONLINE). |
| 46 |
type: str |
| 47 |
choices: [ "ONLINE", "OFFLINE_SOFT", "OFFLINE_HARD"] |
| 48 |
weight: |
| 49 |
description: |
| 50 |
- The bigger the weight of a server relative to other weights, the higher |
| 51 |
the probability of the server being chosen from the hostgroup. If |
| 52 |
omitted the proxysql database default for I(weight) is 1. |
| 53 |
type: int |
| 54 |
compression: |
| 55 |
description: |
| 56 |
- If the value of I(compression) is greater than 0, new connections to |
| 57 |
that server will use compression. If omitted the proxysql database |
| 58 |
default for I(compression) is 0. |
| 59 |
type: int |
| 60 |
max_connections: |
| 61 |
description: |
| 62 |
- The maximum number of connections ProxySQL will open to this backend |
| 63 |
server. If omitted the proxysql database default for I(max_connections) |
| 64 |
is 1000. |
| 65 |
type: int |
| 66 |
max_replication_lag: |
| 67 |
description: |
| 68 |
- If greater than 0, ProxySQL will regularly monitor replication lag. If |
| 69 |
replication lag goes above I(max_replication_lag), proxysql will |
| 70 |
temporarily shun the server until replication catches up. If omitted |
| 71 |
the proxysql database default for I(max_replication_lag) is 0. |
| 72 |
type: int |
| 73 |
use_ssl: |
| 74 |
description: |
| 75 |
- If I(use_ssl) is set to C(True), connections to this server will be |
| 76 |
made using SSL connections. If omitted the proxysql database default |
| 77 |
for I(use_ssl) is C(False). |
| 78 |
type: bool |
| 79 |
max_latency_ms: |
| 80 |
description: |
| 81 |
- Ping time is monitored regularly. If a host has a ping time greater |
| 82 |
than I(max_latency_ms) it is excluded from the connection pool |
| 83 |
(although the server stays ONLINE). If omitted the proxysql database |
| 84 |
default for I(max_latency_ms) is 0. |
| 85 |
type: int |
| 86 |
comment: |
| 87 |
description: |
| 88 |
- Text field that can be used for any purposed defined by the user. |
| 89 |
Could be a description of what the host stores, a reminder of when the |
| 90 |
host was added or disabled, or a JSON processed by some checker script. |
| 91 |
type: str |
| 92 |
default: '' |
| 93 |
state: |
| 94 |
description: |
| 95 |
- When C(present) - adds the host, when C(absent) - removes the host. |
| 96 |
type: str |
| 97 |
choices: [ "present", "absent" ] |
| 98 |
default: present |
| 99 |
extends_documentation_fragment: |
| 100 |
- community.proxysql.proxysql.managing_config |
| 101 |
- community.proxysql.proxysql.connectivity |
| 102 |
notes: |
| 103 |
- Supports C(check_mode). |
| 104 |
''' |
| 105 |
|
| 106 |
EXAMPLES = ''' |
| 107 |
--- |
| 108 |
# This example adds a server, it saves the mysql server config to disk, but |
| 109 |
# avoids loading the mysql server config to runtime (this might be because |
| 110 |
# several servers are being added and the user wants to push the config to |
| 111 |
# runtime in a single batch using the community.general.proxysql_manage_config |
| 112 |
# module). It uses supplied credentials to connect to the proxysql admin |
| 113 |
# interface. |
| 114 |
|
| 115 |
- name: Add a server |
| 116 |
community.proxysql.proxysql_backend_servers: |
| 117 |
login_user: 'admin' |
| 118 |
login_password: 'admin' |
| 119 |
hostname: 'mysql01' |
| 120 |
state: present |
| 121 |
load_to_runtime: false |
| 122 |
|
| 123 |
# This example removes a server, saves the mysql server config to disk, and |
| 124 |
# dynamically loads the mysql server config to runtime. It uses credentials |
| 125 |
# in a supplied config file to connect to the proxysql admin interface. |
| 126 |
|
| 127 |
- name: Remove a server |
| 128 |
community.proxysql.proxysql_backend_servers: |
| 129 |
config_file: '~/proxysql.cnf' |
| 130 |
hostname: 'mysql02' |
| 131 |
state: absent |
| 132 |
''' |
| 133 |
|
| 134 |
RETURN = ''' |
| 135 |
stdout: |
| 136 |
description: The mysql host modified or removed from proxysql |
| 137 |
returned: On create/update will return the newly modified host, on delete |
| 138 |
it will return the deleted record. |
| 139 |
type: dict |
| 140 |
"sample": { |
| 141 |
"changed": true, |
| 142 |
"hostname": "192.168.52.1", |
| 143 |
"msg": "Added server to mysql_hosts", |
| 144 |
"server": { |
| 145 |
"comment": "", |
| 146 |
"compression": "0", |
| 147 |
"hostgroup_id": "1", |
| 148 |
"hostname": "192.168.52.1", |
| 149 |
"max_connections": "1000", |
| 150 |
"max_latency_ms": "0", |
| 151 |
"max_replication_lag": "0", |
| 152 |
"port": "3306", |
| 153 |
"status": "ONLINE", |
| 154 |
"use_ssl": "0", |
| 155 |
"weight": "1" |
| 156 |
}, |
| 157 |
"state": "present" |
| 158 |
} |
| 159 |
''' |
| 160 |
|
| 161 |
from ansible.module_utils.basic import AnsibleModule |
| 162 |
from ansible_collections.community.proxysql.plugins.module_utils.mysql import ( |
| 163 |
mysql_connect, |
| 164 |
mysql_driver, |
| 165 |
proxysql_common_argument_spec, |
| 166 |
save_config_to_disk, |
| 167 |
load_config_to_runtime, |
| 168 |
) |
| 169 |
from ansible.module_utils._text import to_native |
| 170 |
|
| 171 |
# =========================================== |
| 172 |
# proxysql module specific support methods. |
| 173 |
# |
| 174 |
|
| 175 |
|
| 176 |
def perform_checks(module): |
| 177 |
if module.params["port"] < 0 \ |
| 178 |
or module.params["port"] > 65535: |
| 179 |
module.fail_json( |
| 180 |
msg="port must be a valid unix port number (0-65535)" |
| 181 |
) |
| 182 |
|
| 183 |
if module.params["compression"]: |
| 184 |
if module.params["compression"] < 0 \ |
| 185 |
or module.params["compression"] > 102400: |
| 186 |
module.fail_json( |
| 187 |
msg="compression must be set between 0 and 102400" |
| 188 |
) |
| 189 |
|
| 190 |
if module.params["max_replication_lag"]: |
| 191 |
if module.params["max_replication_lag"] < 0 \ |
| 192 |
or module.params["max_replication_lag"] > 126144000: |
| 193 |
module.fail_json( |
| 194 |
msg="max_replication_lag must be set between 0 and 102400" |
| 195 |
) |
| 196 |
|
| 197 |
|
| 198 |
class ProxySQLServer(object): |
| 199 |
|
| 200 |
def __init__(self, module): |
| 201 |
self.state = module.params["state"] |
| 202 |
self.save_to_disk = module.params["save_to_disk"] |
| 203 |
self.load_to_runtime = module.params["load_to_runtime"] |
| 204 |
|
| 205 |
self.hostgroup_id = module.params["hostgroup_id"] |
| 206 |
self.hostname = module.params["hostname"] |
| 207 |
self.port = module.params["port"] |
| 208 |
|
| 209 |
config_data_keys = ["status", |
| 210 |
"weight", |
| 211 |
"compression", |
| 212 |
"max_connections", |
| 213 |
"max_replication_lag", |
| 214 |
"use_ssl", |
| 215 |
"max_latency_ms", |
| 216 |
"comment"] |
| 217 |
|
| 218 |
self.config_data = dict((k, module.params[k]) |
| 219 |
for k in config_data_keys) |
| 220 |
|
| 221 |
def check_server_config_exists(self, cursor): |
| 222 |
query_string = \ |
| 223 |
"""SELECT count(*) AS `host_count` |
| 224 |
FROM mysql_servers |
| 225 |
WHERE hostgroup_id = %s |
| 226 |
AND hostname = %s |
| 227 |
AND port = %s""" |
| 228 |
|
| 229 |
query_data = \ |
| 230 |
[self.hostgroup_id, |
| 231 |
self.hostname, |
| 232 |
self.port] |
| 233 |
|
| 234 |
cursor.execute(query_string, query_data) |
| 235 |
check_count = cursor.fetchone() |
| 236 |
return (int(check_count['host_count']) > 0) |
| 237 |
|
| 238 |
def check_server_config(self, cursor): |
| 239 |
query_string = \ |
| 240 |
"""SELECT count(*) AS `host_count` |
| 241 |
FROM mysql_servers |
| 242 |
WHERE hostgroup_id = %s |
| 243 |
AND hostname = %s |
| 244 |
AND port = %s""" |
| 245 |
|
| 246 |
query_data = \ |
| 247 |
[self.hostgroup_id, |
| 248 |
self.hostname, |
| 249 |
self.port] |
| 250 |
|
| 251 |
for col, val in self.config_data.items(): |
| 252 |
if val is not None: |
| 253 |
query_data.append(val) |
| 254 |
query_string += "\n AND " + col + " = %s" |
| 255 |
|
| 256 |
cursor.execute(query_string, query_data) |
| 257 |
check_count = cursor.fetchone() |
| 258 |
|
| 259 |
if isinstance(check_count, tuple): |
| 260 |
return int(check_count[0]) > 0 |
| 261 |
|
| 262 |
return (int(check_count['host_count']) > 0) |
| 263 |
|
| 264 |
def get_server_config(self, cursor): |
| 265 |
query_string = \ |
| 266 |
"""SELECT * |
| 267 |
FROM mysql_servers |
| 268 |
WHERE hostgroup_id = %s |
| 269 |
AND hostname = %s |
| 270 |
AND port = %s""" |
| 271 |
|
| 272 |
query_data = \ |
| 273 |
[self.hostgroup_id, |
| 274 |
self.hostname, |
| 275 |
self.port] |
| 276 |
|
| 277 |
cursor.execute(query_string, query_data) |
| 278 |
server = cursor.fetchone() |
| 279 |
return server |
| 280 |
|
| 281 |
def create_server_config(self, cursor): |
| 282 |
query_string = \ |
| 283 |
"""INSERT INTO mysql_servers ( |
| 284 |
hostgroup_id, |
| 285 |
hostname, |
| 286 |
port""" |
| 287 |
|
| 288 |
cols = 3 |
| 289 |
query_data = \ |
| 290 |
[self.hostgroup_id, |
| 291 |
self.hostname, |
| 292 |
self.port] |
| 293 |
|
| 294 |
for col, val in self.config_data.items(): |
| 295 |
if val is not None: |
| 296 |
cols += 1 |
| 297 |
query_data.append(val) |
| 298 |
query_string += ",\n" + col |
| 299 |
|
| 300 |
query_string += \ |
| 301 |
(")\n" + |
| 302 |
"VALUES (" + |
| 303 |
"%s ," * cols) |
| 304 |
|
| 305 |
query_string = query_string[:-2] |
| 306 |
query_string += ")" |
| 307 |
|
| 308 |
cursor.execute(query_string, query_data) |
| 309 |
return True |
| 310 |
|
| 311 |
def update_server_config(self, cursor): |
| 312 |
query_string = """UPDATE mysql_servers""" |
| 313 |
|
| 314 |
cols = 0 |
| 315 |
query_data = [] |
| 316 |
|
| 317 |
for col, val in self.config_data.items(): |
| 318 |
if val is not None: |
| 319 |
cols += 1 |
| 320 |
query_data.append(val) |
| 321 |
if cols == 1: |
| 322 |
query_string += "\nSET " + col + "= %s," |
| 323 |
else: |
| 324 |
query_string += "\n " + col + " = %s," |
| 325 |
|
| 326 |
query_string = query_string[:-1] |
| 327 |
query_string += ("\nWHERE hostgroup_id = %s\n AND hostname = %s" + |
| 328 |
"\n AND port = %s") |
| 329 |
|
| 330 |
query_data.append(self.hostgroup_id) |
| 331 |
query_data.append(self.hostname) |
| 332 |
query_data.append(self.port) |
| 333 |
|
| 334 |
cursor.execute(query_string, query_data) |
| 335 |
return True |
| 336 |
|
| 337 |
def delete_server_config(self, cursor): |
| 338 |
query_string = \ |
| 339 |
"""DELETE FROM mysql_servers |
| 340 |
WHERE hostgroup_id = %s |
| 341 |
AND hostname = %s |
| 342 |
AND port = %s""" |
| 343 |
|
| 344 |
query_data = \ |
| 345 |
[self.hostgroup_id, |
| 346 |
self.hostname, |
| 347 |
self.port] |
| 348 |
|
| 349 |
cursor.execute(query_string, query_data) |
| 350 |
return True |
| 351 |
|
| 352 |
def manage_config(self, cursor, state): |
| 353 |
if state: |
| 354 |
if self.save_to_disk: |
| 355 |
save_config_to_disk(cursor, "SERVERS") |
| 356 |
if self.load_to_runtime: |
| 357 |
load_config_to_runtime(cursor, "SERVERS") |
| 358 |
|
| 359 |
def create_server(self, check_mode, result, cursor): |
| 360 |
if not check_mode: |
| 361 |
result['changed'] = \ |
| 362 |
self.create_server_config(cursor) |
| 363 |
result['msg'] = "Added server to mysql_hosts" |
| 364 |
result['server'] = \ |
| 365 |
self.get_server_config(cursor) |
| 366 |
self.manage_config(cursor, |
| 367 |
result['changed']) |
| 368 |
else: |
| 369 |
result['changed'] = True |
| 370 |
result['msg'] = ("Server would have been added to" + |
| 371 |
" mysql_hosts, however check_mode" + |
| 372 |
" is enabled.") |
| 373 |
|
| 374 |
def update_server(self, check_mode, result, cursor): |
| 375 |
if not check_mode: |
| 376 |
result['changed'] = \ |
| 377 |
self.update_server_config(cursor) |
| 378 |
result['msg'] = "Updated server in mysql_hosts" |
| 379 |
result['server'] = \ |
| 380 |
self.get_server_config(cursor) |
| 381 |
self.manage_config(cursor, |
| 382 |
result['changed']) |
| 383 |
else: |
| 384 |
result['changed'] = True |
| 385 |
result['msg'] = ("Server would have been updated in" + |
| 386 |
" mysql_hosts, however check_mode" + |
| 387 |
" is enabled.") |
| 388 |
|
| 389 |
def delete_server(self, check_mode, result, cursor): |
| 390 |
if not check_mode: |
| 391 |
result['server'] = \ |
| 392 |
self.get_server_config(cursor) |
| 393 |
result['changed'] = \ |
| 394 |
self.delete_server_config(cursor) |
| 395 |
result['msg'] = "Deleted server from mysql_hosts" |
| 396 |
self.manage_config(cursor, |
| 397 |
result['changed']) |
| 398 |
else: |
| 399 |
result['changed'] = True |
| 400 |
result['msg'] = ("Server would have been deleted from" + |
| 401 |
" mysql_hosts, however check_mode is" + |
| 402 |
" enabled.") |
| 403 |
|
| 404 |
# =========================================== |
| 405 |
# Module execution. |
| 406 |
# |
| 407 |
|
| 408 |
|
| 409 |
def main(): |
| 410 |
argument_spec = proxysql_common_argument_spec() |
| 411 |
argument_spec.update( |
| 412 |
hostgroup_id=dict(default=0, type='int'), |
| 413 |
hostname=dict(required=True, type='str'), |
| 414 |
port=dict(default=3306, type='int'), |
| 415 |
status=dict(choices=['ONLINE', |
| 416 |
'OFFLINE_SOFT', |
| 417 |
'OFFLINE_HARD']), |
| 418 |
weight=dict(type='int'), |
| 419 |
compression=dict(type='int'), |
| 420 |
max_connections=dict(type='int'), |
| 421 |
max_replication_lag=dict(type='int'), |
| 422 |
use_ssl=dict(type='bool'), |
| 423 |
max_latency_ms=dict(type='int'), |
| 424 |
comment=dict(default='', type='str'), |
| 425 |
state=dict(default='present', choices=['present', |
| 426 |
'absent']), |
| 427 |
save_to_disk=dict(default=True, type='bool'), |
| 428 |
load_to_runtime=dict(default=True, type='bool') |
| 429 |
) |
| 430 |
|
| 431 |
module = AnsibleModule( |
| 432 |
supports_check_mode=True, |
| 433 |
argument_spec=argument_spec |
| 434 |
) |
| 435 |
|
| 436 |
perform_checks(module) |
| 437 |
|
| 438 |
login_user = module.params["login_user"] |
| 439 |
login_password = module.params["login_password"] |
| 440 |
config_file = module.params["config_file"] |
| 441 |
|
| 442 |
cursor = None |
| 443 |
try: |
| 444 |
cursor, db_conn, version = mysql_connect(module, |
| 445 |
login_user, |
| 446 |
login_password, |
| 447 |
config_file, |
| 448 |
cursor_class='DictCursor') |
| 449 |
except mysql_driver.Error as e: |
| 450 |
module.fail_json( |
| 451 |
msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e) |
| 452 |
) |
| 453 |
|
| 454 |
proxysql_server = ProxySQLServer(module) |
| 455 |
result = {} |
| 456 |
|
| 457 |
result['state'] = proxysql_server.state |
| 458 |
if proxysql_server.hostname: |
| 459 |
result['hostname'] = proxysql_server.hostname |
| 460 |
|
| 461 |
if proxysql_server.state == "present": |
| 462 |
try: |
| 463 |
if not proxysql_server.check_server_config(cursor): |
| 464 |
if not proxysql_server.check_server_config_exists(cursor): |
| 465 |
proxysql_server.create_server(module.check_mode, |
| 466 |
result, |
| 467 |
cursor) |
| 468 |
else: |
| 469 |
proxysql_server.update_server(module.check_mode, |
| 470 |
result, |
| 471 |
cursor) |
| 472 |
else: |
| 473 |
result['changed'] = False |
| 474 |
result['msg'] = ("The server already exists in mysql_hosts" + |
| 475 |
" and doesn't need to be updated.") |
| 476 |
result['server'] = \ |
| 477 |
proxysql_server.get_server_config(cursor) |
| 478 |
except mysql_driver.Error as e: |
| 479 |
module.fail_json( |
| 480 |
msg="unable to modify server.. %s" % to_native(e) |
| 481 |
) |
| 482 |
|
| 483 |
elif proxysql_server.state == "absent": |
| 484 |
try: |
| 485 |
if proxysql_server.check_server_config_exists(cursor): |
| 486 |
proxysql_server.delete_server(module.check_mode, |
| 487 |
result, |
| 488 |
cursor) |
| 489 |
else: |
| 490 |
result['changed'] = False |
| 491 |
result['msg'] = ("The server is already absent from the" + |
| 492 |
" mysql_hosts memory configuration") |
| 493 |
except mysql_driver.Error as e: |
| 494 |
module.fail_json( |
| 495 |
msg="unable to remove server.. %s" % to_native(e) |
| 496 |
) |
| 497 |
|
| 498 |
module.exit_json(**result) |
| 499 |
|
| 500 |
|
| 501 |
if __name__ == '__main__': |
| 502 |
main() |