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
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
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.
sslverify
property
TLS verification for this host -- see :attr:TrueNASConfig.sslverify.
Read from the config rather than stored, so every transport -- the JSON-RPC connection, the REST calls, and the web shell -- answers from the one value the caller set, with no second copy to drift.
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
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 | |
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
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 | |
dump_api()
Run middlewared --dump-api on the target and parse the JSON.
Source code in src/pytruenas/host.py
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 | |
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.
Does not require the optional ssh extra: provisioning runs
entirely over the middleware API and opens no SSH connection. The one
exception is passing private_key= for a key the host does not
already know, where the public half has to be derived locally (see
:func:_public_key). Using the SSH transport this configures still
needs the extra, as it always did.
Source code in src/pytruenas/host.py
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 | |
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
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 | |
logout()
End the current session (auth.logout).
Source code in src/pytruenas/host.py
989 990 991 | |
me()
The current session's authenticated user (auth.me).
Source code in src/pytruenas/host.py
985 986 987 | |
ping()
Round-trip the middleware (core.ping -> "pong").
Source code in src/pytruenas/host.py
993 994 995 | |
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
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 | |
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
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 | |