Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing DbusRemote #93

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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()
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
Loading