All Projects → mayn → packer.py

mayn / packer.py

Licence: Apache-2.0 license
use python to run hashicorp packer cli commands

Programming Languages

python
139335 projects - #7 most used programming language
powershell
5483 projects
shell
77523 projects

Projects that are alternatives of or similar to packer.py

python-packer
A Packer interface for Python
Stars: ✭ 22 (+4.76%)
Mutual labels:  packer, hashicorp, python-packer
ansible-role-packer-debian
Ansible Role - Packer Debian/Ubuntu Configuration for Vagrant VirtualBox
Stars: ✭ 32 (+52.38%)
Mutual labels:  packer, hashicorp
Hcloud Okd4
Deploy OKD4 (OpenShift) on Hetzner Cloud
Stars: ✭ 29 (+38.1%)
Mutual labels:  packer, hashicorp
vim-hcl
Syntax highlighting for HashiCorp Configuration Language (HCL)
Stars: ✭ 83 (+295.24%)
Mutual labels:  packer, hashicorp
icp-ce-on-linux-containers
Multi node IBM Cloud Private Community Edition 3.2.x w/ Kubernetes 1.13.5 in a Box. Terraform, Packer and BASH based Infrastructure as Code script sets up a multi node LXD cluster, installs ICP-CE and clis on a metal or VM Ubuntu 18.04 host.
Stars: ✭ 52 (+147.62%)
Mutual labels:  packer, hashicorp
Ansible Role Packer rhel
Ansible Role - Packer RHEL/CentOS Configuration for Vagrant VirtualBox
Stars: ✭ 45 (+114.29%)
Mutual labels:  packer, hashicorp
Packerlicious
use python to make hashicorp packer templates
Stars: ✭ 90 (+328.57%)
Mutual labels:  packer, hashicorp
learn-terraform-provisioning
Companion code repository for learning to provision Terraform instances with Packer & cloud-init
Stars: ✭ 56 (+166.67%)
Mutual labels:  packer, hashicorp
local-hashicorp-stack
Local Hashicorp Stack for DevOps Development without Hypervisor or Cloud
Stars: ✭ 23 (+9.52%)
Mutual labels:  packer, hashicorp
packer-plugin-vultr
Packer Builder plugin for Vultr snapshots
Stars: ✭ 42 (+100%)
Mutual labels:  packer, hashicorp-packer
vault-ctrl-tool
Simple tool for managing authentication, secrets, and leases for services.
Stars: ✭ 23 (+9.52%)
Mutual labels:  hashicorp
madalynn-packer
Packer configuration for Ubuntu Server 18.04, 20.04 and 22.04 for Proxmox.
Stars: ✭ 48 (+128.57%)
Mutual labels:  packer
azure-devops-terraform
Recipe to deploy Azure Infrastructure with Terraform via Azure DevOps
Stars: ✭ 18 (-14.29%)
Mutual labels:  hashicorp
ubuntu-vagrant
Ubuntu Linux Vagrant Base Box (https://app.vagrantup.com/rgl)
Stars: ✭ 25 (+19.05%)
Mutual labels:  packer
docker-swarm-aws
This is a small example of provisioning a docker swarm cluster on aws using terraform and packer
Stars: ✭ 27 (+28.57%)
Mutual labels:  packer
vault-unseal
auto-unseal utility for Hashicorp Vault
Stars: ✭ 57 (+171.43%)
Mutual labels:  hashicorp
cstruct-go
a fast c-style struct packer & unpacker for golang
Stars: ✭ 28 (+33.33%)
Mutual labels:  packer
dinivas
AWS, GCP alternative on premise. Dinivas manage your private Cloud (OpenStack) infrastructure by providing many features based on popular Open Source projects
Stars: ✭ 15 (-28.57%)
Mutual labels:  packer
packer-vsphere-iso-windows
Create Packer Templates for Windows Server on VMware vSphere (and vCenter)
Stars: ✭ 73 (+247.62%)
Mutual labels:  packer
consul-templaterb
consul-template-like with erb (ruby) template expressiveness
Stars: ✭ 65 (+209.52%)
Mutual labels:  hashicorp

packer.py

https://travis-ci.org/mayn/packer.py.svg?branch=master https://ci.appveyor.com/api/projects/status/n1627wlm52q12db8/branch/master?svg=true https://coveralls.io/repos/github/mayn/packer.py/badge.svg?branch=master

About

packer.py - python library for interacting with hashicorp packer CLI executable.

Project follows semantic versioning , v0.x.x API should be considered unstable, API will change frequently, please plan accordingly.

Installation

packer.py can be installed via pip:

$ pip install packer.py

Configuration

Packer Executable

Specify Packer Location

Access to hashicorp packer executable is needed in order to use packer.py. When generating commands, if no location is specified, the following defaults are used.

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

These defaults can be overridden by explicitly setting the location of packer for your environment.

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

or

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

Machine Readable Output

Commands are executed with packer's machine readable format enabled by default. This can be disabled by setting

PackerExecutable(machine_readable=False)

Running Commands

The following commands are currently supported:

  • build
  • inspect
  • validate
  • version

Below is the packer.py equivalent of running these packer CLI commands

Configuration

Templates

Templates are specified by passing their filepath to the command.

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

Templates can also be specified as a string literal.

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 templates can also be used and combined with packer.py.

from packerlicious import builder, Template, EnvVar
from packerpy import PackerExecutable
var1 = 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
        )

Command Arguments

packer CLI commands arguments can be specified by passing them as packer.py method arguments.

$ 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"
           )

The following rules are used by packer.py when converting to packer CLI commands.

Dashes in Packer Command Option Names

If the packer command option has a dash in it, pass it to packer.py with an underscore.

-on-error=cleanup:on_error='cleanup'
Boolean Values and Implicit Value Command Options

If the packer command option is either a boolean option or an option with an implicit value, pass it to packer.py as a boolean.

-color=false:color=False
-force:force=True
Repeating Command Options

If the packer command options can be specified multiple times, pass the value as a dictionary to packer.py. Multiple -var option is an example of this.

$ packer build -var 'my_var1=my_val1' -var 'my_var2=my_val2' tests/templates/test_template1.json
from packerpy import PackerExecutable
p = 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
           )

Build

$ packer build template.json
>>> from packerpy import PackerExecutable
>>> p = PackerExecutable("/usr/local/bin/packer")
>>> (ret, out, err) = p.build('tests/templates/test_template1.json')
>>> ret==0
True
>>> 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''

Example of a failed build.

>>> from packerpy import PackerExecutable
>>> p = PackerExecutable("/usr/local/bin/packer")
>>> bad_template = """{
...     "builders": [
...         {
...             "type": "amazon-ebs",
...             "access_key": "..."
...         }
...     ]
... }
... """
>>> (ret, out, err) = p.build(bad_template)
>>> ret==0
False
>>> 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''

Inspect

$ packer inspect template.json
>>> from packerpy import PackerExecutable
>>> p = PackerExecutable("/usr/local/bin/packer")
>>> (ret, out, err) = p.inspect('tests/templates/test_template1.json')
>>> ret==0
True
>>> 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''

Validate

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

Example of a template which failed to validation.

>>> from packerpy import PackerExecutable
>>> p = PackerExecutable("/usr/local/bin/packer")
>>> bad_template = """{
...     "builders": [
...         {
...             "type": "amazon-ebs",
...             "access_key": "..."
...         }
...     ]
... }
... """
>>> (ret, out, err) = p.validate(bad_template)
>>> ret==0
False
>>> 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''

Version

$ packer version
>>> from packerpy import PackerExecutable
>>> p = PackerExecutable("/usr/local/bin/packer")
>>> (ret, out, err) = p.version()
>>> ret==0
True
>>> 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''

Licensing

packer.py is licensed under the Apache license 2.0. See LICENSE for the full license text.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].