lighthouse.haproxy.balancer

class lighthouse.haproxy.balancer.HAProxy(*args, **kwargs)[source]

Bases: lighthouse.balancer.Balancer

The HAProxy balancer class.

Leverages the HAProxy control, config and stanza-related classes in order to keep the HAProxy config file in sync with the services and nodes discovered.

name = 'haproxy'
classmethod validate_dependencies()[source]

The HAProxy Balancer doesn’t use any specific python libraries so there are no extra dependencies to check for.

classmethod validate_config(config)[source]

Validates that a config file path and a control socket file path and pid file path are all present in the HAProxy config.

classmethod validate_proxies_config(proxies)[source]

Specific config validation method for the “proxies” portion of a config.

Checks that each proxy defines a port and a list of upstreams, and that each upstream entry has a host and port defined.

apply_config(config)[source]

Constructs HAProxyConfig and HAProxyControl instances based on the contents of the config.

This is mostly a matter of constructing the configuration stanzas.

sync_file(clusters)[source]

Generates new HAProxy config file content and writes it to the file at haproxy_config_path.

If a restart is not necessary the nodes configured in HAProxy will be synced on the fly. If a restart is necessary, one will be triggered.

restart()[source]

Tells the HAProxy control object to restart the process.

If it’s been fewer than restart_interval seconds since the previous restart, it will wait until the interval has passed. This staves off situations where the process is constantly restarting, as it is possible to drop packets for a short interval while doing so.

sync_nodes(clusters)[source]

Syncs the enabled/disabled status of nodes existing in HAProxy based on the given clusters.

This is used to inform HAProxy of up/down nodes without necessarily doing a restart of the process.

get_current_nodes(clusters)[source]

Returns two dictionaries, the current nodes and the enabled nodes.

The current_nodes dictionary is keyed off of the cluster name and values are a list of nodes known to HAProxy.

The enabled_nodes dictionary is also keyed off of the cluster name and values are list of enabled nodes, i.e. the same values as current_nodes but limited to servers currently taking traffic.