2014-07-09 22:34:08 +02:00
|
|
|
package localexec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/armon/circbuf"
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceProvisioner struct{}
|
|
|
|
|
|
|
|
func (p *ResourceProvisioner) Apply(
|
2014-09-17 02:08:27 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 19:38:39 +02:00
|
|
|
c *terraform.ResourceConfig) error {
|
2014-07-09 22:34:08 +02:00
|
|
|
|
|
|
|
// Get the command
|
2014-07-10 00:50:10 +02:00
|
|
|
commandRaw, ok := c.Config["command"]
|
2014-07-09 22:34:08 +02:00
|
|
|
if !ok {
|
2014-07-22 19:38:39 +02:00
|
|
|
return fmt.Errorf("local-exec provisioner missing 'command'")
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
command, ok := commandRaw.(string)
|
|
|
|
if !ok {
|
2014-07-22 19:38:39 +02:00
|
|
|
return fmt.Errorf("local-exec provisioner command must be a string")
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the command using a shell
|
|
|
|
var shell, flag string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
shell = "cmd"
|
|
|
|
flag = "/C"
|
|
|
|
} else {
|
|
|
|
shell = "/bin/sh"
|
|
|
|
flag = "-c"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the command
|
|
|
|
cmd := exec.Command(shell, flag, command)
|
|
|
|
output, _ := circbuf.NewBuffer(maxBufSize)
|
|
|
|
cmd.Stderr = output
|
|
|
|
cmd.Stdout = output
|
|
|
|
|
|
|
|
// Run the command to completion
|
|
|
|
if err := cmd.Run(); 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-07-22 19:38:39 +02:00
|
|
|
return nil
|
2014-07-09 22:34:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
validator := config.Validator{
|
|
|
|
Required: []string{"command"},
|
|
|
|
}
|
|
|
|
return validator.Validate(c)
|
|
|
|
}
|