py-运行hashicorp packer cli命令的python库

packer.p的Python项目详细描述


https://img.shields.io/pypi/v/packer.py.svghttps://travis-ci.org/mayn/packer.py.svg?branch=masterhttps://ci.appveyor.com/api/projects/status/n1627wlm52q12db8/branch/master?svg=truehttps://coveralls.io/repos/github/mayn/packer.py/badge.svg?branch=master

关于

py-python库,用于与hashicorppackercli可执行文件交互。

项目遵循semantic versioning,v0.x.x api应该被认为是不稳定的,api会经常变化,请相应地计划。

安装

packer.py可以通过pip安装:

$ pip install packer.py

配置

packer可执行文件

指定封隔器位置

使用packer.py需要访问hashicorppacker可执行文件。 生成命令时,如果未指定位置,则使用以下默认值。

*nix:/usr/local/packer
macOS:/usr/local/packer
windows:packer.exe

可以通过显式地为您的环境设置打包程序的位置来覆盖这些默认值。

PackerExecutable(executable_path="/usr/local/bin/packer")

PackerExecutable("/usr/local/bin/packer")

机器可读输出

默认情况下,命令在packer的machine readable format启用的情况下执行。 这可以通过设置禁用

PackerExecutable(machine_readable=False)

运行命令

当前支持以下命令:

  • 构建
  • 检查
  • 验证
  • 版本

下面是packer.py等价于运行这些packer CLI commands

配置

模板

通过将模板的文件路径传递给命令来指定模板。

template='tests/templates/test_template1.json'PackerExecutable().validate(template)

模板也可以指定为字符串文本。

template="""
{
    "variables": {
        "my_var1": "{{env `key1`}}"
    },
    "builders": [
        {
            "type": "file",
            "content": "Lorem ipsum dolor sit amet {{user `my_var1`}} ",
            "target": "/tmp/packer.test"
        }
    ]
}
"""PackerExecutable().validate(template)

packerlicious模板也可以与packer.py结合使用。

frompackerliciousimportbuilder,Template,EnvVarfrompackerpyimportPackerExecutablevar1=EnvVar("my_var1")var2=EnvVar("my_var2")file_builder=builder.File()file_builder=builder.File(content="{} more words {}".format(var2.ref().data,var1.ref().data),target="/tmp/packer.test")template=Template()template.add_variable([var1,var2])template.add_builder(file_builder)p=PackerExecutable("/usr/local/bin/packer")template_vars={'my_var1':'my_val1','my_var2':'my_val2'}p.build(template.to_json(),var=template_vars)

命令参数

packer CLI commands参数可以通过将它们作为packer.py方法参数传递来指定。

$ packer validate -syntax-only -var “key1=my_value” tests/templates/test_template1.json
p=PackerExecutable("/usr/local/bin/packer")template='tests/templates/test_template1.json'p.validate(template,syntax_only=True,var="key1=my_value")

packer.py在转换为packer CLI commands时使用以下规则。

封隔器命令选项名称中的破折号

如果packer命令选项中有一个破折号,请将其传递给packer.py并使用下划线。

^{tt1}$:
^{tt2}$
布尔值和隐式值命令选项

如果packer命令选项是布尔选项或具有隐式值的选项,则将其作为布尔值传递给packer.py。

^{tt3}$:^{tt4}$
^{tt5}$:^{tt6}$
重复命令选项

如果可以多次指定packer命令选项,请将该值作为字典传递给packer.py。 多个-var选项就是一个例子。

$ packer build -var ‘my_var1=my_val1’ -var ‘my_var2=my_val2’ tests/templates/test_template1.json
frompackerpyimportPackerExecutablep=PackerExecutable("/usr/local/bin/packer")template='tests/templates/test_template1.json'template_vars={'my_var1':'my_val1','my_var2':'my_val2'}p.build(template,var=template_vars)

建造

$ packer build template.json
>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>(ret,out,err)=p.build('tests/templates/test_template1.json')>>>ret==0True>>>print(ret)0>>>print(out)b"1552841678,,ui,say,Build 'file' finished.\n1552841678,,ui,say,\\n==> Builds finished. The artifacts of successful builds are:\n1552841678,file,artifact-count,1\n1552841678,file,artifact,0,builder-id,packer.file\n1552841678,file,artifact,0,id,File\n1552841678,file,artifact,0,string,Stored file: /tmp/packer.test \n1552841678,file,artifact,0,files-count,1\n1552841678,file,artifact,0,file,0,/tmp/packer.test \n1552841678,file,artifact,0,end\n1552841678,,ui,say,--> file: Stored file: /tmp/packer.test \n">>>print(err)b''

失败生成的示例。

>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>bad_template="""{
...     "builders": [
...         {
...             "type": "amazon-ebs",
...             "access_key": "..."
...         }
...     ]
... }
... """>>>(ret,out,err)=p.build(bad_template)>>>ret==0False>>>print(ret)1>>>print(out)b'1552841800,,ui,error,5 error(s) occurred:\\n\\n* ami_name must be specified\\n* ami_name must be between 3 and 128 characters long\\n* An ssh_username must be specified\\n  Note: some builders used to default ssh_username to "root".\\n* A source_ami or source_ami_filter must be specified\\n* An instance_type must be specified\n'>>>print(err)b''

检查

$ packer inspect template.json
>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>(ret,out,err)=p.inspect('tests/templates/test_template1.json')>>>ret==0True>>>print(ret)0>>>print(out)b"1552841499,,ui,say,Optional variables and their defaults:\\n\n1552841499,,template-variable,my_var1,{{env `key1`}},0\n1552841499,,ui,say,  my_var1 = {{env `key1`}}\n1552841499,,ui,say,\n1552841499,,ui,say,Builders:\\n\n1552841499,,template-builder,file,file\n1552841499,,ui,say,  file\n1552841499,,ui,say,\n1552841499,,ui,say,Provisioners:\\n\n1552841499,,ui,say,  <No provisioners>\n1552841499,,ui,say,\\nNote: If your build names contain user variables or template\\nfunctions such as 'timestamp'%!(PACKER_COMMA) these are processed at build time%!(PACKER_COMMA)\\nand therefore only show in their raw form here.\n">>>print(err)b''

验证

$ packer validate template.json
>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>(ret,out,err)=p.validate('tests/templates/test_template1.json')>>>ret==0True>>>print(ret)0>>>print(out)b'1552840497,,ui,say,Template validated successfully.\n'>>>print(err)b''

未能验证的模板的示例。

>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>bad_template="""{
...     "builders": [
...         {
...             "type": "amazon-ebs",
...             "access_key": "..."
...         }
...     ]
... }
... """>>>(ret,out,err)=p.validate(bad_template)>>>ret==0False>>>print(ret)1>>>print(out)b'1552840892,,ui,error,Template validation failed. Errors are shown below.\\n\n1552840892,,ui,error,Errors validating build \'amazon-ebs\'. 5 error(s) occurred:\\n\\n* ami_name must be specified\\n* ami_name must be between 3 and 128 characters long\\n* An ssh_username must be specified\\n  Note: some builders used to default ssh_username to "root".\\n* A source_ami or source_ami_filter must be specified\\n* An instance_type must be specified\n'>>>print(err)b''

版本

$ packer version
>>>frompackerpyimportPackerExecutable>>>p=PackerExecutable("/usr/local/bin/packer")>>>(ret,out,err)=p.version()>>>ret==0True>>>print(ret)0>>>print(out)b'1552840138,,version,1.0.3\n1552840138,,version-prelease,\n1552840138,,version-commit,c0ddb4a+CHANGES\n1552840138,,ui,say,Packer v1.0.3\n1552840138,,ui,say,\\nYour version of Packer is out of date! The latest version\\nis 1.3.5. You can update by downloading from www.packer.io\n'>>>print(err)b''

许可

packer.py是根据Apache license 2.0授权的。 请参阅LICENSE以获取完整的许可文本。

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

推荐PyPI第三方库


热门话题
java如何使用URLhttp://localhost:8080/appcontext/METAINF/index.html?   Google应用程序引擎Java错误   java此GUI在设置BoxLayout后不显示任何内容   setParameter情况下的java临时类型(字符串名称、对象值)   Rijndael 256加密与Java&Bouncy Castle   java如何检查字符串是否为空?   java InvalidDataAccessResourceUsageException:无法提取结果集   AWTGLCanvas正在隐藏java JToggleButton工具提示   使用PDFBox的java格式数字   java Datainputstream和readUTF数据丢失   在java中使用axiomapi解码XJWT断言   为什么Java和PHP的相同代码不起作用?   JavaGoogleSigin错误,代码工作真实设备工作良好,但不工作模拟器获得错误ApiException 12500,谢谢,我也是新的   java通过使用JNA将void**参数作为char[]获取函数的结果   java如何使用泛型树集创建比较器?   未正确解析java简单文本文件   在java中将字符串转换为MyString对象类型   java Spring继承不起作用