Refactor shared SSH setup code
This commit is contained in:
parent
2c3e619960
commit
b84814539f
|
@ -11,7 +11,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.google.com/p/go.crypto/ssh"
|
|
||||||
helper "github.com/hashicorp/terraform/helper/ssh"
|
helper "github.com/hashicorp/terraform/helper/ssh"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
@ -163,36 +162,16 @@ func (p *ResourceProvisioner) collectScripts(c *terraform.ResourceConfig) ([]io.
|
||||||
|
|
||||||
// runScripts is used to copy and execute a set of scripts
|
// runScripts is used to copy and execute a set of scripts
|
||||||
func (p *ResourceProvisioner) runScripts(conf *helper.SSHConfig, scripts []io.ReadCloser) error {
|
func (p *ResourceProvisioner) runScripts(conf *helper.SSHConfig, scripts []io.ReadCloser) error {
|
||||||
sshConf := &ssh.ClientConfig{
|
// Get the SSH client config
|
||||||
User: conf.User,
|
config, err := helper.PrepareConfig(conf)
|
||||||
}
|
if err != nil {
|
||||||
if conf.KeyFile != "" {
|
return err
|
||||||
key, err := ioutil.ReadFile(conf.KeyFile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to read key file '%s': %v", conf.KeyFile, err)
|
|
||||||
}
|
|
||||||
signer, err := ssh.ParsePrivateKey(key)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to parse key file '%s': %v", conf.KeyFile, err)
|
|
||||||
}
|
|
||||||
sshConf.Auth = append(sshConf.Auth, ssh.PublicKeys(signer))
|
|
||||||
}
|
|
||||||
if conf.Password != "" {
|
|
||||||
sshConf.Auth = append(sshConf.Auth,
|
|
||||||
ssh.Password(conf.Password))
|
|
||||||
sshConf.Auth = append(sshConf.Auth,
|
|
||||||
ssh.KeyboardInteractive(helper.PasswordKeyboardInteractive(conf.Password)))
|
|
||||||
}
|
|
||||||
host := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
||||||
config := &helper.Config{
|
|
||||||
SSHConfig: sshConf,
|
|
||||||
Connection: helper.ConnectFunc("tcp", host),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait and retry until we establish the SSH connection
|
// Wait and retry until we establish the SSH connection
|
||||||
var comm *helper.SSHCommunicator
|
var comm *helper.SSHCommunicator
|
||||||
err := retryFunc(conf.TimeoutVal, func() error {
|
err = retryFunc(conf.TimeoutVal, func() error {
|
||||||
var err error
|
host := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
||||||
comm, err = helper.New(host, config)
|
comm, err = helper.New(host, config)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,9 +2,11 @@ package ssh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"code.google.com/p/go.crypto/ssh"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
)
|
)
|
||||||
|
@ -91,3 +93,34 @@ func safeDuration(dur string, defaultDur time.Duration) time.Duration {
|
||||||
}
|
}
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrepareConfig is used to turn the *SSHConfig provided into a
|
||||||
|
// usable *Config for client initialization.
|
||||||
|
func PrepareConfig(conf *SSHConfig) (*Config, error) {
|
||||||
|
sshConf := &ssh.ClientConfig{
|
||||||
|
User: conf.User,
|
||||||
|
}
|
||||||
|
if conf.KeyFile != "" {
|
||||||
|
key, err := ioutil.ReadFile(conf.KeyFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Failed to read key file '%s': %v", conf.KeyFile, err)
|
||||||
|
}
|
||||||
|
signer, err := ssh.ParsePrivateKey(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Failed to parse key file '%s': %v", conf.KeyFile, err)
|
||||||
|
}
|
||||||
|
sshConf.Auth = append(sshConf.Auth, ssh.PublicKeys(signer))
|
||||||
|
}
|
||||||
|
if conf.Password != "" {
|
||||||
|
sshConf.Auth = append(sshConf.Auth,
|
||||||
|
ssh.Password(conf.Password))
|
||||||
|
sshConf.Auth = append(sshConf.Auth,
|
||||||
|
ssh.KeyboardInteractive(PasswordKeyboardInteractive(conf.Password)))
|
||||||
|
}
|
||||||
|
host := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
||||||
|
config := &Config{
|
||||||
|
SSHConfig: sshConf,
|
||||||
|
Connection: ConnectFunc("tcp", host),
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue