2014-07-15 23:37:55 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2016-12-23 01:17:34 +01:00
|
|
|
"context"
|
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"
|
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator"
|
2016-12-23 01:17:34 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-15 23:37:55 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-04-17 01:53:04 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2014-07-15 23:37:55 +02:00
|
|
|
)
|
|
|
|
|
2016-12-23 01:17:34 +01:00
|
|
|
func Provisioner() terraform.ResourceProvisioner {
|
|
|
|
return &schema.Provisioner{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"source": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"content"},
|
|
|
|
},
|
|
|
|
|
|
|
|
"content": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ConflictsWith: []string{"source"},
|
|
|
|
},
|
|
|
|
|
|
|
|
"destination": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-11-29 21:00:26 +01:00
|
|
|
ApplyFunc: applyFn,
|
2017-05-19 20:42:14 +02:00
|
|
|
ValidateFunc: validateFn,
|
2016-12-23 01:17:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyFn(ctx context.Context) error {
|
2016-12-23 01:43:52 +01:00
|
|
|
connState := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
|
2016-12-23 01:17:34 +01:00
|
|
|
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
2014-07-15 23:37:55 +02:00
|
|
|
|
2015-04-09 21:58:00 +02:00
|
|
|
// Get a new communicator
|
2016-12-23 01:43:52 +01:00
|
|
|
comm, err := communicator.New(connState)
|
2014-07-16 01:52:00 +02:00
|
|
|
if err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 00:30:20 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, comm.Timeout())
|
|
|
|
defer cancel()
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
// Get the source
|
2016-12-23 01:17:34 +01:00
|
|
|
src, deleteSource, err := getSrc(data)
|
2015-04-17 01:53:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
2016-12-23 01:17:34 +01:00
|
|
|
dst := data.Get("destination").(string)
|
2018-02-15 00:30:20 +01:00
|
|
|
|
|
|
|
if err := copyFiles(ctx, comm, src, dst); err != nil {
|
2016-12-23 01:47:32 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-02-15 00:30:20 +01:00
|
|
|
return nil
|
2015-11-13 12:50:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-15 19:57:04 +02:00
|
|
|
func validateFn(c *terraform.ResourceConfig) (ws []string, es []error) {
|
|
|
|
if !c.IsSet("source") && !c.IsSet("content") {
|
|
|
|
es = append(es, fmt.Errorf("Must provide one of 'source' or 'content'"))
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
2017-06-15 19:57:04 +02:00
|
|
|
|
|
|
|
return ws, es
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
// getSrc returns the file to use as source
|
2016-12-23 01:17:34 +01:00
|
|
|
func getSrc(data *schema.ResourceData) (string, bool, error) {
|
|
|
|
src := data.Get("source").(string)
|
|
|
|
if content, ok := data.GetOk("content"); ok {
|
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
|
|
|
|
2016-12-23 01:17:34 +01:00
|
|
|
if _, err = file.WriteString(content.(string)); 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
|
|
|
}
|
|
|
|
|
2016-07-08 20:34:37 +02:00
|
|
|
expansion, err := homedir.Expand(src)
|
|
|
|
return expansion, false, err
|
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 {
|
2015-04-09 21:58:00 +02:00
|
|
|
// Wait and retry until we establish the connection
|
2018-02-15 00:30:20 +01:00
|
|
|
err := communicator.Retry(ctx, 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)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|