File: haproxy_cmd.py

package info (click to toggle)
haproxy-cmd 0.0.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 80 kB
  • sloc: python: 250; sh: 132; makefile: 14
file content (305 lines) | stat: -rw-r--r-- 11,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

# Module importation
import os
import logging
import re
import socket
import sys
import time

from oslo_config import cfg


# This generic function will be called by individual actions below
def send_command_to_haproxy(CONF, command):
    with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
        s.connect(CONF.socket)
        s.send(command.encode())
        return s.recv(4096).decode()


def usage():
    print("Usage: haproxy-cmd <action> [--socket <admin-socket-file>] [--config <haproxy-config-file>] [--backend <backend>] [--server <server>] [--frontend <frontend>] [--verbose] [--details]")
    print("Action can be one of:")
    print("list-frontends, list-backends, list-config-backends, list-servers, list-connections, enable-server, drain-server, stop-server, check-safe-to-remove, reload-haproxy")
    print("For these actions, the --backend <backend> parameter is mandatory: list-servers")
    print("For these actions, the --backend <backend> and --server <server> parameters are mandatory: list-connections, enable-server, drain-server, stop-server, check-safe-to-remove")
    print("The list-servers action, the --details option can be specified")
    print("For the drain-server action, --wait can be used to wait for server to be fully drained")
    sys.exit(1)


def get_haproxy_cfg(CONF):
    with open(CONF.config, 'r') as f:
        yield from f


def get_socket(CONF):
    """
    Returns first admin socket found
    """
    socket_re = re.compile(r'[^#]\s+socket\s+([^ ]+)\s+.*\badmin\b')
    for line in get_haproxy_cfg(CONF):
        search = socket_re.search(line)
        if search:
            return search.group(1)
    return '/var/lib/haproxy/admin.sock'
            

def list_backends(CONF):
    if CONF.verbose:
        print("Listing backends...")
    backends = send_command_to_haproxy(CONF, "show backend\n")
    lines = backends.split("\n")
    for line in lines:
        if line != "# name" and line != "MASTER" and line != "":
            print(line)

def list_frontends(CONF):
    if CONF.verbose:
        print("Listing frontends...")
    for line in get_haproxy_cfg(CONF):
        line_array = line.strip().split(' ')
        if (line_array[0] == 'frontend'):
            print(line_array[1])

def list_config_backends(CONF):
    # Show backend via socket cannot retrieve backends depending on frontend.
    # Take care with output, as we have to parse haproxy configfile, not sure that service has been restarted after changes or that research is working.
    if CONF.verbose:
        print("Listing config backends...")
    if CONF.frontend:
        text = "".join([line for line in get_haproxy_cfg(CONF)])
        start_index = text.find(f"frontend {CONF.frontend}")
        end_index = text.find("\n\n", start_index)
        result = "\n".join(
            set(re.findall(r"use_backend (\w+)", text[start_index:end_index]))
        )
        result = "\n".join(
            set(re.findall(r"default_backend (\w+)", text[start_index:end_index]))
        )
        print(f"{result}")
    else:
        print("Missing parameter: --frontend")
        usage()


def list_servers(CONF):
    if CONF.backend:
        # List each servers for a specific backend and his state
        op_state_mapping = {
            0: "SRV_ST_STOPPED, The server op state is down",
            1: "SRV_ST_STARTING, The server op state is warming up",
            2: "SRV_ST_RUNNING, The server op state is fully up",
            3: "SRV_ST_STOPPING, The server op state is up but soft-stopping",
        }

        admin_state_mapping = {
            0: "ACTIVE, The server admin state is ACTIVE",
            1: "SRV_ADMF_FMAINT, The server admin state was explicitly forced into maintenance",
            2: "SRV_ADMF_IMAINT, The server admin state has inherited the maintenance status from a tracked server",
            4: "SRV_ADMF_CMAINT, The server admin state is in maintenance because of the configuration",
            8: "SRV_ADMF_FDRAIN, The server admin state was explicitly forced into drain state",
            10: "SRV_ADMF_IDRAIN, The server admin state has inherited the drain status from a tracked server",
            20: "SRV_ADMF_RMAINT, The server admin state is in maintenance because of an IP address resolution failure",
        }

        lines = send_command_to_haproxy(CONF, f"show servers state {CONF.backend}\n").split("\n")
        headers = lines[1].split(" ")
        srv_admin_state_index = headers.index("srv_admin_state")
        srv_op_state_index = headers.index("srv_op_state")
        srv_name_index = headers.index("srv_name")

        for line in lines[2:]:
            fields = line.split(" ")
            if (
                len(fields) > srv_admin_state_index
                and fields[1] == CONF.backend
            ):
                current_srv_op_state = int(fields[srv_op_state_index - 1])
                current_srv_admin_state = int(fields[srv_admin_state_index - 1])
                current_srv_name = str(fields[srv_name_index - 1])

                op_state_description = op_state_mapping.get(
                    current_srv_op_state, "Unknown op state"
                )
                admin_state_description = admin_state_mapping.get(
                    current_srv_admin_state, "Unknown admin state"
                )

                if CONF.details:
                    print(f"{current_srv_name}, {op_state_description}, {admin_state_description}")
                else:
                    print(f"{current_srv_name}")
    else:
        usage()

# Like below, but for internal use, without print...
def _list_conn(CONF):
    lines = send_command_to_haproxy(CONF, f"show servers conn {CONF.backend} \n").split("\n")
    connections_index = (
        lines[0].split(" ").index("used_cur")
    )  # Current connections column index
    for line in lines[1:]:
        fields = line.split(" ")
        if len(fields) > connections_index:
            if fields[0].startswith(CONF.backend + "/" + CONF.server):
                current_connections = int(fields[connections_index - 1])
    return current_connections

def list_connections(CONF):
    if CONF.verbose:
        print(f"Listing servers for backend: {CONF.backend}...")
    if CONF.backend and CONF.server:
        lines = send_command_to_haproxy(CONF, f"show servers conn {CONF.backend} \n").split("\n")
        connections_index = (
            lines[0].split(" ").index("used_cur")
        )  # Current connections column index
        for line in lines[1:]:
            fields = line.split(" ")
            if len(fields) > connections_index:
                if fields[0].startswith(CONF.backend + "/" + CONF.server):
                    current_connections = int(fields[connections_index - 1])
                    if CONF.verbose:
                        print(f"{current_srv_name}, {op_state_description}, {admin_state_description}")
                    else:
                        print(f"{str(current_connections)}")
    else:
        usage()


def check_safe_to_remove(CONF):
    if CONF.verbose:
        print(f"Checking if safe to remove server: {CONF.backend}/{CONF.server}...")
    nb_conn = _list_conn(CONF)
    if nb_conn == 0:
        if CONF.verbose:
            print(f"Server {CONF.server} can be removed from backend {CONF.backend}.")
        sys.exit(0)
    else:
        if CONF.verbose:
            print(f"Server {CONF.server} has ongoing {nb_conn} connection(s) for backend {CONF.backend}.")
        sys.exit(1)

def enable_server(CONF):
    if CONF.verbose:
        print(f"Enabling server: {CONF.backend}/{CONF.server}...")
    if CONF.backend and CONF.server:
        send_command_to_haproxy(CONF, f"enable server {CONF.backend}/{CONF.server}\n")
    else:
        usage()

def drain_server(CONF):
    if CONF.verbose:
        print(f"Draining server: {CONF.backend}/{CONF.server}...")
    if CONF.backend and CONF.server:
        send_command_to_haproxy(CONF, f"set server {CONF.backend}/{CONF.server} state drain\n")
        if CONF.wait:
            nb_conn = _list_conn(CONF)
            cnt = 0
            while nb_conn != 0:
                time.sleep(1)
                cnt = cnt + 1
                # If timeout is zero (the default), then cnt is already 1
                # on first run, so it wont ever timeout (what we want).
                if cnt == CONF.timeout:
                    if CONF.verbose:
                        print("Timed out")
                    sys.exit(1)
                if CONF.verbose:
                    print(".")
                nb_conn = _list_conn(CONF)
            if CONF.verbose:
                print("done.")
    else:
        usage()

def stop_server(CONF):
    if CONF.verbose:
        print(f"Stopping server: {CONF.backend}/{CONF.server}...")
    if CONF.backend and CONF.server:
        send_command_to_haproxy(CONF, f"disable server {CONF.backend}/{CONF.server}\n")
    else:
        usage()


def reload_haproxy(CONF):
    if CONF.verbose:
        print("Reloading HAProxy")
    os.system("systemctl restart haproxy")
    time.sleep(5)
    if os.popen("systemctl is-active haproxy").read().replace("\n", "") != "active":
        raise Exception("Haproxy service not active.")
    uptime = (
        os.popen(r'systemctl status haproxy | grep -Po ".*; \K(.*)(?= ago)"')
        .read()
        .replace("\n", "")
    )
    if CONF.verbose:
        print(f"Haproxy service has been successfully reloaded with {uptime} uptime.")

#############################################
### Command line parsing with oslo.config ###
#############################################

CLI_OPTS = [
    cfg.StrOpt('backend', short='b',
               help='Backend to use'),

    cfg.StrOpt('socket', short='k',
               help='Socket file to use'),

    cfg.StrOpt('config', default='/etc/haproxy/haproxy.cfg', short='c',
               help='Config file to use'),

    cfg.BoolOpt('details', default=False, short='d',
               help='Wait for server to be drained'),

    cfg.BoolOpt('wait', default=False, short='w',
               help='Wait until drain is finished'),

    cfg.IntOpt('timeout', default=0, short='t',
               help='Timeout for drain'),

    cfg.BoolOpt('verbose', default=False, short='v',
               help='Show details'),

    cfg.StrOpt('frontend', short='f',
               help='Frontend to use'),

    cfg.StrOpt('server', short='s',
               help='Server to operate on'),

]

# Map actions to functions
ACTIONS = {
    'list-backends': list_backends,
    'list-frontends': list_frontends,
    'list-config-backends': list_config_backends,
    'list-servers': list_servers,
    'list-connections': list_connections,
    'enable-server': enable_server,
    'drain-server': drain_server,
    'stop-server': stop_server,
    'check-safe-to-remove': check_safe_to_remove,
    'reload-haproxy': reload_haproxy,
}

def main():
    action = sys.argv[1] if len(sys.argv) > 1 else None

    CONF = cfg.ConfigOpts()
    CONF.register_cli_opts(CLI_OPTS)
    CONF(sys.argv[2:])

    if CONF.socket is None:
        CONF.socket = get_socket(CONF)

    # Perform the corresponding action
    if action in ACTIONS:
        ACTIONS[action](CONF)
        sys.exit(0)
    else:
        usage()