From d545431f9bc735f5200489917512da5230ec7418 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Jun 2015 17:42:43 -0400 Subject: [PATCH] builder/null: adopt helper/communicator --- builder/null/builder.go | 17 +++++++++++------ builder/null/config.go | 30 +++++++++++------------------- builder/null/config_test.go | 10 +++++----- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/builder/null/builder.go b/builder/null/builder.go index 7ca1b57fd..925075ee0 100644 --- a/builder/null/builder.go +++ b/builder/null/builder.go @@ -1,11 +1,12 @@ package null import ( + "log" + "github.com/mitchellh/multistep" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/communicator" "github.com/mitchellh/packer/packer" - "log" - "time" ) const BuilderId = "fnoeding.null" @@ -27,10 +28,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { steps := []multistep.Step{ - &common.StepConnectSSH{ - SSHAddress: SSHAddress(b.config.Host, b.config.Port), - SSHConfig: SSHConfig(b.config.SSHUsername, b.config.SSHPassword, b.config.SSHPrivateKeyFile), - SSHWaitTimeout: 1 * time.Minute, + &communicator.StepConnect{ + Config: &b.config.CommConfig, + SSHAddress: SSHAddress( + b.config.CommConfig.SSHHost, b.config.CommConfig.SSHPort), + SSHConfig: SSHConfig( + b.config.CommConfig.SSHUsername, + b.config.CommConfig.SSHPassword, + b.config.CommConfig.SSHPrivateKey), }, &common.StepProvision{}, } diff --git a/builder/null/config.go b/builder/null/config.go index aa3f15120..a6a12332e 100644 --- a/builder/null/config.go +++ b/builder/null/config.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/packer/common" + "github.com/mitchellh/packer/helper/communicator" "github.com/mitchellh/packer/helper/config" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template/interpolate" @@ -12,49 +13,40 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` - Host string `mapstructure:"host"` - Port int `mapstructure:"port"` - SSHUsername string `mapstructure:"ssh_username"` - SSHPassword string `mapstructure:"ssh_password"` - SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"` + CommConfig communicator.Config `mapstructure:",squash"` } func NewConfig(raws ...interface{}) (*Config, []string, error) { var c Config err := config.Decode(&c, &config.DecodeOpts{ - Interpolate: true, - InterpolateFilter: &interpolate.RenderFilter{ - Exclude: []string{ - "run_command", - }, - }, + Interpolate: true, + InterpolateFilter: &interpolate.RenderFilter{}, }, raws...) if err != nil { return nil, nil, err } - if c.Port == 0 { - c.Port = 22 - } - var errs *packer.MultiError - if c.Host == "" { + if es := c.CommConfig.Prepare(nil); len(es) > 0 { + errs = packer.MultiErrorAppend(errs, es...) + } + if c.CommConfig.SSHHost == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("host must be specified")) } - if c.SSHUsername == "" { + if c.CommConfig.SSHUsername == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("ssh_username must be specified")) } - if c.SSHPassword == "" && c.SSHPrivateKeyFile == "" { + if c.CommConfig.SSHPassword == "" && c.CommConfig.SSHPrivateKey == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("one of ssh_password and ssh_private_key_file must be specified")) } - if c.SSHPassword != "" && c.SSHPrivateKeyFile != "" { + if c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKey != "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("only one of ssh_password and ssh_private_key_file must be specified")) } diff --git a/builder/null/config_test.go b/builder/null/config_test.go index dd574de35..12123378d 100644 --- a/builder/null/config_test.go +++ b/builder/null/config_test.go @@ -6,7 +6,7 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ - "host": "foo", + "ssh_host": "foo", "ssh_username": "bar", "ssh_password": "baz", } @@ -48,8 +48,8 @@ func TestConfigPrepare_port(t *testing.T) { // default port should be 22 delete(raw, "port") c, warns, errs := NewConfig(raw) - if c.Port != 22 { - t.Fatalf("bad: port should default to 22, not %d", c.Port) + if c.CommConfig.SSHPort != 22 { + t.Fatalf("bad: port should default to 22, not %d", c.CommConfig.SSHPort) } testConfigOk(t, warns, errs) } @@ -58,12 +58,12 @@ func TestConfigPrepare_host(t *testing.T) { raw := testConfig() // No host - delete(raw, "host") + delete(raw, "ssh_host") _, warns, errs := NewConfig(raw) testConfigErr(t, warns, errs) // Good host - raw["host"] = "good" + raw["ssh_host"] = "good" _, warns, errs = NewConfig(raw) testConfigOk(t, warns, errs) }