dataset/seed/base-machine/tests/execute.py

29 lines
1 KiB
Python
Raw Normal View History

from os import fdopen
from dbus import SystemBus, Array
2022-08-21 18:59:02 +02:00
def run(host, cmd, user=None):
bus = SystemBus()
remote_object = bus.get_object('org.freedesktop.machine1',
'/org/freedesktop/machine1',
False,
)
2022-08-21 18:59:02 +02:00
if user is not None:
cmd = ['/bin/su', '-', user, '-s', '/bin/bash', '-c', ' '.join(cmd)]
res = remote_object.OpenMachineShell(host,
'',
cmd[0],
Array(cmd, signature='s'),
Array(['TERM=dumb'], signature='s'),
dbus_interface='org.freedesktop.machine1.Manager',
)
fd = res[0].take()
fh = fdopen(fd)
while True:
try:
yield fh.readline().strip()
except OSError as err:
if err.errno != 5:
raise err from err
break