vagrant-hypervnet 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f024899cc74a4e7385cd03355e8bef3b435890225cff8b0a6148d25b52a86f9
4
- data.tar.gz: 1e00433fbdc4d9c3a134eb154c185b186cf8c1188816dd38f80d7af8db92adb7
3
+ metadata.gz: 544605d534c0917561ad9b958cb089f342f336727a5adcc4d49a3f090b53e2e3
4
+ data.tar.gz: dc4e721381b9f876de43774067e04b823714c98cc96fba5b34d8874a8abef292
5
5
  SHA512:
6
- metadata.gz: 57d673018b5faef0970e51671bd53033908c914e5af64d526ff2c41115552686988067d905b347c005ffc1edd9794e70f77be3865784c952e6be517412c7d7d4
7
- data.tar.gz: e58247102736397f56cc419b56d3df1940f2f6704c2c7c81cdbb6211b533c98ce9c5710ec2e30818a1798b06bc5b0e0c71e8cc6cc2a67be1d26bbb5d1a54eed6
6
+ metadata.gz: bce06beb7ad946a45e1effeb1a106788f346925702468d7393eb509b147256888e12b5eb50cbe4956d7d6906f7752899de0ed96790a8b4f2978fe1ae85fac13b
7
+ data.tar.gz: 23a3c4218e7ced43be5e1b994a3e43bf7ff1e6ff74b8890300e58e9aecd68163de655f0a06d4042606baeb76b9a5a0f01357a90a91ab0736bde702c0e080c314
@@ -160,6 +160,7 @@ module VagrantPlugins
160
160
  return {
161
161
  type: :external,
162
162
  switch: switch[:name],
163
+ switch_id: switch[:id]
163
164
  }
164
165
  else
165
166
  raise Errors::NetworkNotFound, name: config[:bridge]
@@ -206,7 +207,25 @@ module VagrantPlugins
206
207
  if !switch
207
208
  @logger.info("Switch not found. Creating if we can.")
208
209
  if !config[:bridge]
209
- config[:bridge] = netaddr.to_s
210
+ index = 0
211
+ loop do
212
+ config[:bridge] = netaddr.to_s + (index > 0 ? "_#{index}" : "")
213
+ exists = @driver.find_switch_by_name(config[:bridge])
214
+ if !exists
215
+ break
216
+ else
217
+ index += 1
218
+ end
219
+ end
220
+ end
221
+
222
+ routes = @driver.get_routes
223
+ @logger.debug("Search if subnet #{netaddr.to_s} is already defined in host: #{routes.inspect} ")
224
+ existing_route = routes.find{|range| range.include?(netaddr)}
225
+ if existing_route
226
+ raise Errors::NetworkAddressOverlapping,
227
+ address: netaddr,
228
+ subnet: existing_route
210
229
  end
211
230
 
212
231
  # Create a new switch
@@ -216,6 +235,7 @@ module VagrantPlugins
216
235
 
217
236
  return {
218
237
  switch: switch[:name],
238
+ switch_id: switch[:id],
219
239
  type: :internal
220
240
  }
221
241
  end
@@ -255,6 +275,7 @@ module VagrantPlugins
255
275
  return {
256
276
  type: :private,
257
277
  switch: switch[:name],
278
+ switch_id: switch[:id]
258
279
  }
259
280
  end
260
281
 
@@ -273,9 +294,11 @@ module VagrantPlugins
273
294
  end
274
295
 
275
296
  def nat_adapter(config)
297
+ switch = @driver.find_switch_by_name(@env[:machine].config.hypervnet.default_switch)
276
298
  return {
277
- type: :nat,
278
- switch: "Default Switch"
299
+ type: :nat,
300
+ switch: switch[:name],
301
+ switch_id: switch[:id]
279
302
  }
280
303
  end
281
304
 
@@ -292,12 +315,12 @@ module VagrantPlugins
292
315
  adapters.each.with_index(0) do |adapter, index|
293
316
  if index < vm_adapters.length
294
317
  vm_adapter = vm_adapters[index]
295
- @logger.info("Connecting adapter #{vm_adapter.inspect} to switch #{adapter[:switch]}")
296
- @driver.connect_vm_adapter(vm_adapter[:id], adapter[:switch])
297
318
  else
298
- vm_adapter = @driver.add_vm_adapter(adapter[:switch])
319
+ vm_adapter = @driver.add_vm_adapter
299
320
  @logger.info("Created adapter: #{vm_adapter.inspect}")
300
321
  end
322
+ @logger.info("Connecting adapter #{vm_adapter.inspect} to switch #{adapter[:switch]}")
323
+ @driver.connect_vm_adapter(vm_adapter[:id], adapter[:switch_id])
301
324
  end
302
325
 
303
326
  if vm_adapters.length > adapters.length
@@ -8,11 +8,13 @@ module VagrantPlugins
8
8
 
9
9
  attr_accessor :install_ssh_server
10
10
  attr_accessor :install_rsync
11
+ attr_accessor :default_switch
11
12
 
12
13
  def initialize
13
14
  @network_adapters = []
14
15
  @install_ssh_server = UNSET_VALUE
15
16
  @install_rsync = UNSET_VALUE
17
+ @default_switch = UNSET_VALUE
16
18
 
17
19
  network_adapter(:nat)
18
20
  end
@@ -24,6 +26,7 @@ module VagrantPlugins
24
26
  def finalize!
25
27
  @install_ssh_server = true if @install_ssh_server == UNSET_VALUE
26
28
  @install_rsync = true if @install_rsync == UNSET_VALUE
29
+ @default_switch = "Default Switch" if @default_switch == UNSET_VALUE
27
30
  end
28
31
 
29
32
  def validate(machine)
@@ -63,11 +63,19 @@ module VagrantPlugins
63
63
  def find_switch_by_name(name)
64
64
  data = execute(:get_switch_by_name, Name: name)
65
65
  if data && data.kind_of?(Array)
66
- data = data[0]
66
+ if data.length > 1
67
+ raise Errors::NetworkNotUnique,
68
+ networks: data.inspect
69
+ elsif data.length == 1
70
+ data = data[0]
71
+ else
72
+ data = nil
73
+ end
67
74
  end
68
75
 
69
76
  if data
70
77
  switch = {}
78
+ switch[:id] = data["Id"]
71
79
  switch[:name] = data["Name"]
72
80
  switch[:type] = data["SwitchType"]
73
81
  end
@@ -78,11 +86,19 @@ module VagrantPlugins
78
86
  def find_switch_by_address(ip_address, prefix_length)
79
87
  data = execute(:get_switch_by_address, DestinationPrefix: "#{ip_address}/#{prefix_length}")
80
88
  if data && data.kind_of?(Array)
81
- data = data[0]
89
+ if data.length > 1
90
+ raise Errors::NetworkNotUnique,
91
+ networks: data.inspect
92
+ elsif data.length == 1
93
+ data = data[0]
94
+ else
95
+ data = nil
96
+ end
82
97
  end
83
98
 
84
99
  if data
85
100
  switch = {}
101
+ switch[:id] = data["Id"]
86
102
  switch[:name] = data["Name"]
87
103
  switch[:type] = data["SwitchType"]
88
104
  end
@@ -118,7 +134,8 @@ module VagrantPlugins
118
134
  data.each do |value|
119
135
  adapter = {}
120
136
  adapter[:id] = value["Id"]
121
- adapter[:name] = value["Name"]
137
+ adapter[:name] = value["Name"]
138
+ adapter[:switch_id] = value["SwitchId"]
122
139
  adapter[:switch] = value["SwitchName"]
123
140
  adapter[:mac_address] = value["MacAddress"]
124
141
  adapters << adapter
@@ -127,6 +144,29 @@ module VagrantPlugins
127
144
  adapters
128
145
  end
129
146
 
147
+ def get_routes
148
+ routes = []
149
+ data = execute(:get_routes)
150
+ if data
151
+ if data.kind_of?(Hash)
152
+ data = [] << data
153
+ end
154
+ data.each do |value|
155
+ begin
156
+ netaddr = IPAddr.new(value["DestinationPrefix"])
157
+ if netaddr.prefix > 0
158
+ routes << netaddr
159
+ end
160
+ rescue IPAddr::Error => e
161
+ raise Vagrant::Errors::NetworkAddressInvalid,
162
+ address: data["DestinationPrefix"], mask: "",
163
+ error: e.message
164
+ end
165
+ end
166
+ end
167
+ routes
168
+ end
169
+
130
170
  def create_switch(type, name, ip_address = nil, prefix_length = nil)
131
171
  case type
132
172
  when :internal
@@ -137,6 +177,7 @@ module VagrantPlugins
137
177
 
138
178
  if data
139
179
  switch = {}
180
+ switch[:id] = data["Id"]
140
181
  switch[:name] = data["Name"]
141
182
  switch[:type] = data["SwitchType"]
142
183
  end
@@ -144,14 +185,12 @@ module VagrantPlugins
144
185
  switch
145
186
  end
146
187
 
147
- def add_vm_adapter(switch)
148
- data = execute(:add_vm_adapter, VmId: @vmId, SwitchName: switch)
188
+ def add_vm_adapter
189
+ data = execute(:add_vm_adapter, VmId: @vmId)
149
190
 
150
191
  adapter = {}
151
192
  adapter[:id] = data["Id"]
152
193
  adapter[:name] = data["Name"]
153
- adapter[:switch] = data["SwitchName"]
154
- adapter[:mac_address] = data["MacAddress"]
155
194
 
156
195
  adapter
157
196
  end
@@ -160,8 +199,8 @@ module VagrantPlugins
160
199
  execute(:remove_vm_adapter, VmId: @vmId, Id: id)
161
200
  end
162
201
 
163
- def connect_vm_adapter(id, switch)
164
- execute(:connect_vm_adapter, VmId: @vmId, Id: id, SwitchName: switch)
202
+ def connect_vm_adapter(id, switch_id)
203
+ execute(:connect_vm_adapter, VmId: @vmId, Id: id, SwitchId: switch_id)
165
204
  end
166
205
 
167
206
  protected
@@ -22,6 +22,14 @@ module VagrantPlugins
22
22
  error_key(:network_address_invalid)
23
23
  end
24
24
 
25
+ class NetworkAddressOverlapping < HyperVNetError
26
+ error_key(:network_address_overlapping)
27
+ end
28
+
29
+ class NetworkNotUnique < HyperVNetError
30
+ error_key(:network_not_unique)
31
+ end
32
+
25
33
  class NetworkNotFound < HyperVNetError
26
34
  error_key(:network_not_found)
27
35
  end
@@ -2,14 +2,11 @@
2
2
 
3
3
  param (
4
4
  [parameter (Mandatory=$true)]
5
- [string]$VmId,
6
- [parameter (Mandatory=$true)]
7
- [string]$SwitchName
8
-
5
+ [string]$VmId
9
6
  )
10
7
 
11
8
  $vm = Get-VM -Id $VmId
12
- $adapter = Add-VMNetworkAdapter -PassThru -VM $vm -SwitchName $SwitchName |
13
- Select-Object -Property "Name", "Id", "SwitchName", "MacAddress"
9
+ $adapter = Add-VMNetworkAdapter -PassThru -VM $vm |
10
+ Select-Object -Property "Name", "Id"
14
11
 
15
12
  Write-OutputMessage $(ConvertTo-JSON $adapter)
@@ -4,9 +4,10 @@ param (
4
4
  [parameter (Mandatory=$true)]
5
5
  [string]$Id,
6
6
  [parameter (Mandatory=$true)]
7
- [string]$SwitchName
7
+ [string]$SwitchId
8
8
 
9
9
  )
10
10
 
11
11
  $vm = Get-VM -Id $VmId
12
- Get-VMNetworkAdapter -VM $vm | Where-Object -Property Id -EQ -Value $Id | Connect-VMNetworkAdapter -SwitchName $SwitchName
12
+ $switch = Get-VMSwitch -Id $SwitchId
13
+ Get-VMNetworkAdapter -VM $vm | Where-Object -Property Id -EQ -Value $Id | Connect-VMNetworkAdapter -VMSwitch $switch
@@ -0,0 +1,5 @@
1
+ #Requires -Modules VagrantMessages
2
+
3
+ $routes = Get-NetRoute | Select-Object -Property "DestinationPrefix"
4
+
5
+ Write-OutputMessage $(ConvertTo-JSON $routes)
@@ -11,7 +11,7 @@ foreach($route in Get-NetRoute | Where-Object -Property DestinationPrefix -EQ -
11
11
  foreach($adapter in Get-NetAdapter -InterfaceIndex $route.ifIndex) {
12
12
  foreach($vmAdapter in Get-VMNetworkAdapter -ManagementOS | Where-Object -Property DeviceId -EQ -Value $adapter.DeviceId) {
13
13
  foreach($switch in Get-VMSwitch -Name $vmAdapter.SwitchName |
14
- Select-Object -Property Name,
14
+ Select-Object -Property Name, Id,
15
15
  @{Name='SwitchType';Expression={"$($_.SwitchType)"}},
16
16
  @{Name='NetAdapter';Expression={$switch_adapter[$_.Name]}}) {
17
17
  $switches += $switch
@@ -8,7 +8,7 @@ param (
8
8
  $switches = @()
9
9
 
10
10
  foreach($switch in Get-VMSwitch | Where-Object -Property Name -EQ -Value $Name |
11
- Select-Object -Property Name,
11
+ Select-Object -Property Name, Id,
12
12
  @{Name='SwitchType';Expression={"$($_.SwitchType)"}},
13
13
  @{Name='NetAdapter';Expression={$switch_adapter[$_.Name]}}) {
14
14
  $switches += $switch
@@ -6,6 +6,6 @@ param (
6
6
  )
7
7
  $vm = Get-VM -Id $VmId
8
8
  $adapters = Get-VMNetworkAdapter -VM $vm |
9
- Select-Object -Property "Name", "Id", "SwitchName", "MacAddress"
9
+ Select-Object -Property "Name", "Id", "SwitchName", "SwitchId", "MacAddress"
10
10
 
11
11
  Write-OutputMessage $(ConvertTo-JSON $adapters)
@@ -20,7 +20,7 @@ param (
20
20
  New-NetIPAddress -IPAddress $IPAddress -PrefixLength $PrefixLength -InterfaceIndex $adapter.ifIndex
21
21
  }
22
22
 
23
- Write-OutputMessage $($switch | Select-Object -Property Name,
23
+ Write-OutputMessage $($switch | Select-Object -Property Name, Id,
24
24
  @{Name='SwitchType';Expression={"$($_.SwitchType)"}},
25
25
  @{Name='NetAdapter';Expression={$switch_adapter[$_.Name]}} |
26
26
  ConvertTo-JSON)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module VagrantPlugins
4
4
  module HyperVNet
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
data/locales/en.yml CHANGED
@@ -62,6 +62,16 @@ en:
62
62
  Address: %{address}
63
63
  Netmask: %{mask}
64
64
  Error: %{error}
65
+ network_address_overlapping: |-
66
+ The IP address configured for the host-only network is overlapping
67
+ a subnet already defined. Please update the address used and run the command again.
68
+
69
+ Address: %{address}
70
+ Subnet: %{subnet}
71
+ network_not_unique: |-
72
+ The specified host network match morte then one item:
73
+
74
+ Networks: '%{networks}.'
65
75
  network_not_found: |-
66
76
  The specified host network could not be found: '%{name}.'
67
77
  If the name specification is removed, Vagrant will create a new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-hypervnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Bompani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-05 00:00:00.000000000 Z
11
+ date: 2023-04-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Vagrant plugin which extends Hyper-V provider implementing networks creation
14
14
  and configuration.
@@ -38,6 +38,7 @@ files:
38
38
  - lib/vagrant-hypervnet/errors.rb
39
39
  - lib/vagrant-hypervnet/scripts/add_vm_adapter.ps1
40
40
  - lib/vagrant-hypervnet/scripts/connect_vm_adapter.ps1
41
+ - lib/vagrant-hypervnet/scripts/get_routes.ps1
41
42
  - lib/vagrant-hypervnet/scripts/get_switch_by_address.ps1
42
43
  - lib/vagrant-hypervnet/scripts/get_switch_by_name.ps1
43
44
  - lib/vagrant-hypervnet/scripts/get_vm_adapters.ps1