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
20d04795
Commit
20d04795
authored
Aug 07, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #249 from rgarcia/config-decoding
provisioner/shell,file: use common for config decoding
parents
acd09243
ec556044
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
69 deletions
+22
-69
provisioner/file/provisioner.go
provisioner/file/provisioner.go
+8
-33
provisioner/shell/provisioner.go
provisioner/shell/provisioner.go
+14
-36
No files found.
provisioner/file/provisioner.go
View file @
20d04795
...
...
@@ -3,11 +3,9 @@ package file
import
(
"errors"
"fmt"
"github.com/mitchellh/
mapstructure
"
"github.com/mitchellh/
packer/common
"
"github.com/mitchellh/packer/packer"
"os"
"sort"
"strings"
)
type
config
struct
{
...
...
@@ -23,49 +21,26 @@ type Provisioner struct {
}
func
(
p
*
Provisioner
)
Prepare
(
raws
...
interface
{})
error
{
var
md
mapstructure
.
Metadata
decoderConfig
:=
&
mapstructure
.
DecoderConfig
{
Metadata
:
&
md
,
Result
:
&
p
.
config
,
}
decoder
,
err
:=
mapstructure
.
NewDecoder
(
decoderConfig
)
md
,
err
:=
common
.
DecodeConfig
(
&
p
.
config
,
raws
...
)
if
err
!=
nil
{
return
err
}
for
_
,
raw
:=
range
raws
{
err
:=
decoder
.
Decode
(
raw
)
if
err
!=
nil
{
return
err
}
}
// Accumulate any errors
errs
:=
make
([]
error
,
0
)
// Unused keys are errors
if
len
(
md
.
Unused
)
>
0
{
sort
.
Strings
(
md
.
Unused
)
for
_
,
unused
:=
range
md
.
Unused
{
if
unused
!=
"type"
&&
!
strings
.
HasPrefix
(
unused
,
"packer_"
)
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Unknown configuration key: %s"
,
unused
))
}
}
}
errs
:=
common
.
CheckUnusedConfig
(
md
)
if
_
,
err
:=
os
.
Stat
(
p
.
config
.
Source
);
err
!=
nil
{
errs
=
a
ppend
(
errs
,
errs
=
packer
.
MultiErrorA
ppend
(
errs
,
fmt
.
Errorf
(
"Bad source '%s': %s"
,
p
.
config
.
Source
,
err
))
}
if
p
.
config
.
Destination
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"Destination must be specified."
))
errs
=
packer
.
MultiErrorAppend
(
errs
,
errors
.
New
(
"Destination must be specified."
))
}
if
len
(
er
rs
)
>
0
{
return
&
packer
.
MultiError
{
errs
}
if
errs
!=
nil
&&
len
(
errs
.
Erro
rs
)
>
0
{
return
errs
}
return
nil
...
...
provisioner/shell/provisioner.go
View file @
20d04795
...
...
@@ -7,12 +7,11 @@ import (
"bytes"
"errors"
"fmt"
"github.com/mitchellh/
mapstructure
"
"github.com/mitchellh/
packer/common
"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"text/template"
)
...
...
@@ -61,37 +60,13 @@ type ExecuteCommandTemplate struct {
}
func
(
p
*
Provisioner
)
Prepare
(
raws
...
interface
{})
error
{
var
md
mapstructure
.
Metadata
decoderConfig
:=
&
mapstructure
.
DecoderConfig
{
Metadata
:
&
md
,
Result
:
&
p
.
config
,
}
decoder
,
err
:=
mapstructure
.
NewDecoder
(
decoderConfig
)
md
,
err
:=
common
.
DecodeConfig
(
&
p
.
config
,
raws
...
)
if
err
!=
nil
{
return
err
}
for
_
,
raw
:=
range
raws
{
err
:=
decoder
.
Decode
(
raw
)
if
err
!=
nil
{
return
err
}
}
// Accumulate any errors
errs
:=
make
([]
error
,
0
)
// Unused keys are errors
if
len
(
md
.
Unused
)
>
0
{
sort
.
Strings
(
md
.
Unused
)
for
_
,
unused
:=
range
md
.
Unused
{
if
unused
!=
"type"
&&
!
strings
.
HasPrefix
(
unused
,
"packer_"
)
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Unknown configuration key: %s"
,
unused
))
}
}
}
errs
:=
common
.
CheckUnusedConfig
(
md
)
if
p
.
config
.
ExecuteCommand
==
""
{
p
.
config
.
ExecuteCommand
=
"chmod +x {{.Path}}; {{.Vars}} {{.Path}}"
...
...
@@ -118,7 +93,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if
p
.
config
.
Script
!=
""
&&
len
(
p
.
config
.
Scripts
)
>
0
{
errs
=
append
(
errs
,
errors
.
New
(
"Only one of script or scripts can be specified."
))
errs
=
packer
.
MultiErrorAppend
(
errs
,
errors
.
New
(
"Only one of script or scripts can be specified."
))
}
if
p
.
config
.
Script
!=
""
{
...
...
@@ -126,14 +102,17 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if
len
(
p
.
config
.
Scripts
)
==
0
&&
p
.
config
.
Inline
==
nil
{
errs
=
append
(
errs
,
errors
.
New
(
"Either a script file or inline script must be specified."
))
errs
=
packer
.
MultiErrorAppend
(
errs
,
errors
.
New
(
"Either a script file or inline script must be specified."
))
}
else
if
len
(
p
.
config
.
Scripts
)
>
0
&&
p
.
config
.
Inline
!=
nil
{
errs
=
append
(
errs
,
errors
.
New
(
"Only a script file or an inline script can be specified, not both."
))
errs
=
packer
.
MultiErrorAppend
(
errs
,
errors
.
New
(
"Only a script file or an inline script can be specified, not both."
))
}
for
_
,
path
:=
range
p
.
config
.
Scripts
{
if
_
,
err
:=
os
.
Stat
(
path
);
err
!=
nil
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Bad script '%s': %s"
,
path
,
err
))
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"Bad script '%s': %s"
,
path
,
err
))
}
}
...
...
@@ -141,14 +120,13 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
for
_
,
kv
:=
range
p
.
config
.
Vars
{
vs
:=
strings
.
Split
(
kv
,
"="
)
if
len
(
vs
)
!=
2
||
vs
[
0
]
==
""
{
errs
=
append
(
errs
,
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"Environment variable not in format 'key=value': %s"
,
kv
))
}
}
if
len
(
er
rs
)
>
0
{
return
&
packer
.
MultiError
{
errs
}
if
errs
!=
nil
&&
len
(
errs
.
Erro
rs
)
>
0
{
return
errs
}
return
nil
...
...
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