From 43085e472b9067546c557b43b8dbd93769322928 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 23 Jun 2013 14:30:52 -0700 Subject: [PATCH] builder/vmware: Ability to specify the SSH port with "ssh_port" --- builder/vmware/builder.go | 5 +++++ builder/vmware/builder_test.go | 28 ++++++++++++++++++++++++++++ builder/vmware/step_wait_for_ssh.go | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 9e12a49a5..c61237136 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -40,6 +40,7 @@ type config struct { ShutdownTimeout time.Duration `` SSHUser string `mapstructure:"ssh_username"` SSHPassword string `mapstructure:"ssh_password"` + SSHPort uint `mapstructure:"ssh_port"` SSHWaitTimeout time.Duration `` VMXData map[string]string `mapstructure:"vmx_data"` VNCPortMin uint `mapstructure:"vnc_port_min"` @@ -92,6 +93,10 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.OutputDir = "vmware" } + if b.config.SSHPort == 0 { + b.config.SSHPort = 22 + } + // Accumulate any errors var err error errs := make([]error, 0) diff --git a/builder/vmware/builder_test.go b/builder/vmware/builder_test.go index 2ecac2d32..1d386fb03 100644 --- a/builder/vmware/builder_test.go +++ b/builder/vmware/builder_test.go @@ -200,6 +200,34 @@ func TestBuilderPrepare_SSHUser(t *testing.T) { } } +func TestBuilderPrepare_SSHPort(t *testing.T) { + var b Builder + config := testConfig() + + // Test with a bad value + delete(config, "ssh_port") + err := b.Prepare(config) + if err != nil { + t.Fatalf("bad err: %s", err) + } + + if b.config.SSHPort != 22 { + t.Fatalf("bad ssh port: %d", b.config.SSHPort) + } + + // Test with a good one + config["ssh_port"] = 44 + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if b.config.SSHPort != 44 { + t.Fatalf("bad ssh port: %d", b.config.SSHPort) + } +} + func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/vmware/step_wait_for_ssh.go b/builder/vmware/step_wait_for_ssh.go index 6fea3415e..f3a8d1916 100644 --- a/builder/vmware/step_wait_for_ssh.go +++ b/builder/vmware/step_wait_for_ssh.go @@ -144,7 +144,7 @@ func (s *stepWaitForSSH) waitForSSH(state map[string]interface{}) (packer.Commun log.Printf("Detected IP: %s", ip) // Attempt to connect to SSH port - nc, err = net.Dial("tcp", fmt.Sprintf("%s:22", ip)) + nc, err = net.Dial("tcp", fmt.Sprintf("%s:%d", ip, config.SSHPort)) if err != nil { log.Printf("TCP connection to SSH ip/port failed: %s", err) continue