系统编排框架

giga的Python项目详细描述


千兆

Giga是一个面向Python(gt;=3.6)程序员的Unix系统编排框架。在

安装

python3 -m pip install giga

特点

下面的代码片段通过 创建自动安装和卸载python包example-pkg 来自git。

  • 用于操作Unix系统的API。
^{pr2}$
  • 用于创建可重用系统配置的API。
importgigaclassExamplePkg(giga.Config):'Apply or delete the example-pkg python package.'@propertydefinstalled(self):returnself.system.zero("pip3 list|grep '^example-pkg '")# extend giga.Config.on_apply()defon_apply(self):super().on_apply()withgiga.Task('Apply example-pkg')astask:ifnotself.installed:url='git+https://github.com/mykelalvis/example_pkg'self.system.run(f'pip3 install {url}')task.change()# extend giga.Config.on_delete()defon_delete(self):super().on_delete()withgiga.Task('Delete example-pkg')astask:ifself.installed:self.system.run('yes|pip3 uninstall example-pkg')task.change()# extend giga.Config.on_is_applied()defon_is_applied(self):returnsuper().on_is_applied()andself.installed
# create a systemsystem=giga.unix.Ssh(host='albatross.local',user='jrandom',super=True)# apply ExamplePkg to the systemsystem.apply(ExamplePkg)# print ExamplePkg apply statusprint(system.is_applied(ExamplePkg))# => 'True'# delete ExamplePkg from the systemsystem.delete(ExamplePkg)# print ExamplePkg apply statusprint(system.is_applied(ExamplePkg))# => 'False'
  • 定制和组合系统配置。
# existing configurations can be customized via inheritance or instantiation.# here we use one of giga's base configurations to implement our ExamplePkg# configurationclassExamplePkg(giga.configs.python.Packages):packages=[('example-pkg','git+https://github.com/mykelalvis/example_pkg'),]example_pkg=giga.configs.python.Packages(packages=[('example-pkg','git+https://github.com/mykelalvis/example_pkg'),])
# composition assembles many configurations into one and is delegated by# `giga.Config.config_include`, which may be a sequence or a callable# returning a sequenceclassExamplePkg(giga.Config):pkg='example-pkg'url='git+https://github.com/mykelalvis/example_pkg'config_include=[giga.configs.os.debian.Packages(packages=['python3-pip']),giga.configs.python.Packages(packages=[(pkg,url)]),]classExamplePkg(giga.Config):defconfig_include(self):pkg='example-pkg'url='git+https://github.com/mykelalvis/example_pkg'return[giga.configs.os.debian.Packages(packages=['python3-pip']),giga.configs.python.Packages(packages=[(pkg,url)]),]
# the callable form of `config_include` can branch on things like os familyclassExamplePkg(giga.Config):@propertydefos_packages(self):family=self.system.os.familyiffamily=='debian':returngiga.configs.os.debian.Packages(packages=['python3-pip'])eliffamily=='redhat':returngiga.configs.os.redhat.Packages(packages=['python36u-pip'])else:raisegiga.error.NotImplementedFor(family)@propertydefpy_packages(self):pkg='example-pkg'url='git+https://github.com/mykelalvis/example_pkg'returngiga.configs.python.Packages(packages=[(pkg,url)])defconfig_include(self):return[self.os_packages,self.py_packages,]
  • Unix系统操作组的API。命令行 接口通常在幕后处理所有这些,但它确实是 从其他python代码驱动giga是非常合理的。
# a simple version using the built-in result handlerimportgigaclassExamplePkg(giga.Config):pass# see previous exampleshosts=('server1.local','server2.local','server3.local',)# create a groupgroup=giga.Group(hosts=hosts,user='jrandom',super=True)# apply ExamplePkg to the groupok,err=group.apply(ExamplePkg)group.log_results(ok,err)# print ExamplePkg apply statusok,err=group.is_applied(ExamplePkg)group.log_results(ok,err)# delete ExamplePkg from the groupok,err=group.delete(ExamplePkg)group.log_results(ok,err)
# a fuller version showing the structure of the group result lists, ok and errimportgiga,tracebackclassExamplePkg(giga.Config):pass# see previous examplesdefresults(ok,err):'Handle the results of Group.apply(), Group.delete(), or Group.is_applied().'# print tracebacks for all failed hostsforsystem,exc_infoinerr:_,exc,_=exc_info# configs can raise either giga.Cancel or giga.Failifisinstance(exc,giga.Cancel):# we should ignore these and look for the root cause, the giga.Failcontinueassert(isinstance(exc,giga.Fail))print('-',system.name)traceback.print_exception(*exc.exc_info)# the exc_info of the real errorprint()# print results for all successful hostsforsystem,resultinok:ifisinstance(result,int):# apply/delete return int, the number of changes made to the systemprint(f'- {system.name} made {result} changes')elifisinstance(result,bool):# is_applied returns bool, True if a configuration is applied else Falsestatus='applied'ifresultelse'not applied'print(f'- configuration is {status} to {system.name}')hosts=('server1.local','server2.local','server3.local',)# create a groupgroup=giga.Group(hosts=hosts,user='jrandom',super=True)# apply ExamplePkg to the groupok,err=group.apply(ExamplePkg)results(ok,err)# print ExamplePkg apply statusok,err=group.is_applied(ExamplePkg)results(ok,err)# delete ExamplePkg from the groupok,err=group.delete(ExamplePkg)results(ok,err)
  • 命令行界面。
# apply config ExamplePkg in module examplepkg to 3 hosts
giga apply examplepkg.ExamplePkg -h host1,host2,host3 -u jrandom -s

# check apply status for config ExamplePkg in module examplepkg on 3 hosts
giga is-applied examplepkg.ExamplePkg -h host1,host2,host3 -u jrandom -s

# delete config ExamplePkg in module examplepkg from 3 hosts
giga delete examplepkg.ExamplePkg -h host1,host2,host3 -u jrandom -s

# show help
giga --help

状态

Giga正在进行大量的开发和改进。但有些事情 它仍然用胶带和胶水粘合在一起,继续稳步提高,并且 它的作者每天都使用它来部署和维护企业基础设施。在

贡献

如果你有兴趣投稿,哇,我真的很震惊。:)我可以 在eckso@eckso.io联系。在

许可证

Giga是在Apache许可证版本2.0下授权的。在

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

推荐PyPI第三方库


热门话题
java消费者。如何指定要读取的分区?[卡夫卡]   网络可以创建一个Java UDPsocket来监听所有地址吗?   java如何在某一天之前到达   JAVAlang.SecurityException:需要蓝牙权限:用户10065和当前进程都没有安卓。准许蓝牙   java将字符串转换为datetime SQLServer2008   将字节码转换为dex:com时发生java错误。安卓dx。rop。科技委。CstFieldRef无法转换为com。安卓dx。rop。科技委。CstString   ajax如何将变量值从java类传递到jsp页面   不访问私钥的java加密操作(如Android密钥库)   试图保存一个或多个文件时,执行create HH000437时发生java错误   继承JAVA实现接口,方法采用并返回类型接口   soap SoapUI验证错误。无法将lang.String转换为org。阿帕奇。xmlbeans。XmlError   java无法使用Spring HATEOAS和Jersey实现HAL渲染   使用泛型的Java工厂。阶级vs。getClass()   java何时会发生“SecurityException:不正确的签名”?   java如何将JOptionPane中的文本存储到文本文件中   java双向通信(询问还是告知)?   java为什么我不能移动数组初始值设定项?