github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/provisioners/local-exec/resource_provisioner.go (about) 1 package localexec 2 3 import ( 4 "fmt" 5 "os/exec" 6 "runtime" 7 8 "github.com/armon/circbuf" 9 "github.com/hashicorp/terraform/helper/config" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 const ( 14 // maxBufSize limits how much output we collect from a local 15 // invocation. This is to prevent TF memory usage from growing 16 // to an enormous amount due to a faulty process. 17 maxBufSize = 8 * 1024 18 ) 19 20 type ResourceProvisioner struct{} 21 22 func (p *ResourceProvisioner) Apply( 23 s *terraform.ResourceState, 24 c *terraform.ResourceConfig) error { 25 26 // Get the command 27 commandRaw, ok := c.Config["command"] 28 if !ok { 29 return fmt.Errorf("local-exec provisioner missing 'command'") 30 } 31 command, ok := commandRaw.(string) 32 if !ok { 33 return fmt.Errorf("local-exec provisioner command must be a string") 34 } 35 36 // Execute the command using a shell 37 var shell, flag string 38 if runtime.GOOS == "windows" { 39 shell = "cmd" 40 flag = "/C" 41 } else { 42 shell = "/bin/sh" 43 flag = "-c" 44 } 45 46 // Setup the command 47 cmd := exec.Command(shell, flag, command) 48 output, _ := circbuf.NewBuffer(maxBufSize) 49 cmd.Stderr = output 50 cmd.Stdout = output 51 52 // Run the command to completion 53 if err := cmd.Run(); err != nil { 54 return fmt.Errorf("Error running command '%s': %v. Output: %s", 55 command, err, output.Bytes()) 56 } 57 return nil 58 } 59 60 func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) { 61 validator := config.Validator{ 62 Required: []string{"command"}, 63 } 64 return validator.Validate(c) 65 }