[systemd-devel] make pystemd part of systemd repo.

aleivag aleivag at gmail.com
Tue Aug 16 12:10:56 UTC 2022


Hello systemd developers:

We talked last week about entertaining the idea of make pystemd part of
systemd github org and eventually merge (in functionalities, not literally
merge both) with the current python-systemd, This is our proposal to do
this. happy to hear feedback :)

What do we propose to do?
-----

We want to bring pystemd[1] into the systemd organization, and merge
(functionality wise) with python-systemd[2], so that there is a single
Python library, supported by the systemd community, that interacts with the
systemd API.

Why?
-----

systemd has a Python library already under the organization:
python-systemd. This library does some interesting stuff with the journal
(my personal favorite is creating a Python logger that talks directly with
the journal, kudos to whoever did that!).

But in my honest opinion, this library is missing some core functionality
to interact with systemd: the service manager, start/stop/interaction with
Units, create ephemeral units, etc. At Meta (the company formerly known as
Facebook) we created a python library that does that… among other things.

What is pystemd?
-----

pystemd, at its core, is a Python library that allows users to talk to
systemd over dbus, without thinking that you are talking to systemd over
dbus. It does this by binding sd_bus and other sd_* libraries from C to
Python.

This enables users to interact with systemd in many ways. Most notably, but
not restricted to, start/stop/kill and checking the status of units.

It was created because Instagram (at the time) needed to expose systemd
features, like managing units, creating ephemeral units, using the notify
and watchdog interface, etc. to Python and reduce the need to “shell-out”
in order to parse results.

Example of usage:

from pystemd.systemd1 import Unit

with Unit(b'postfix.service') as unit:
   if unit.Unit.ActiveState == b"active":
       unit.Unit.Stop(b'replace')


Or

from pystemd.systemd1 import Manager

with Manager() as manager:
   print("Version", manager.Manager.Version)
   print("Architecture", manager.Manager.Architecture)

   # List Units
   for unit, state in manager.Manager.ListUnitFiles():
       print(f"    {unit} is {state}")


It does a few more things (I won't bore you with details), but my
particular favorite is an analogous feature to systemd-run called
pystemd.run. It’s similar to systemd-run in that it is a smart wrapper
around StartTransientUnit. But it's meant to be “programatically called”
instead of a CLI.

>>> import pystemd.run, time
>>> unit = pystemd.run(
    [b'/usr/bin/psql', b'postgres'],
    machine=b'db1',
    user=b'postgres',
    wait=False,
    env={b'PGTZ': b'UTC'}
)
>>> time.sleep(300)
>>> unit.Unit.Stop(b'postgres') # you lived too long, time to stop


If you want to know more, Anita Zhang and I did a talk in SCaLE [3] where
we abused pystemd.

How do we propose to do this?
-----

Preliminary plan (of course we are open to any suggestions) look like this:

1. We move pystemd repo [1] into the systemd org, something like
https://github.com/systemd/pystemd
2. People with commit access to this repo (Davide, Anita and me) would
remain having commit access to this repo, and whoever has access to the
python-systemd repo [2] would be added here.
3. We merge all features of python-systemd (that are not in pystemd yet)
into pystemd, this will probably mean re-writing most of those features.
Development would be done by mostly me, with code review and from hopefully
as close to the original authors as possible… Basically I don't want to be
disrespectful of python-systemd.
4. If we want to keep the name python-systemd (and the import namespace of
systemd) we can rename the repo as python-systemd, if we want to keep
pystemd as the name of the package and the import namespace, we can make
python-systemd as “a link” to pystemd
5. Whatever we decide on the last point, we cut the version 1.0.0 of
pystemd, or the next version of python-systemd. And we let the new era of
Python bindings start!

Best regards
Alvaro Leiva Geisse

[1] https://github.com/facebookincubator/pystemd
[2] https://github.com/systemd/python-systemd
[3] https://youtu.be/FMnvvfBYypY?t=24789
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20220816/9abe118b/attachment.htm>


More information about the systemd-devel mailing list