Skip to content

Commit

Permalink
fixing DbusRemote
Browse files Browse the repository at this point in the history
there was a typo where self.remote should have been self.host ... I also added an example (also reformat run.py)
  • Loading branch information
aleivag committed Jun 21, 2024
1 parent a5281ac commit f79a8ae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
34 changes: 34 additions & 0 deletions examples/connect_to_remote_bus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

from pystemd.systemd1 import Unit
from pystemd.dbuslib import DBusRemote


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()
13 changes: 11 additions & 2 deletions pystemd/dbuslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,24 @@ 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

def __init__(self, char* host):
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"
Expand Down
12 changes: 6 additions & 6 deletions pystemd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
}
)

Expand Down

0 comments on commit f79a8ae

Please sign in to comment.