Client
TrueNASClient and TrueNASHost are the same class. "Client" is the friendlier
name at a call site; the class is documented under Host, where its
hostctl half (run, path, capabilities, last_selection) lives.
pytruenas.TrueNASClient(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 | |