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"
|
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
|
|
|
|
2018-03-20 18:06:28 +01:00
|
|
|
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
|
|
|
|
defer cancel()
|
2016-12-27 01:29:44 +01:00
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Wait and retry until we establish the connection
|
2018-03-20 18:06:28 +01:00
|
|
|
err := communicator.Retry(retryCtx, func() error {
|
2018-02-15 20:16:37 +01:00
|
|
|
return comm.Connect(o)
|
2014-07-15 02:24:22 +02:00
|
|
|
})
|
2014-07-15 01:12:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:06:28 +01:00
|
|
|
// Wait for the context to end and then disconnect
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
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
|
2018-02-15 21:01:55 +01:00
|
|
|
|
2014-10-06 08:23:30 +02:00
|
|
|
outR, outW := io.Pipe()
|
|
|
|
errR, errW := io.Pipe()
|
2018-02-15 21:01:55 +01:00
|
|
|
defer outW.Close()
|
|
|
|
defer errW.Close()
|
2018-02-15 00:21:26 +01:00
|
|
|
|
2018-02-15 21:01:55 +01:00
|
|
|
go copyOutput(o, outR)
|
|
|
|
go copyOutput(o, errR)
|
2014-07-15 01:12:05 +02:00
|
|
|
|
2018-02-15 21:01:55 +01:00
|
|
|
remotePath := comm.ScriptPath()
|
2015-04-09 21:58:00 +02:00
|
|
|
|
2018-02-15 21:01:55 +01:00
|
|
|
if err := comm.UploadScript(remotePath, script); err != nil {
|
|
|
|
return fmt.Errorf("Failed to upload script: %v", err)
|
2014-07-15 01:12:05 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:01:55 +01:00
|
|
|
cmd = &remote.Cmd{
|
|
|
|
Command: remotePath,
|
|
|
|
Stdout: outW,
|
|
|
|
Stderr: errW,
|
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf("Error starting script: %v", err)
|
2016-12-27 01:29:44 +01:00
|
|
|
}
|
2018-03-15 17:25:44 +01:00
|
|
|
|
2018-03-16 15:54:53 +01:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2018-03-23 16:36:57 +01:00
|
|
|
return err
|
2018-02-15 21:01:55 +01:00
|
|
|
}
|
2014-10-06 08:23:30 +02:00
|
|
|
|
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-07-15 01:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-23 02:05:45 +01:00
|
|
|
func copyOutput(
|
2018-02-15 21:01:55 +01:00
|
|
|
o terraform.UIOutput, r io.Reader) {
|
2014-10-06 08:23:30 +02:00
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|