Commit b61ed3ad authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: convert to helper/comm

parent 89af447c
...@@ -18,8 +18,8 @@ func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error) ...@@ -18,8 +18,8 @@ func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error)
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
vmxPath := state.Get("vmx_path").(string) vmxPath := state.Get("vmx_path").(string)
if config.SSHHost != "" { if config.Comm.SSHHost != "" {
return fmt.Sprintf("%s:%d", config.SSHHost, config.SSHPort), nil return fmt.Sprintf("%s:%d", config.Comm.SSHHost, config.Comm.SSHPort), nil
} }
log.Println("Lookup up IP information...") log.Println("Lookup up IP information...")
...@@ -62,20 +62,20 @@ func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error) ...@@ -62,20 +62,20 @@ func SSHAddressFunc(config *SSHConfig) func(multistep.StateBag) (string, error)
} }
log.Printf("Detected IP: %s", ipAddress) log.Printf("Detected IP: %s", ipAddress)
return fmt.Sprintf("%s:%d", ipAddress, config.SSHPort), nil return fmt.Sprintf("%s:%d", ipAddress, config.Comm.SSHPort), nil
} }
} }
func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) { func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*gossh.ClientConfig, error) { return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []gossh.AuthMethod{ auth := []gossh.AuthMethod{
gossh.Password(config.SSHPassword), gossh.Password(config.Comm.SSHPassword),
gossh.KeyboardInteractive( gossh.KeyboardInteractive(
ssh.PasswordKeyboardInteractive(config.SSHPassword)), ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
} }
if config.SSHKeyPath != "" { if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.SSHKeyPath) signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -84,7 +84,7 @@ func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientCon ...@@ -84,7 +84,7 @@ func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientCon
} }
return &gossh.ClientConfig{ return &gossh.ClientConfig{
User: config.SSHUser, User: config.Comm.SSHUsername,
Auth: auth, Auth: auth,
}, nil }, nil
} }
......
package common package common
import ( import (
"errors"
"fmt"
"net"
"os"
"time" "time"
commonssh "github.com/mitchellh/packer/common/ssh" "github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/template/interpolate" "github.com/mitchellh/packer/template/interpolate"
) )
type SSHConfig struct { type SSHConfig struct {
SSHUser string `mapstructure:"ssh_username"` Comm communicator.Config `mapstructure:",squash"`
// These are deprecated, but we keep them around for BC
// TODO(@mitchellh): remove
SSHKeyPath string `mapstructure:"ssh_key_path"` SSHKeyPath string `mapstructure:"ssh_key_path"`
SSHPassword string `mapstructure:"ssh_password"`
SSHHost string `mapstructure:"ssh_host"`
SSHPort uint `mapstructure:"ssh_port"`
SSHSkipRequestPty bool `mapstructure:"ssh_skip_request_pty"` SSHSkipRequestPty bool `mapstructure:"ssh_skip_request_pty"`
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"` SSHWaitTimeout time.Duration `mapstructure:"ssh_wait_timeout"`
SSHWaitTimeout time.Duration
} }
func (c *SSHConfig) Prepare(ctx *interpolate.Context) []error { func (c *SSHConfig) Prepare(ctx *interpolate.Context) []error {
if c.SSHPort == 0 { // TODO: backwards compatibility, write fixer instead
c.SSHPort = 22
}
if c.RawSSHWaitTimeout == "" {
c.RawSSHWaitTimeout = "20m"
}
var errs []error
if c.SSHKeyPath != "" { if c.SSHKeyPath != "" {
if _, err := os.Stat(c.SSHKeyPath); err != nil { c.Comm.SSHPrivateKey = c.SSHKeyPath
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
} else if _, err := commonssh.FileSigner(c.SSHKeyPath); err != nil {
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
} }
if c.SSHWaitTimeout != 0 {
c.Comm.SSHTimeout = c.SSHWaitTimeout
} }
if c.SSHSkipRequestPty {
if c.SSHHost != "" { c.Comm.SSHPty = false
if ip := net.ParseIP(c.SSHHost); ip == nil {
if _, err := net.LookupHost(c.SSHHost); err != nil {
errs = append(errs, errors.New("ssh_host is an invalid IP or hostname"))
}
}
}
if c.SSHUser == "" {
errs = append(errs, errors.New("An ssh_username must be specified."))
}
var err error
c.SSHWaitTimeout, err = time.ParseDuration(c.RawSSHWaitTimeout)
if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err))
} }
return errs return c.Comm.Prepare(ctx)
} }
...@@ -4,11 +4,15 @@ import ( ...@@ -4,11 +4,15 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/mitchellh/packer/helper/communicator"
) )
func testSSHConfig() *SSHConfig { func testSSHConfig() *SSHConfig {
return &SSHConfig{ return &SSHConfig{
SSHUser: "foo", Comm: communicator.Config{
SSHUsername: "foo",
},
} }
} }
...@@ -19,8 +23,8 @@ func TestSSHConfigPrepare(t *testing.T) { ...@@ -19,8 +23,8 @@ func TestSSHConfigPrepare(t *testing.T) {
t.Fatalf("err: %#v", errs) t.Fatalf("err: %#v", errs)
} }
if c.SSHPort != 22 { if c.Comm.SSHPort != 22 {
t.Errorf("bad ssh port: %d", c.SSHPort) t.Errorf("bad ssh port: %d", c.Comm.SSHPort)
} }
} }
...@@ -73,57 +77,6 @@ func TestSSHConfigPrepare_SSHKeyPath(t *testing.T) { ...@@ -73,57 +77,6 @@ func TestSSHConfigPrepare_SSHKeyPath(t *testing.T) {
} }
} }
func TestSSHConfigPrepare_SSHUser(t *testing.T) {
var c *SSHConfig
var errs []error
c = testSSHConfig()
c.SSHUser = ""
errs = c.Prepare(testConfigTemplate(t))
if len(errs) == 0 {
t.Fatalf("should have error")
}
c = testSSHConfig()
c.SSHUser = "exists"
errs = c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("should not have error: %#v", errs)
}
}
func TestSSHConfigPrepare_SSHWaitTimeout(t *testing.T) {
var c *SSHConfig
var errs []error
// Defaults
c = testSSHConfig()
c.RawSSHWaitTimeout = ""
errs = c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("should not have error: %#v", errs)
}
if c.RawSSHWaitTimeout != "20m" {
t.Fatalf("bad value: %s", c.RawSSHWaitTimeout)
}
// Test with a bad value
c = testSSHConfig()
c.RawSSHWaitTimeout = "this is not good"
errs = c.Prepare(testConfigTemplate(t))
if len(errs) == 0 {
t.Fatal("should have error")
}
// Test with a good one
c = testSSHConfig()
c.RawSSHWaitTimeout = "5s"
errs = c.Prepare(testConfigTemplate(t))
if len(errs) > 0 {
t.Fatalf("should not have error: %#v", errs)
}
}
const testPem = ` const testPem = `
-----BEGIN RSA PRIVATE KEY----- -----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common" vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate" "github.com/mitchellh/packer/template/interpolate"
...@@ -298,11 +299,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -298,11 +299,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
VMName: b.config.VMName, VMName: b.config.VMName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
}, },
&common.StepConnectSSH{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm,
SSHAddress: driver.SSHAddress, SSHAddress: driver.SSHAddress,
SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig),
SSHWaitTimeout: b.config.SSHWaitTimeout,
Pty: !b.config.SSHSkipRequestPty,
}, },
&vmwcommon.StepUploadTools{ &vmwcommon.StepUploadTools{
RemoteType: b.config.RemoteType, RemoteType: b.config.RemoteType,
......
package iso package iso
import ( import (
"github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/mitchellh/packer/packer"
) )
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
...@@ -138,10 +138,6 @@ func TestBuilderPrepare_Defaults(t *testing.T) { ...@@ -138,10 +138,6 @@ func TestBuilderPrepare_Defaults(t *testing.T) {
t.Errorf("bad Version: %s", b.config.Version) t.Errorf("bad Version: %s", b.config.Version)
} }
if b.config.SSHWaitTimeout != (20 * time.Minute) {
t.Errorf("bad wait timeout: %s", b.config.SSHWaitTimeout)
}
if b.config.VMName != "packer-foo" { if b.config.VMName != "packer-foo" {
t.Errorf("bad vm name: %s", b.config.VMName) t.Errorf("bad vm name: %s", b.config.VMName)
} }
......
...@@ -253,7 +253,7 @@ func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) { ...@@ -253,7 +253,7 @@ func (d *ESX5Driver) SSHAddress(state multistep.StateBag) (string, error) {
return "", errors.New("VM network port found, but no IP address") return "", errors.New("VM network port found, but no IP address")
} }
address := fmt.Sprintf("%s:%d", record["IPAddress"], config.SSHPort) address := fmt.Sprintf("%s:%d", record["IPAddress"], config.Comm.SSHPort)
state.Put("vm_address", address) state.Put("vm_address", address)
return address, nil return address, nil
} }
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common" vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
...@@ -90,11 +91,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -90,11 +91,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
VMName: b.config.VMName, VMName: b.config.VMName,
Ctx: b.config.ctx, Ctx: b.config.ctx,
}, },
&common.StepConnectSSH{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm,
SSHAddress: driver.SSHAddress, SSHAddress: driver.SSHAddress,
SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig), SSHConfig: vmwcommon.SSHConfigFunc(&b.config.SSHConfig),
SSHWaitTimeout: b.config.SSHWaitTimeout,
Pty: !b.config.SSHSkipRequestPty,
}, },
&vmwcommon.StepUploadTools{ &vmwcommon.StepUploadTools{
RemoteType: b.config.RemoteType, RemoteType: b.config.RemoteType,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment