src.core.haproxyupdater package


src.core.haproxyupdater.confighandler module

class src.core.haproxyupdater.confighandler.ConfigHandler[source]

Bases: object

Class to handler haproxy config file updation

This class contains method for updating the haproxy config file with the provided formatted haproxy config template.

The template is first populated with the fetched backends using jinja templating engine and then the haproxy config file is updated with this formatted template.

Parameters:**kwargs (dictionary) – Dictionary containing params
static read_write_file(**kwargs)[source]

Method to read and write haproxy config file

Parameters:**kwargs (dictionary) – Dictionary containing params
Returns:successfully updated or not str : error string if any
Return type:bool
static update_config(**kwargs)[source]

Method for updating haproxy config

This is the method which actually updates the haproxy config file using the provided template file after properly formatting it

Parameters:**kwargs (dictionary) – Dictionary containing params
Returns:Successfully updated or not
Return type:bool

src.core.haproxyupdater.haproxyreloader module

class src.core.haproxyupdater.haproxyreloader.HaproxyReloader[source]

Bases: object

Class for handling haproxy reload

This class provides methods to reload haproxy, either via systemd or via the binary.

In order to reload via bianry, the socket file and the PID file should be present as params along with the binary location.

For systemd, the systemd service name should be provided as param.

Both reload via systemd and reload via binary are done by execting shell commands via subprocess library

static reload_haproxy(**kwargs)[source]

Method for reloading haproxy

Method for reloading haproxy. This takes the help of util method to reload haproxy either via systemd or binary.

Other classes and methods will call this method for updating haporoxy with the required param.

Parameters:**kwargs (dictionary) – Dictionary conatining params
Returns:Successfully reloaded or not
Return type:bool
static start_by_systemd(service_name, logger=None)[source]

Method for starting haproxy via systemd

Starts haproxy via systemd. Executes systemd start as a shell command.

Parameters:logger (object) – logger object for logging
Returns:Successfully started or not
Return type:bool

src.core.haproxyupdater.haproxyupdate module

class src.core.haproxyupdater.haproxyupdate.HaproxyUpdate(**kwargs)[source]

Bases: object

Class for handling haproxy update and reload

This class contains handlers which controls haproxy uptation and reload. Haproxy can be updated wither by updating its config file followed by a reload via systemd or via binary. The other way to reload haproxy is via the exposed socket. This type of update does not require any reload

For updating via runtime haproxy needs to maintain a pool if inactive backends. When a new live backend comes, we can pull an inactive live backend and make it live changing its ip to that of the live backend

Parameters:**kwargs (dictionary) – params in key/value dict format
logger = None

Valid methods to start haproxy.


init methods is not supported yet.


Updates haproxy config

This method updates haproxy config with the help of the util methods.

Returns:Wether haproxy was updated successfully or not.
Return type:bool

Method to update haproxy via config reload

This method will update haproxy via updating its config and subsequently reloading it. The actual update will be done by the confighandler module and reload will be done by haproxyreloader. Optinaly is upate_only is set to True then only config will be updated and reload will not be done.

Parameters:update_only (bool) – Whether only update is required or both update and reload is required.
Returns:Whether successfully updated/reloaded as the case may be
Return type:bool

Method to update active node list

This method will e called to update the list of active backends. Haproxy needs to be updated and optionally reloaded if this list changes

Parameters:node_list (list) – List containing IPs/Hostnames of active backends
valid_start_by = None

Valid methods to update haproxy

src.core.haproxyupdater.runtimeupdater module

class src.core.haproxyupdater.runtimeupdater.RuntimeUpdater[source]

Bases: object

Class for updating haproxy at runtime

This class conatins methods for updating haproxy backends at runtime without reloading it.

This is done by communicating with haproxy over the unix socket file expsed by it.

Once ep2 gets the ips/hostnames of the live backends, it communicates with haproxy over socket, extracts servers from inactive pool and updating their address with that of the live ones.

static update_haproxy_runtime(**kwargs)[source]

Method to update haproxy at runtime using the util method present above

Parameters:**kwargs (dictionary) – Dictionary containing params
Returns:Successfully updated haproxy or not stats : dictionary containing active nodes and inactive node count
Return type:bool
static update_runtime_util(haproxy_sock, node_ips, nodes, backend_name, port, logger=None)[source]

Method for updating haproxy backends using unix socket

This method updates the haproxy backends by sending commands over the exposed unix socket.


  • First it iterates over the active backends currently present in haproxy.
  • If they are not present in the current fetched list of backends, then we disable those backends and add them to the inactive pool.
  • Next we iterate over the list of current live nodes fetched from orchestrator
  • if they are already present as live backends in haproxy even after the above elimination then we skip.
  • If they are not present then we fetch a inactive node from the inactive pool, change its address to that of the live node and enable back that node.
  • haproxy_sock (str) – Location of the haproxy unix socket file
  • node_ips (list) – List of current live nodes fetched from orchestrator. (IP or hostname)
  • nodes (dictionary) – Dictionary conatining haproxy active and inactive backends
  • backend_name (str) – Name of the haproxy backend that needs to be updated.
  • port (int) – port for the backend nodes
  • logger (object) – Logger object
dict : Dictionary conatining active node_ips and inactive nodes count

src.core.haproxyupdater.sockethandler module

class src.core.haproxyupdater.sockethandler.SocketHandler(**kwargs)[source]

Bases: object

Class containing methods for handling socket operation

This is a generic class for handling all socket operation. All the commands which are to be sent to haproxy and done via methods in this class.

Parameters:**kwargs (dictionary) – Dictionary containing params

Method to connect to haproxy unix socket

This method creates a socket connection to the given haproxy unix socket

Returns:Successfully created socket connection or not
Return type:bool

Method to send command to haproxy unix socket and get response

It will first create a socket connection to the haproxy socket and then send the given command and get response.

Parameters:**kwargs (dictionary) – Dictionary containing params
Returns:Successfully sent command or not str : response sent by the haproxy unix socket
Return type:bool
send_one(sock_file, command)[source]

Module contents