From 6d7c6ba18c3eab7fb06d2bd4ff60c204edb5b0ec Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Fri, 7 Feb 2020 10:38:48 +0100 Subject: [PATCH] Add step_http_ip_discover to virtualbox to allow HTTPIP in vboxmanage (#8700) --- .../common/step_http_ip_discover.go | 21 ++++++++++++++ .../common/step_http_ip_discover_test.go | 29 +++++++++++++++++++ .../common/step_type_boot_command.go | 3 +- builder/virtualbox/common/step_vboxmanage.go | 14 ++++++++- builder/virtualbox/iso/builder.go | 1 + builder/virtualbox/ovf/builder.go | 1 + builder/virtualbox/vm/builder.go | 1 + 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 builder/virtualbox/common/step_http_ip_discover.go create mode 100644 builder/virtualbox/common/step_http_ip_discover_test.go diff --git a/builder/virtualbox/common/step_http_ip_discover.go b/builder/virtualbox/common/step_http_ip_discover.go new file mode 100644 index 000000000..888602482 --- /dev/null +++ b/builder/virtualbox/common/step_http_ip_discover.go @@ -0,0 +1,21 @@ +package common + +import ( + "context" + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/multistep" +) + +// Step to discover the http ip +// which guests use to reach the vm host +// To make sure the IP is set before boot command and http server steps +type StepHTTPIPDiscover struct{} + +func (s *StepHTTPIPDiscover) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + hostIP := "10.0.2.2" + common.SetHTTPIP(hostIP) + + return multistep.ActionContinue +} + +func (s *StepHTTPIPDiscover) Cleanup(state multistep.StateBag) {} diff --git a/builder/virtualbox/common/step_http_ip_discover_test.go b/builder/virtualbox/common/step_http_ip_discover_test.go new file mode 100644 index 000000000..c6c4ec70b --- /dev/null +++ b/builder/virtualbox/common/step_http_ip_discover_test.go @@ -0,0 +1,29 @@ +package common + +import ( + "context" + "github.com/hashicorp/packer/common" + "github.com/hashicorp/packer/helper/multistep" + "testing" +) + +func TestStepHTTPIPDiscover_Run(t *testing.T) { + state := new(multistep.BasicStateBag) + step := new(StepHTTPIPDiscover) + hostIp := "10.0.2.2" + previousHttpIp := common.GetHTTPIP() + + // Test the run + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + httpIp := common.GetHTTPIP() + if httpIp != hostIp { + t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, hostIp) + } + + common.SetHTTPIP(previousHttpIp) +} diff --git a/builder/virtualbox/common/step_type_boot_command.go b/builder/virtualbox/common/step_type_boot_command.go index 88d92f321..85f0a7d57 100644 --- a/builder/virtualbox/common/step_type_boot_command.go +++ b/builder/virtualbox/common/step_type_boot_command.go @@ -63,8 +63,7 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag) pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn) } - hostIP := "10.0.2.2" - common.SetHTTPIP(hostIP) + hostIP := common.GetHTTPIP() s.Ctx.Data = &bootCommandTemplateData{ HTTPIP: hostIP, HTTPPort: httpPort, diff --git a/builder/virtualbox/common/step_vboxmanage.go b/builder/virtualbox/common/step_vboxmanage.go index f6df32a07..b02eb1aa8 100644 --- a/builder/virtualbox/common/step_vboxmanage.go +++ b/builder/virtualbox/common/step_vboxmanage.go @@ -3,6 +3,7 @@ package common import ( "context" "fmt" + "github.com/hashicorp/packer/common" "strings" "github.com/hashicorp/packer/helper/multistep" @@ -11,6 +12,12 @@ import ( ) type commandTemplate struct { + // HTTPIP is the HTTP server's IP address. + HTTPIP string + + // HTTPPort is the HTTP server port. + HTTPPort int + Name string } @@ -37,8 +44,13 @@ func (s *StepVBoxManage) Run(ctx context.Context, state multistep.StateBag) mult ui.Say("Executing custom VBoxManage commands...") } + hostIP := common.GetHTTPIP() + httpPort := state.Get("http_port").(int) + s.Ctx.Data = &commandTemplate{ - Name: vmName, + Name: vmName, + HTTPIP: hostIP, + HTTPPort: httpPort, } for _, originalCommand := range s.Commands { diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index ccc3c44e5..f8743ff0b 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -308,6 +308,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Directories: b.config.FloppyConfig.FloppyDirectories, Label: b.config.FloppyConfig.FloppyLabel, }, + new(vboxcommon.StepHTTPIPDiscover), &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, HTTPPortMin: b.config.HTTPPortMin, diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 1c820c93c..ab4b524cc 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -60,6 +60,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Directories: b.config.FloppyConfig.FloppyDirectories, Label: b.config.FloppyConfig.FloppyLabel, }, + new(vboxcommon.StepHTTPIPDiscover), &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, HTTPPortMin: b.config.HTTPPortMin, diff --git a/builder/virtualbox/vm/builder.go b/builder/virtualbox/vm/builder.go index 96ac5ad80..f8f493d60 100644 --- a/builder/virtualbox/vm/builder.go +++ b/builder/virtualbox/vm/builder.go @@ -59,6 +59,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack AttachSnapshot: b.config.AttachSnapshot, KeepRegistered: b.config.KeepRegistered, }, + new(vboxcommon.StepHTTPIPDiscover), &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, HTTPPortMin: b.config.HTTPPortMin,