kitchen-hyperv 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,18 @@
1
1
  #requires -Version 2 -Modules Hyper-V
2
2
 
3
3
  #implicitly import hyperv module to avoid powercli cmdlets
4
- if((Get-Module -Name 'hyper-v') -ne $null)
5
- {
4
+ if ((Get-Module -Name 'hyper-v') -ne $null) {
6
5
  Remove-Module -Name hyper-v
7
6
  Import-Module -Name hyper-v
8
7
  }
9
- else
10
- {
8
+ else {
11
9
  Import-Module -Name hyper-v
12
10
  }
13
11
 
14
12
  $ProgressPreference = 'SilentlyContinue'
15
13
 
16
14
 
17
- function New-DifferencingDisk
18
- {
15
+ function New-DifferencingDisk {
19
16
  [cmdletbinding()]
20
17
  param (
21
18
  [parameter(Mandatory)]
@@ -25,106 +22,117 @@ function New-DifferencingDisk
25
22
  [ValidateNotNullOrEmpty()]
26
23
  [string]$ParentPath
27
24
  )
28
- if (-not (Test-Path $Path))
29
- {
25
+ if (-not (Test-Path $Path)) {
30
26
  $null = new-vhd @psboundparameters -Differencing
31
27
  }
32
28
  }
33
29
 
34
- function Assert-VmRunning
35
- {
30
+ function Assert-VmRunning {
36
31
  [cmdletbinding()]
37
32
  param([string]$Id)
38
33
 
39
- if ([string]::IsNullOrEmpty($Id))
40
- {
34
+ if ([string]::IsNullOrEmpty($Id)) {
41
35
  $Output = [pscustomobject]@{
42
- Name = ''
36
+ Name = ''
43
37
  State = ''
44
38
  }
45
39
  }
46
- else
47
- {
40
+ else {
48
41
  $Output = Get-VM -Id $Id |
49
- ForEach-Object -Process {
50
- if ($_.State -notlike 'Running')
51
- {
42
+ ForEach-Object -Process {
43
+ if ($_.State -notlike 'Running') {
52
44
  $_ |
53
- Start-VM -passthru
45
+ Start-VM -passthru
54
46
  }
55
- else
56
- {
47
+ else {
57
48
  $_
58
49
  }
59
50
  } |
60
- Select-Object -Property Name, Id, State
51
+ Select-Object -Property Name, Id, State
61
52
  }
62
53
  $Output
63
54
  }
64
55
 
65
- function New-KitchenVM
66
- {
56
+ function New-KitchenVM {
67
57
  [cmdletbinding()]
68
58
  param (
69
- $Generation = 1,
70
- $MemoryStartupBytes,
71
- $Name,
72
- $Path,
73
- $VHDPath,
74
- $SwitchName,
75
- $ProcessorCount,
76
- $UseDynamicMemory,
77
- $DynamicMemoryMinBytes,
78
- $DynamicMemoryMaxBytes,
79
- $boot_iso_path,
80
- $EnableGuestServices
81
- )
82
- $null = $psboundparameters.remove('ProcessorCount')
83
- $null = $psboundparameters.remove('UseDynamicMemory')
84
- $null = $psboundparameters.remove('DynamicMemoryMinBytes')
85
- $null = $psboundparameters.remove('DynamicMemoryMaxBytes')
86
- $null = $psboundparameters.remove('boot_iso_path')
87
- $null = $psboundparameters.remove('EnableGuestServices')
88
- $UseDynamicMemory = [Convert]::ToBoolean($UseDynamicMemory)
89
- $null = [bool]::TryParse($EnableGuestServices,[ref]$EnableGuestServices)
90
-
91
- $vm = new-vm @psboundparameters |
92
- Set-Vm -ProcessorCount $ProcessorCount -passthru
59
+ $Generation = 1,
60
+ $DisableSecureBoot,
61
+ $MemoryStartupBytes,
62
+ $Name,
63
+ $Path,
64
+ $VHDPath,
65
+ $SwitchName,
66
+ $VlanId,
67
+ $ProcessorCount,
68
+ $UseDynamicMemory,
69
+ $DynamicMemoryMinBytes,
70
+ $DynamicMemoryMaxBytes,
71
+ $boot_iso_path,
72
+ $EnableGuestServices,
73
+ $AdditionalDisks
74
+ )
75
+ $null = $psboundparameters.remove('DisableSecureBoot')
76
+ $null = $psboundparameters.remove('ProcessorCount')
77
+ $null = $psboundparameters.remove('UseDynamicMemory')
78
+ $null = $psboundparameters.remove('DynamicMemoryMinBytes')
79
+ $null = $psboundparameters.remove('DynamicMemoryMaxBytes')
80
+ $null = $psboundparameters.remove('boot_iso_path')
81
+ $null = $psboundparameters.remove('EnableGuestServices')
82
+ $null = $psboundparameters.remove('VlanId')
83
+ $null = $psboundparameters.remove('AdditionalDisks')
84
+ $DisableSecureBoot = [Convert]::ToBoolean($DisableSecureBoot)
85
+ $UseDynamicMemory = [Convert]::ToBoolean($UseDynamicMemory)
86
+ $null = [bool]::TryParse($EnableGuestServices, [ref]$EnableGuestServices)
87
+
88
+ $vm = new-vm @psboundparameters |
89
+ Set-Vm -ProcessorCount $ProcessorCount -passthru
90
+
93
91
  if ($UseDynamicMemory) {
94
- $vm | Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $DynamicMemoryMinBytes -MaximumBytes $DynamicMemoryMaxBytes
92
+ $vm | Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $DynamicMemoryMinBytes -MaximumBytes $DynamicMemoryMaxBytes
95
93
  }
96
94
  else {
97
- $vm | Set-VMMemory -DynamicMemoryEnabled $false
95
+ $vm | Set-VMMemory -DynamicMemoryEnabled $false
98
96
  }
99
- if (-not [string]::IsNullOrEmpty($boot_iso_path)) {
97
+ if (-not [string]::IsNullOrEmpty($boot_iso_path)) {
100
98
  Mount-VMISO -Id $vm.Id -Path $boot_iso_path
101
- }
102
- if ($EnableGuestServices -and (Get-command Enable-VMIntegrationService -ErrorAction SilentlyContinue)) {
99
+ }
100
+ if ($EnableGuestServices -and (Get-command Enable-VMIntegrationService -ErrorAction SilentlyContinue)) {
103
101
  Enable-VMIntegrationService -VM $vm -Name 'Guest Service Interface'
104
- }
105
- $vm | Start-Vm -passthru |
106
- foreach {
107
- $vm = $_
108
- do {
109
- start-sleep -seconds 2
110
- } while ($vm.state -notlike 'Running')
111
- $vm
112
- } |
113
- select Name, Id, State
102
+ }
103
+ if (($VlanId -ne $null) -and (Get-command Set-VMNetworkAdapterVlan -ErrorAction SilentlyContinue)) {
104
+ Set-VMNetworkAdapterVlan -VM $vm -Access -VlanId $VlanId
105
+ }
106
+ if ($AdditionalDisks -and (Get-command Add-VMHardDiskDrive -ErrorAction SilentlyContinue)) {
107
+ foreach ($AdditionalDisk in $AdditionalDisks) {
108
+ Add-VMHardDiskDrive -VM $vm -Path $AdditionalDisk
109
+ }
110
+ }
111
+ if ($DisableSecureBoot -and ($Generation -eq 2) -and (Get-command Set-VMFirmware -ErrorAction SilentlyContinue)) {
112
+ Set-VMFirmware -VM $vm -EnableSecureBoot Off
113
+ }
114
+ $vm | Start-Vm -passthru |
115
+ foreach {
116
+ $vm = $_
117
+ do {
118
+ start-sleep -seconds 2
119
+ }
120
+ while ($vm.state -notlike 'Running')
121
+ $vm
122
+ } |
123
+ select Name, Id, State
114
124
  }
115
125
 
116
- function Get-VmIP($vm)
117
- {
126
+ function Get-VmIP($vm) {
118
127
  start-sleep -seconds 10
119
128
  $vm.networkadapters.ipaddresses |
120
- Where-Object {
129
+ Where-Object {
121
130
  $_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
122
131
  } |
123
- Select-Object -First 1
132
+ Select-Object -First 1
124
133
  }
125
134
 
126
- Function Set-VMNetworkConfiguration
127
- {
135
+ Function Set-VMNetworkConfiguration {
128
136
  [CmdletBinding()]
129
137
  Param (
130
138
  [parameter(valuefrompipeline)]
@@ -144,10 +152,8 @@ Function Set-VMNetworkConfiguration
144
152
  $VMNetAdapters = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData')
145
153
 
146
154
  $NetworkSettings = @()
147
- foreach ($NetAdapter in $VMNetAdapters)
148
- {
149
- if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress)
150
- {
155
+ foreach ($NetAdapter in $VMNetAdapters) {
156
+ if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) {
151
157
  $NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated('Msvm_GuestNetworkAdapterConfiguration')
152
158
  }
153
159
  }
@@ -163,48 +169,42 @@ Function Set-VMNetworkConfiguration
163
169
  $Service = Get-WmiObject -Class 'Msvm_VirtualSystemManagementService' -Namespace 'root\virtualization\v2'
164
170
  $setIP = $Service.SetGuestNetworkAdapterConfiguration($vm, $NetworkSettings[0].GetText(1))
165
171
 
166
- if ($setIP.ReturnValue -eq 4096)
167
- {
172
+ if ($setIP.ReturnValue -eq 4096) {
168
173
  $job = [WMI]$setIP.job
169
174
 
170
- while ($job.JobState -eq 3 -or $job.JobState -eq 4)
171
- {
175
+ while ($job.JobState -eq 3 -or $job.JobState -eq 4) {
172
176
  Start-Sleep 1
173
177
  $job = [WMI]$setIP.job
174
178
  }
175
179
 
176
- if ($job.JobState -ne 7)
177
- {
180
+ if ($job.JobState -ne 7) {
178
181
  $job.GetError()
179
182
  }
180
183
  }
181
184
  (Get-VM -Id $NetworkAdapter.VmId).NetworkAdapter | Select-Object Name, IpAddress
182
185
  }
183
186
 
184
- function Get-VmDetail
185
- {
187
+ function Get-VmDetail {
186
188
  [cmdletbinding()]
187
189
  param($Id)
188
190
 
189
191
  Get-VM -Id $Id |
190
- ForEach-Object {
192
+ ForEach-Object {
191
193
  $vm = $_
192
- do
193
- {
194
+ do {
194
195
  Start-Sleep -Seconds 1
195
196
  }
196
197
  while (-not (Get-VmIP $vm))
197
198
 
198
199
  [pscustomobject]@{
199
- Name = $vm.name
200
- Id = $vm.ID
200
+ Name = $vm.name
201
+ Id = $vm.ID
201
202
  IpAddress = (Get-VmIP $vm)
202
203
  }
203
204
  }
204
205
  }
205
206
 
206
- function Get-DefaultVMSwitch
207
- {
207
+ function Get-DefaultVMSwitch {
208
208
  [CmdletBinding()]
209
209
  param ($Name)
210
210
  Get-VMSwitch @PSBoundParameters |
@@ -212,13 +212,11 @@ function Get-DefaultVMSwitch
212
212
  Select-Object Name, Id
213
213
  }
214
214
 
215
- function Mount-VMISO
216
- {
215
+ function Mount-VMISO {
217
216
  [cmdletbinding()]
218
217
  param($Id, $Path)
219
218
 
220
- if ((Get-VM -Id $Id).Generation -eq 2)
221
- {
219
+ if ((Get-VM -Id $Id).Generation -eq 2) {
222
220
  Add-VMDvdDrive (Get-VM -Id $Id).Name | Set-VMDvdDrive -VMName (Get-VM -Id $Id).Name -Path $Path
223
221
  }
224
222
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-hyperv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Murawski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-28 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  version: '0'
180
180
  requirements: []
181
181
  rubyforge_project:
182
- rubygems_version: 2.6.6
182
+ rubygems_version: 2.6.10
183
183
  signing_key:
184
184
  specification_version: 4
185
185
  summary: Hyper-V Driver for Test-Kitchen