Sysctl

class rmote.tools.sysctl.Sysctl[source]

Bases: Tool

Manage kernel parameters via sysctl.

Runtime values are applied immediately via /proc/sys/. Changes are persisted to a file under CONF_DIR so they survive reboots. Requires root.

Examples:

# default file: /etc/sysctl.d/99-rmote.conf
Sysctl.present("net.ipv4.ip_forward", "1")

# custom file name inside CONF_DIR
Sysctl.present("net.ipv4.ip_forward", "1", name="50-network.conf")
Sysctl.converge({"vm.swappiness": "10"}, name="50-network.conf")

# custom directory via subclass
class MySysctl(Sysctl):
    CONF_DIR = Path("/etc/sysctl.d")

Change a namespaced network parameter in a disposable container. The example checks the runtime value, persistent configuration and repeated application:

>>> from rmote.tools import FileSystem
>>> remote = getfixture("debian_tool")
>>> value = "0" if remote(Sysctl.get, "net.ipv4.ip_forward") == "1" else "1"
>>> remote(Sysctl.present, "net.ipv4.ip_forward", value).changed
True
>>> remote(Sysctl.get, "net.ipv4.ip_forward") == value
True
>>> "net.ipv4.ip_forward = " + value in remote(FileSystem.read_str, "/etc/sysctl.d/99-rmote.conf")
True
>>> remote(Sysctl.present, "net.ipv4.ip_forward", value).changed
False
>>> remote(Sysctl.absent, "net.ipv4.ip_forward")
True
>>> remote(Sysctl.absent, "net.ipv4.ip_forward")
False
CONF_DIR: ClassVar[Path] = PosixPath('/etc/sysctl.d')
classmethod absent(key, *, name='99-rmote.conf')[source]

Remove key from CONF_DIR/name.

Does not reset the runtime value — the current kernel value is left untouched.

Parameters:
  • key (Text) – Sysctl key to remove from persistent config.

  • name (Text) – Filename inside CONF_DIR (default "99-rmote.conf").

Return type:

bool

Returns:

True if the key was present and removed, False if it was already absent.

classmethod converge(params, *, name='99-rmote.conf')[source]

Ensure multiple sysctl keys have the desired values.

Reads the conf file once, applies all changes, then writes the conf file once — more efficient than calling present() in a loop.

Parameters:
  • params (WSGIEnvironment[Text, Text]) – Mapping of sysctl key → desired value, e.g. {"net.ipv4.ip_forward": "1", "vm.swappiness": "10"}.

  • name (Text) – Filename inside CONF_DIR (default "99-rmote.conf").

Return type:

list[Result]

Returns:

List of Result objects, one per key.

classmethod get(key)[source]

Return the current runtime value of key.

Parameters:

key (Text) – Sysctl key (e.g. "net.ipv4.ip_forward").

Return type:

Text

Returns:

Current value as a string.

classmethod present(key, value, *, name='99-rmote.conf')[source]

Ensure key has value at runtime and in CONF_DIR/name.

Reads the current runtime value and the persisted conf. Only makes changes if the runtime value or the conf entry differ from value.

Parameters:
  • key (Text) – Sysctl key (e.g. "net.ipv4.ip_forward").

  • value (Text) – Desired value (e.g. "1").

  • name (Text) – Filename inside CONF_DIR (default "99-rmote.conf").

Return type:

Result

Returns:

Result with the key, applied value, and whether anything changed.

Types

class rmote.tools.sysctl.Result(key, value, changed)[source]

Bases: object

changed: bool
key: str
value: str