2014-07-10 22:30:44 +02:00
|
|
|
package remoteexec
|
|
|
|
|
|
|
|
import (
|
2014-07-14 23:28:37 +02:00
|
|
|
"bytes"
|
2016-12-23 02:05:45 +01:00
|
|
|
"context"
|
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"
|
2017-02-07 01:51:51 +01:00
|
|
|
"strings"
|
2016-12-27 01:29:44 +01:00
|
|
|
"sync/atomic"
|
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"
|
2016-12-23 02:05:45 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
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
|
|
|
)
|
|
|
|
|
2017-08-09 21:05:51 +02:00
|
|
|
// maxBackoffDealy is the maximum delay between retry attempts
|
|
|
|
var maxBackoffDelay = 10 * time.Second
|
|
|
|
var initialBackoffDelay = time.Second
|
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
func Provisioner() terraform.ResourceProvisioner {
|
|
|
|
return &schema.Provisioner{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-05-01 22:48:42 +02:00
|
|
|
"inline": {
|
2016-12-23 02:05:45 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-12-23 02:55:23 +01:00
|
|
|
PromoteSingle: true,
|
2016-12-23 02:05:45 +01:00
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"script", "scripts"},
|
|
|
|
},
|
|
|
|
|
2017-05-01 22:48:42 +02:00
|
|
|
"script": {
|
2016-12-23 02:05:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"inline", "scripts"},
|
|
|
|
},
|
|
|
|
|
2017-05-01 22:48:42 +02:00
|
|
|
"scripts": {
|
2016-12-23 02:05:45 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"script", "inline"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ApplyFunc: applyFn,
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 22:30:44 +02:00
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Apply executes the remote exec provisioner
|
2016-12-23 02:05:45 +01:00
|
|
|
func applyFn(ctx context.Context) error {
|
|
|
|
connState := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
|
|
|
|
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
|
|
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Get a new communicator
|
2016-12-23 02:05:45 +01:00
|
|
|
comm, err := communicator.New(connState)
|
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
|
2016-12-23 02:05:45 +01:00
|
|
|
scripts, err := collectScripts(data)
|
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
|
2016-12-27 01:29:44 +01:00
|
|
|
if err := runScripts(ctx, 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-10 22:30:44 +02:00
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
return nil
|
2014-07-10 22:30:44 +02:00
|
|
|
}
|
2014-07-14 21:47:04 +02:00
|
|
|
|
2017-01-20 15:04:43 +01:00
|
|
|
// generateScripts takes the configuration and creates a script from each inline config
|
2016-12-23 02:05:45 +01:00
|
|
|
func generateScripts(d *schema.ResourceData) ([]string, error) {
|
2017-02-07 01:51:51 +01:00
|
|
|
var lines []string
|
2016-12-23 02:05:45 +01:00
|
|
|
for _, l := range d.Get("inline").([]interface{}) {
|
2017-05-01 22:48:42 +02:00
|
|
|
line, ok := l.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Error parsing %v as a string", l)
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
2014-07-14 21:47:04 +02:00
|
|
|
}
|
2017-02-07 01:51:51 +01:00
|
|
|
lines = append(lines, "")
|
|
|
|
|
|
|
|
return []string{strings.Join(lines, "\n")}, nil
|
2014-07-14 21:47:04 +02:00
|
|
|
}
|
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.
|
2016-12-23 02:05:45 +01:00
|
|
|
func collectScripts(d *schema.ResourceData) ([]io.ReadCloser, error) {
|
2014-07-14 23:28:37 +02:00
|
|
|
// Check if inline
|
2016-12-23 02:05:45 +01:00
|
|
|
if _, ok := d.GetOk("inline"); ok {
|
|
|
|
scripts, err := generateScripts(d)
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-20 15:04:43 +01:00
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
var r []io.ReadCloser
|
2017-01-20 15:04:43 +01:00
|
|
|
for _, script := range scripts {
|
|
|
|
r = append(r, ioutil.NopCloser(bytes.NewReader([]byte(script))))
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect scripts
|
|
|
|
var scripts []string
|
2016-12-23 02:05:45 +01:00
|
|
|
if script, ok := d.GetOk("script"); ok {
|
2017-05-01 22:48:42 +02:00
|
|
|
scr, ok := script.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Error parsing script %v as string", script)
|
|
|
|
}
|
|
|
|
scripts = append(scripts, scr)
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
if scriptList, ok := d.GetOk("scripts"); ok {
|
|
|
|
for _, script := range scriptList.([]interface{}) {
|
2017-05-01 22:48:42 +02:00
|
|
|
scr, ok := script.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Error parsing script %v as string", script)
|
|
|
|
}
|
|
|
|
scripts = append(scripts, scr)
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-12-23 02:05:45 +01:00
|
|
|
func runScripts(
|
2016-12-27 01:29:44 +01:00
|
|
|
ctx context.Context,
|
2014-10-06 08:23:30 +02:00
|
|
|
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 {
|
2016-12-27 01:29:44 +01:00
|
|
|
// Wrap out context in a cancelation function that we use to
|
|
|
|
// kill the connection.
|
|
|
|
ctx, cancelFunc := context.WithCancel(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
// Wait for the context to end and then disconnect
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
comm.Disconnect()
|
|
|
|
}()
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Wait and retry until we establish the connection
|
2016-12-27 01:29:44 +01:00
|
|
|
err := retryFunc(ctx, comm.Timeout(), func() error {
|
2015-04-09 21:58:00 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
2016-12-23 02:05:45 +01:00
|
|
|
go copyOutput(o, outR, outDoneCh)
|
|
|
|
go copyOutput(o, errR, errDoneCh)
|
2014-10-06 08:23:30 +02:00
|
|
|
|
2016-03-21 15:53:54 +01:00
|
|
|
remotePath := comm.ScriptPath()
|
2016-12-27 01:29:44 +01:00
|
|
|
err = retryFunc(ctx, comm.Timeout(), func() error {
|
2015-04-30 18:02:33 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-27 01:29:44 +01:00
|
|
|
// If we have an error, end our context so the disconnect happens.
|
|
|
|
// This has to happen before the output cleanup below since during
|
|
|
|
// an interrupt this will cause the outputs to end.
|
|
|
|
if err != nil {
|
|
|
|
cancelFunc()
|
|
|
|
}
|
|
|
|
|
2014-10-06 08:23:30 +02:00
|
|
|
// Wait for output to clean up
|
|
|
|
outW.Close()
|
|
|
|
errW.Close()
|
|
|
|
<-outDoneCh
|
|
|
|
<-errDoneCh
|
|
|
|
|
2016-03-21 15:53:54 +01:00
|
|
|
// Upload a blank follow up file in the same path to prevent residual
|
|
|
|
// script contents from remaining on remote machine
|
|
|
|
empty := bytes.NewReader([]byte(""))
|
|
|
|
if err := comm.Upload(remotePath, empty); err != nil {
|
|
|
|
// This feature is best-effort.
|
|
|
|
log.Printf("[WARN] Failed to upload empty follow up script: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 08:23:30 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
func copyOutput(
|
2014-10-06 08:23:30 +02:00
|
|
|
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
|
2016-12-27 01:29:44 +01:00
|
|
|
func retryFunc(ctx context.Context, timeout time.Duration, f func() error) error {
|
|
|
|
// Build a new context with the timeout
|
|
|
|
ctx, done := context.WithTimeout(ctx, timeout)
|
|
|
|
defer done()
|
2014-07-15 01:26:47 +02:00
|
|
|
|
2017-02-08 01:00:27 +01:00
|
|
|
// container for atomic error value
|
|
|
|
type errWrap struct {
|
|
|
|
E error
|
|
|
|
}
|
|
|
|
|
2016-12-27 01:29:44 +01:00
|
|
|
// Try the function in a goroutine
|
|
|
|
var errVal atomic.Value
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
|
2017-08-09 21:05:51 +02:00
|
|
|
delay := time.Duration(0)
|
2016-12-27 01:29:44 +01:00
|
|
|
for {
|
|
|
|
// If our context ended, we want to exit right away.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2017-08-09 21:05:51 +02:00
|
|
|
case <-time.After(delay):
|
2016-12-27 01:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try the function call
|
|
|
|
err := f()
|
2017-02-08 01:00:27 +01:00
|
|
|
errVal.Store(&errWrap{err})
|
|
|
|
|
2016-12-27 01:29:44 +01:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-09 21:05:51 +02:00
|
|
|
log.Printf("[WARN] retryable error: %v", err)
|
|
|
|
|
|
|
|
delay *= 2
|
|
|
|
|
|
|
|
if delay == 0 {
|
|
|
|
delay = initialBackoffDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
if delay > maxBackoffDelay {
|
|
|
|
delay = maxBackoffDelay
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] sleeping for %s", delay)
|
2014-07-15 01:26:47 +02:00
|
|
|
}
|
2016-12-27 01:29:44 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for completion
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-02-08 01:00:27 +01:00
|
|
|
case <-doneCh:
|
2016-12-27 01:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a context error to check if we're interrupted or timeout
|
|
|
|
switch ctx.Err() {
|
|
|
|
case context.Canceled:
|
|
|
|
return fmt.Errorf("interrupted")
|
|
|
|
case context.DeadlineExceeded:
|
|
|
|
return fmt.Errorf("timeout")
|
2014-07-15 01:26:47 +02:00
|
|
|
}
|
2016-12-27 01:29:44 +01:00
|
|
|
|
|
|
|
// Check if we got an error executing
|
2017-02-08 01:00:27 +01:00
|
|
|
if ev, ok := errVal.Load().(errWrap); ok {
|
|
|
|
return ev.E
|
2016-12-27 01:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-07-15 01:26:47 +02:00
|
|
|
}
|