2014-07-15 23:37:55 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2016-12-23 01:17:34 +01:00
|
|
|
"context"
|
2020-11-25 23:06:17 +01:00
|
|
|
"errors"
|
2014-07-16 01:52:00 +02:00
|
|
|
"fmt"
|
2015-11-13 12:50:31 +01:00
|
|
|
"io/ioutil"
|
2014-07-16 01:52:00 +02:00
|
|
|
"os"
|
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2021-05-17 19:00:12 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/communicator"
|
2021-05-17 19:51:48 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/provisioners"
|
2015-04-17 01:53:04 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2020-11-25 23:06:17 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-07-15 23:37:55 +02:00
|
|
|
)
|
|
|
|
|
2020-11-25 23:06:17 +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 23:06:17 +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 23:06:17 +01:00
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) GetSchema() (resp provisioners.GetSchemaResponse) {
|
|
|
|
schema := &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"source": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
2016-12-23 01:17:34 +01:00
|
|
|
},
|
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
"content": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
2016-12-23 01:17:34 +01:00
|
|
|
},
|
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
"destination": {
|
|
|
|
Type: cty.String,
|
2016-12-23 01:17:34 +01:00
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
2020-11-25 23:06:17 +01:00
|
|
|
}
|
|
|
|
resp.Provisioner = schema
|
|
|
|
return resp
|
|
|
|
}
|
2016-12-23 01:17:34 +01:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
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)
|
2016-12-23 01:17:34 +01:00
|
|
|
}
|
2020-11-25 23:06:17 +01:00
|
|
|
|
|
|
|
source := cfg.GetAttr("source")
|
|
|
|
content := cfg.GetAttr("content")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !source.IsNull() && !content.IsNull():
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("Cannot set both 'source' and 'content'"))
|
|
|
|
return resp
|
|
|
|
case source.IsNull() && content.IsNull():
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("Must provide one of 'source' or 'content'"))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2016-12-23 01:17:34 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 23:06:17 +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 23:06:17 +01:00
|
|
|
comm, err := communicator.New(req.Connection)
|
2014-07-16 01:52:00 +02:00
|
|
|
if err != nil {
|
2020-11-25 23:06:17 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
// Get the source
|
2020-11-25 23:06:17 +01:00
|
|
|
src, deleteSource, err := getSrc(req.Config)
|
2015-04-17 01:53:04 +02:00
|
|
|
if err != nil {
|
2020-11-25 23:06:17 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2015-04-17 01:53:04 +02:00
|
|
|
}
|
2016-07-08 20:34:37 +02:00
|
|
|
if deleteSource {
|
|
|
|
defer os.Remove(src)
|
|
|
|
}
|
2015-04-17 01:53:04 +02:00
|
|
|
|
2016-12-23 01:47:32 +01:00
|
|
|
// Begin the file copy
|
2020-11-25 23:06:17 +01:00
|
|
|
dst := req.Config.GetAttr("destination").AsString()
|
2021-01-08 18:36:45 +01:00
|
|
|
if err := copyFiles(p.ctx, comm, src, dst); err != nil {
|
2020-11-25 23:06:17 +01:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
2017-06-15 19:57:04 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
return resp
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
// getSrc returns the file to use as source
|
2020-11-25 23:06:17 +01:00
|
|
|
func getSrc(v cty.Value) (string, bool, error) {
|
|
|
|
content := v.GetAttr("content")
|
|
|
|
src := v.GetAttr("source")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !content.IsNull():
|
2015-11-13 12:50:31 +01:00
|
|
|
file, err := ioutil.TempFile("", "tf-file-content")
|
|
|
|
if err != nil {
|
2016-07-08 20:34:37 +02:00
|
|
|
return "", true, err
|
2015-11-13 12:50:31 +01:00
|
|
|
}
|
2016-07-08 20:34:37 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
if _, err = file.WriteString(content.AsString()); err != nil {
|
2016-07-08 20:34:37 +02:00
|
|
|
return "", true, err
|
2015-11-13 12:50:31 +01:00
|
|
|
}
|
2016-07-08 20:34:37 +02:00
|
|
|
|
|
|
|
return file.Name(), true, nil
|
2015-11-13 12:50:31 +01:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
case !src.IsNull():
|
|
|
|
expansion, err := homedir.Expand(src.AsString())
|
|
|
|
return expansion, false, err
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("source and content cannot both be null")
|
|
|
|
}
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// copyFiles is used to copy the files from a source to a destination
|
2018-02-15 00:30:20 +01:00
|
|
|
func copyFiles(ctx context.Context, comm communicator.Communicator, src, dst string) error {
|
2018-03-20 18:06:28 +01:00
|
|
|
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
|
|
|
|
defer cancel()
|
|
|
|
|
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 21:01:55 +01:00
|
|
|
return comm.Connect(nil)
|
2014-07-16 01:52:00 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-15 21:01:55 +01:00
|
|
|
|
|
|
|
// disconnect when the context is canceled, which will close this after
|
|
|
|
// Apply as well.
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
comm.Disconnect()
|
|
|
|
}()
|
2014-07-16 01:52:00 +02:00
|
|
|
|
|
|
|
info, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're uploading a directory, short circuit and do that
|
|
|
|
if info.IsDir() {
|
2015-04-10 20:34:46 +02:00
|
|
|
if err := comm.UploadDir(dst, src); err != nil {
|
2014-07-16 01:52:00 +02:00
|
|
|
return fmt.Errorf("Upload failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're uploading a file...
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
err = comm.Upload(dst, f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Upload failed: %v", err)
|
|
|
|
}
|
2020-11-25 23:06:17 +01:00
|
|
|
|
2014-07-16 01:52:00 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-11-25 23:06:17 +01:00
|
|
|
|
|
|
|
func (p *provisioner) Stop() error {
|
|
|
|
p.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|