provisioners/local-exec: stoppable

This modifies local-exec to be stoppable with the new Stop API call that
provisioners can listen to.
pull/10934/head
Mitchell Hashimoto 10 years ago
parent c5b784c33f
commit 0fb87cd96b
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A

@ -68,8 +68,24 @@ func applyFn(ctx context.Context) error {
"Executing: %s %s \"%s\"",
shell, flag, command))
// Run the command to completion
err := cmd.Run()
// Start the command
err := cmd.Start()
if err == nil {
// Wait for the command to complete in a goroutine
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
err = cmd.Wait()
}()
// Wait for the command to finish or for us to be interrupted
select {
case <-doneCh:
case <-ctx.Done():
cmd.Process.Kill()
err = cmd.Wait()
}
}
// Close the write-end of the pipe so that the goroutine mirroring output
// ends properly.

@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
@ -35,6 +36,37 @@ func TestResourceProvider_Apply(t *testing.T) {
}
}
func TestResourceProvider_stop(t *testing.T) {
c := testConfig(t, map[string]interface{}{
"command": "sleep 60",
})
output := new(terraform.MockUIOutput)
p := Provisioner()
var err error
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
err = p.Apply(output, nil, c)
}()
select {
case <-doneCh:
t.Fatal("should not finish quickly")
case <-time.After(10 * time.Millisecond):
}
// Stop it
p.Stop()
select {
case <-doneCh:
case <-time.After(100 * time.Millisecond):
t.Fatal("should finish")
}
}
func TestResourceProvider_Validate_good(t *testing.T) {
c := testConfig(t, map[string]interface{}{
"command": "echo foo",

Loading…
Cancel
Save