https://github.com/sb0y/pysysdc
Python3.5+ sd-bus adapter which allows to manage services, publish and call methods
https://github.com/sb0y/pysysdc
ipc python sd-bus systemd systemd-service systemd-unit
Last synced: 2 months ago
JSON representation
Python3.5+ sd-bus adapter which allows to manage services, publish and call methods
- Host: GitHub
- URL: https://github.com/sb0y/pysysdc
- Owner: sb0y
- License: mit
- Created: 2023-01-07T08:23:55.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-26T20:22:12.000Z (almost 2 years ago)
- Last Synced: 2025-03-07T04:20:46.682Z (3 months ago)
- Topics: ipc, python, sd-bus, systemd, systemd-service, systemd-unit
- Language: C
- Homepage: https://bagrintsev.me
- Size: 130 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pysysdc [](https://app.travis-ci.com/sb0y/pysysdc) 
Python3.5+ sd-bus adapter which allows to manage services, publish and call methods.
Tested on Ubuntu 16.04 and Ubuntu 20.04.
## Idea
If you are orchestrating a large number of services on a Linux system, you have to run the command `service` or `systemctl` quite frequently.
If you want to do this programmatically, you have to start the process in most cases with a shell (to parse `systemctl` commands arguments) which sometimes may lead you to arguments symbols escaping.What will do the `systemctl restart nginx.service` command in the end? This command will send message over SD-Bus to systemd daemon (PID 1) which will restart `nginx` service.
What if it is possible to send the message to systemd through the SD-Bus directly from Python? What if the same mechanism can be used for IPC?## How to compile
```
sudo apt update
sudo apt install python3-setuptools python3-stdeb python3-all-dev libsystemd-dev
python3 ./setup.py build
```## How to build DEB package
Just run `build.sh` and make sure you installed `fakeroot` package.
```
sudo apt update
sudo apt install fakeroot python3-setuptools python3-stdeb python3-all-dev libsystemd-dev
./build.sh
```## How to use
For example you can reload `nginx` service in the following way:
```python
from pysysdc.unit import UnitUnit("nginx").reload()
```All supported unit methods can be found [here](https://github.com/sb0y/pysysdc/blob/main/pysysdc/unit.py#L32).
It's also possible to set your hostname through this library without any subprocess spawn, configuration editing and etc:
```python3
from pysysdc.methods import MethodsMethods.set_pretty_hostname("myhostname")
```
or reboot your machine
```python3
from pysysdc.methods import MethodsMethods.machine_reboot()
```
see other implemented systemd methods [here](https://github.com/sb0y/pysysdc/blob/ffa3b75bb782efdb167980a3a52b4ae4ec96ca0d/pysysdc/methods.py#L16).Also you can publish method in you service.
```python
from pysysdc.sdbus import SDBusdata = "example data string"
def handler():
return True, datasd = SDBus(path="/dc/cloud/auth", if_name="dc.cloud.auth", method_name="Token", method_args="s", output_sig="s", method_return="s")
sd.set_func(handler)
sd.listen()
```and call it from another program
```python
from pysysdc.sdbus import SDBussd = SDBus(
path="/dc/cloud/auth",
if_name="dc.cloud.auth",
method_name="Token",
method_args="s",
method_return="s",
output_sig="s")
ret = sd.send(method_name="Token")
print(ret)
```