Always save HAProxy server state before stop

Solution 1:

We ended up creating a "fake" service with just a stop action that depends on haproxy.service and therefor will have its ExecStop run before HAProxy is stopped.

haproxy-state.service :

[Unit]
Description=Save HAProxy State Before Service Stop
After=haproxy.service
Requires=haproxy.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/bin/sh -c "/usr/bin/socat /run/haproxy/control.socket - <<< \"show servers state\" > /var/lib/haproxy/last_state"

[Install]
WantedBy=multi-user.target haproxy.service

It needs to be after haproxy.service so it will stop before. It needs to be WantedBy so that it will always be started any time HAProxy is running.

This isn't perfect because the state could change if there is a long pause before systemd stops the next service, but that is a risk we will take.