Host
TrueNASHost is a hostctl PosixHost:
run, path, spawn, info, connect, close, shell, capabilities, and
last_selection are inherited, and it adds the TrueNAS surface (api,
websocket, login, subscribe, upload, download, dump_api,
install_sshcreds).
TrueNASConfig is the matching HostConfig. It accepts every connection string
TrueNASClient does and normalizes to a truenas+* scheme, so
HostConfig("truenas+wss://nas") resolves through hostctl's own registry.
pytruenas.host
hostctl integration: the TrueNAS host configuration.
:class:TrueNASConfig is the hostctl :class:~hostctl.host.HostConfig for
a TrueNAS middleware target. It carries parsed connection settings and nothing
more -- constructing one performs no network I/O, which is what lets a config be
built offline, logged, and round-tripped through its canonical URI.
Scheme handling has two deliberately separate layers:
- The hostctl registry sees only
truenas/truenas+auto/truenas+ws/truenas+wss/truenas+unix. pytruenas does not claim barewss://orhttps://globally -- hostctl is protocol-agnostic, and hijacking a generic scheme would risk an "ambiguous host URI matched" collision with any other configuration that legitimately wants it. - pytruenas' own entry point additionally understands every connection
string :class:
~pytruenas.TrueNASClienthas always accepted -- a bare host,host:port,ws/wss,http/https, a unix socket path, orNone-- by rewriting it to atruenas+*URI first. That rewrite is :func:_normalize_target: one pure string function, no I/O.
The scheme and API-path probes that :class:TrueNASClient historically ran
inside __init__ are recorded here (:attr:~TrueNASConfig.needs_scheme_probe,
:attr:~TrueNASConfig.needs_path_probe) and performed later, on connect.
AUTO_SCHEME = 'truenas+auto'
module-attribute
ApiVersion = _ty.TypeVar('ApiVersion', bound=_Namespace, default=Current)
module-attribute
DEFAULT_SOCKET_PATH = DEFAULT_UNIX_SOCKET
module-attribute
EXECUTOR_NAMES = ('local', 'ssh', 'webshell')
module-attribute
PATH_NAMES = ('local', 'sftp', 'tnasws')
module-attribute
__all__ = ['AUTO_SCHEME', 'DEFAULT_SOCKET_PATH', 'TrueNASConfig', 'TrueNASHost']
module-attribute
TrueNASConfig(host='', *, port=0, secure=None, socket_path=None, api_path=None, version='current', sslverify=True, credentials=None, ssh=None, shell=None, executor=None, path=None, autologin=True, logger=None)
Bases: HostConfig
Parsed, credential-safe connection settings for a TrueNAS middleware host.
Source code in src/pytruenas/host.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
api_path = api_path
instance-attribute
autologin = autologin
instance-attribute
connection_uri
property
The canonical, credential-free URI for this configuration.
credentials = credentials if isinstance(credentials, _auth.Credentials) else _auth.Credentials(credentials)
instance-attribute
executors = _as_names(executor)
instance-attribute
host = host
instance-attribute
is_local
property
Whether this target is the local middleware unix socket.
logger = logger
instance-attribute
name
property
A short label for this target: the hostname, not a whole URI.
This is what belongs in a log prefix or a progress line. The scheme,
port, API path and userinfo that :attr:connection_uri carries are
noise once every record on the line repeats them -- and on a fan-out
across ten hosts, the one thing a reader needs is which machine.
localhost for the local middleware socket; the bare hostname
otherwise, with the port appended only when it is non-default (a
:8443 is a real distinguisher between two entries for one host,
where :443 just repeats the scheme).
needs_path_probe
property
Whether the API path still has to be resolved against the server.
needs_scheme_probe
property
Whether ws-vs-wss still has to be resolved against the server.
paths = _as_names(path)
instance-attribute
port = int(port or 0)
instance-attribute
secure = secure
instance-attribute
socket_path = socket_path
instance-attribute
ssh = ssh if ssh is not None else _ssh_config_from(shell)
instance-attribute
sslverify = sslverify
instance-attribute
uri_credentials = ('password', 'otp', 'api_key', 'token', 'credentials', 'sslverify', 'ssh', 'version', 'shell', 'executor', 'path', 'autologin', 'logger')
class-attribute
instance-attribute
version = version
instance-attribute
__repr__()
Source code in src/pytruenas/host.py
541 542 543 544 | |
from_target(target=None, **options)
classmethod
Build a config from any connection string TrueNASClient accepts.
This is pytruenas' entry point: it normalizes the string first (so bare
wss:// and friends work) and then hands it to hostctl's registry,
which strips and parses any credentials in the userinfo.
Source code in src/pytruenas/host.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
TrueNASHost(config=None, credentials=None, *, client=None, **options)
Bases: PosixHost, Generic[ApiVersion]
A TrueNAS middleware host: POSIX semantics over composed transports.
Everything generic -- run, path, spawn, info, connect,
close, shell, capabilities, last_selection -- is inherited
from :class:hostctl.host.PosixHost, which selects between the providers
assembled below. What this class adds is only the part no other host has:
the middleware JSON-RPC websocket and the API surface built on it.
Construct it from a connection string, a :class:TrueNASConfig, or nothing
at all (the local middleware socket)::
TrueNASHost("wss://nas")
TrueNASHost("nas", credentials="1-...", executor=["ssh"])
TrueNASHost(TrueNASConfig.from_target("wss://nas"))
TrueNASHost()
A string accepts every form :class:~pytruenas.TrueNASClient does and takes
the same keyword options as :meth:TrueNASConfig.from_target.
Provider order depends on the target, and is overridable per selector with
executor=/path=:
- local -- hostctl's stock
localpair, and nothing else. Reaching this same machine over SSH, a PTY, or the filesystem API would be slower and strictly less capable. - remote --
sshthenwebshellfor commands,sftpthentnaswsfor paths. SSH leads on capability; the websocket legs follow. This reproduces :class:~pytruenas.fs.truenas.TruenasPath's hand-rolled fallback through hostctl's selector, which additionally records a redacted trace of what was tried (host.last_selection).
Source code in src/pytruenas/host.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
api
cached
property
The root API namespace (host.api.<namespace>.<method>(...)).
Parameterise the host to type it: TrueNASHost[Current]("nas").api
completes exactly as the old TrueNASClient[Current] did.
client
property
Deprecated alias for self.
The host is the client -- they were two objects forwarding halves of
their surface to each other, which is now one class. Kept so
host.client.api and similar keep working.
config_type = TrueNASConfig
class-attribute
instance-attribute
conn
property
The live JSON-RPC connection; opens on first access.
Logs in first when autologin is set (the default) and there is no
live connection. Reconnects if the previous one closed.
logger = _resolve_logger(config.logger, config.name)
instance-attribute
name
property
This host's short label -- see :attr:TrueNASConfig.name.
ssh
property
The underlying asyncssh connection (requires the ssh extra).
For the rare caller that needs the raw connection -- port forwarding,
an SFTP client of its own. Ordinary command and path work should go
through :meth:run and :meth:path, which pick a transport rather
than assuming this one exists.
websocket
property
Former name of :attr:conn, kept because it is public API.
Defined as a property that reads self.conn rather than as
websocket = conn: the latter makes two independent class
attributes, so patching or overriding one would silently leave the
other pointing at the original.
close()
Close the transports, then the websocket.
Order matters: the tnasws path provider talks over the websocket,
so it must be torn down before the connection it depends on.
Source code in src/pytruenas/host.py
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 | |
download(method, *args, filename=None, buffered=False, wait=True, **kwargs)
Call method for a download link and fetch it over HTTP(S).
Source code in src/pytruenas/host.py
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | |
dump_api()
Run middlewared --dump-api on the target and parse the JSON.
Source code in src/pytruenas/host.py
987 988 989 990 991 992 993 994 995 996 | |
install_sshcreds(name=None, private_key=None)
Provision an SSH keypair and wire it into this host's SSH transport.
Generates (or reuses) a SSH_KEY_PAIR keychain credential, installs
the public half on root's authorized_keys, and stores the private
half on :attr:TrueNASConfig.ssh as a real
:class:hostctl.host.SshConfig -- with client_keys as an actual
field rather than the "client_keys|root" string that the pre-hostctl
client packed into a username.
Adding an SSH transport changes what this host can do, so the providers are rebuilt: a host that had no executor at all (remote, no SSH) gains one, and paths gain the richer SFTP leg.
Source code in src/pytruenas/host.py
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | |
login(creds=None, *, login_ex=False, login_options=None, otp_provider=None)
Open a fresh connection and authenticate.
By default uses the legacy auth.login/login_with_* path. Pass
login_ex=True for the modern mechanism, which supports 2FA via an
OTP_REQUIRED continuation: the OTP comes from the credential's own
otp_token if set, else from otp_provider(). A credential with no
login_ex form (e.g. local-socket auth) falls back automatically.
Source code in src/pytruenas/host.py
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |
logout()
End the current session (auth.logout).
Source code in src/pytruenas/host.py
905 906 907 | |
me()
The current session's authenticated user (auth.me).
Source code in src/pytruenas/host.py
901 902 903 | |
ping()
Round-trip the middleware (core.ping -> "pong").
Source code in src/pytruenas/host.py
909 910 911 | |
subscribe(event, callback=None, *, maxsize=_connection.DEFAULT_EVENT_QUEUE_SIZE)
Subscribe to a middleware event; return a Subscription.
host.subscribe("alert.list") is shorthand for
host.api.alert.list.subscribe(). A subscription is bound to the
current websocket and does not survive a reconnect -- the
events() iterator ending is that signal.
Source code in src/pytruenas/host.py
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 | |
upload(file, method, *params, token=None, wait=True, **kwargs)
Upload file via /_upload, then call method with it.
Source code in src/pytruenas/host.py
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | |
Providers
pytruenas.providers
hostctl providers for a TrueNAS middleware target.
Only one adapter is TrueNAS-specific: :class:TnasWsPathProvider, paths
served by the middleware filesystem.* API. It is deliberately thin -- it
owns connection behaviour only, and leaves operating-system semantics to
:class:hostctl.host.PosixHost.
Local execution needs no adapter at all: :func:local_providers returns
hostctl's stock pair. A target reached over the middleware unix socket is
this machine, so a command there is a plain subprocess call and a path is a
plain local path -- there is nothing about TrueNAS to add.
On ordering (see TrueNASHost._build_providers):
local -> ssh -> webshell, and local -> sftp -> tnasws.
Local comes first because reaching this machine through SSH, a PTY, or the
filesystem API would be slower and strictly less capable; it is only built when
the target is local, and everything after it is a way of reaching a machine
somewhere else. Among the remote options SSH wins on capability, which also
reproduces :class:~pytruenas.fs.truenas.TruenasPath's hand-rolled "try SFTP,
fall back to the websocket" behaviour -- now through hostctl's selector, which
additionally records a redacted trace of what was tried and why
(host.last_selection).
On honesty. probe() reports what a transport can actually do rather
than what would be convenient. The JSON-RPC endpoint is not a general command
channel: it exposes filesystem.* and friends, not arbitrary exec (verified
on 26.0.0-BETA.1 -- of 781 methods only core.resize_shell and
user.shell_choices are shell-adjacent, and the former merely resizes an
already-open session). That limitation belongs to the JSON-RPC endpoint, not to
middlewared as a whole: it also serves the PTY behind
/websocket/shell, which :mod:pytruenas.webshell drives to give a
remote host without SSH a real command channel.
WS_PATH_CAPABILITIES = frozenset(('stat', 'scandir', 'open', 'open_read', 'open_write', 'read', 'write', 'exists', 'is_file', 'is_dir', 'mkdir', 'chmod', 'unlink', 'rmdir'))
module-attribute
__all__ = ['TnasWsPathProvider', 'WS_PATH_CAPABILITIES', 'local_providers']
module-attribute
TnasWsPathProvider(client)
Bases: PathProvider
Paths served by the middleware filesystem.* websocket API.
Always available for a connected client -- the middleware socket is the client's own connection, so there is no separate transport to fail. It is ordered after SFTP because its operation surface is narrower, not because it is less reliable.
Source code in src/pytruenas/providers.py
87 88 89 90 91 92 93 | |
client = client
instance-attribute
probe()
Source code in src/pytruenas/providers.py
115 116 | |
local_providers()
hostctl's stock local executor and path providers.
A target reached over the middleware unix socket is this machine, so a
command there is a plain subprocess call and a path is a plain local
path. hostctl already provides both, and this is the same one-liner its own
system.py:_local_provider uses -- there is nothing TrueNAS-specific to
add, so pytruenas does not define a provider class for it.
(There was one. It wrapped LocalExecutor behind an is_local guard
and called itself MiddlewareExecutorProvider, which was doubly
misleading: nothing about the dispatch involved middlewared, and the
guard only duplicated the decision the caller had already made by choosing
to build it.)
Source code in src/pytruenas/providers.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
Web shell
pytruenas.webshell
Command execution over the TrueNAS web-shell endpoint.
Reached at /websocket/shell (nginx), which proxies to /_shell on the
middleware's own port -- see :data:WEBSHELL_PATH for why that distinction
costs an afternoon if you get it wrong.
This is the executor for a host reachable on the API port but not on 22 --
NAT without a forwarded SSH port, a firewall allowing only 443, an appliance
behind a reverse proxy. Such a host otherwise has no run() at all: the
JSON-RPC API exposes no remote command execution (of 781 methods on
26.0.0-BETA.1 only core.resize_shell and user.shell_choices are
shell-adjacent, and the former only resizes an already-open session).
middlewared serves /_shell as a separate websocket app beside the RPC
socket -- a sibling of /_upload and /_download, which pytruenas already
uses. It is what the web UI's Shell page drives, so a command channel
demonstrably exists wherever the API does. nginx proxies it on 443 with a 7-day
timeout, so it reaches exactly the hosts SSH cannot.
Protocol (verified against 26.0.0-BETA.1, 18/18 live):
- Open a websocket to
/websocket/shell. - Receive
{"msg": "connected", "id": "<uuid>"}. - Send one JSON frame
{"token": ..., "options": {}}. The token comes fromauth.generate_token; the server validates it viaauth.get_token_for_shell_application, which requires a token with no attributes and a user holding theweb_shellprivilege. - Every frame after that is raw PTY bytes, both directions.
Server-side it is os.forkpty() + os.execve("/usr/bin/login", ...), so
this is a real login shell -- not a request/response API.
Input must be sent as BINARY frames. The handler queues msg.data
verbatim and the writer thread calls os.write(master_fd, ...), which
requires bytes. A text frame delivers a str, os.write raises, the
worker thread dies without closing the pty, and the connection resets with no
error message. This is the single least obvious thing about the endpoint.
Known limits, all deliberate and declared rather than papered over:
- stdout and stderr are one stream -- a PTY has no separate error channel,
so
capture_output="stderr"cannot be honoured. - No exit-status channel -- the return code is recovered by appending a
sentinel (
printf "__END__%s\n" "$?") and reading until it appears. - No raw multi-line input -- an embedded newline submits a partial line to the PTY and desynchronises every later read. Pipes and here-strings work because they are ordinary single-line shell syntax.
- A command that exits the shell (
exit 3) ends the session; the next call reconnects.
Because of those, this provider is ordered after SSH. It is a real executor for ordinary commands, not a degraded fallback -- but SSH's clean separate channels are better when available.
DEFAULT_TIMEOUT = 120.0
module-attribute
WEBSHELL_PATH = '/websocket/shell'
module-attribute
__all__ = ['WebShellExecutorProvider', 'WebShellSession', 'clean_output']
module-attribute
WebShellExecutorProvider(client)
Bases: ExecutorProvider
Executor for hosts reachable on the API port but not over SSH.
Source code in src/pytruenas/webshell.py
266 267 268 269 270 271 | |
client = client
instance-attribute
session
property
close()
Source code in src/pytruenas/webshell.py
310 311 312 313 | |
connect()
Source code in src/pytruenas/webshell.py
300 301 302 303 304 305 306 307 308 | |
probe()
Report availability without dispatching a command.
A local target has the unix socket and does not need this. A user
without the web_shell privilege would be rejected at the handshake,
so decline up front rather than fail mid-command.
Source code in src/pytruenas/webshell.py
286 287 288 289 290 291 292 293 294 295 296 297 298 | |
WebShellSession(client, *, options=None)
One authenticated /_shell websocket running a login shell.
Source code in src/pytruenas/webshell.py
135 136 137 138 139 140 | |
client = client
instance-attribute
options = options or {}
instance-attribute
shell_id = None
instance-attribute
close()
Source code in src/pytruenas/webshell.py
193 194 195 196 197 198 199 200 201 | |
connect()
Open and authenticate the session; idempotent.
Source code in src/pytruenas/webshell.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
run_script(script, *, timeout=None)
Run one shell script; return its cleaned output and exit status.
script must be a single line -- an embedded newline submits a
partial line to the PTY and desynchronises every later read.
Source code in src/pytruenas/webshell.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
clean_output(data)
Strip escape sequences, prompts, and CRs from raw PTY bytes.
What comes off a PTY is a terminal rendering, not a clean stream: cursor moves, line redraws, and the shell's own prompt are interleaved with the command's output. This removes the rendering so a caller sees what the command actually printed.
Source code in src/pytruenas/webshell.py
118 119 120 121 122 123 124 125 126 127 128 129 | |