Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder: call Comm.Prepare before resetting host #91

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions builder/qemu/comm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package qemu

import (
"errors"
"log"

"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
Expand Down Expand Up @@ -46,11 +47,6 @@ func (c *CommConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs
c.HostPortMax = c.SSHHostPortMax
}

if c.Comm.Host() == "" && c.SkipNatMapping {
c.Comm.SSHHost = "127.0.0.1"
c.Comm.WinRMHost = "127.0.0.1"
}

if c.HostPortMin == 0 {
c.HostPortMin = 2222
}
Expand All @@ -60,6 +56,13 @@ func (c *CommConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs
}

errs = c.Comm.Prepare(ctx)

if c.Comm.Host() == "" && c.SkipNatMapping {
log.Printf("Resetting ssh host to 127.0.0.1")
c.Comm.SSHHost = "127.0.0.1"
c.Comm.WinRMHost = "127.0.0.1"
}

if c.HostPortMin > c.HostPortMax {
errs = append(errs,
errors.New("host_port_min must be less than host_port_max"))
Expand Down
48 changes: 48 additions & 0 deletions builder/qemu/comm_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,54 @@ func TestCommConfigPrepare_SSHHostPort(t *testing.T) {
if len(warns) != 0 {
t.Fatal("should not have any warnings")
}

tc := []struct {
name string
host, expectedHost string
skipNat bool
}{
{
name: "skip_nat_mapping false should not change host",
host: "192.168.1.1",
expectedHost: "192.168.1.1",
},
{
name: "skip_nat_mapping true should not change host",
host: "192.168.1.1",
expectedHost: "192.168.1.1",
skipNat: true,
},
{
name: "skip_nat_mapping true with no set host",
expectedHost: "127.0.0.1",
skipNat: true,
},
}

for _, tt := range tc {
tt := tt
t.Run(tt.name, func(t *testing.T) {
c := &CommConfig{
SkipNatMapping: tt.skipNat,
Comm: communicator.Config{
SSH: communicator.SSH{
SSHUsername: "tester",
SSHHost: tt.host,
},
},
}
warns, errs := c.Prepare(interpolate.NewContext())
if len(errs) > 0 {
t.Fatalf("should not have error: %s", errs)
}
if len(warns) != 0 {
t.Fatal("should not have any warnings")
}
if c.Comm.SSHHost != tt.expectedHost {
t.Errorf("unexpected SSHHost: wanted: %s, got: %s", tt.expectedHost, c.Comm.SSHHost)
}
})
}
}

func TestCommConfigPrepare_SSHPrivateKey(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion builder/qemu/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (s *stepRun) getDefaultArgs(config *Config, state multistep.StateBag) map[s
// Configure "-netdev" arguments
defaultArgs["-netdev"] = fmt.Sprintf("bridge,id=user.0,br=%s", config.NetBridge)
if config.NetBridge == "" {
defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0")
defaultArgs["-netdev"] = "user,id=user.0"
if config.CommConfig.Comm.Type != "none" {
commHostPort := state.Get("commHostPort").(int)
defaultArgs["-netdev"] = fmt.Sprintf("user,id=user.0,hostfwd=tcp::%v-:%d", commHostPort, config.CommConfig.Comm.Port())
Expand Down