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"
|
2020-11-25 20:06:32 +01:00
|
|
|
"errors"
|
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-12 01:02:51 +02:00
|
|
|
|
2021-05-17 19:00:12 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/communicator"
|
|
|
|
"github.com/hashicorp/terraform/internal/communicator/remote"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 19:51:48 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/provisioners"
|
2014-10-06 08:23:30 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
2020-11-25 20:06:32 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-07-14 21:47:04 +02:00
|
|
|
)
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
func New() provisioners.Interface {
|
2021-01-08 18:36:45 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
return &provisioner{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
2020-11-25 20:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type provisioner struct {
|
2021-01-08 18:36:45 +01:00
|
|
|
// We store a context here tied to the lifetime of the provisioner.
|
|
|
|
// This allows the Stop method to cancel any in-flight requests.
|
|
|
|
ctx context.Context
|
2020-11-25 20:06:32 +01:00
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
2017-08-09 21:05:51 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
func (p *provisioner) GetSchema() (resp provisioners.GetSchemaResponse) {
|
|
|
|
schema := &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
2017-05-01 22:48:42 +02:00
|
|
|
"inline": {
|
2020-11-25 20:06:32 +01:00
|
|
|
Type: cty.List(cty.String),
|
|
|
|
Optional: true,
|
2016-12-23 02:05:45 +01:00
|
|
|
},
|
2017-05-01 22:48:42 +02:00
|
|
|
"script": {
|
2020-11-25 20:06:32 +01:00
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
2016-12-23 02:05:45 +01:00
|
|
|
},
|
2017-05-01 22:48:42 +02:00
|
|
|
"scripts": {
|
2020-11-25 20:06:32 +01:00
|
|
|
Type: cty.List(cty.String),
|
|
|
|
Optional: true,
|
2016-12-23 02:05:45 +01:00
|
|
|
},
|
|
|
|
},
|
2020-11-25 20:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Provisioner = schema
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) ValidateProvisionerConfig(req provisioners.ValidateProvisionerConfigRequest) (resp provisioners.ValidateProvisionerConfigResponse) {
|
|
|
|
cfg, err := p.GetSchema().Provisioner.CoerceValue(req.Config)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
inline := cfg.GetAttr("inline")
|
|
|
|
script := cfg.GetAttr("script")
|
|
|
|
scripts := cfg.GetAttr("scripts")
|
2016-12-23 02:05:45 +01:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
set := 0
|
|
|
|
if !inline.IsNull() {
|
|
|
|
set++
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2020-11-25 20:06:32 +01:00
|
|
|
if !script.IsNull() {
|
|
|
|
set++
|
|
|
|
}
|
|
|
|
if !scripts.IsNull() {
|
|
|
|
set++
|
|
|
|
}
|
|
|
|
if set != 1 {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New(
|
|
|
|
`only one of "inline", "script", or "scripts" must be set`))
|
|
|
|
}
|
|
|
|
return resp
|
2016-12-23 02:05:45 +01:00
|
|
|
}
|
2014-07-10 22:30:44 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceRequest) (resp provisioners.ProvisionResourceResponse) {
|
2021-02-24 19:25:58 +01:00
|
|
|
if req.Connection.IsNull() {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing connection configuration for provisioner"))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
comm, err := communicator.New(req.Connection)
|
2014-07-14 21:47:04 +02:00
|
|
|
if err != nil {
|
2020-11-25 20:06:32 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2014-07-14 21:47:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 23:28:37 +02:00
|
|
|
// Collect the scripts
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts, err := collectScripts(req.Config)
|
2014-07-14 23:28:37 +02:00
|
|
|
if err != nil {
|
2020-11-25 20:06:32 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
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
|
2021-01-08 18:36:45 +01:00
|
|
|
if err := runScripts(p.ctx, req.UIOutput, comm, scripts); err != nil {
|
2020-11-25 20:06:32 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2014-07-15 01:12:05 +02:00
|
|
|
}
|
2014-07-10 22:30:44 +02:00
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) Stop() error {
|
|
|
|
p.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) Close() error {
|
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
|
2020-11-25 20:06:32 +01:00
|
|
|
func generateScripts(inline cty.Value) ([]string, error) {
|
2017-02-07 01:51:51 +01:00
|
|
|
var lines []string
|
2020-11-25 20:06:32 +01:00
|
|
|
for _, l := range inline.AsValueSlice() {
|
2021-04-20 18:31:32 +02:00
|
|
|
if l.IsNull() {
|
|
|
|
return nil, errors.New("invalid null string in 'scripts'")
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
s := l.AsString()
|
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("invalid empty string in 'scripts'")
|
2017-05-01 22:48:42 +02:00
|
|
|
}
|
2020-11-25 20:06:32 +01:00
|
|
|
lines = append(lines, s)
|
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.
|
2020-11-25 20:06:32 +01:00
|
|
|
func collectScripts(v cty.Value) ([]io.ReadCloser, error) {
|
2014-07-14 23:28:37 +02:00
|
|
|
// Check if inline
|
2020-11-25 20:06:32 +01:00
|
|
|
if inline := v.GetAttr("inline"); !inline.IsNull() {
|
|
|
|
scripts, err := generateScripts(inline)
|
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
|
2020-11-25 20:06:32 +01:00
|
|
|
if script := v.GetAttr("script"); !script.IsNull() {
|
|
|
|
s := script.AsString()
|
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("invalid empty string in 'script'")
|
2017-05-01 22:48:42 +02:00
|
|
|
}
|
2020-11-25 20:06:32 +01:00
|
|
|
scripts = append(scripts, s)
|
2014-07-14 23:28:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
if scriptList := v.GetAttr("scripts"); !scriptList.IsNull() {
|
|
|
|
for _, script := range scriptList.AsValueSlice() {
|
2021-04-20 18:31:32 +02:00
|
|
|
if script.IsNull() {
|
|
|
|
return nil, errors.New("invalid null string in 'script'")
|
|
|
|
}
|
2020-11-25 20:06:32 +01:00
|
|
|
s := script.AsString()
|
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("invalid empty string in 'script'")
|
2017-05-01 22:48:42 +02:00
|
|
|
}
|
2021-04-20 18:31:32 +02:00
|
|
|
scripts = append(scripts, s)
|
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
|
2020-11-25 20:06:32 +01:00
|
|
|
func runScripts(ctx context.Context, o provisioners.UIOutput, comm communicator.Communicator, scripts []io.ReadCloser) error {
|
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
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
go copyUIOutput(o, outR)
|
|
|
|
go copyUIOutput(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
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:32 +01:00
|
|
|
func copyUIOutput(o provisioners.UIOutput, r io.Reader) {
|
2014-10-06 08:23:30 +02:00
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|