用于软件部署的机器架构师。
marchitect的Python项目详细描述
marchitect
用于将文件上载到远程主机并在远程主机上运行命令的工具。
安装
$ pip3 install marchitect
示例
假设你的机器很容易 使其成为远程机器。
frommarchitect.site_planimportSitePlan,Stepfrommarchitect.whiteprintimportWhiteprintclassHelloWorldWhiteprint(Whiteprint):name='hello_world'def_execute(self,mode:str)->None:ifmode=='install':# Write file by running remote shell commands.self.exec('echo "hello, world." > /tmp/helloworld1')# Write file by uploadingself.scp_up_from_bytes(b'hello, world.','/tmp/helloworld2')classMyMachine(SitePlan):plan=[Step(HelloWorldWhiteprint)]if__name__=='__main__':# SSH into your own machine, prompting you for your password.importgetpassimportosuser=os.getlogin()password=getpass.getpass('%s@localhost password: '%user)sp=MyMachine.from_password('localhost',22,user,password,{},[])# If you want to auth by private key, use the below:# (Note: The password prompt will be for your private key, empty for none.)#sp = MyMachine.from_private_key(# 'localhost', 22, user, '/home/%s/.ssh/id_rsa' % user, password, {}, [])sp.install()# Sets the mode of _execute() to install.
这个例子要求您可以通过密码ssh进入您的机器。使用
取而代之的是您的ssh密钥,取消注释上面的行。处决后,你应该
在您的机器上安装/tmp/helloworld1
和/tmp/helloworld2
。
希望可以清楚地看到,whiteprints允许您运行命令并将文件上载到 目标机器。白字应该包含 目的。站点计划包含所有应该在 单机类。
将代码存储库部署到计算机的步骤将有助于 白皮书。运行Web服务器的计算机的网站计划可能会使用 白字及其他。
目标
- 很容易开始。
- 配置文件的模板化。
- 命令式和陈述式的混合。
- 任意执行模式(安装、更新、清理、启动、停止等)。
- 用于验证机器状态的接口。
- 因为大多数复杂的配置都发生在 无论如何都是容器。
非目标
- 使白纸和网站计划能够与其他人和公司共享。
- 非Linux部署目标。
概念
白字
要创建一个whiteprint,扩展whiteprint
并定义一个name
类变量
以及_execute()
方法;可以选择定义validate()
方法。名称
应该是白字的合理名称。在上面的例子中,
HelloWorldWhiteprint
类的名称就是Hello\u World
。名称
是
对于下面讨论的文件分辨率很重要。
\u execute()
是所有魔术发生的地方。该方法接受一个名为
模式
。超出惯例,您的白纸应处理以下模式:
安装
(安装软件)更新
(更新软件)清理
(如果需要,请删除软件)启动
(启动服务)停止
(停止服务)。
尽管有这个约定,模式
可以是任何您将要选择的模式
以执行您的站点计划。
在\u execute()
中,您可以在
脚。使用self.exec()
在目标计算机上运行任何命令。
exec()
返回一个带有变量exit_status
(int)的对象,
stdout
(字节)和stderr
(字节)。您可以使用这些输出来控制
流动。如果退出状态为非零,则会引发一个remoteexecerror
。到
抑制异常,设置error\u ok=true
\u execute()
可以访问self.cfg,它是
白皮书。请参阅下面的模板和配置变量部分。
使用各种功能在主机之间复制文件:
scp_up()
-将文件从本地主机上载到目标主机。sp_up_from_bytes()
-从bytes arg在目标主机上创建一个文件。scp_down()
-将文件从目标下载到本地主机。scp_down_to_bytes()
-从目标下载文件并返回。
模板和配置VARS
您可以上传属于jinja2模板的文件。这个 模板将由传递给白纸的配置变量填充。 配置变量可以通过多种方式设置,我们现在将探讨这些方法。
下面是一个使用jinja2符号指定
名称变量,默认值为john doe
:
name="{{ name|default('John Doe') }}"
白纸可以按如下方式填充用于上载的模板:
frommarchitect.whiteprintimportWhiteprintclassWhiteprintExample(Whiteprint):default_cfg={'name':'Alice'}def_execute(self,mode:str)->None:ifmode=='install':self.scp_up_template('/path/to/test.toml','~/test.toml')
白纸还可以上传存储在字符串中的填充模板 而不是文件:
frommarchitect.whiteprintimportWhiteprintclassWhiteprintExample(Whiteprint):default_cfg={'name':'Alice'}def_execute(self,mode:str)->None:ifmode=='install':self.scp_up_template_from_str('name = "{{ name }}"','~/test.toml')
配置变量可以在str的scp up template中重写
frommarchitect.whiteprintimportWhiteprintclassWhiteprintExample(Whiteprint):default_cfg={'name':'Alice'}def_execute(self,mode:str)->None:ifmode=='install':# 'Bob' overrides 'Alice'self.scp_up_template_from_str('name = "{{ name }}"','~/test.toml',cfg_override={'name':'Bob'})
配置变量也可以由计划中的siteplan
设置,或者在
实例化。
frommarchitect.site_planimportStep,SitePlanclassMyMachine(SitePlan):plan=[Step(WhiteprintExample,{'name':'Eve'})]if__name__=='__main__':MyMachine.from_password(...,cfg_override={'name':'Foo'})
在上面的代码中,foo
优先于eve
优先于任何
白皮书中定义的name
的值。
最后,可以给步骤
一个别名作为另一个标识符来指定
配置变量。当网站中多次使用白字时,这很有用
计划,
frommarchitect.site_planimportStep,SitePlanclassMyMachine(SitePlan):plan=[Step(WhiteprintExample,alias="ex1"),Step(WhiteprintExample,alias="ex2"),]if__name__=='__main__':MyMachine.from_password(...,cfg_override={'ex1':'Eve','ex2':'Foo'})
在上面,第一个whiteprintexample
uploadseve
和第二个
将其替换为foo
还有一些配置变量是自动派生的,并且始终可用。
它们存储在self.cfg['''u target']
:
用户
:ssh连接的登录用户。主机
:ssh连接的目标主机。kernel
:目标主机的内核版本。例如:4.15.0-43-通用
发行版
:目标主机的Linux发行版。例如:ubuntu
disto_version
:Linux发行版的版本。例如:18.04
主机名
:目标主机的主机名。fqdn
:目标主机的完全限定域名。CPU计数:目标主机上的CPU数量。例如:
8
文件分辨率
上载本地文件的方法(scp_-up()
和scp_-up_-template()
)将
根据siteplan中的
rsrc_paths
参数搜索文件
构造器。搜索按rsrc_路径和
白色打印应该是
rsrc\u路径中的子文件夹的名称
例如,假设rsrc_paths
是[路径('/srv/rsrcs')]
,则
名称为foobar
,文件c
被引用为a/b/c
。旋转变压器
将查找是否存在/srv/rsrcs/foobar/a/b/c
如果文件路径被指定为绝对路径,例如/a/b/c
,则不会rsrc_path
前缀。但是,对于跨机器的可移植性,不鼓励使用此表单
因为资源可能位于不同计算机上的不同文件夹中。
等幂
努力使你的白印具有等幂性是很重要的。在其他 换句话说,假设您在任何模式下(安装、更新,…)都可以 在任何时候被打断。能否成功重新应用您的白字 没有任何问题?
如果是的话,你的白字是等幂的,因此对连接是有弹性的。 错误和软件故障。错误处理将与重试 有限制的次数。如果没有,你需要找出 错误处理策略。在极端情况下,可以终止 产生错误并重新开始,假设你身处云端 环境。
预制
预设是内置的等幂组件,您可以添加到您的白描。
这使得使用\u execute()
和
\u validate()
方法已定义。这些是现成的:
apt
:通用Linux包管理器。pip3
:python包管理器。文件夹存在
:确保文件夹存在于指定路径。line in file
:确保指定的行存在于指定的文件中。
例如:
frommarchitect.prefabimportAptfrommarchitect.whiteprintimportWhiteprintclassHelloWorld2Whiteprint(Whiteprint):prefabs=[Apt(['curl']),]def_execute(self,mode:str)->None:ifmode=='install':self.exec('curl https://www.nytimes.com > /tmp/nytimes')
预制在\u execute()
方法之前执行。
如果预置依赖于配置变量,则定义一个 方法:
$ pip3 install marchitect0
_compute_prefacts()
返回的预置将在这些之后执行
在预置的
类变量中指定。
现场平面图
网站计划是白纸的集合。你可能有不同的角色 基础结构中的计算机:Web主机、API主机、数据库主机… 每一个都应该映射到他们自己的站点计划,该计划将安装 适当的白色打印(postgres用于数据库主机,uwsgi用于web主机,…)。
测试
测试是针对真实的ssh连接运行的,不幸的是
很难在ci环境中运行。为此原因,未启用Travis CI。
通过py.test
或tox
运行测试时,可以指定ssh凭据
作为用户/密码对或用户/私钥。例如:
$ pip3 install marchitect1
可能会转向模仿ssh命令,但要可靠地
模拟ssh2 python的接口
mypy
和lint
也受支持:tox-e mypy,lint
待办事项
- []添加"公共"依赖项以最小化对以下命令的调用
apt update
到每个站点计划一次。 - []向目标主机写入应用的站点计划和白板的日志 便于调试。
- []为
validate()
方法添加文档。 - []使用
ssh2 python而不是
paramiko
来验证速度是否获胜- []文档
siteplan.one_off_exec()
- []文档