fester/backend/executor/adapters.py

52 lines
1.8 KiB
Python
Executable File

"""Runtime executor adapters — subprocess wrappers for each supported backend.
Supports:
- host: run locally via subprocess (default)
- lxc: run inside an LXC container via ``lxc exec``
- libvirt: run inside a libvirt VM via ``virsh domexec``
- tmux: run in a detached tmux session
- podman: run inside a rootless Podman container
- firecracker: run inside a Firecracker microVM via SSH
"""
import subprocess
def run_host(command: str, cwd: str, env: dict) -> int:
"""Execute a command on the host."""
return subprocess.run(command, shell=True, cwd=cwd, env=env).returncode
def run_lxc(container: str, command: str) -> int:
"""Execute a command inside an LXC container."""
cmd = f"lxc exec {container} -- bash -lc '{command}'"
return subprocess.run(cmd, shell=True).returncode
def run_libvirt(vm_name: str, command: str) -> int:
"""Execute a command inside a libvirt VM via qemu-agent."""
cmd = f"virsh domexec {vm_name} -- bash -c '{command}'"
return subprocess.run(cmd, shell=True).returncode
def run_podman(container: str, command: str, cwd: str = None) -> int:
"""Execute a command inside a Podman container."""
cmd = f"podman exec"
if cwd:
cmd += f" --workdir {cwd}"
cmd += f" {container} bash -c '{command}'"
return subprocess.run(cmd, shell=True).returncode
def run_firecracker(host: str, ssh_key: str, ssh_port: int,
command: str, cwd: str = None) -> int:
"""Execute a command inside a Firecracker microVM via SSH."""
cmd = (
f"ssh -i {ssh_key} -p {ssh_port} "
f"-o StrictHostKeyChecking=no "
f"-o UserKnownHostsFile=/dev/null "
f"-o ConnectTimeout=5 "
f"root@{host} "
f"'cd {cwd or '/root'} && {command}'"
)
return subprocess.run(cmd, shell=True).returncode