Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Add winrm functionality to vmware-iso builder (hashicorp#3738)
Browse files Browse the repository at this point in the history
* Use winrm_host, if provided, this allows packer to work in ESXi
environments without DHCP.

Signed-off-by: Charlie Vieth <[email protected]>
  • Loading branch information
charlievieth authored and rickard-von-essen committed Aug 19, 2016
1 parent a7de2d0 commit d14d620
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions builder/vmware/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Config struct {
RemotePassword string `mapstructure:"remote_password"`
RemotePrivateKey string `mapstructure:"remote_private_key_file"`

CommConfig communicator.Config `mapstructure:",squash"`

ctx interpolate.Context
}

Expand Down
58 changes: 58 additions & 0 deletions builder/vmware/iso/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,61 @@ func TestBuilderPrepare_VNCPort(t *testing.T) {
t.Fatalf("should not have error: %s", err)
}
}

func TestBuilderPrepare_CommConfig(t *testing.T) {
// Test Winrm
{
config := testConfig()
config["communicator"] = "winrm"
config["winrm_username"] = "username"
config["winrm_password"] = "password"
config["winrm_host"] = "1.2.3.4"

var b Builder
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}

if b.config.CommConfig.WinRMUser != "username" {
t.Errorf("bad winrm_username: %s", b.config.CommConfig.WinRMUser)
}
if b.config.CommConfig.WinRMPassword != "password" {
t.Errorf("bad winrm_password: %s", b.config.CommConfig.WinRMPassword)
}
if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
t.Errorf("bad host: %s", host)
}
}

// Test SSH
{
config := testConfig()
config["communicator"] = "ssh"
config["ssh_username"] = "username"
config["ssh_password"] = "password"
config["ssh_host"] = "1.2.3.4"

var b Builder
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}

if b.config.CommConfig.SSHUsername != "username" {
t.Errorf("bad ssh_username: %s", b.config.CommConfig.SSHUsername)
}
if b.config.CommConfig.SSHPassword != "password" {
t.Errorf("bad ssh_password: %s", b.config.CommConfig.SSHPassword)
}
if host := b.config.CommConfig.Host(); host != "1.2.3.4" {
t.Errorf("bad host: %s", host)
}
}
}
5 changes: 5 additions & 0 deletions builder/vmware/iso/driver_esx5.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ func (d *ESX5Driver) CommHost(state multistep.StateBag) (string, error) {
return address.(string), nil
}

if address := config.CommConfig.Host(); address != "" {
state.Put("vm_address", address)
return address, nil
}

r, err := d.esxcli("network", "vm", "list")
if err != nil {
return "", err
Expand Down
45 changes: 44 additions & 1 deletion builder/vmware/iso/driver_esx5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package iso

import (
"fmt"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"net"
"testing"

"github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
)

func TestESX5Driver_implDriver(t *testing.T) {
Expand Down Expand Up @@ -33,3 +35,44 @@ func TestESX5Driver_HostIP(t *testing.T) {
t.Error(fmt.Sprintf("Expected string, %s but got %s", expected_host, host))
}
}

func TestESX5Driver_CommHost(t *testing.T) {
const expected_host = "127.0.0.1"

config := testConfig()
config["communicator"] = "winrm"
config["winrm_username"] = "username"
config["winrm_password"] = "password"
config["winrm_host"] = expected_host

var b Builder
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if host := b.config.CommConfig.Host(); host != expected_host {
t.Fatalf("setup failed, bad host name: %s", host)
}

state := new(multistep.BasicStateBag)
state.Put("config", &b.config)

var driver ESX5Driver
host, err := driver.CommHost(state)
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if host != expected_host {
t.Errorf("bad host name: %s", host)
}
address, ok := state.GetOk("vm_address")
if !ok {
t.Error("state not updated with vm_address")
}
if address.(string) != expected_host {
t.Errorf("bad vm_address: %s", address.(string))
}
}

0 comments on commit d14d620

Please sign in to comment.