2014-07-15 23:37:55 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2014-07-16 01:52:00 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/config"
|
|
|
|
helper "github.com/hashicorp/terraform/helper/ssh"
|
2014-07-15 23:37:55 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceProvisioner struct{}
|
|
|
|
|
2014-09-17 02:08:27 +02:00
|
|
|
func (p *ResourceProvisioner) Apply(s *terraform.InstanceState,
|
2014-07-22 19:38:39 +02:00
|
|
|
c *terraform.ResourceConfig) error {
|
2014-07-16 01:52:00 +02:00
|
|
|
// Ensure the connection type is SSH
|
|
|
|
if err := helper.VerifySSH(s); err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the SSH configuration
|
|
|
|
conf, err := helper.ParseSSHConfig(s)
|
|
|
|
if err != nil {
|
2014-07-22 19:38:39 +02:00
|
|
|
return err
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the source and destination
|
|
|
|
sRaw := c.Config["source"]
|
|
|
|
src, ok := sRaw.(string)
|
|
|
|
if !ok {
|
2014-07-22 19:38:39 +02:00
|
|
|
return fmt.Errorf("Unsupported 'source' type! Must be string.")
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dRaw := c.Config["destination"]
|
|
|
|
dst, ok := dRaw.(string)
|
|
|
|
if !ok {
|
2014-07-22 19:38:39 +02:00
|
|
|
return fmt.Errorf("Unsupported 'destination' type! Must be string.")
|
2014-07-16 01:52:00 +02:00
|
|
|
}
|
2014-07-22 19:38:39 +02:00
|
|
|
return p.copyFiles(conf, src, dst)
|
2014-07-15 23:37:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {
|
2014-07-16 01:52:00 +02:00
|
|
|
v := &config.Validator{
|
|
|
|
Required: []string{
|
|
|
|
"source",
|
|
|
|
"destination",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return v.Validate(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFiles is used to copy the files from a source to a destination
|
|
|
|
func (p *ResourceProvisioner) copyFiles(conf *helper.SSHConfig, src, dst string) error {
|
|
|
|
// Get the SSH client config
|
|
|
|
config, err := helper.PrepareConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait and retry until we establish the SSH connection
|
|
|
|
var comm *helper.SSHCommunicator
|
|
|
|
err = retryFunc(conf.TimeoutVal, func() error {
|
|
|
|
host := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
|
|
comm, err = helper.New(host, config)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're uploading a directory, short circuit and do that
|
|
|
|
if info.IsDir() {
|
|
|
|
if err := comm.UploadDir(dst, src, nil); err != nil {
|
|
|
|
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
|
|
|
}
|