Skip to content

Commit

Permalink
🐛 Server state change command
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammed Hussein Karimi <[email protected]>
  • Loading branch information
mhkarimi1383 committed Nov 13, 2024
1 parent 0514ac8 commit fca0474
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions charts/haproxy-redis-sentinel/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v2
name: haproxy-redis-sentinel
description: A Helm chart for HAProxy with Redis Sentinel
version: 0.0.16
appVersion: "0.0.9"
version: 0.0.17
appVersion: "0.0.10"
sources:
- https://github.com/ParminCloud/haproxy-redis-sentinel
maintainers:
Expand Down
28 changes: 17 additions & 11 deletions haproxy_redis_sentinel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,22 @@ def run(
address = None
sentinel_info: dict[str, Any] = conn.info() # type: ignore
try:
master_id = [k for k in sentinel_info.keys()
if k.startswith("master") and
sentinel_info[k]["name"] == master_name][0]
master_id = [
k for k in sentinel_info.keys()
if k.startswith("master") and
sentinel_info[k]["name"] == master_name
][0]
except IndexError:
raise Exception("Unable to find given master by name")
address = sentinel_info[master_id]["address"]
info(f"Setting initial master address: {address}")

# Remove server in case of restarts
out = send_command(haproxy_socket, [
f"set server {
haproxy_backend}/{haproxy_server_name} state maint",
f"del server {haproxy_backend}/{haproxy_server_name}"])
f"set server {haproxy_backend}/{haproxy_server_name} state maint",
f"shutdown sessions server {haproxy_backend}/{haproxy_server_name}",
f"del server {haproxy_backend}/{haproxy_server_name}",
])
if len(out) > 0 and \
not any(item in out for item in {HAProxyOutput.SERVER_DELETED,
HAProxyOutput.SERVER_NOT_FOUND,
Expand All @@ -132,9 +135,12 @@ def run(
host = data[3]
port = data[4]
info("Master Changed, Terminating clients")
info(send_command(haproxy_socket,
[f"set server {haproxy_backend}/{haproxy_server_name} state maint", # noqa: E501
f"shutdown sessions server {haproxy_backend}/{haproxy_server_name}"])) # noqa: E501
info(send_command(haproxy_socket, [
f"set server {haproxy_backend}/{haproxy_server_name} state maint", # noqa: E501
f"shutdown sessions server {haproxy_backend}/{haproxy_server_name}", # noqa: E501
]))
info(f"Switching to new master Host: {host}, Port: {port}")
info(send_command(haproxy_socket,
f"set server {haproxy_backend}/{haproxy_server_name} addr {host} port {port} state ready")) # noqa: E501
info(send_command(haproxy_socket, [
f"set server {haproxy_backend}/{haproxy_server_name} addr {host} port {port}", # noqa: E501
f"set server {haproxy_backend}/{haproxy_server_name} state ready", # noqa: E501
]))

0 comments on commit fca0474

Please sign in to comment.