provisioner/local-exec: Optional "working_dir" argument

This new argument allows overriding of the working directory of the child process, with the default still being the working directory of Terraform itself.
This commit is contained in:
chrisjob1021 2018-02-16 11:31:11 -08:00 committed by Martin Atkins
parent df9446f490
commit 10bb21e9c4
3 changed files with 44 additions and 0 deletions

View File

@ -34,6 +34,11 @@ func Provisioner() terraform.ResourceProvisioner {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"working_dir": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
ApplyFunc: applyFn,
@ -69,6 +74,8 @@ func applyFn(ctx context.Context) error {
}
cmdargs = append(cmdargs, command)
workingdir := data.Get("working_dir").(string)
// Setup the reader that will read the output from the command.
// We use an os.Pipe so that the *os.File can be passed directly to the
// process, and not rely on goroutines copying the data which may block.
@ -82,6 +89,10 @@ func applyFn(ctx context.Context) error {
cmd := exec.CommandContext(ctx, cmdargs[0], cmdargs[1:]...)
cmd.Stderr = pw
cmd.Stdout = pw
// Dir specifies the working directory of the command.
// If Dir is the empty string (this is default), runs the command
// in the calling process's current directory.
cmd.Dir = workingdir
output, _ := circbuf.NewBuffer(maxBufSize)

View File

@ -145,3 +145,32 @@ func TestResourceProvider_ApplyCustomInterpreter(t *testing.T) {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}
func TestResourceProvider_ApplyCustomWorkingDirectory(t *testing.T) {
testdir := "working_dir_test"
os.Mkdir(testdir, 0755)
defer os.Remove(testdir)
c := testConfig(t, map[string]interface{}{
"working_dir": testdir,
"command": "echo `pwd`",
})
output := new(terraform.MockUIOutput)
p := Provisioner()
if err := p.Apply(output, nil, c); err != nil {
t.Fatalf("err: %v", err)
}
dir, err := os.Getwd()
if err != nil {
t.Fatalf("err: %v", err)
}
got := strings.TrimSpace(output.OutputMessage)
want := dir + "/" + testdir
if got != want {
t.Errorf("wrong output\ngot: %s\nwant: %s", got, want)
}
}

View File

@ -39,6 +39,10 @@ The following arguments are supported:
It is evaluated in a shell, and can use environment variables or Terraform
variables.
* `working_dir` - (Optional) If provided, specifies the working directory where
`command` will be executed. It can be provided as as a relative path to the
current working directory or as an absolute path. The directory must exist.
* `interpreter` - (Optional) If provided, this is a list of interpreter
arguments used to execute the command. The first argument is the
interpreter itself. It can be provided as a relative path to the current