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

Commit fae548f

Browse files
committed
yay! alphabetaized functional methods
1 parent 7faeb1a commit fae548f

1 file changed

Lines changed: 89 additions & 95 deletions

File tree

packer/__init__.py

Lines changed: 89 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -32,95 +32,6 @@ def __init__(self, packerfile, exc=None, only=None, vars=None,
3232
self.packerfile))
3333
self.packer = sh.Command(exec_path)
3434

35-
def _validate_argtype(self, arg, argtype):
36-
if not isinstance(arg, argtype):
37-
raise PackerException('{0} argument must be of type {1}'.format(
38-
arg, argtype))
39-
return arg
40-
41-
def _append_base_arguments(self, command):
42-
"""Appends base arguments to packer commands.
43-
44-
-except, -only, -var and -vars-file are appeneded to almost
45-
all subcommands in packer. As such this can be called to add
46-
these flags to the subcommand.
47-
"""
48-
if self.exc and self.only:
49-
raise PackerException('Cannot provide both "except" and "only"')
50-
elif self.exc:
51-
command = command.bake('-except={0}'.format(self._joinc(self.exc)))
52-
elif self.only:
53-
command = command.bake('-only={0}'.format(self._joinc(self.only)))
54-
for var, value in self.vars.items():
55-
command = command.bake("-var '{0}={1}'".format(var, value))
56-
if self.vars_file:
57-
command = command.bake('-vars-file={0}'.format(self.vars_file))
58-
return command
59-
60-
def _joinc(self, lst):
61-
"""Returns a comma delimited string from a list"""
62-
return str(','.join(lst))
63-
64-
def _joins(self, lst):
65-
"""Returns a space delimited string from a list"""
66-
return str(' '.join(lst))
67-
68-
def version(self):
69-
"""Returns Packer's version number (`packer version`)
70-
71-
As of v0.7.5, the format shows when running `packer version`
72-
is: Packer vX.Y.Z. This method will only returns the number, without
73-
the `packer v` prefix so that you don't have to parse the version
74-
yourself.
75-
"""
76-
return self.packer.version().split('v')[1].rstrip('\n')
77-
78-
def validate(self, syntax_only=False):
79-
"""Validates a Packer Template file (`packer validate`)
80-
81-
If the validation failed, an `sh` exception will be raised.
82-
:param bool syntax_only: Whether to validate the syntax only
83-
without validating the configuration itself.
84-
"""
85-
command = self.packer.validate
86-
if syntax_only:
87-
command = command.bake('-syntax-only')
88-
command = self._append_base_arguments(command)
89-
command = command.bake(self.packerfile)
90-
return command()
91-
# err.. need to return normal values with validation result
92-
# validated.succeeded = True if validated.exit_code == 0 else False
93-
# validated.failed = not validated.succeeded
94-
95-
def push(self, create=True, token=False):
96-
"""Implmenets the `packer push` function
97-
98-
UNTESTED!
99-
"""
100-
command = self.packer.push
101-
if create:
102-
command = command.bake('-create=true')
103-
if token:
104-
command = command.bake('-token={0}'.format(token))
105-
command = command.bake(self.packerfile)
106-
return command()
107-
108-
def fix(self, to_file=None):
109-
"""Implements the `packer fix` function
110-
"""
111-
cmd = self.packer.fix
112-
cmd = cmd.bake(self.packerfile)
113-
result = cmd()
114-
if to_file:
115-
with open(to_file, 'w') as f:
116-
f.write(result.stdout)
117-
return result
118-
119-
def _add_opt(self, command, option):
120-
if option:
121-
return command.bake(option)
122-
return command
123-
12435
def build(self, parallel=True, debug=False, force=False):
12536
"""Executes a Packer build (`packer build`)
12637
@@ -132,16 +43,21 @@ def build(self, parallel=True, debug=False, force=False):
13243
cmd = self._add_opt(cmd, '-parallel=true' if parallel else None)
13344
cmd = self._add_opt(cmd, '-debug' if debug else None)
13445
cmd = self._add_opt(cmd, '-force' if force else None)
135-
# if parallel:
136-
# cmd = cmd.bake('-parallel=true')
137-
# if debug:
138-
# cmd = cmd.bake('-debug')
139-
# if force:
140-
# cmd = cmd.bake('-force')
14146
cmd = self._append_base_arguments(cmd)
14247
cmd = cmd.bake(self.packerfile)
14348
return cmd()
14449

50+
def fix(self, to_file=None):
51+
"""Implements the `packer fix` function
52+
"""
53+
cmd = self.packer.fix
54+
cmd = cmd.bake(self.packerfile)
55+
result = cmd()
56+
if to_file:
57+
with open(to_file, 'w') as f:
58+
f.write(result.stdout)
59+
return result
60+
14561
def inspect(self):
14662
"""Inspects a Packer Templates file (`packer inspect -machine-readable`)
14763
@@ -177,6 +93,84 @@ def inspect(self):
17793
result.stdout)
17894
return result
17995

96+
def push(self, create=True, token=False):
97+
"""Implmenets the `packer push` function
98+
99+
UNTESTED!
100+
"""
101+
command = self.packer.push
102+
if create:
103+
command = command.bake('-create=true')
104+
if token:
105+
command = command.bake('-token={0}'.format(token))
106+
command = command.bake(self.packerfile)
107+
return command()
108+
109+
def validate(self, syntax_only=False):
110+
"""Validates a Packer Template file (`packer validate`)
111+
112+
If the validation failed, an `sh` exception will be raised.
113+
:param bool syntax_only: Whether to validate the syntax only
114+
without validating the configuration itself.
115+
"""
116+
command = self.packer.validate
117+
if syntax_only:
118+
command = command.bake('-syntax-only')
119+
command = self._append_base_arguments(command)
120+
command = command.bake(self.packerfile)
121+
return command()
122+
# err.. need to return normal values with validation result
123+
# validated.succeeded = True if validated.exit_code == 0 else False
124+
# validated.failed = not validated.succeeded
125+
126+
def version(self):
127+
"""Returns Packer's version number (`packer version`)
128+
129+
As of v0.7.5, the format shows when running `packer version`
130+
is: Packer vX.Y.Z. This method will only returns the number, without
131+
the `packer v` prefix so that you don't have to parse the version
132+
yourself.
133+
"""
134+
return self.packer.version().split('v')[1].rstrip('\n')
135+
136+
def _add_opt(self, command, option):
137+
if option:
138+
return command.bake(option)
139+
return command
140+
141+
def _validate_argtype(self, arg, argtype):
142+
if not isinstance(arg, argtype):
143+
raise PackerException('{0} argument must be of type {1}'.format(
144+
arg, argtype))
145+
return arg
146+
147+
def _append_base_arguments(self, command):
148+
"""Appends base arguments to packer commands.
149+
150+
-except, -only, -var and -vars-file are appeneded to almost
151+
all subcommands in packer. As such this can be called to add
152+
these flags to the subcommand.
153+
"""
154+
if self.exc and self.only:
155+
raise PackerException('Cannot provide both "except" and "only"')
156+
elif self.exc:
157+
command = command.bake('-except={0}'.format(self._joinc(self.exc)))
158+
elif self.only:
159+
command = command.bake('-only={0}'.format(self._joinc(self.only)))
160+
for var, value in self.vars.items():
161+
command = command.bake("-var '{0}={1}'".format(var, value))
162+
if self.vars_file:
163+
command = command.bake('-vars-file={0}'.format(self.vars_file))
164+
return command
165+
166+
def _joinc(self, lst):
167+
"""Returns a comma delimited string from a list"""
168+
return str(','.join(lst))
169+
170+
def _joins(self, lst):
171+
"""Returns a space delimited string from a list"""
172+
return str(' '.join(lst))
173+
180174
def _parse_inspection_output(self, output):
181175
"""Parses the machine-readable output `packer inspect` provides.
182176

0 commit comments

Comments
 (0)