packer-plugin-xenserver/builder/xenserver/common/step_set_vm_host_ssh_address.go
Rob King 4e5b3bee63 Fix SSH handling in pool environment.
This adds a new build step executed after StepStartVmPaused. This sets
the "ssh_address" state variable to the IP of the host the VM was started on.
This enables SSH commands to work correctly in a pool environment.

This also modifies SSH calls to use this address rather than config.HostIp

Fixes #47
2017-02-02 03:13:23 -05:00

43 lines
1.1 KiB
Go

package common
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
xsclient "github.com/xenserver/go-xenserver-client"
)
type StepSetVmHostSshAddress struct{}
func (self *StepSetVmHostSshAddress) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(xsclient.XenAPIClient)
ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Set SSH address to VM host IP")
uuid := state.Get("instance_uuid").(string)
instance, err := client.GetVMByUuid(uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
host, err := instance.GetResidentOn()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM Host for VM '%s': %s", uuid, err.Error()))
}
address, err := host.GetAddress()
if err != nil {
ui.Error(fmt.Sprintf("Unable to get address from VM Host: %s", err.Error()))
}
state.Put("ssh_address", address)
ui.Say(fmt.Sprintf("Set host SSH address to '%s'.", address))
return multistep.ActionContinue
}
func (self *StepSetVmHostSshAddress) Cleanup(state multistep.StateBag) {}