lib.ec.ssh.Exec: Support Caps.Env

Add support for modifying the execution environment via the env
parameter to Exec.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-17 17:08:56 +02:00
commit 3574c0f1bf

View file

@ -12,10 +12,14 @@ if TYPE_CHECKING:
class Exec(Base): class Exec(Base):
def __init__(self, *args, **kwargs) -> None: def __init__(self, uri, *args, **kwargs) -> None:
self.__askpass: str|None = None self.__askpass: str|None = None
self.__askpass_orig: dict[str, str|None] = dict() self.__askpass_orig: dict[str, str|None] = dict()
super().__init__(*args, **kwargs) super().__init__(
uri = uri,
caps = self.Caps.Env,
**kwargs
)
def __del__(self): def __del__(self):
for key, val in self.__askpass_orig.items(): for key, val in self.__askpass_orig.items():
@ -39,8 +43,21 @@ class Exec(Base):
self.__askpass_orig[key] = os.getenv(key) self.__askpass_orig[key] = os.getenv(key)
os.environ[key] = val os.environ[key] = val
async def _run_ssh(self, cmd: list[str], cmd_input: bytes|None, *args, **kwargs) -> Result: async def _run_ssh(
self,
cmd: list[str],
wd: str|None,
verbose: bool,
cmd_input: bytes|None,
env: dict[str, str]|None,
interactive: bool,
log_prefix: str
) -> Result:
self.__init_askpass() self.__init_askpass()
if cmd_input is None: if cmd_input is None:
cmd_input = InputMode.Interactive if self.interactive else InputMode.NonInteractive cmd_input = InputMode.Interactive if self.interactive else InputMode.NonInteractive
return await run_cmd(['ssh', self.hostname, join_cmd(cmd)], cmd_input=cmd_input, throw=False) opts: dict[str, str] = []
if env:
for key, val in env.items():
opts.extend(['-o', f'SetEnv {key}="{val}"'])
return await run_cmd(['ssh', *opts, self.hostname, join_cmd(cmd)], cmd_input=cmd_input, throw=False)