2014-09-30 06:45:40 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Ravi Bhure <ravibhure@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: haproxy
|
2014-12-09 18:29:28 +01:00
|
|
|
version_added: "1.9"
|
2015-07-20 00:35:32 +02:00
|
|
|
short_description: Enable, disable, and set weights for HAProxy backend servers using socket commands.
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Enable, disable, and set weights for HAProxy backend servers using socket
|
|
|
|
commands.
|
2014-09-30 06:45:40 +02:00
|
|
|
notes:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Enable and disable commands are restricted and can only be issued on
|
|
|
|
sockets configured for level 'admin'. For example, you can add the line
|
|
|
|
'stats socket /var/run/haproxy.sock level admin' to the general section of
|
|
|
|
haproxy.cfg. See http://haproxy.1wt.eu/download/1.5/doc/configuration.txt.
|
2014-09-30 06:45:40 +02:00
|
|
|
options:
|
2015-07-20 00:35:32 +02:00
|
|
|
backend:
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Name of the HAProxy backend pool.
|
|
|
|
required: false
|
|
|
|
default: auto-detected
|
2014-09-30 06:45:40 +02:00
|
|
|
host:
|
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Name of the backend host to change.
|
2014-09-30 06:45:40 +02:00
|
|
|
required: true
|
|
|
|
default: null
|
2015-07-20 00:35:32 +02:00
|
|
|
shutdown_sessions:
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- When disabling a server, immediately terminate all the sessions attached
|
|
|
|
to the specified server. This can be used to terminate long-running
|
|
|
|
sessions after a server is put into maintenance mode.
|
2014-09-30 06:45:40 +02:00
|
|
|
required: false
|
2015-07-20 00:35:32 +02:00
|
|
|
default: false
|
|
|
|
socket:
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Path to the HAProxy socket file.
|
2014-09-30 06:45:40 +02:00
|
|
|
required: false
|
2015-07-20 00:35:32 +02:00
|
|
|
default: /var/run/haproxy.sock
|
|
|
|
state:
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Desired state of the provided backend host.
|
|
|
|
required: true
|
2014-09-30 06:45:40 +02:00
|
|
|
default: null
|
2015-07-20 00:35:32 +02:00
|
|
|
choices: [ "enabled", "disabled" ]
|
2016-05-31 18:26:25 +02:00
|
|
|
fail_on_not_found:
|
|
|
|
description:
|
|
|
|
- Fail whenever trying to enable/disable a backend host that does not exist
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "2.2"
|
2015-07-20 00:35:32 +02:00
|
|
|
wait:
|
2014-09-30 06:45:40 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Wait until the server reports a status of 'UP' when `state=enabled`, or
|
|
|
|
status of 'MAINT' when `state=disabled`.
|
2014-09-30 06:45:40 +02:00
|
|
|
required: false
|
|
|
|
default: false
|
2015-07-20 00:35:32 +02:00
|
|
|
version_added: "2.0"
|
|
|
|
wait_interval:
|
2015-06-17 17:29:38 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Number of seconds to wait between retries.
|
2015-06-17 17:29:38 +02:00
|
|
|
required: false
|
2015-07-20 00:35:32 +02:00
|
|
|
default: 5
|
2015-06-18 09:11:16 +02:00
|
|
|
version_added: "2.0"
|
2015-06-17 17:29:38 +02:00
|
|
|
wait_retries:
|
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- Number of times to check for status after changing the state.
|
2015-06-17 17:29:38 +02:00
|
|
|
required: false
|
2015-06-22 20:09:54 +02:00
|
|
|
default: 25
|
2015-06-18 09:11:16 +02:00
|
|
|
version_added: "2.0"
|
2015-07-20 00:35:32 +02:00
|
|
|
weight:
|
2015-06-17 17:29:38 +02:00
|
|
|
description:
|
2015-07-20 00:35:32 +02:00
|
|
|
- The value passed in argument. If the value ends with the `%` sign, then
|
|
|
|
the new weight will be relative to the initially configured weight.
|
|
|
|
Relative weights are only permitted between 0 and 100% and absolute
|
|
|
|
weights are permitted between 0 and 256.
|
2015-06-17 17:29:38 +02:00
|
|
|
required: false
|
2015-07-20 00:35:32 +02:00
|
|
|
default: null
|
2014-09-30 06:45:40 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-12-05 19:44:43 +01:00
|
|
|
# disable server in 'www' backend pool
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }} backend=www
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
# disable server without backend pool name (apply to all available backend pool)
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }}
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
# disable server, provide socket file
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }} socket=/var/run/haproxy.sock backend=www
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2015-06-17 17:29:38 +02:00
|
|
|
# disable server, provide socket file, wait until status reports in maintenance
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }} socket=/var/run/haproxy.sock backend=www wait=yes
|
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
# disable backend server in 'www' backend pool and drop open sessions to it
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }} backend=www socket=/var/run/haproxy.sock shutdown_sessions=true
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2016-05-31 18:26:25 +02:00
|
|
|
# disable server without backend pool name (apply to all available backend pool) but fail when the backend host is not found
|
|
|
|
- haproxy: state=disabled host={{ inventory_hostname }} fail_on_not_found=yes
|
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
# enable server in 'www' backend pool
|
|
|
|
- haproxy: state=enabled host={{ inventory_hostname }} backend=www
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2015-06-17 17:29:38 +02:00
|
|
|
# enable server in 'www' backend pool wait until healthy
|
|
|
|
- haproxy: state=enabled host={{ inventory_hostname }} backend=www wait=yes
|
|
|
|
|
|
|
|
# enable server in 'www' backend pool wait until healthy. Retry 10 times with intervals of 5 seconds to retrieve the health
|
|
|
|
- haproxy: state=enabled host={{ inventory_hostname }} backend=www wait=yes wait_retries=10 wait_interval=5
|
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
# enable server in 'www' backend pool with change server(s) weight
|
|
|
|
- haproxy: state=enabled host={{ inventory_hostname }} socket=/var/run/haproxy.sock weight=10 backend=www
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Ravi Bhure (@ravibhure)"
|
2014-09-30 06:45:40 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import socket
|
2015-06-17 17:29:38 +02:00
|
|
|
import csv
|
|
|
|
import time
|
2016-05-31 18:26:25 +02:00
|
|
|
from string import Template
|
2014-09-30 06:45:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_SOCKET_LOCATION="/var/run/haproxy.sock"
|
|
|
|
RECV_SIZE = 1024
|
2014-12-07 10:29:16 +01:00
|
|
|
ACTION_CHOICES = ['enabled', 'disabled']
|
2015-06-22 20:09:54 +02:00
|
|
|
WAIT_RETRIES=25
|
2015-06-22 20:13:12 +02:00
|
|
|
WAIT_INTERVAL=5
|
2014-09-30 06:45:40 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
class TimeoutException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class HAProxy(object):
|
|
|
|
"""
|
|
|
|
Used for communicating with HAProxy through its local UNIX socket interface.
|
|
|
|
Perform common tasks in Haproxy related to enable server and
|
|
|
|
disable server.
|
|
|
|
|
|
|
|
The complete set of external commands Haproxy handles is documented
|
|
|
|
on their website:
|
|
|
|
|
|
|
|
http://haproxy.1wt.eu/download/1.5/doc/configuration.txt#Unix Socket commands
|
|
|
|
"""
|
|
|
|
|
2014-12-09 15:06:04 +01:00
|
|
|
def __init__(self, module):
|
2014-09-30 06:45:40 +02:00
|
|
|
self.module = module
|
2014-12-09 15:06:04 +01:00
|
|
|
|
|
|
|
self.state = self.module.params['state']
|
|
|
|
self.host = self.module.params['host']
|
|
|
|
self.backend = self.module.params['backend']
|
|
|
|
self.weight = self.module.params['weight']
|
|
|
|
self.socket = self.module.params['socket']
|
|
|
|
self.shutdown_sessions = self.module.params['shutdown_sessions']
|
2016-05-31 18:26:25 +02:00
|
|
|
self.fail_on_not_found = self.module.params['fail_on_not_found']
|
2015-06-17 17:29:38 +02:00
|
|
|
self.wait = self.module.params['wait']
|
|
|
|
self.wait_retries = self.module.params['wait_retries']
|
|
|
|
self.wait_interval = self.module.params['wait_interval']
|
2016-05-31 18:26:25 +02:00
|
|
|
self.command_results = {}
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2015-06-17 17:29:38 +02:00
|
|
|
def execute(self, cmd, timeout=200, capture_output=True):
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
|
|
|
Executes a HAProxy command by sending a message to a HAProxy's local
|
|
|
|
UNIX socket and waiting up to 'timeout' milliseconds for the response.
|
|
|
|
"""
|
|
|
|
self.client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
self.client.connect(self.socket)
|
|
|
|
self.client.sendall('%s\n' % cmd)
|
|
|
|
result = ''
|
|
|
|
buf = ''
|
|
|
|
buf = self.client.recv(RECV_SIZE)
|
|
|
|
while buf:
|
2014-12-07 10:29:16 +01:00
|
|
|
result += buf
|
|
|
|
buf = self.client.recv(RECV_SIZE)
|
2015-06-17 17:29:38 +02:00
|
|
|
if capture_output:
|
2016-05-31 18:26:25 +02:00
|
|
|
self.capture_command_output(cmd, result.strip())
|
2014-09-30 06:45:40 +02:00
|
|
|
self.client.close()
|
|
|
|
return result
|
|
|
|
|
2016-05-31 18:26:25 +02:00
|
|
|
|
|
|
|
def capture_command_output(self, cmd, output):
|
|
|
|
"""
|
|
|
|
Capture the output for a command
|
|
|
|
"""
|
|
|
|
if not 'command' in self.command_results.keys():
|
|
|
|
self.command_results['command'] = []
|
|
|
|
self.command_results['command'].append(cmd)
|
|
|
|
if not 'output' in self.command_results.keys():
|
|
|
|
self.command_results['output'] = []
|
|
|
|
self.command_results['output'].append(output)
|
|
|
|
|
|
|
|
|
|
|
|
def discover_all_backends(self):
|
|
|
|
"""
|
|
|
|
Discover all entries with svname = 'BACKEND' and return a list of their corresponding
|
|
|
|
pxnames
|
|
|
|
"""
|
|
|
|
data = self.execute('show stat', 200, False).lstrip('# ')
|
|
|
|
r = csv.DictReader(data.splitlines())
|
|
|
|
return map(lambda d: d['pxname'], filter(lambda d: d['svname'] == 'BACKEND', r))
|
|
|
|
|
|
|
|
|
|
|
|
def execute_for_backends(self, cmd, pxname, svname, wait_for_status = None):
|
|
|
|
"""
|
|
|
|
Run some command on the specified backends. If no backends are provided they will
|
|
|
|
be discovered automatically (all backends)
|
|
|
|
"""
|
|
|
|
# Discover backends if none are given
|
|
|
|
if pxname is None:
|
|
|
|
backends = self.discover_all_backends()
|
|
|
|
else:
|
|
|
|
backends = [pxname]
|
|
|
|
|
|
|
|
# Run the command for each requested backend
|
|
|
|
for backend in backends:
|
|
|
|
# Fail when backends were not found
|
|
|
|
state = self.get_state_for(backend, svname)
|
|
|
|
if (self.fail_on_not_found or self.wait) and state is None:
|
|
|
|
self.module.fail_json(msg="The specified backend '%s/%s' was not found!" % (backend, svname))
|
|
|
|
|
|
|
|
self.execute(Template(cmd).substitute(pxname = backend, svname = svname))
|
|
|
|
if self.wait:
|
|
|
|
self.wait_until_status(backend, svname, wait_for_status)
|
|
|
|
|
|
|
|
|
|
|
|
def get_state_for(self, pxname, svname):
|
|
|
|
"""
|
|
|
|
Find the state of specific services. When pxname is not set, get all backends for a specific host.
|
|
|
|
Returns a list of dictionaries containing the status and weight for those services.
|
|
|
|
"""
|
|
|
|
data = self.execute('show stat', 200, False).lstrip('# ')
|
|
|
|
r = csv.DictReader(data.splitlines())
|
|
|
|
state = map(lambda d: { 'status': d['status'], 'weight': d['weight'] }, filter(lambda d: (pxname is None or d['pxname'] == pxname) and d['svname'] == svname, r))
|
|
|
|
return state or None
|
|
|
|
|
|
|
|
|
2015-06-17 17:29:38 +02:00
|
|
|
def wait_until_status(self, pxname, svname, status):
|
|
|
|
"""
|
2015-06-18 09:11:16 +02:00
|
|
|
Wait for a service to reach the specified status. Try RETRIES times
|
2015-06-17 17:29:38 +02:00
|
|
|
with INTERVAL seconds of sleep in between. If the service has not reached
|
|
|
|
the expected status in that time, the module will fail. If the service was
|
|
|
|
not found, the module will fail.
|
|
|
|
"""
|
|
|
|
for i in range(1, self.wait_retries):
|
2016-05-31 18:26:25 +02:00
|
|
|
state = self.get_state_for(pxname, svname)
|
|
|
|
|
|
|
|
# We can assume there will only be 1 element in state because both svname and pxname are always set when we get here
|
|
|
|
if state[0]['status'] == status:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
time.sleep(self.wait_interval)
|
2015-06-17 17:29:38 +02:00
|
|
|
|
|
|
|
self.module.fail_json(msg="server %s/%s not status '%s' after %d retries. Aborting." % (pxname, svname, status, self.wait_retries))
|
|
|
|
|
2015-12-02 02:30:26 +01:00
|
|
|
|
2014-12-05 19:44:43 +01:00
|
|
|
def enabled(self, host, backend, weight):
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
2014-12-07 10:29:16 +01:00
|
|
|
Enabled action, marks server to UP and checks are re-enabled,
|
|
|
|
also supports to get current weight for server (default) and
|
|
|
|
set the weight for haproxy backend server when provides.
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
2016-05-31 18:26:25 +02:00
|
|
|
cmd = "get weight $pxname/$svname; enable server $pxname/$svname"
|
|
|
|
if weight:
|
|
|
|
cmd += "; set weight $pxname/$svname %s" % weight
|
|
|
|
self.execute_for_backends(cmd, backend, host, 'UP')
|
2014-09-30 06:45:40 +02:00
|
|
|
|
|
|
|
|
2014-12-03 16:14:35 +01:00
|
|
|
def disabled(self, host, backend, shutdown_sessions):
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
2014-12-07 10:29:16 +01:00
|
|
|
Disabled action, marks server to DOWN for maintenance. In this mode, no more checks will be
|
|
|
|
performed on the server until it leaves maintenance,
|
|
|
|
also it shutdown sessions while disabling backend host server.
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
2016-05-31 18:26:25 +02:00
|
|
|
cmd = "get weight $pxname/$svname; disable server $pxname/$svname"
|
|
|
|
if shutdown_sessions:
|
|
|
|
cmd += "; shutdown sessions server $pxname/$svname"
|
|
|
|
self.execute_for_backends(cmd, backend, host, 'MAINT')
|
2014-09-30 06:45:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def act(self):
|
|
|
|
"""
|
2014-12-09 15:06:04 +01:00
|
|
|
Figure out what you want to do from ansible, and then do it.
|
2014-09-30 06:45:40 +02:00
|
|
|
"""
|
2016-05-31 18:26:25 +02:00
|
|
|
# Get the state before the run
|
|
|
|
state_before = self.get_state_for(self.backend, self.host)
|
|
|
|
self.command_results['state_before'] = state_before
|
2015-12-02 02:30:26 +01:00
|
|
|
|
2014-09-30 06:45:40 +02:00
|
|
|
# toggle enable/disbale server
|
2014-12-05 19:44:43 +01:00
|
|
|
if self.state == 'enabled':
|
|
|
|
self.enabled(self.host, self.backend, self.weight)
|
|
|
|
elif self.state == 'disabled':
|
2014-12-03 16:14:35 +01:00
|
|
|
self.disabled(self.host, self.backend, self.shutdown_sessions)
|
2014-09-30 06:45:40 +02:00
|
|
|
else:
|
2014-12-09 15:06:04 +01:00
|
|
|
self.module.fail_json(msg="unknown state specified: '%s'" % self.state)
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2016-05-31 18:26:25 +02:00
|
|
|
# Get the state after the run
|
|
|
|
state_after = self.get_state_for(self.backend, self.host)
|
|
|
|
self.command_results['state_after'] = state_after
|
2015-12-02 02:30:26 +01:00
|
|
|
|
2016-05-31 18:26:25 +02:00
|
|
|
# Report change status
|
|
|
|
if state_before != state_after:
|
|
|
|
self.command_results['changed'] = True
|
|
|
|
self.module.exit_json(**self.command_results)
|
2015-12-02 02:30:26 +01:00
|
|
|
else:
|
2016-05-31 18:26:25 +02:00
|
|
|
self.command_results['changed'] = False
|
|
|
|
self.module.exit_json(**self.command_results)
|
|
|
|
|
2014-09-30 06:45:40 +02:00
|
|
|
|
2014-12-07 10:29:16 +01:00
|
|
|
def main():
|
|
|
|
|
|
|
|
# load ansible module object
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
state = dict(required=True, default=None, choices=ACTION_CHOICES),
|
|
|
|
host=dict(required=True, default=None),
|
|
|
|
backend=dict(required=False, default=None),
|
|
|
|
weight=dict(required=False, default=None),
|
|
|
|
socket = dict(required=False, default=DEFAULT_SOCKET_LOCATION),
|
|
|
|
shutdown_sessions=dict(required=False, default=False),
|
2016-05-31 18:26:25 +02:00
|
|
|
fail_on_not_found=dict(required=False, default=False, type='bool'),
|
2015-06-22 20:09:54 +02:00
|
|
|
wait=dict(required=False, default=False, type='bool'),
|
|
|
|
wait_retries=dict(required=False, default=WAIT_RETRIES, type='int'),
|
|
|
|
wait_interval=dict(required=False, default=WAIT_INTERVAL, type='int'),
|
2014-12-07 10:29:16 +01:00
|
|
|
),
|
|
|
|
)
|
2014-12-09 15:06:04 +01:00
|
|
|
|
2014-12-07 10:29:16 +01:00
|
|
|
if not socket:
|
|
|
|
module.fail_json(msg="unable to locate haproxy socket")
|
|
|
|
|
2015-02-05 13:11:16 +01:00
|
|
|
ansible_haproxy = HAProxy(module)
|
2014-12-07 10:29:16 +01:00
|
|
|
ansible_haproxy.act()
|
|
|
|
|
2014-09-30 06:45:40 +02:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
|
|
|
main()
|
2016-05-31 18:26:25 +02:00
|
|
|
|