Built-in Tools¶
- class rmote.tools.Apt[source]¶
Bases:
ToolManage Debian/Ubuntu packages via apt-get. Requires root on the remote host.
- classmethod converge(*packages)[source]¶
Ensure all packages are present, reading dpkg status once for efficiency.
- classmethod package(package, state=State.PRESENT)[source]¶
Install, remove, or upgrade a single package.
- Parameters:
- Return type:
- Returns:
Resultwith the package name, installed version, and whether the system was changed.- Raises:
RuntimeError – If the underlying
apt-getinvocation fails.
- static update(ttl=-1)[source]¶
Run
apt-get update, with optional TTL-based skipping.- Parameters:
ttl (
int|float) – Minimum age in seconds of the last successful update before re-running.-1(default) always runs the update.- Return type:
- Returns:
Trueifapt-get updatewas executed,Falseif the cache is fresher than ttl seconds.- Raises:
RuntimeError – If
apt-get updateexits with a non-zero status.
- class rmote.tools.AptRepository[source]¶
Bases:
ToolManage APT repository sources and GPG keys on Debian/Ubuntu.
Repository sources are written as DEB822
.sourcesfiles under/etc/apt/sources.list.d/. GPG keyrings are stored in/etc/apt/keyrings/. All operations are idempotent and require root.- static absent(name)[source]¶
Remove the DEB822
.sourcesfile for name, if it exists.- Parameters:
name (
Text) – Repository label (the{name}.sourcesfilename stem).- Return type:
Result- Returns:
Resultindicating whether the file was removed.
- static key(name, data)[source]¶
Install a GPG signing key to
/etc/apt/keyrings/{name}.The operation is idempotent — if the file already contains identical bytes it is left untouched.
- static present(name, *, types=('deb',), uris, suites, components, signed_by='')[source]¶
Ensure a DEB822
.sourcesfile is configured for name.Writes
/etc/apt/sources.list.d/{name}.sources. The operation is idempotent — if the file already contains the expected content it is left untouched.- Parameters:
name (
Text) – Repository label used as the filename stem.types (
Iterable[Text]) – Package types — typically("deb",)for binary packages or("deb-src",)for source packages.uris (
Iterable[Text]) – Repository base URIs (e.g.("https://download.docker.com/linux/ubuntu",)).suites (
Iterable[Text]) – Distribution suites (e.g.("noble",)).components (
Iterable[Text]) – Repository components (e.g.("stable",)).signed_by (
Text) – Absolute path to the GPG keyring that signs this repository.
- Return type:
Result- Returns:
Resultindicating whether the file was created or updated.
- class rmote.tools.Exec[source]¶
Bases:
ToolRun commands and shell expressions on the remote host.
- static command(*args, check=True, env=None, cwd=None, stdin=None)[source]¶
Run a command with explicit argument list.
- Parameters:
*args (
Text) – Command and its arguments (e.g."ls","-la").check (
bool) – Raisesubprocess.CalledProcessErroron non-zero exit.env (
WSGIEnvironment[Text,Text] |None) – Override environment variables for the subprocess.cwd (
None|Text|Path) – Working directory; defaults to the remote process’s cwd.stdin (
None|Text|bytes) – Data written to stdin before the command reads it.
- Return type:
- Returns:
subprocess.CompletedProcesswithreturncode,stdout, andstderr.
- static shell(expression, check=True, env=None, cwd=None, stdin=None)[source]¶
Run expression through the remote shell (
/bin/sh -c).- Parameters:
expression (
Text) – Shell expression, including pipes, redirects, etc.check (
bool) – Raisesubprocess.CalledProcessErroron non-zero exit.env (
WSGIEnvironment[Text,Text] |None) – Override environment variables for the subprocess.cwd (
None|Text|Path) – Working directory; defaults to the remote process’s cwd.stdin (
None|Text|bytes) – Data written to stdin before the command reads it.
- Return type:
- Returns:
subprocess.CompletedProcesswithreturncode,stdout, andstderr.
- class rmote.tools.FileSystem[source]¶
Bases:
ToolRemote filesystem operations - read, glob, and idempotent line-in-file.
- static line_in_file(path, *, line, regexp=None, strip=True, create=False, match=LineInFileMatch.FIRST)[source]¶
Ensure line is present in the file at path, idempotently.
If regexp is given, replace the first (or all, with
match=ALL) lines that match the pattern with line. If no line matches, or if regexp is not given and line is not found, line is appended.- Parameters:
path (
Text) – Path to the file to modify.line (
Text) – The desired line content to insert or substitute.regexp (
Text|None) – A regex pattern to match against existing lines. When a match is found, the matching line(s) are replaced with line.strip (
bool) – Compare lines after stripping whitespace when looking for an exact match (no regexp). DefaultTrue.create (
bool) – Create the file if it does not exist. DefaultFalse.match (
LineInFileMatch) – Whether to replace theFIRSTmatching line orALLmatching lines.
- Return type:
- Returns:
A unified diff string describing the change, or an empty string if the file was already in the desired state.
- Raises:
FileNotFoundError – If path does not exist and create is
False.
- class rmote.tools.Logger[source]¶
Bases:
ToolControl logging on the remote side and forward records to the local process.
Log records emitted on the remote side are forwarded over the protocol channel and appear locally under the
rmote.remote.<name>logger hierarchy.- classmethod log(level, message, *args)[source]¶
Emit a log record on the remote side.
The record is forwarded to the local logging system via the protocol’s LOG packet and appears under the
rmote.remote.<name>logger.- Parameters:
- Raises:
ValueError – If level is not a recognised level name.
- Return type:
- classmethod set_log_level(level)[source]¶
Set the root logger level on the remote side.
- Parameters:
level (
Text) – One of"NOTSET","DEBUG","INFO","WARNING","ERROR", or"CRITICAL"(case-insensitive).- Raises:
ValueError – If level is not a recognised level name.
- Return type:
- class rmote.tools.Pacman[source]¶
Bases:
ToolManage Arch Linux packages via pacman. Requires root on the remote host.
- static package(package, state=State.PRESENT)[source]¶
Install, remove, or upgrade a single package.
- Parameters:
- Return type:
- Returns:
Resultwith the package name, installed version, and whether the system was changed.- Raises:
RuntimeError – If the underlying
pacmaninvocation fails.
- static update(ttl=-1)[source]¶
Synchronise the package database (
pacman -Sy), with optional TTL-based skipping.- Parameters:
ttl (
int|float) – Minimum age in seconds of the last sync before re-running.-1(default) always synchronises.- Return type:
- Returns:
Trueifpacman -Sywas executed,Falseif the database is fresher than ttl seconds.- Raises:
RuntimeError – If
pacman -Syexits with a non-zero status.
- class rmote.tools.PacmanRepository[source]¶
Bases:
ToolManage Arch Linux repository sections and GPG keys.
Repository sections are read from and written to
/etc/pacman.conf. GPG key files are stored in/etc/pacman.d/. All operations are idempotent and require root.- static absent(name)[source]¶
Remove the repository section name from
/etc/pacman.conf.- Parameters:
name (
Text) – Repository section name to remove.- Return type:
Result- Returns:
Resultindicating whetherpacman.confwas modified.
- static key(name, data)[source]¶
Write a GPG key to
/etc/pacman.d/{name}.Idempotent — the file is left untouched if it already contains identical bytes.
- static present(name, *, servers, sig_level='Optional TrustAll')[source]¶
Ensure a repository section is present in
/etc/pacman.conf.If the section already exists with the same content it is left unchanged. If it exists with different content it is updated in-place. Otherwise it is appended at the end of the file.
- class rmote.tools.Quit[source]¶
Bases:
ToolSignal the remote process to exit cleanly.
- async static exit(code=0)[source]¶
Exit the remote process with code.
- Parameters:
code (
int) – Exit status code passed tosys.exit(). Defaults to 0.- Return type:
- class rmote.tools.Service[source]¶
Bases:
ToolManage systemd services on the remote host. Requires systemd and root.
- static converge(name, *, started=True, enabled=True)[source]¶
Idempotently ensure a service is in the desired state.
- class rmote.tools.Template[source]¶
Bases:
ToolBuilt-in tool that renders Mako-like templates on the remote side.
All three methods execute on the remote process. See Templating for a full description of the template syntax.
- static render(template, **kwargs)[source]¶
Compile template and render it with kwargs as the variable namespace.
- static render_compiled(template, **kwargs)[source]¶
Render a pre-compiled
Templateinstance.Use this method when the template was compiled locally and passed as an argument to avoid re-compiling it on the remote side.
- class rmote.tools.User[source]¶
Bases:
ToolManage users, groups, SSH keys, and sudoers on the remote host. Requires root.
- static absent(name, *, remove_home=False)[source]¶
Ensure a user does not exist. Returns True if user was removed.
- static authorized_key(name, key, *, exclusive=False)[source]¶
Ensure an SSH public key is present in ~/.ssh/authorized_keys.
- static group_absent(name)[source]¶
Ensure a group does not exist. Returns True if group was removed.
- Return type:
- static group_present(name, *, gid=None, system=False)[source]¶
Ensure a group exists. Idempotent.
- Return type:
- static present(name, *, uid=None, gid=None, comment='', home=None, shell='/bin/bash', groups=None, append_groups=True, system=False, create_home=True)[source]¶
Ensure a user exists with the given attributes. Idempotent.
- Parameters:
name (
Text) – Usernamecomment (
Text) – GECOS fieldhome (
Text|None) – Home directory path (default: /home/<name>)shell (
Text) – Login shellappend_groups (
bool) – If True, add to groups without removing existing onessystem (
bool) – Create as system user (no home, lower UID range)create_home (
bool) – Create home directory if it does not exist
- Return type: