Skip to content

Commit

Permalink
Adds support for managing services using s6
Browse files Browse the repository at this point in the history
The ServiceManager can now stop, start, and restart services inside container that use
s6 to manage services.

The restart command sends a SIGKILL to the service and relies on s6 to restart the service.
This was needed because some services such as PostgreSQL shut down gracefully when SIGTERM
is sent. The graceful shutdown does not happen until all connections are closed by clients.
When testing Pulp, a graceful shutdown is not needed.

[noissue]
  • Loading branch information
dkliban committed Oct 29, 2021
1 parent 4fe9d27 commit 1d53d00
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pulp_smash/cli.py
Expand Up @@ -356,6 +356,8 @@ def _get_service_manager(cfg, pulp_host):

client = Client(cfg, echo_handler, pulp_host=pulp_host)
commands_managers = (
("which s6-svc", "s6"),
("test -x /bin/s6-svc", "s6"),
("which systemctl", "systemd"),
("which service", "sysv"),
("test -x /sbin/service", "sysv"),
Expand Down Expand Up @@ -392,6 +394,15 @@ def _start_systemd(client, services):
cmd = ("systemctl", "start") + tuple(services)
return (client.run(cmd, sudo=True),)

@staticmethod
def _start_s6(client, services):
return tuple(
(
client.run(("s6-svc", "-u", "/var/run/s6/services/{}".format(service)))
for service in services
)
)

@staticmethod
def _stop_sysv(client, services):
return tuple((client.run(("service", service, "stop"), sudo=True) for service in services))
Expand All @@ -401,6 +412,15 @@ def _stop_systemd(client, services):
cmd = ("systemctl", "stop") + tuple(services)
return (client.run(cmd, sudo=True),)

@staticmethod
def _stop_s6(client, services):
return tuple(
(
client.run(("s6-svc", "-d", "/var/run/s6/services/{}".format(service)))
for service in services
)
)

@staticmethod
def _restart_sysv(client, services):
return tuple(
Expand All @@ -412,6 +432,15 @@ def _restart_systemd(client, services):
cmd = ("systemctl", "restart") + tuple(services)
return (client.run(cmd, sudo=True),)

@staticmethod
def _restart_s6(client, services):
return tuple(
(
client.run(("s6-svc", "-k", "/var/run/s6/services/{}".format(service)))
for service in services
)
)

@staticmethod
def _is_active_sysv(client, services):
with contextlib.suppress(exceptions.CalledProcessError):
Expand All @@ -427,6 +456,19 @@ def _is_active_systemd(client, services):
return (client.run(cmd, sudo=True),)
return False

def _is_active_s6(self, client, services):
def process_ps_output(command_output, service):
if command_output.stdout.find(service) > -1:
return True
else:
return False

with contextlib.suppress(exceptions.CalledProcessError):
return tuple(
(process_ps_output(client.run(("ps", "x")), service) for service in services)
)
return False

@abstractmethod
def start(self, services):
"""Start the given services.
Expand Down Expand Up @@ -527,6 +569,8 @@ def start(self, services):
result[host.hostname] = self._start_sysv(client, services)
elif svc_mgr == "systemd":
result[host.hostname] = self._start_systemd(client, services)
elif svc_mgr == "s6":
result[host.hostname] = self._start_s6(client, services)
else:
raise NotImplementedError(
'Service manager "{}" not supported on "{}"'.format(svc_mgr, host.hostname)
Expand All @@ -552,6 +596,8 @@ def stop(self, services):
result[host.hostname] = self._stop_sysv(client, services)
elif svc_mgr == "systemd":
result[host.hostname] = self._stop_systemd(client, services)
elif svc_mgr == "s6":
result[host.hostname] = self._stop_s6(client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(svc_mgr))
return result
Expand All @@ -575,6 +621,8 @@ def restart(self, services):
result[host.hostname] = self._restart_sysv(client, services)
elif svc_mgr == "systemd":
result[host.hostname] = self._restart_systemd(client, services)
elif svc_mgr == "s6":
result[host.hostname] = self._restart_s6(client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(svc_mgr))
return result
Expand All @@ -597,6 +645,8 @@ def is_active(self, services):
result[host.hostname] = self._is_active_sysv(client, services)
elif svc_mgr == "systemd":
result[host.hostname] = self._is_active_systemd(client, services)
elif svc_mgr == "s6":
result[host.hostname] = self._is_active_s6(client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(svc_mgr))
return result
Expand Down Expand Up @@ -657,6 +707,8 @@ def start(self, services):
return self._start_sysv(self._client, services)
elif self._svc_mgr == "systemd":
return self._start_systemd(self._client, services)
elif self._svc_mgr == "s6":
return self._start_s6(self._client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(self._svc_mgr))

Expand All @@ -672,6 +724,8 @@ def stop(self, services):
return self._stop_sysv(self._client, services)
elif self._svc_mgr == "systemd":
return self._stop_systemd(self._client, services)
elif self._svc_mgr == "s6":
return self._stop_s6(self._client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(self._svc_mgr))

Expand All @@ -687,6 +741,8 @@ def restart(self, services):
return self._restart_sysv(self._client, services)
elif self._svc_mgr == "systemd":
return self._restart_systemd(self._client, services)
elif self._svc_mgr == "s6":
return self._restart_s6(self._client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(self._svc_mgr))

Expand All @@ -701,6 +757,8 @@ def is_active(self, services):
return self._is_active_sysv(self._client, services)
elif self._svc_mgr == "systemd":
return self._is_active_systemd(self._client, services)
elif self._svc_mgr == "s6":
return self._is_active_s6(self._client, services)
else:
raise NotImplementedError("Service manager not supported: {}".format(self._svc_mgr))

Expand Down

0 comments on commit 1d53d00

Please sign in to comment.