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

Commit aba7250

Browse files
committed
readme readme readme
1 parent 6745d5d commit aba7250

3 files changed

Lines changed: 96 additions & 25 deletions

File tree

README.md

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,49 @@ You must have Packer installed prior to using this client (DUH!)
2020
pip install https://github.com/nir0s/python-packer/archive/master.tar.gz
2121
```
2222

23-
## Usage Example
23+
## Usage Examples
24+
25+
### [Packer.build()](https://www.packer.io/docs/command-line/build.html)
2426

2527
```python
2628
import packer
2729

2830
packerfile = 'packer/tests/resources/packerfile.json'
29-
output_file = 'packer/tests/resources/packerfile_fixed.json'
30-
atlas_token = 'oi21mok3mwqtk31om51o2joj213m1oo1i23n1o2'
31-
packer_exec_path = '/usr/bin/packer'
3231
exc = []
3332
only = ['my_first_image', 'my_second_image']
3433
vars = {"variable1": "value1", "variable2": "value2"}
3534
vars_file = 'path/to/vars/file'
35+
packer_exec_path = '/usr/bin/packer'
3636

3737
p = packer.Packer(packerfile, exc=exc, only=only, vars=vars,
3838
vars_file=vars_file, exec_path=packer_exec_path)
39-
print(p.version()) # `packer version`
40-
p.validate(syntax_only=False) # `packer validates`
41-
print(p.fix(output_file)) # `packer fix`
42-
result = p.inspect() # `packer inspect`
43-
print(result.parsed_output)
44-
p.build(parallel=True, debug=False, force=False) # `packer build`
45-
# if you're logged into Atlas, you can also:
46-
p.push(create=True, token=atlas_token) # `packer push`
39+
p.build(parallel=True, debug=False, force=False)
4740
```
4841

49-
The `inspect` method will return a dictionary containing the components:
42+
43+
### [Packer.fix()](https://www.packer.io/docs/command-line/fix.html)
5044

5145
```python
46+
...
47+
48+
output_file = 'packer/tests/resources/packerfile_fixed.json'
49+
print(p.fix(output_file))
50+
```
51+
52+
The `output_file` parameter will write the output of the `fix` function to a file.
53+
54+
55+
### [Packer.inspect()](https://www.packer.io/docs/command-line/inspect.html)
56+
57+
If the `mrf` argument is set to `True`, the output will be parsed and returned as a dictionary containing the components:
58+
59+
```python
60+
...
61+
62+
result = p.inspect(mrf=True)
63+
print(result.parsed_output)
64+
65+
# output:
5266
"variables": [
5367
{
5468
"name": "aws_access_key",
@@ -72,6 +86,60 @@ The `inspect` method will return a dictionary containing the components:
7286
]
7387
```
7488

89+
If the `mrf` argument is set to `False`, the output will not be parsed but rather returned as is:
90+
91+
```python
92+
...
93+
94+
result = p.inspect(mrf=True)
95+
print(result.stdout)
96+
97+
# output:
98+
Optional variables and their defaults:
99+
100+
aws_access_key = {{env `AWS_ACCESS_KEY_ID`}}
101+
aws_secret_key = {{env `AWS_ACCESS_KEY`}}
102+
103+
Builders:
104+
105+
amazon (amazon-ebs)
106+
107+
Provisioners:
108+
109+
shell
110+
111+
...
112+
113+
```
114+
115+
116+
### [Packer.push()](https://www.packer.io/docs/command-line/push.html)
117+
118+
You must be logged into Atlas to use the `push` function:
119+
120+
```python
121+
...
122+
123+
atlas_token = 'oi21mok3mwqtk31om51o2joj213m1oo1i23n1o2'
124+
p.push(create=True, token=atlas_token)
125+
```
126+
127+
### [Packer.validate()](https://www.packer.io/docs/command-line/validate.html)
128+
129+
```python
130+
...
131+
132+
p.validate(syntax_only=False)
133+
```
134+
135+
### Packer.version()
136+
137+
```python
138+
...
139+
140+
print(p.version())
141+
```
142+
75143
## Shell Interaction
76144

77145
The [sh](http://amoffat.github.io/sh/) Python module is used to execute Packer.

packer/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def fix(self, to_file=None):
5858
f.write(result.stdout)
5959
return result
6060

61-
def inspect(self):
61+
def inspect(self, mrf=True):
6262
"""Inspects a Packer Templates file (`packer inspect -machine-readable`)
6363
6464
To return the output in a readable form, the `-machine-readable` flag
@@ -85,12 +85,15 @@ def inspect(self):
8585
"name": "amazon"
8686
}
8787
]
88+
:param bool machine_readable: output in machine-readable form.
8889
"""
89-
command = self.packer.inspect
90-
command = command.bake('-machine-readable', self.packerfile)
91-
result = command()
92-
result.parsed_output = self._parse_inspection_output(
93-
result.stdout)
90+
cmd = self.packer.inspect
91+
cmd = self._add_opt(cmd, '-machine-readable' if mrf else None)
92+
cmd = cmd.bake(self.packerfile)
93+
result = cmd()
94+
if mrf:
95+
result.parsed_output = self._parse_inspection_output(
96+
result.stdout)
9497
return result
9598

9699
def push(self, create=True, token=False):

tester.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
vars_file = '/x'
1111

1212
p = packer.Packer(packerfile, exc=exc, only=only, vars=vars)
13-
print(p.version())
14-
print(p.validate(syntax_only=False))
15-
result = p.inspect()
16-
print result.parsed_output
17-
print(p.fix('TEST.json'))
18-
print(p.build())
13+
# print(p.version())
14+
# print(p.validate(syntax_only=False))
15+
result = p.inspect(mrf=False)
16+
print result.stdout
17+
# print(p.fix('TEST.json'))
18+
# print(p.build())

0 commit comments

Comments
 (0)