python的systemd绑定
pystemd的Python项目详细描述
Pystemd
这个库允许您在没有
实际上认为您正在通过dbus与systemd对话。这样你就可以
以编程方式启动/停止/重新启动/终止并验证服务状态
系统的观点,避免执行subprocess.Popen(['systemctl', ...
然后分析输出以了解结果。
显示不告诉
在软件方面,就像在屏幕上一样,最好是展示事物是如何工作的,而不是 告诉我。因此,这就是如何使用交互式shell中的库。
In [1]: from pystemd.systemd1 import Unit
In [2]: unit = Unit(b'postfix.service')
In [3]: unit.load()
注意:您需要调用unit.load()
,因为默认情况下Unit
不会加载
单位信息,因为这将需要做一些IO你可以通过
Unit(b'postfix.service', _autoload=True)
一旦加载了单元,就可以与它交互,可以通过访问它的 systemd的接口:
In [4]: unit.Unit.ActiveState
Out[4]: b'active'
In [5]: unit.Unit.StopWhenUnneeded
Out[5]: False
In [6]: unit.Unit.Stop(b'replace') # require privilege account
Out[6]: b'/org/freedesktop/systemd1/job/6601531'
In [7]: unit.Unit.ActiveState
Out[7]: b'inactive'
In [8]: unit.Unit.Start(b'replace') # require privilege account
Out[8]: b'/org/freedesktop/systemd1/job/6601532'
In [9]: unit.Unit.ActiveState
Out[9]: b'active'
In [10]: unit.Service.GetProcesses()
Out[10]:
[(b'/system.slice/postfix.service',
1754222,
b'/usr/libexec/postfix/master -w'),
(b'/system.slice/postfix.service', 1754224, b'pickup -l -t fifo -u'),
(b'/system.slice/postfix.service', 1754225, b'qmgr -l -t fifo -u')]
In [11]: unit.Service.MainPID
Out[11]: 1754222
systemd1.Unit
类为systemd中的接口提供快捷方式
命名空间,如上所述,我们有服务(org.freedesktop.systemd1.Service)
和单位(org.freedesktop.systemd1.unit)。其他的可以在
unit._interfaces
作为:
In [12]: unit._interfaces
Out[12]:
{'org.freedesktop.DBus.Introspectable': <org.freedesktop.DBus.Introspectable of /org/freedesktop/systemd1/unit/postfix_2eservice>,
'org.freedesktop.DBus.Peer': <org.freedesktop.DBus.Peer of /org/freedesktop/systemd1/unit/postfix_2eservice>,
'org.freedesktop.DBus.Properties': <org.freedesktop.DBus.Properties of /org/freedesktop/systemd1/unit/postfix_2eservice>,
'org.freedesktop.systemd1.Service': <org.freedesktop.systemd1.Service of /org/freedesktop/systemd1/unit/postfix_2eservice>,
'org.freedesktop.systemd1.Unit': <org.freedesktop.systemd1.Unit of /org/freedesktop/systemd1/unit/postfix_2eservice>}
In [13]: unit.Service
Out[13]: <org.freedesktop.systemd1.Service of /org/freedesktop/systemd1/unit/postfix_2eservice>
每个接口都有方法和属性,可以直接访问
unit.Service.MainPID
,属性和方法的列表在.properties
以及每个接口的.methods
。
除了systemd1.Unit
,我们还有一个systemd1.Manager
,它允许
您要与Systemd Manager交互。
In [14]: from pystemd.systemd1 import Manager
In [15]: manager = Manager()
In [16]: manager.load()
In [17]: manager.Manager.ListUnitFiles()
Out[17]:
...
(b'/usr/lib/systemd/system/rhel-domainname.service', b'disabled'),
(b'/usr/lib/systemd/system/fstrim.timer', b'disabled'),
(b'/usr/lib/systemd/system/getty.target', b'static'),
(b'/usr/lib/systemd/system/systemd-user-sessions.service', b'static'),
...
In [18]: manager.Manager.Architecture
Out[18]: b'x86-64'
In [19]: manager.Manager.Virtualization
Out[19]: b'kvm'
额外费用:
我们还包括pystemd.run
,systemd run的精神端口
对Python来说。example of usage:
# run this as root>>>importpystemd.run,sys>>>pystemd.run([b'/usr/bin/psql',b'postgres'],machine=b'db1',user=b'postgres',wait=True,pty=True,stdin=sys.stdin,stdout=sys.stdout,env={b'PGTZ':b'UTC'})
将在本地nspawn计算机中打开postgres交互提示。
您还将获得一个以pystemd.daemon.notify
docs形式的sd_notify
接口。
# run this as root>>>importpystemd.daemon>>>pystemd.daemon.notify(False,ready=1,status='Gimme! Gimme! Gimme!')
以及访问套接字激活脚本的侦听文件描述符。
# run this as root>>>importpystemd.daemon>>>pystemd.daemon.LISTEN_FDS_START3>>>pystemd.daemon.listen_fds()1# you normally only open 1 socket
如果已启用监视程序并ping它,则访问。
importtimeimportpystemd.daemonwatchdog_usec=pystemd.daemon.watchdog_enabled()watchdog_sec=watchdog_usec/10**6ifnotwatchdog_usec:print(f'watchdog was not enabled!')foriinrange(20):pystemd.daemon.notify(False,watchdog=1,status=f'count {i+1}')time.sleep(watchdog_sec*0.5)print('sleeping for 30 seconds')time.sleep(watchdog_sec*2)print('you will never reach me in a watchdog env')
安装
因此,您喜欢看到的内容,最简单的安装方法是:
$ pip install pystemd
您需要有:
- python头:只需使用发行版的包(例如python dev)。
- 系统标题:可能你已经有了这个。通常,它被称为
libsystemd-dev
或systemd-devel
你至少要有V221。 请注意,CentOS 7带有219版。为了解决这个问题,请阅读 this。 - systemd库:检查
pkg-config --cflags --libs libsystemd
是否返回-lsystemd
如果没有,可以正常安装systemd-libs
或libsystemd
根据您的发行版,版本必须至少为 第221页 - 或者
setup.py
将接受的任何编译器。
如果要从源安装,则在克隆此repo后,需要
$ pip install -r requirements.txt # get six $ python3 setup.py install # only python3 supported
但除了先前的要求外,您还需要:
- setuptools:只需使用发行版的包(例如python setuptools)
- 六库:用于Python2和3的兼容性(按要求安装)
- cython:至少0.21a1版,只需pip安装或使用官方的 从cython主页获取最新安装指南 http://cython.readthedocs.io/en/latest/src/quickstart/install.html。
了解更多
这个项目已经在许多会议上讨论过:
- Using systemd in high level languages在All Systems Go! 2018
- systemd: why you should care as a Python developer在PyCon 2018
- Better security for Python with systemd在Pyninsula #10
还开发了Vagrant-based demo 为Pycon 2018。
许可证
pystemd是根据LGPL 2.1或更高版本授权的