2014-07-09 22:34:08 +02:00
|
|
|
package localexec
|
|
|
|
|
|
|
|
import (
|
2016-12-22 23:11:41 +01:00
|
|
|
"context"
|
2014-07-09 22:34:08 +02:00
|
|
|
"fmt"
|
2014-10-06 08:05:49 +02:00
|
|
|
"io"
|
2017-02-01 00:42:56 +01:00
|
|
|
"os"
|
2014-07-09 22:34:08 +02:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/armon/circbuf"
|
2016-12-22 23:11:41 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-09 22:34:08 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-10-06 08:05:49 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
2014-07-09 22:34:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// maxBufSize limits how much output we collect from a local
|
|
|
|
// invocation. This is to prevent TF memory usage from growing
|
|
|
|
// to an enormous amount due to a faulty process.
|
|
|
|
maxBufSize = 8 * 1024
|
|
|
|
)
|
|
|
|
|
2016-12-22 23:11:41 +01:00
|
|
|
func Provisioner() terraform.ResourceProvisioner {
|
|
|
|
return &schema.Provisioner{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"command": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-08-22 19:07:32 +02:00
|
|
|
|
|
|
|
"interpreter": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-12-22 23:11:41 +01:00
|
|
|
},
|
2014-07-09 22:34:08 +02:00
|
|
|
|
2016-12-22 23:11:41 +01:00
|
|
|
ApplyFunc: applyFn,
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
2016-12-22 23:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func applyFn(ctx context.Context) error {
|
|
|
|
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
|
|
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
|
|
|
|
|
|
|
command := data.Get("command").(string)
|
2017-08-22 19:07:32 +02:00
|
|
|
|
2016-12-22 23:11:41 +01:00
|
|
|
if command == "" {
|
|
|
|
return fmt.Errorf("local-exec provisioner command must be a non-empty string")
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the command using a shell
|
2017-08-22 19:07:32 +02:00
|
|
|
interpreter := data.Get("interpreter").([]interface{})
|
|
|
|
|
|
|
|
var cmdargs []string
|
|
|
|
if len(interpreter) > 0 {
|
|
|
|
for _, i := range interpreter {
|
|
|
|
if arg, ok := i.(string); ok {
|
|
|
|
cmdargs = append(cmdargs, arg)
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 22:34:08 +02:00
|
|
|
} else {
|
2017-08-22 19:07:32 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cmdargs = []string{"cmd", "/C"}
|
|
|
|
} else {
|
|
|
|
cmdargs = []string{"/bin/sh", "-c"}
|
|
|
|
}
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
2017-08-22 19:07:32 +02:00
|
|
|
cmdargs = append(cmdargs, command)
|
2014-07-09 22:34:08 +02:00
|
|
|
|
2017-02-01 00:42:56 +01:00
|
|
|
// 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.
|
|
|
|
// See golang.org/issue/18874
|
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize pipe for output: %s", err)
|
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
2014-07-09 22:34:08 +02:00
|
|
|
// Setup the command
|
2017-08-22 19:07:32 +02:00
|
|
|
cmd := exec.CommandContext(ctx, cmdargs[0], cmdargs[1:]...)
|
2017-02-01 00:42:56 +01:00
|
|
|
cmd.Stderr = pw
|
|
|
|
cmd.Stdout = pw
|
2017-02-01 00:07:26 +01:00
|
|
|
|
2014-07-09 22:34:08 +02:00
|
|
|
output, _ := circbuf.NewBuffer(maxBufSize)
|
2017-02-01 00:42:56 +01:00
|
|
|
|
|
|
|
// Write everything we read from the pipe to the output buffer too
|
|
|
|
tee := io.TeeReader(pr, output)
|
|
|
|
|
|
|
|
// copy the teed output to the UI output
|
|
|
|
copyDoneCh := make(chan struct{})
|
|
|
|
go copyOutput(o, tee, copyDoneCh)
|
2014-10-06 08:05:49 +02:00
|
|
|
|
|
|
|
// Output what we're about to run
|
2017-08-22 19:07:32 +02:00
|
|
|
o.Output(fmt.Sprintf("Executing: %q", cmdargs))
|
2014-07-09 22:34:08 +02:00
|
|
|
|
2016-12-23 01:06:40 +01:00
|
|
|
// Start the command
|
2017-02-01 00:42:56 +01:00
|
|
|
err = cmd.Start()
|
2016-12-23 01:06:40 +01:00
|
|
|
if err == nil {
|
2017-02-01 00:42:56 +01:00
|
|
|
err = cmd.Wait()
|
2016-12-23 01:06:40 +01:00
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
|
|
|
// Close the write-end of the pipe so that the goroutine mirroring output
|
|
|
|
// ends properly.
|
|
|
|
pw.Close()
|
2017-02-01 00:42:56 +01:00
|
|
|
|
|
|
|
// Cancelling the command may block the pipe reader if the file descriptor
|
|
|
|
// was passed to a child process which hasn't closed it. In this case the
|
|
|
|
// copyOutput goroutine will just hang out until exit.
|
|
|
|
select {
|
|
|
|
case <-copyDoneCh:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return fmt.Errorf("Error running command '%s': %v. Output: %s",
|
2014-07-09 22:34:08 +02:00
|
|
|
command, err, output.Bytes())
|
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
2014-07-22 19:38:39 +02:00
|
|
|
return nil
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 23:11:41 +01:00
|
|
|
func copyOutput(o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
2014-10-06 08:05:49 +02:00
|
|
|
defer close(doneCh)
|
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|