Giter Club home page Giter Club logo

python-packer's Introduction

python-packer

A Python interface for packer.io

Compatibility

Python Packer Release Packer Release
v1.0.0 v1.7.4
v1.0.1 v1.7.4
v1.0.2 v1.7.4
v1.0.3 v1.7.4
v1.0.4 v1.7.4
v1.0.5 v1.7.4
v1.0.6 v1.7.4

Installation

You must have Packer installed prior to using this client though as installer class is provided to install packer for you.

pip install git+https://github.com/abhi1693/python-packer@main

Usage Examples

import packer

template = 'path/to/template'
exc = []
only = ['my_first_image', 'my_second_image']
vars = {"variable1": "value1", "variable2": "value2"}
var_file = 'path/to/var/file'
packer_exec_path = '/usr/bin/packer'

p = packer.Packer(template=template, exc=exc, only=only, vars=vars,
                  var_file=var_file, exec_path=packer_exec_path)
p.build(parallel=True, debug=False, force=False)
...

p = packer.Packer(template, ...)
output_file = '/tmp/template_fixed.json'
print(p.fix(output_file))

The output_file parameter will write the output of the fix function to a file.

A -machine-readable (mrf) argument is provided.

If the mrf argument is set to True, the output will be parsed and an object containing the parsed output will be exposed as a dictionary containing the components:

import packer

p = packer.Packer(template, ...)
result = p.inspect(mrf=True)
print(result.parsed_output)
# print(result.stdout) can also be used here
"variables": [
  {
    "name": "aws_access_key",
    "value": "{{env `AWS_ACCESS_KEY_ID`}}"
  },
  {
    "name": "aws_secret_key",
    "value": "{{env `AWS_ACCESS_KEY`}}"
  }
],
"provisioners": [
  {
    "type": "shell"
  }
],
"builders": [
  {
    "type": "amazon-ebs",
    "name": "amazon"
  }
]

If the mrf argument is set to False, the output will not be parsed but rather returned as is:

...

p = packer.Packer(template, ...)
result = p.inspect(mrf=True)
print(result.stdout)

# output:
Optional variables and their defaults:

  aws_access_key          = {{env `AWS_ACCESS_KEY_ID`}}
  aws_secret_key          = {{env `AWS_ACCESS_KEY`}}

Builders:

  amazon                   (amazon-ebs)

Provisioners:

  shell

...

You must be logged into Atlas to use the push function:

...

p = packer.Packer(template, ...)
atlas_token = 'oi21mok3mwqtk31om51o2joj213m1oo1i23n1o2'
p.push(create=True, token=atlas_token)
...

p = packer.Packer(template, ...)
p.validate(syntax_only=False)

Packer.version()

...

p = packer.Packer(template, ...)
print(p.version())

PackerInstaller.install()

This installs packer to packer_path using the installer_path and verifies that the installation was successful.

packer_path = '/usr/bin/'
installer_path = 'Downloads/packer_1.7.4_linux_amd64.zip'

p = packer.Installer(packer_path, installer_path)
p.install()

Shell Interaction

The sh Python module is used to execute Packer. As such, return values from all functional methods (validate, build, etc..) other than the version method will return an sh execution object. This is meant for you to be able to read stdout, stderr, exit codes and more after executing the commands. With the progression of python-packer less abstract objects will return and more concise return values will be provided.

Additionally, to verify that all errors return with as much info as possible, error handling is done gently. Most errors will raise an sh exception so that you're able to interact with them. Again, as this module progresses, these exceptions will be handled properly.

Testing

Tests have not been developed yet.

git clone [email protected]:abhi1693/python-packer.git
cd python-packer
pip install tox
tox

Contributions..

..are always welcome.

python-packer's People

Contributors

abhi1693 avatar dependabot[bot] avatar github-actions[bot] avatar oxr463 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

oxr463 outzhu

python-packer's Issues

No $HOME environment variable found, required to set Config Directory

Describe the issue:

Steps to reproduce (least amount of steps as possible):

  1. Run p.build(parallel=False, debug=True, force=False)

Result:

python-packer.docker.ubuntu: Error making temp dir: No $HOME environment variable found, required to set Config Directory

Other details that may be helpful:

I clearly have a HOME variable:

echo $HOME
/home/oxr463

Here is the packer template I used: https://github.com/abhi1693/python-packer/pull/19/files#diff-dc05b4673ee12cb0c8cd8db7946644b9fbad6339264a47defc00bf4668e15750

I'm also using virtualenv.

Environment information

tox fails due to python3.6 missing

Describe the issue:

I cannot run tox due to expected older version of Python.

Steps to reproduce (least amount of steps as possible):

  1. Run the following command:
tox

Result:

GLOB sdist-make: /home/oxr463/src/python-packer/setup.py
flake8 create: /home/oxr463/src/python-packer/.tox/flake8
flake8 installdeps: flake8, -rdev-requirements.txt
ERROR: invocation failed (exit code 1), logfile: /home/oxr463/src/python-packer/.tox/flake8/log/flake8-1.log
====================================================== log start ======================================================
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'dev-requirements.txt'

======================================================= log end =======================================================
ERROR: could not install deps [flake8, -rdev-requirements.txt]; v = InvocationError('/home/oxr463/src/python-packer/.tox/flake8/bin/python -m pip install flake8 -rdev-requirements.txt', 1)
py36 create: /home/oxr463/src/python-packer/.tox/py36
ERROR: InterpreterNotFound: python3.6
_______________________________________________________ summary _______________________________________________________
ERROR:   flake8: could not install deps [flake8, -rdev-requirements.txt]; v = InvocationError('/home/oxr463/src/python-packer/.tox/flake8/bin/python -m pip install flake8 -rdev-requirements.txt', 1)
ERROR:  py36: InterpreterNotFound: python3.6

Other details that may be helpful:

python --version
Python 3.10.5

Environment information

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.