Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 0d8d09e

Browse files
committed
removed logging, added in-code docs
1 parent 81fa84f commit 0d8d09e

3 files changed

Lines changed: 138 additions & 117 deletions

File tree

packer/__init__.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,139 @@
1+
import sh
2+
13
__author__ = 'Gigaspaces'
24
__version__ = '0.1.2'
5+
6+
DEFAULT_PACKER_PATH = 'packer'
7+
8+
9+
class Packer():
10+
11+
def __init__(self, packerfile, exc=None, only=None, vars=None,
12+
vars_file=None, exec_path=DEFAULT_PACKER_PATH):
13+
self.packerfile = packerfile
14+
self.exc = exc if exc else []
15+
self.only = only if only else []
16+
self.vars = vars if vars else {}
17+
self.vars_file = vars_file
18+
self.packer = sh.Command(exec_path)
19+
20+
def _append_base_arguments(self, cmd):
21+
"""Add base arguments to packer commands.
22+
23+
-except, -only, -var and -vars-file are appeneded to almost
24+
all subcommands in packer. As such this can be called to add
25+
these flags to the subcommand.
26+
"""
27+
if self.exc and self.only:
28+
raise PackerException('Cannot provide both "except" and "only"')
29+
if self.exc:
30+
cmd = cmd.bake('-except={0}'.format(self._joinc(self.exc)))
31+
if self.only:
32+
cmd = cmd.bake('-only={0}'.format(self._joinc(self.only)))
33+
for var, value in self.vars.iteritems():
34+
cmd = cmd.bake("-var '{0}={1}'".format(var, value))
35+
if self.vars_file:
36+
cmd = cmd.bake('-vars-file={0}'.format(self.vars_file))
37+
return cmd
38+
39+
def _joinc(self, lst):
40+
"""Returns a comma separated string from a list"""
41+
return str(','.join(lst))
42+
43+
def _join(self, lst):
44+
"""Returns a space separated string from a list"""
45+
return str(' '.join(lst))
46+
47+
def version(self):
48+
"""Returns Packer's version number (`packer version`)
49+
50+
As of v0.7.5, the format shows when running `packer version`
51+
is: Packer vX.Y.Z. This only returns the number, without the
52+
`packer v` prefix so that you don't have to parse the version
53+
yourself.
54+
"""
55+
return self.packer.version().split('v')[1].rstrip('\n')
56+
57+
def validate(self, syntax_only=False):
58+
"""Validates a Packer Template file (`packer validate`)
59+
"""
60+
validator = self.packer.validate
61+
if syntax_only:
62+
validator = validator.bake('-syntax-only')
63+
validator = self._append_base_arguments(validator)
64+
validator = validator.bake(self.packerfile)
65+
return validator()
66+
67+
def build(self, parallel=True, debug=False, force=False):
68+
"""Executes a Packer build (`packer build`)
69+
"""
70+
builder = self.packer.build
71+
if parallel:
72+
builder = builder.bake('-parallel=true')
73+
if debug:
74+
builder = builder.bake('-debug')
75+
if force:
76+
builder = builder.bake('-force')
77+
builder = self._append_base_arguments(builder)
78+
builder = builder.bake(self.packerfile)
79+
return builder()
80+
81+
def inspect(self):
82+
"""Inspects a Packer Templates file (`packer inspect -machine-readable`)
83+
84+
To return the output in a readable form, the `-machine-readable` flag
85+
is appended automatically, afterwhich the output is parsed and returned
86+
as a dict of the following format:
87+
"variables": [
88+
{
89+
"name": "aws_access_key",
90+
"value": "{{env `AWS_ACCESS_KEY_ID`}}"
91+
},
92+
{
93+
"name": "aws_secret_key",
94+
"value": "{{env `AWS_ACCESS_KEY`}}"
95+
}
96+
],
97+
"provisioners": [
98+
{
99+
"type": "shell"
100+
}
101+
],
102+
"builders": [
103+
{
104+
"type": "amazon-ebs",
105+
"name": "amazon"
106+
}
107+
]
108+
"""
109+
inspector = self.packer.inspect
110+
inspector = inspector.bake('-machine-readable', self.packerfile)
111+
result = inspector()
112+
result.parsed_output = self._parse_inspection_output(result.stdout)
113+
return result
114+
115+
def _parse_inspection_output(self, output):
116+
"""Parses the machine-readable output `packer inspect` provides.
117+
118+
See the inspect method for more info.
119+
"""
120+
parts = {'variables': [], 'builders': [], 'provisioners': []}
121+
for l in output.splitlines():
122+
l = l.split(',')
123+
if l[2].startswith('template'):
124+
del l[0:2]
125+
component = l[0]
126+
if component == 'template-variable':
127+
variable = {"name": l[1], "value": l[2]}
128+
parts['variables'].append(variable)
129+
elif component == 'template-builder':
130+
builder = {"name": l[1], "type": l[2]}
131+
parts['builders'].append(builder)
132+
elif component == 'template-provisioner':
133+
provisioner = {"type": l[1]}
134+
parts['provisioners'].append(provisioner)
135+
return parts
136+
137+
138+
class PackerException():
139+
pass

packer/interface.py

Lines changed: 0 additions & 116 deletions
This file was deleted.

tester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import packer.interface as packer
1+
import packer
22
import os
33

44
packerfile = os.path.expanduser('packer/tests/resources/packerfile.json') # NOQA

0 commit comments

Comments
 (0)