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"
|
2020-11-25 15:24:10 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
2014-10-06 08:05:49 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
2020-11-25 15:24:10 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
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
|
|
|
|
)
|
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
func New() provisioners.Interface {
|
2021-01-08 18:36:45 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
return &provisioner{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
2020-11-25 15:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type provisioner struct {
|
2021-01-08 18:36:45 +01:00
|
|
|
// We store a context here tied to the lifetime of the provisioner.
|
|
|
|
// This allows the Stop method to cancel any in-flight requests.
|
|
|
|
ctx context.Context
|
2020-11-25 15:24:10 +01:00
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) GetSchema() (resp provisioners.GetSchemaResponse) {
|
|
|
|
schema := &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"command": {
|
|
|
|
Type: cty.String,
|
2016-12-22 23:11:41 +01:00
|
|
|
Required: true,
|
|
|
|
},
|
2020-11-25 15:24:10 +01:00
|
|
|
"interpreter": {
|
|
|
|
Type: cty.List(cty.String),
|
2017-08-22 19:07:32 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
2020-11-25 15:24:10 +01:00
|
|
|
"working_dir": {
|
|
|
|
Type: cty.String,
|
2018-02-16 20:31:11 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2020-11-25 15:24:10 +01:00
|
|
|
"environment": {
|
|
|
|
Type: cty.Map(cty.String),
|
2018-03-06 00:58:49 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2016-12-22 23:11:41 +01:00
|
|
|
},
|
2020-11-25 15:24:10 +01:00
|
|
|
}
|
2014-07-09 22:34:08 +02:00
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
resp.Provisioner = schema
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) ValidateProvisionerConfig(req provisioners.ValidateProvisionerConfigRequest) (resp provisioners.ValidateProvisionerConfigResponse) {
|
|
|
|
if _, err := p.GetSchema().Provisioner.CoerceValue(req.Config); err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
2020-11-25 15:24:10 +01:00
|
|
|
return resp
|
2016-12-22 23:11:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceRequest) (resp provisioners.ProvisionResourceResponse) {
|
|
|
|
command := req.Config.GetAttr("command").AsString()
|
2016-12-22 23:11:41 +01:00
|
|
|
if command == "" {
|
2020-11-25 15:24:10 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("local-exec provisioner command must be a non-empty string"))
|
|
|
|
return resp
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
envVal := req.Config.GetAttr("environment")
|
2018-03-06 00:58:49 +01:00
|
|
|
var env []string
|
2020-11-25 15:24:10 +01:00
|
|
|
|
|
|
|
if !envVal.IsNull() {
|
|
|
|
for k, v := range envVal.AsValueMap() {
|
|
|
|
entry := fmt.Sprintf("%s=%s", k, v.AsString())
|
|
|
|
env = append(env, entry)
|
|
|
|
}
|
2018-03-06 00:58:49 +01:00
|
|
|
}
|
|
|
|
|
2014-07-09 22:34:08 +02:00
|
|
|
// Execute the command using a shell
|
2020-11-25 15:24:10 +01:00
|
|
|
intrVal := req.Config.GetAttr("interpreter")
|
2017-08-22 19:07:32 +02:00
|
|
|
|
|
|
|
var cmdargs []string
|
2020-11-25 15:24:10 +01:00
|
|
|
if !intrVal.IsNull() && intrVal.LengthInt() > 0 {
|
|
|
|
for _, v := range intrVal.AsValueSlice() {
|
|
|
|
cmdargs = append(cmdargs, v.AsString())
|
2017-08-22 19:07:32 +02:00
|
|
|
}
|
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
|
|
|
}
|
2020-11-25 15:24:10 +01:00
|
|
|
|
2017-08-22 19:07:32 +02:00
|
|
|
cmdargs = append(cmdargs, command)
|
2014-07-09 22:34:08 +02:00
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
workingdir := ""
|
|
|
|
if wdVal := req.Config.GetAttr("working_dir"); !wdVal.IsNull() {
|
|
|
|
workingdir = wdVal.AsString()
|
|
|
|
}
|
2018-02-16 20:31:11 +01:00
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up the reader that will read the output from the command.
|
2017-02-01 00:42:56 +01:00
|
|
|
// 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 {
|
2020-11-25 15:24:10 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("failed to initialize pipe for output: %s", err))
|
|
|
|
return resp
|
2017-02-01 00:42:56 +01:00
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
2018-03-06 00:58:49 +01:00
|
|
|
var cmdEnv []string
|
|
|
|
cmdEnv = os.Environ()
|
|
|
|
cmdEnv = append(cmdEnv, env...)
|
|
|
|
|
2021-01-26 20:39:11 +01:00
|
|
|
// Set up the command
|
2021-01-08 18:36:45 +01:00
|
|
|
cmd := exec.CommandContext(p.ctx, cmdargs[0], cmdargs[1:]...)
|
2017-02-01 00:42:56 +01:00
|
|
|
cmd.Stderr = pw
|
|
|
|
cmd.Stdout = pw
|
2018-02-16 20:31:11 +01:00
|
|
|
// 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
|
2018-03-06 00:58:49 +01:00
|
|
|
// Env specifies the environment of the command.
|
|
|
|
// By default will use the calling process's environment
|
|
|
|
cmd.Env = cmdEnv
|
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{})
|
2020-11-25 15:24:10 +01:00
|
|
|
go copyUIOutput(req.UIOutput, tee, copyDoneCh)
|
2014-10-06 08:05:49 +02:00
|
|
|
|
|
|
|
// Output what we're about to run
|
2020-11-25 15:24:10 +01:00
|
|
|
req.UIOutput.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:
|
2021-01-08 18:36:45 +01:00
|
|
|
case <-p.ctx.Done():
|
2017-02-01 00:42:56 +01:00
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-11-25 15:24:10 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Error running command '%s': %v. Output: %s",
|
|
|
|
command, err, output.Bytes()))
|
|
|
|
return resp
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
2014-10-06 08:05:49 +02:00
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) Stop() error {
|
|
|
|
p.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) Close() error {
|
2014-07-22 19:38:39 +02:00
|
|
|
return nil
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:24:10 +01:00
|
|
|
func copyUIOutput(o provisioners.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)
|
|
|
|
}
|
|
|
|
}
|