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
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
2016-12-23 01:47:32 +01:00
|
|
|
resultCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
resultCh <- copyFiles(comm, src, dst)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Allow the file copy to complete unless there is an interrupt.
|
|
|
|
// If there is an interrupt we make no attempt to cleanly close
|
|
|
|
// the connection currently. We just abruptly exit. Because Terraform
|
|
|
|
// taints the resource, this is fine.
|
|
|
|
select {
|
|
|
|
case err := <-resultCh:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("file transfer interrupted")
|
|
|
|
}
|
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
|
2016-12-23 01:17:34 +01:00
|
|
|
func copyFiles(comm communicator.Communicator, src, dst string) error {
|
2015-04-09 21:58:00 +02:00
|
|
|
// Wait and retry until we establish the connection
|
|
|
|
err := retryFunc(comm.Timeout(), func() error {
|
|
|
|
err := comm.Connect(nil)
|
2014-07-16 01:52:00 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-09 21:58:00 +02:00
|
|
|
defer 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryFunc is used to retry a function for a given duration
|
|
|
|
func retryFunc(timeout time.Duration, f func() error) error {
|
|
|
|
finish := time.After(timeout)
|
|
|
|
for {
|
|
|
|
err := f()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("Retryable error: %v", err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-finish:
|
|
|
|
return err
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 23:37:55 +02:00
|
|
|
}
|