Sysctl¶
- class rmote.tools.sysctl.Sysctl[source]¶
Bases:
ToolManage kernel parameters via sysctl.
Runtime values are applied immediately via
/proc/sys/. Changes are persisted to a file underCONF_DIRso 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
- 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.
- 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.