Add step_http_ip_discover to virtualbox to allow HTTPIP in vboxmanage (#8700)

This commit is contained in:
Sylvia Moss 2020-02-07 10:38:48 +01:00 committed by GitHub
parent a1d9ba0e32
commit 6d7c6ba18c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 3 deletions

View File

@ -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) {}

View File

@ -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)
}

View File

@ -63,8 +63,7 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn) pauseFn = state.Get("pauseFn").(multistep.DebugPauseFn)
} }
hostIP := "10.0.2.2" hostIP := common.GetHTTPIP()
common.SetHTTPIP(hostIP)
s.Ctx.Data = &bootCommandTemplateData{ s.Ctx.Data = &bootCommandTemplateData{
HTTPIP: hostIP, HTTPIP: hostIP,
HTTPPort: httpPort, HTTPPort: httpPort,

View File

@ -3,6 +3,7 @@ package common
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/hashicorp/packer/common"
"strings" "strings"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -11,6 +12,12 @@ import (
) )
type commandTemplate struct { type commandTemplate struct {
// HTTPIP is the HTTP server's IP address.
HTTPIP string
// HTTPPort is the HTTP server port.
HTTPPort int
Name string Name string
} }
@ -37,8 +44,13 @@ func (s *StepVBoxManage) Run(ctx context.Context, state multistep.StateBag) mult
ui.Say("Executing custom VBoxManage commands...") ui.Say("Executing custom VBoxManage commands...")
} }
hostIP := common.GetHTTPIP()
httpPort := state.Get("http_port").(int)
s.Ctx.Data = &commandTemplate{ s.Ctx.Data = &commandTemplate{
Name: vmName, Name: vmName,
HTTPIP: hostIP,
HTTPPort: httpPort,
} }
for _, originalCommand := range s.Commands { for _, originalCommand := range s.Commands {

View File

@ -308,6 +308,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Directories: b.config.FloppyConfig.FloppyDirectories, Directories: b.config.FloppyConfig.FloppyDirectories,
Label: b.config.FloppyConfig.FloppyLabel, Label: b.config.FloppyConfig.FloppyLabel,
}, },
new(vboxcommon.StepHTTPIPDiscover),
&common.StepHTTPServer{ &common.StepHTTPServer{
HTTPDir: b.config.HTTPDir, HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,

View File

@ -60,6 +60,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Directories: b.config.FloppyConfig.FloppyDirectories, Directories: b.config.FloppyConfig.FloppyDirectories,
Label: b.config.FloppyConfig.FloppyLabel, Label: b.config.FloppyConfig.FloppyLabel,
}, },
new(vboxcommon.StepHTTPIPDiscover),
&common.StepHTTPServer{ &common.StepHTTPServer{
HTTPDir: b.config.HTTPDir, HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,

View File

@ -59,6 +59,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
AttachSnapshot: b.config.AttachSnapshot, AttachSnapshot: b.config.AttachSnapshot,
KeepRegistered: b.config.KeepRegistered, KeepRegistered: b.config.KeepRegistered,
}, },
new(vboxcommon.StepHTTPIPDiscover),
&common.StepHTTPServer{ &common.StepHTTPServer{
HTTPDir: b.config.HTTPDir, HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin, HTTPPortMin: b.config.HTTPPortMin,