python的systemd绑定

pystemd的Python项目详细描述


Pystemd

Build StatusCode style: black

这个库允许您在没有 实际上认为您正在通过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.notifydocs形式的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-devsystemd-devel你至少要有V221。 请注意,CentOS 7带有219版。为了解决这个问题,请阅读 this
  • systemd库:检查pkg-config --cflags --libs libsystemd是否返回 -lsystemd如果没有,可以正常安装systemd-libslibsystemd根据您的发行版,版本必须至少为 第221页
  • 或者setup.py将接受的任何编译器。

如果要从源安装,则在克隆此repo后,需要

$ pip install -r requirements.txt # get six
$ python3 setup.py install # only python3 supported

但除了先前的要求外,您还需要:

了解更多

这个项目已经在许多会议上讨论过:

还开发了Vagrant-based demo 为Pycon 2018。

许可证

pystemd是根据LGPL 2.1或更高版本授权的

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java以编程方式从TableLayout中删除所有行   Java Web Sart Ubuntu12.1。加载资源(图像)   人们如何让Java SPNEGO客户端在Windows中工作?   java Eclipselink:ObjectArrayMapping和ClassDescriptor   控制台中带有引号/空格的java Javafx命名参数   java创建服务失败:找不到对象?   java Hazelcast:如何在测试期间禁用自动发现?   使用TuckyUrlRewriteFilter rermoteaddr的java块IP   javascript如何在angular 4中进行同步http调用   输入字符串的java排列   java Jenkins不是以Xrs Xmx2048m XX:MaxPermSize=512m(windows)开始的   AWS EMR上的java avro错误   java从两个ArrayList中提取特定连接的对象,组合这些相关对象并从子类中打印   java从主题中删除什么时间点消息?   谷歌像素设备上的java InvalidKeyException   java如何在执行测试后重建项目   java Android 2.2 readUTF()socket问题   按下安卓设备上的“后退”按钮后java Toast没有完成?