Service

Manage systemd units on the target. The target must run systemd, and the connection user must have permission to manage the requested service. See Quickstart for rendering a unit and checking a running service.

class rmote.tools.service.Service[source]

Bases: Tool

Manage systemd services on the remote host. Requires systemd and root.

Every operation runs systemctl. A target without it refuses with NotImplementedError, which names systemd as what provides it.

Start and enable a real systemd unit in a disposable Debian container:

>>> from rmote.tools import Exec, FileSystem
>>> remote = getfixture("debian_tool")
>>> unit = "[Service]\nExecStart=/bin/sleep infinity\n[Install]\nWantedBy=multi-user.target\n"
>>> remote(FileSystem.write, "/etc/systemd/system/example.service", unit)
True
>>> _ = remote(Exec.command, "systemctl", "daemon-reload")
>>> result = remote(Service.converge, "example.service", started=True, enabled=True)
>>> result.started, result.enabled, result.changed
(True, True, True)
>>> remote(Service.converge, "example.service", started=True, enabled=True).changed
False
>>> result = remote(Service.converge, "example.service", started=False, enabled=False)
>>> result.started, result.enabled, result.changed
(False, False, True)
>>> remote(Service.converge, "example.service", started=False, enabled=False).changed
False
async static converge(name, *, started=True, enabled=True)[source]

Idempotently ensure a service is in the desired state.

Parameters:
  • name (Text) – Service unit name (e.g. “nginx”, “nginx.service”)

  • started (bool) – Whether the service should be running

  • enabled (bool) – Whether the service should start on boot

Return type:

Result

async static daemon_reload()[source]

Reload systemd manager configuration.

Return type:

None

async static disable(name)[source]

Disable a service at boot. No-op if already disabled.

Return type:

Result

async static enable(name)[source]

Enable a service at boot. No-op if already enabled.

Return type:

Result

async static reload(name)[source]

Reload a service unconditionally (SIGHUP).

Return type:

Result

async static restart(name)[source]

Restart a service unconditionally.

Return type:

Result

async static start(name)[source]

Start a service. No-op if already running.

Return type:

Result

async static status(name)[source]

Return current active/enabled status of a service.

Return type:

Result

async static stop(name)[source]

Stop a service. No-op if already stopped.

Return type:

Result

Types

class rmote.tools.service.State(*values)[source]

Bases: IntEnum

RELOADED = 3
RESTARTED = 2
STARTED = 0
STOPPED = 1
class rmote.tools.service.Result(name, started, enabled, changed)[source]

Bases: object

changed: bool
enabled: bool
name: str
started: bool