Throw error if we can't get mac or ip address during steps

This commit is contained in:
Taliesin Sisson 2015-06-22 22:26:06 +01:00
parent 87b3dec3d2
commit 2d7cfcd65d
4 changed files with 44 additions and 25 deletions

View File

@ -68,12 +68,33 @@ func (d *HypervPS4Driver) Verify() error {
// Get mac address for VM. // Get mac address for VM.
func (d *HypervPS4Driver) Mac(vmName string) (string, error) { func (d *HypervPS4Driver) Mac(vmName string) (string, error) {
return hyperv.Mac(vmName) res, err := hyperv.Mac(vmName)
if err != nil {
return res, err
}
if res == "" {
err := fmt.Errorf("%s", "No mac address.")
return res, err
}
return res, err
} }
// Get ip address for mac address. // Get ip address for mac address.
func (d *HypervPS4Driver) IpAddress(mac string) (string, error) { func (d *HypervPS4Driver) IpAddress(mac string) (string, error) {
return hyperv.IpAddress(mac) res, err := hyperv.IpAddress(mac)
if err != nil {
return res, err
}
if res == "" {
err := fmt.Errorf("%s", "No ip address.")
return res, err
}
return res, err
} }
func (d *HypervPS4Driver) verifyPSVersion() error { func (d *HypervPS4Driver) verifyPSVersion() error {

View File

@ -3,8 +3,8 @@ package common
import ( import (
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
commonssh "github.com/mitchellh/packer/common/ssh" commonssh "github.com/mitchellh/packer/common/ssh"
packerssh "github.com/mitchellh/packer/communicator/ssh" "github.com/mitchellh/packer/communicator/ssh"
"golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
) )
func CommHost(state multistep.StateBag) (string, error) { func CommHost(state multistep.StateBag) (string, error) {
@ -24,28 +24,26 @@ func CommHost(state multistep.StateBag) (string, error) {
return ip, nil return ip, nil
} }
func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*ssh.ClientConfig, error) { func SSHConfigFunc(config *SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) {
return func(state multistep.StateBag) (*ssh.ClientConfig, error) { return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
auth := []ssh.AuthMethod{ auth := []gossh.AuthMethod{
ssh.Password(config.Comm.SSHPassword), gossh.Password(config.Comm.SSHPassword),
ssh.KeyboardInteractive( gossh.KeyboardInteractive(
packerssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)), ssh.PasswordKeyboardInteractive(config.Comm.SSHPassword)),
} }
if config.SSHKeyPath != "" { if config.Comm.SSHPrivateKey != "" {
signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey) signer, err := commonssh.FileSigner(config.Comm.SSHPrivateKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
auth = append(auth, ssh.PublicKeys(signer)) auth = append(auth, gossh.PublicKeys(signer))
} }
return &ssh.ClientConfig{ return &gossh.ClientConfig{
User: config.Comm.SSHUsername, User: config.Comm.SSHUsername,
Auth: auth, Auth: auth,
}, nil }, nil
} }
} }

View File

@ -299,7 +299,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.SSHConfig.Comm, Config: &b.config.SSHConfig.Comm,
Host: hypervcommon.CommHost, Host: hypervcommon.CommHost,
SSHConfig: hypervcommon.SSHConfigFunc(b.config.SSHConfig), SSHConfig: hypervcommon.SSHConfigFunc(&b.config.SSHConfig),
}, },
// provision requires communicator to be setup // provision requires communicator to be setup

View File

@ -77,7 +77,7 @@ Set-VMFloppyDiskDrive -VMName $vmName -Path $null
func CreateVirtualMachine(vmName string, path string, ram string, diskSize string, switchName string, generation string) error { func CreateVirtualMachine(vmName string, path string, ram string, diskSize string, switchName string, generation string) error {
var script = ` var script = `
param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [int]generation) param([string]$vmName, [string]$path, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [string]$switchName, [int]$generation)
$vhdx = $vmName + '.vhdx' $vhdx = $vmName + '.vhdx'
$vhdPath = Join-Path -Path $path -ChildPath $vhdx $vhdPath = Join-Path -Path $path -ChildPath $vhdx
New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName -Generation $generation New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHDPath $vhdPath -NewVHDSizeBytes $newVHDSizeBytes -SwitchName $switchName -Generation $generation
@ -91,8 +91,8 @@ New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -NewVHD
func SetVirtualMachineCpu(vmName string, cpu string) error { func SetVirtualMachineCpu(vmName string, cpu string) error {
var script = ` var script = `
param([string]$vmName, [int]cpu) param([string]$vmName, [int]$cpu)
Set-VMProcessor -VMName $vmName Count $cpu Set-VMProcessor -VMName $vmName -Count $cpu
` `
var ps powershell.PowerShellCmd var ps powershell.PowerShellCmd
@ -359,15 +359,15 @@ $vm.State -eq [Microsoft.HyperV.PowerShell.VMState]::Running
func Mac(vmName string) (string, error) { func Mac(vmName string) (string, error) {
var script = ` var script = `
param([string]$vmName, [int]$addressIndex) param([string]$vmName, [int]$adapterIndex)
try { try {
$adapter = Get-VMNetworkAdapter -VMName $vmName -ErrorAction SilentlyContinue $adapter = Get-VMNetworkAdapter -VMName $vmName -ErrorAction SilentlyContinue
$mac = $adapter.MacAddress[$addressIndex] $mac = $adapter[$adapterIndex].MacAddress
if($mac -eq $null) { if($mac -eq $null) {
return $false return ""
} }
} catch { } catch {
return $false return ""
} }
$mac $mac
` `
@ -385,10 +385,10 @@ try {
$ip = Get-Vm | %{$_.NetworkAdapters} | ?{$_.MacAddress -eq $mac} | %{$_.IpAddresses[$addressIndex]} $ip = Get-Vm | %{$_.NetworkAdapters} | ?{$_.MacAddress -eq $mac} | %{$_.IpAddresses[$addressIndex]}
if($ip -eq $null) { if($ip -eq $null) {
return $false return ""
} }
} catch { } catch {
return $false return ""
} }
$ip $ip
` `