2014-07-10 22:30:44 +02:00
|
|
|
package remoteexec
|
|
|
|
|
|
|
|
import (
|
2014-07-14 23:28:37 +02:00
|
|
|
"bytes"
|
2014-07-12 01:02:51 +02:00
|
|
|
"fmt"
|
2014-07-14 23:28:37 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-07-15 01:12:05 +02:00
|
|
|
"log"
|
2014-07-14 23:28:37 +02:00
|
|
|
"os"
|
2014-07-14 21:47:04 +02:00
|
|
|
"strings"
|
2014-07-15 01:26:47 +02:00
|
|
|
"time"
|
2014-07-12 01:02:51 +02:00
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
2014-07-10 22:30:44 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-10-06 08:23:30 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
2014-07-14 21:47:04 +02:00
|
|
|
)
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// ResourceProvisioner represents a remote exec provisioner
|
2014-07-10 22:30:44 +02:00
|
|
|
type ResourceProvisioner struct{}
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Apply executes the remote exec provisioner
|
2014-10-05 01:29:33 +02:00
|
|
|
func (p *ResourceProvisioner) Apply(
|
|
|
|
o terraform.UIOutput,
|
|
|
|
s *terraform.InstanceState,
|
2014-07-22 19:38:39 +02:00
|
|
|
c *terraform.ResourceConfig) error {
|
2015-04-09 21:58:00 +02:00
|
|
|
// Get a new communicator
|
|
|
|
comm, err := communicator.New(s)
|
2014-07-14 21:47:04 +02:00
|
|
|
if err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-14 21:47:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 23:28:37 +02:00
|
|
|
// Collect the scripts
|
2014-07-15 01:12:05 +02:00
|
|
|
scripts, err := p.collectScripts(c)
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
2014-07-15 01:12:05 +02:00
|
|
|
for _, s := range scripts {
|
|
|
|
defer s.Close()
|
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
2014-07-15 01:12:05 +02:00
|
|
|
// Copy and execute each script
|
2015-04-09 21:58:00 +02:00
|
|
|
if err := p.runScripts(o, comm, scripts); err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-15 01:12:05 +02:00
|
|
|
}
|
2014-07-22 19:38:39 +02:00
|
|
|
return nil
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Validate checks if the required arguments are configured
|
2014-07-12 01:02:51 +02:00
|
|
|
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {
|
2014-07-14 21:47:04 +02:00
|
|
|
num := 0
|
2014-07-12 01:02:51 +02:00
|
|
|
for name := range c.Raw {
|
|
|
|
switch name {
|
2015-05-27 03:52:36 +02:00
|
|
|
case "scripts", "script", "inline":
|
2014-07-14 21:47:04 +02:00
|
|
|
num++
|
2014-07-12 01:02:51 +02:00
|
|
|
default:
|
|
|
|
es = append(es, fmt.Errorf("Unknown configuration '%s'", name))
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 21:47:04 +02:00
|
|
|
if num != 1 {
|
|
|
|
es = append(es, fmt.Errorf("Must provide one of 'scripts', 'script' or 'inline' to remote-exec"))
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
2014-07-12 01:02:51 +02:00
|
|
|
return
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
2014-07-14 21:47:04 +02:00
|
|
|
|
|
|
|
// generateScript takes the configuration and creates a script to be executed
|
|
|
|
// from the inline configs
|
|
|
|
func (p *ResourceProvisioner) generateScript(c *terraform.ResourceConfig) (string, error) {
|
2015-04-09 21:58:00 +02:00
|
|
|
var lines []string
|
2014-07-14 21:47:04 +02:00
|
|
|
command, ok := c.Config["inline"]
|
|
|
|
if ok {
|
|
|
|
switch cmd := command.(type) {
|
|
|
|
case string:
|
|
|
|
lines = append(lines, cmd)
|
|
|
|
case []string:
|
|
|
|
lines = append(lines, cmd...)
|
|
|
|
case []interface{}:
|
|
|
|
for _, l := range cmd {
|
|
|
|
lStr, ok := l.(string)
|
|
|
|
if ok {
|
|
|
|
lines = append(lines, lStr)
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("Unsupported 'inline' type! Must be string, or list of strings.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Unsupported 'inline' type! Must be string, or list of strings.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lines = append(lines, "")
|
|
|
|
return strings.Join(lines, "\n"), nil
|
|
|
|
}
|
2014-07-14 23:28:37 +02:00
|
|
|
|
|
|
|
// collectScripts is used to collect all the scripts we need
|
2014-08-07 09:19:56 +02:00
|
|
|
// to execute in preparation for copying them.
|
2014-07-14 23:28:37 +02:00
|
|
|
func (p *ResourceProvisioner) collectScripts(c *terraform.ResourceConfig) ([]io.ReadCloser, error) {
|
|
|
|
// Check if inline
|
|
|
|
_, ok := c.Config["inline"]
|
|
|
|
if ok {
|
|
|
|
script, err := p.generateScript(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rc := ioutil.NopCloser(bytes.NewReader([]byte(script)))
|
|
|
|
return []io.ReadCloser{rc}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect scripts
|
|
|
|
var scripts []string
|
|
|
|
s, ok := c.Config["script"]
|
|
|
|
if ok {
|
|
|
|
sStr, ok := s.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Unsupported 'script' type! Must be a string.")
|
|
|
|
}
|
|
|
|
scripts = append(scripts, sStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
sl, ok := c.Config["scripts"]
|
|
|
|
if ok {
|
|
|
|
switch slt := sl.(type) {
|
|
|
|
case []string:
|
|
|
|
scripts = append(scripts, slt...)
|
|
|
|
case []interface{}:
|
|
|
|
for _, l := range slt {
|
|
|
|
lStr, ok := l.(string)
|
|
|
|
if ok {
|
|
|
|
scripts = append(scripts, lStr)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Unsupported 'scripts' type! Must be list of strings.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported 'scripts' type! Must be list of strings.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open all the scripts
|
|
|
|
var fhs []io.ReadCloser
|
|
|
|
for _, s := range scripts {
|
|
|
|
fh, err := os.Open(s)
|
|
|
|
if err != nil {
|
|
|
|
for _, fh := range fhs {
|
|
|
|
fh.Close()
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Failed to open script '%s': %v", s, err)
|
|
|
|
}
|
|
|
|
fhs = append(fhs, fh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done, return the file handles
|
|
|
|
return fhs, nil
|
|
|
|
}
|
2014-07-15 01:12:05 +02:00
|
|
|
|
|
|
|
// runScripts is used to copy and execute a set of scripts
|
2014-10-06 08:23:30 +02:00
|
|
|
func (p *ResourceProvisioner) runScripts(
|
|
|
|
o terraform.UIOutput,
|
2015-04-09 21:58:00 +02:00
|
|
|
comm communicator.Communicator,
|
2014-10-06 08:23:30 +02:00
|
|
|
scripts []io.ReadCloser) error {
|
2015-04-09 21:58:00 +02:00
|
|
|
// Wait and retry until we establish the connection
|
|
|
|
err := retryFunc(comm.Timeout(), func() error {
|
|
|
|
err := comm.Connect(o)
|
2014-07-15 02:24:22 +02:00
|
|
|
return err
|
|
|
|
})
|
2014-07-15 01:12:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
defer comm.Disconnect()
|
2014-07-15 01:12:05 +02:00
|
|
|
|
|
|
|
for _, script := range scripts {
|
2015-04-09 21:58:00 +02:00
|
|
|
var cmd *remote.Cmd
|
2014-10-06 08:23:30 +02:00
|
|
|
outR, outW := io.Pipe()
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
outDoneCh := make(chan struct{})
|
|
|
|
errDoneCh := make(chan struct{})
|
|
|
|
go p.copyOutput(o, outR, outDoneCh)
|
|
|
|
go p.copyOutput(o, errR, errDoneCh)
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
err = retryFunc(comm.Timeout(), func() error {
|
2015-04-30 18:02:33 +02:00
|
|
|
remotePath := comm.ScriptPath()
|
|
|
|
|
|
|
|
if err := comm.UploadScript(remotePath, script); err != nil {
|
2014-07-15 01:26:47 +02:00
|
|
|
return fmt.Errorf("Failed to upload script: %v", err)
|
|
|
|
}
|
2014-07-15 01:12:05 +02:00
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
cmd = &remote.Cmd{
|
2015-04-30 18:02:33 +02:00
|
|
|
Command: remotePath,
|
2014-10-06 08:23:30 +02:00
|
|
|
Stdout: outW,
|
|
|
|
Stderr: errW,
|
2014-07-15 01:26:47 +02:00
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf("Error starting script: %v", err)
|
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
|
2014-07-15 01:26:47 +02:00
|
|
|
return nil
|
|
|
|
})
|
2014-10-06 08:23:30 +02:00
|
|
|
if err == nil {
|
|
|
|
cmd.Wait()
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
err = fmt.Errorf("Script exited with non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
2014-07-15 01:12:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-06 08:23:30 +02:00
|
|
|
// Wait for output to clean up
|
|
|
|
outW.Close()
|
|
|
|
errW.Close()
|
|
|
|
<-outDoneCh
|
|
|
|
<-errDoneCh
|
|
|
|
|
|
|
|
// If we have an error, return it out now that we've cleaned up
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-15 01:12:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 08:23:30 +02:00
|
|
|
func (p *ResourceProvisioner) copyOutput(
|
|
|
|
o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
|
|
|
defer close(doneCh)
|
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 01:26:47 +02:00
|
|
|
// retryFunc is used to retry a function for a given duration
|
|
|
|
func retryFunc(timeout time.Duration, f func() error) error {
|
|
|
|
finish := time.After(timeout)
|
|
|
|
for {
|
|
|
|
err := f()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("Retryable error: %v", err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-finish:
|
|
|
|
return err
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|