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
919c3935
Commit
919c3935
authored
Jun 28, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'shell-env-vars'
Conflicts: website/source/docs/provisioners/shell.html.markdown
parents
09029fd5
d64797cc
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
7 deletions
+62
-7
provisioner/shell/provisioner.go
provisioner/shell/provisioner.go
+24
-3
provisioner/shell/provisioner_test.go
provisioner/shell/provisioner_test.go
+29
-0
website/source/docs/provisioners/shell.html.markdown
website/source/docs/provisioners/shell.html.markdown
+9
-4
No files found.
provisioner/shell/provisioner.go
View file @
919c3935
...
...
@@ -31,12 +31,17 @@ type config struct {
// An array of multiple scripts to run.
Scripts
[]
string
// An array of environment variables that will be injected before
// your command(s) are executed.
Vars
[]
string
`mapstructure:"environment_vars"`
// The remote path where the local shell script will be uploaded to.
// This should be set to a writable file that is in a pre-existing directory.
RemotePath
string
`mapstructure:"remote_path"`
// The command used to execute the script. The '{{ .Path }}' variable
// should be used to specify where the script goes.
// should be used to specify where the script goes, {{ .Vars }}
// can be used to inject the environment_vars into the environment.
ExecuteCommand
string
`mapstructure:"execute_command"`
}
...
...
@@ -45,6 +50,7 @@ type Provisioner struct {
}
type
ExecuteCommandTemplate
struct
{
Vars
string
Path
string
}
...
...
@@ -56,7 +62,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
if
p
.
config
.
ExecuteCommand
==
""
{
p
.
config
.
ExecuteCommand
=
"sh {{.Path}}"
p
.
config
.
ExecuteCommand
=
"
{{.Vars}}
sh {{.Path}}"
}
if
p
.
config
.
Inline
!=
nil
&&
len
(
p
.
config
.
Inline
)
==
0
{
...
...
@@ -71,6 +77,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p
.
config
.
Scripts
=
make
([]
string
,
0
)
}
if
p
.
config
.
Vars
==
nil
{
p
.
config
.
Vars
=
make
([]
string
,
0
)
}
errs
:=
make
([]
error
,
0
)
if
p
.
config
.
Script
!=
""
&&
len
(
p
.
config
.
Scripts
)
>
0
{
...
...
@@ -93,6 +103,14 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
}
}
// Do a check for bad environment variables, such as '=foo', 'foobar'
for
_
,
kv
:=
range
p
.
config
.
Vars
{
vs
:=
strings
.
Split
(
kv
,
"="
)
if
len
(
vs
)
!=
2
||
vs
[
0
]
==
""
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Environment variable not in format 'key=value': %s"
,
kv
))
}
}
if
len
(
errs
)
>
0
{
return
&
packer
.
MultiError
{
errs
}
}
...
...
@@ -146,10 +164,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return
fmt
.
Errorf
(
"Error uploading shell script: %s"
,
err
)
}
// Flatten the environment variables
flattendVars
:=
strings
.
Join
(
p
.
config
.
Vars
,
" "
)
// Compile the command
var
command
bytes
.
Buffer
t
:=
template
.
Must
(
template
.
New
(
"command"
)
.
Parse
(
p
.
config
.
ExecuteCommand
))
t
.
Execute
(
&
command
,
&
ExecuteCommandTemplate
{
p
.
config
.
RemotePath
})
t
.
Execute
(
&
command
,
&
ExecuteCommandTemplate
{
flattendVars
,
p
.
config
.
RemotePath
})
// Setup the remote command
stdout_r
,
stdout_w
:=
io
.
Pipe
()
...
...
provisioner/shell/provisioner_test.go
View file @
919c3935
...
...
@@ -131,3 +131,32 @@ func TestProvisionerPrepare_Scripts(t *testing.T) {
t
.
Fatalf
(
"should not have error: %s"
,
err
)
}
}
func
TestProvisionerPrepare_EnvironmentVars
(
t
*
testing
.
T
)
{
config
:=
testConfig
()
// Test with a bad case
config
[
"environment_vars"
]
=
[]
string
{
"badvar"
,
"good=var"
}
p
:=
new
(
Provisioner
)
err
:=
p
.
Prepare
(
config
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
// Test with a trickier case
config
[
"environment_vars"
]
=
[]
string
{
"=bad"
}
p
=
new
(
Provisioner
)
err
=
p
.
Prepare
(
config
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
// Test with a good case
// Note: baz= is a real env variable, just empty
config
[
"environment_vars"
]
=
[]
string
{
"FOO=bar"
,
"baz="
}
p
=
new
(
Provisioner
)
err
=
p
.
Prepare
(
config
)
if
err
!=
nil
{
t
.
Fatalf
(
"should not have error: %s"
,
err
)
}
}
website/source/docs/provisioners/shell.html.markdown
View file @
919c3935
...
...
@@ -46,11 +46,16 @@ Exactly _one_ of the following is required:
Optional parameters:
*
`environment_vars`
(array of strings) - An array of key/value pairs
to inject prior to the execute_command. The format should be
`key=value`
.
*
`execute_command`
(string) - The command to use to execute the script.
By default this is
`sh {{ .Path }}`
. The value of this is treated as a
[
configuration template
](
/docs/templates/configuration-templates.html
)
.
The only available variable in it is
`Path`
which is the path to the
script to run.
By default this is
`{{ .Vars }} sh {{ .Path }}`
. The value of this is
treated as
[
configuration template
](
/docs/templates/configuration-
templates.html). There are two available variables:
`Path`
, which is
the path to the script to run, and
`Vars`
, which is the list of
`environment_vars`
, if configured.
*
`remote_path`
(string) - The path where the script will be uploaded to
in the machine. This defaults to "/tmp/script.sh". This value must be
...
...
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