Extract http ip discover to a new step

This commit is contained in:
Moss 2020-01-27 16:49:31 +01:00
parent eab8f43151
commit 0566f1f999
11 changed files with 73 additions and 15 deletions

View File

@ -614,6 +614,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
new(stepCreateDisk),
new(stepCopyDisk),
new(stepResizeDisk),
new(stepHTTPIPDiscover),
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,

View File

@ -0,0 +1,21 @@
package qemu
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

@ -96,8 +96,7 @@ func (s *stepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
log.Printf("Connected to VNC desktop: %s", c.DesktopName)
hostIP := "10.0.2.2"
common.SetHTTPIP(hostIP)
hostIP := common.GetHTTPIP()
configCtx := config.ctx
configCtx.Data = &bootCommandTemplateData{
hostIP,

View File

@ -0,0 +1,36 @@
package common
import (
"context"
"fmt"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"log"
)
// 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 {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
// Determine the host IP
hostIP, err := driver.HostIP(state)
if err != nil {
err := fmt.Errorf("Error detecting host IP: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("Host IP for the VMware machine: %s", hostIP)
common.SetHTTPIP(hostIP)
return multistep.ActionContinue
}
func (*StepHTTPIPDiscover) Cleanup(multistep.StateBag) {}

View File

@ -99,18 +99,7 @@ func (s *StepTypeBootCommand) Run(ctx context.Context, state multistep.StateBag)
log.Printf("Connected to VNC desktop: %s", c.DesktopName)
// Determine the host IP
hostIP, err := driver.HostIP(state)
if err != nil {
err := fmt.Errorf("Error detecting host IP: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("Host IP for the VMware machine: %s", hostIP)
common.SetHTTPIP(hostIP)
hostIP := common.GetHTTPIP()
s.Ctx.Data = &bootCommandTemplateData{
hostIP,
httpPort,

View File

@ -109,6 +109,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
DisplayName: b.config.VMXDisplayName,
},
&vmwcommon.StepSuppressMessages{},
&vmwcommon.StepHTTPIPDiscover{},
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,

View File

@ -103,6 +103,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
DisplayName: b.config.VMXDisplayName,
},
&vmwcommon.StepSuppressMessages{},
&vmwcommon.StepHTTPIPDiscover{},
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,

View File

@ -48,6 +48,7 @@ func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{}
}
hookData["PackerRunUUID"] = os.Getenv("PACKER_RUN_UUID")
hookData["PackerHTTPAddr"] = GetHTTPAddr()
// Read communicator data into hook data
comm, ok := state.GetOk("communicator_config")

View File

@ -39,12 +39,17 @@ func TestPopulateProvisionHookData(t *testing.T) {
generatedData := map[string]interface{}{"Data": "generated"}
instanceId := 11111
packerRunUUID := "1fa225b8-27d1-42d1-9117-221772213962"
httpIP := "10.0.2.2"
httpPort := "2222"
httpAddr := fmt.Sprintf("%s:%s", httpIP, httpPort)
state.Put("generated_data", generatedData)
state.Put("instance_id", instanceId)
state.Put("communicator_config", commConfig)
os.Setenv("PACKER_RUN_UUID", packerRunUUID)
SetHTTPIP(httpIP)
SetHTTPPort(httpPort)
hookData := PopulateProvisionHookData(state)
@ -60,6 +65,9 @@ func TestPopulateProvisionHookData(t *testing.T) {
if hookData["PackerRunUUID"] != packerRunUUID {
t.Fatalf("Bad: Expecting hookData[\"PackerRunUUID\"] was %s but actual value was %s", packerRunUUID, hookData["PackerRunUUID"])
}
if hookData["PackerHTTPAddr"] != httpAddr {
t.Fatalf("Bad: Expecting hookData[\"PackerHTTPAddr\"] was %s but actual value was %s", httpAddr, hookData["PackerHTTPAddr"])
}
if hookData["Host"] != commConfig.Host() {
t.Fatalf("Bad: Expecting hookData[\"Host\"] was %s but actual value was %s", commConfig.Host(), hookData["Host"])
}

View File

@ -68,6 +68,7 @@ func BasicPlaceholderData() map[string]string {
placeholderData["Password"] = fmt.Sprintf(msg, "Password")
placeholderData["ConnType"] = fmt.Sprintf(msg, "Type")
placeholderData["PackerRunUUID"] = fmt.Sprintf(msg, "PackerRunUUID")
placeholderData["PackerHTTPAddr"] = fmt.Sprintf(msg, "PackerHTTPAddr")
placeholderData["SSHPublicKey"] = fmt.Sprintf(msg, "SSHPublicKey")
placeholderData["SSHPrivateKey"] = fmt.Sprintf(msg, "SSHPrivateKey")

View File

@ -76,7 +76,7 @@ Here is a full list of the available functions for reference.
Valid variables to request are: "ID", "Host",
"Port", "User", "Password", "ConnType",
"PackerRunUUID", "SSHPublicKey", and "SSHPrivateKey".
"PackerRunUUID", "PackerHTTPAddr", "SSHPublicKey", and "SSHPrivateKey".
Depending on which communicator you are using, some of these values may be
empty -- for example, the public and private keys are unique to the SSH
communicator. InstanceID represents the vm being provisioned. For example,