Merge pull request #141 from bcoca/haproxy_cleanup

minor cleanup
This commit is contained in:
Brian Coca 2014-12-09 12:35:25 -05:00
commit b8071a8d5e

View file

@ -21,6 +21,7 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: haproxy module: haproxy
version_added: "1.9"
short_description: An Ansible module to handle states enable/disable server and set weight to backend host in haproxy using socket commands. short_description: An Ansible module to handle states enable/disable server and set weight to backend host in haproxy using socket commands.
description: description:
- The Enable Haproxy Backend Server, with - The Enable Haproxy Backend Server, with
@ -91,14 +92,10 @@ examples:
- haproxy: state=enabled host={{ inventory_hostname }} socket=/var/run/haproxy.sock weight=10 backend=www - haproxy: state=enabled host={{ inventory_hostname }} socket=/var/run/haproxy.sock weight=10 backend=www
author: Ravi Bhure <ravibhure@gmail.com> author: Ravi Bhure <ravibhure@gmail.com>
version_added: "1.9"
''' '''
import logging
import socket import socket
import re
logger = logging.getLogger(__name__)
DEFAULT_SOCKET_LOCATION="/var/run/haproxy.sock" DEFAULT_SOCKET_LOCATION="/var/run/haproxy.sock"
RECV_SIZE = 1024 RECV_SIZE = 1024
@ -120,14 +117,15 @@ class HAProxy(object):
http://haproxy.1wt.eu/download/1.5/doc/configuration.txt#Unix Socket commands http://haproxy.1wt.eu/download/1.5/doc/configuration.txt#Unix Socket commands
""" """
def __init__(self, module, **kwargs): def __init__(self, module):
self.module = module self.module = module
self.state = kwargs['state']
self.host = kwargs['host'] self.state = self.module.params['state']
self.backend = kwargs['backend'] self.host = self.module.params['host']
self.weight = kwargs['weight'] self.backend = self.module.params['backend']
self.socket = kwargs['socket'] self.weight = self.module.params['weight']
self.shutdown_sessions = kwargs['shutdown_sessions'] self.socket = self.module.params['socket']
self.shutdown_sessions = self.module.params['shutdown_sessions']
self.command_results = [] self.command_results = []
@ -137,8 +135,6 @@ class HAProxy(object):
UNIX socket and waiting up to 'timeout' milliseconds for the response. UNIX socket and waiting up to 'timeout' milliseconds for the response.
""" """
buffer = ""
self.client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.client.connect(self.socket) self.client.connect(self.socket)
self.client.sendall('%s\n' % cmd) self.client.sendall('%s\n' % cmd)
@ -214,8 +210,7 @@ class HAProxy(object):
def act(self): def act(self):
""" """
Figure out what you want to do from ansible, and then do the Figure out what you want to do from ansible, and then do it.
needful (at the earliest).
""" """
# toggle enable/disbale server # toggle enable/disbale server
@ -226,8 +221,7 @@ class HAProxy(object):
self.disabled(self.host, self.backend, self.shutdown_sessions) self.disabled(self.host, self.backend, self.shutdown_sessions)
else: else:
self.module.fail_json(msg="unknown state specified: '%s'" % \ self.module.fail_json(msg="unknown state specified: '%s'" % self.state)
self.state)
self.module.exit_json(stdout=self.command_results, changed=True) self.module.exit_json(stdout=self.command_results, changed=True)
@ -245,32 +239,12 @@ def main():
), ),
) )
state = module.params['state']
host = module.params['host']
backend = module.params['backend']
weight = module.params['weight']
socket = module.params['socket']
shutdown_sessions = module.params['shutdown_sessions']
##################################################################
# Required args per state:
# (enabled/disabled) = (host)
#
# AnsibleModule will verify most stuff, we need to verify
# 'socket' manually.
##################################################################
##################################################################
if not socket: if not socket:
module.fail_json(msg="unable to locate haproxy socket") module.fail_json(msg="unable to locate haproxy socket")
##################################################################
required_one_of=[['state', 'host']]
ansible_haproxy = HAProxy(module, **module.params) ansible_haproxy = HAProxy(module, **module.params)
ansible_haproxy.act() ansible_haproxy.act()
##################################################################
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *