From bbbf16d14833ce928027c7f24a4ae052caac2909 Mon Sep 17 00:00:00 2001 From: Alvaro Leiva Geisse Date: Thu, 20 Jun 2024 22:09:01 -0700 Subject: [PATCH] fixing DbusRemote there was a typo where self.remote should have been self.host ... I also added an example (also reformat run.py) --- examples/connect_to_remote_bus.py | 34 +++++++++++++++++++++++++++++++ pystemd/dbuslib.pyx | 13 ++++++++++-- pystemd/run.py | 12 +++++------ 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 examples/connect_to_remote_bus.py diff --git a/examples/connect_to_remote_bus.py b/examples/connect_to_remote_bus.py new file mode 100644 index 0000000..bbd8c62 --- /dev/null +++ b/examples/connect_to_remote_bus.py @@ -0,0 +1,34 @@ +import os + +from pystemd.dbuslib import DBusRemote +from pystemd.systemd1 import Unit + + +def full_example(): + # DBusRemote will use ssh to connect to another bus, so whatever a valid ssh conection + # string is, we can use it, you could also use DBusAddress, but thats harder + with DBusRemote(b"localhost") as bus, Unit(b"postfix.service", bus=bus) as sd_unit: + print("ConditionTimestamp", sd_unit.Unit.ConditionTimestamp) + print("StopWhenUnneeded", sd_unit.Unit.StopWhenUnneeded) + print("StartLimitAction", sd_unit.Unit.StartLimitAction) + print("StartLimitBurst", sd_unit.Unit.StartLimitBurst) + print("StartupBlockIOWeight", sd_unit.Service.StartupBlockIOWeight) + print("SyslogPriority", sd_unit.Service.SyslogPriority) + print("SyslogFacility", sd_unit.Service.SyslogFacility) + print("SyslogLevelPrefix", sd_unit.Service.SyslogLevelPrefix) + print("After", sd_unit.Unit.After) + print("Conditions", sd_unit.Unit.Conditions) + print("Job", sd_unit.Unit.Job) + print("InvocationID", sd_unit.Unit.InvocationID) + print("ExecStart", sd_unit.Service.ExecStart) + + # next one require sudo powers! + if os.geteuid() == 0: + print(".GetProcesses", sd_unit.Service.GetProcesses()) + print(".Start(b'replace')", sd_unit.Unit.Start(b"replace")) + else: + print("no root user, no complex method for you!") + + +if __name__ == "__main__": + full_example() diff --git a/pystemd/dbuslib.pyx b/pystemd/dbuslib.pyx index e88f58f..a0dd89b 100644 --- a/pystemd/dbuslib.pyx +++ b/pystemd/dbuslib.pyx @@ -566,7 +566,16 @@ cdef class DBusMachine(DBus): cdef class DBusRemote(DBus): - "DBus class that connects to a remote host" + """ + DBus class that connects to a remote host, this is using ssh, + this uses [sd_bus_open_system_remote](https://manpages.debian.org/testing/libsystemd-dev/sd_bus_open_system_remote.3.en.html) + that will: + + connects to the system bus on the specified host using ssh(1). host consists of an optional user name followed by the "@" symbol, + and the hostname, optionally followed by a ":" and a port, optionally followed by a "/" and a machine name. If the machine name is + given, a connection is created to the system bus in the specified container on the remote machine, and otherwise a connection to + the system bus on the specified host is created. + """ cdef char* host @@ -574,7 +583,7 @@ cdef class DBusRemote(DBus): self.host = host cdef int open_dbus_bus(self): - return dbusc.sd_bus_open_system_remote(&(self.bus), self.remote) + return dbusc.sd_bus_open_system_remote(&(self.bus), self.host) cdef class DBusAddress(DBus): "DBus class that connects to custom address" diff --git a/pystemd/run.py b/pystemd/run.py index 6ca76e2..7b86b54 100644 --- a/pystemd/run.py +++ b/pystemd/run.py @@ -210,12 +210,12 @@ def bus_factory(): unit_properties.update( { b"StandardInputFileDescriptor": get_fno(stdin) if stdin else stdin, - b"StandardOutputFileDescriptor": get_fno(stdout) - if stdout - else stdout, - b"StandardErrorFileDescriptor": get_fno(stderr) - if stderr - else stderr, + b"StandardOutputFileDescriptor": ( + get_fno(stdout) if stdout else stdout + ), + b"StandardErrorFileDescriptor": ( + get_fno(stderr) if stderr else stderr + ), } )