Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
02bb5b0a
Commit
02bb5b0a
authored
Dec 27, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: template process build names [GH-744]
parent
107e47fe
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
21 deletions
+81
-21
CHANGELOG.md
CHANGELOG.md
+2
-0
command/inspect/command.go
command/inspect/command.go
+4
-0
packer/template.go
packer/template.go
+33
-21
packer/template_test.go
packer/template_test.go
+42
-0
No files found.
CHANGELOG.md
View file @
02bb5b0a
...
...
@@ -39,6 +39,8 @@ IMPROVEMENTS:
*
core: Plugins communicate over a single TCP connection per plugin now,
instead of sometimes dozens. Performance around plugin communication
dramatically increased.
*
core: Build names are now template processed so you can use things
like user variables in them. [GH-744]
*
builder/amazon/all: Launched EC2 instances now have a name of
"Packer Builder" so that they are easily recognizable. [GH-642]
*
builder/amazon/all: Copying AMIs to multiple regions now happens
...
...
command/inspect/command.go
View file @
02bb5b0a
...
...
@@ -142,5 +142,9 @@ func (c Command) Run(env packer.Environment, args []string) int {
}
}
ui
.
Say
(
"
\n
Note: If your build names contain user variables or template
\n
"
+
"functions such as 'timestamp', these are processed at build time,
\n
"
+
"and therefore only show in their raw form here."
)
return
0
}
packer/template.go
View file @
02bb5b0a
...
...
@@ -446,6 +446,39 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
return
}
// Prepare the variables
var
varErrors
[]
error
variables
:=
make
(
map
[
string
]
string
)
for
k
,
v
:=
range
t
.
Variables
{
if
v
.
Required
&&
!
v
.
HasValue
{
varErrors
=
append
(
varErrors
,
fmt
.
Errorf
(
"Required user variable '%s' not set"
,
k
))
}
var
val
string
=
v
.
Default
if
v
.
HasValue
{
val
=
v
.
Value
}
variables
[
k
]
=
val
}
if
len
(
varErrors
)
>
0
{
return
nil
,
&
MultiError
{
varErrors
}
}
// Process the name
tpl
,
err
:=
NewConfigTemplate
()
if
err
!=
nil
{
return
nil
,
err
}
tpl
.
UserVars
=
variables
name
,
err
=
tpl
.
Process
(
name
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
// Gather the Hooks
hooks
:=
make
(
map
[
string
][]
Hook
)
for
tplEvent
,
tplHooks
:=
range
t
.
Hooks
{
...
...
@@ -542,27 +575,6 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
provisioners
=
append
(
provisioners
,
coreProv
)
}
// Prepare the variables
var
varErrors
[]
error
variables
:=
make
(
map
[
string
]
string
)
for
k
,
v
:=
range
t
.
Variables
{
if
v
.
Required
&&
!
v
.
HasValue
{
varErrors
=
append
(
varErrors
,
fmt
.
Errorf
(
"Required user variable '%s' not set"
,
k
))
}
var
val
string
=
v
.
Default
if
v
.
HasValue
{
val
=
v
.
Value
}
variables
[
k
]
=
val
}
if
len
(
varErrors
)
>
0
{
return
nil
,
&
MultiError
{
varErrors
}
}
b
=
&
coreBuild
{
name
:
name
,
builder
:
builder
,
...
...
packer/template_test.go
View file @
02bb5b0a
...
...
@@ -688,6 +688,48 @@ func TestTemplate_BuildUnknownBuilder(t *testing.T) {
}
}
func
TestTemplateBuild_names
(
t
*
testing
.
T
)
{
data
:=
`
{
"variables": {
"foo": null
},
"builders": [
{
"name": "test1",
"type": "test-builder"
},
{
"name": "test2-{{user \"foo\"}}",
"type": "test-builder"
}
]
}
`
template
,
err
:=
ParseTemplate
([]
byte
(
data
),
map
[
string
]
string
{
"foo"
:
"bar"
})
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
b
,
err
:=
template
.
Build
(
"test1"
,
testComponentFinder
())
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
b
.
Name
()
!=
"test1"
{
t
.
Fatalf
(
"bad: %#v"
,
b
.
Name
())
}
b
,
err
=
template
.
Build
(
"test2-{{user
\"
foo
\"
}}"
,
testComponentFinder
())
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
b
.
Name
()
!=
"test2-bar"
{
t
.
Fatalf
(
"bad: %#v"
,
b
.
Name
())
}
}
func
TestTemplate_Build_NilBuilderFunc
(
t
*
testing
.
T
)
{
data
:=
`
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment