mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
Merge pull request #417 from joejulian/allow_veryisolated_networks
Allow veryisolated networks
This commit is contained in:
commit
c834ba3285
@ -63,14 +63,11 @@ module VagrantPlugins
|
|||||||
|
|
||||||
if @options[:ip]
|
if @options[:ip]
|
||||||
handle_ip_option(env)
|
handle_ip_option(env)
|
||||||
# in vagrant 1.2.3 and later it is not possible to take this branch
|
|
||||||
# because cannot have name without ip
|
|
||||||
# https://github.com/mitchellh/vagrant/commit/cf2f6da4dbcb4f57c9cdb3b94dcd0bba62c5f5fd
|
|
||||||
elsif @options[:network_name]
|
elsif @options[:network_name]
|
||||||
handle_network_name_option
|
handle_network_name_option(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
autostart_network if !@interface_network[:autostart]
|
autostart_network if @interface_network[:autostart]
|
||||||
activate_network if !@interface_network[:active]
|
activate_network if !@interface_network[:active]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -80,11 +77,25 @@ module VagrantPlugins
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def lookup_network_by_ip(ip)
|
||||||
|
@logger.debug "looking up network with ip == #{ip}"
|
||||||
|
@available_networks.each do |network|
|
||||||
|
if network[:network_address] == ip
|
||||||
|
@logger.debug "found existing network by ip: #{network}"
|
||||||
|
return network
|
||||||
|
end
|
||||||
|
end
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
# Return hash of network for specified name, or nil if not found.
|
# Return hash of network for specified name, or nil if not found.
|
||||||
def lookup_network_by_name(network_name)
|
def lookup_network_by_name(network_name)
|
||||||
@logger.debug "looking up network named #{network_name}"
|
@logger.debug "looking up network named #{network_name}"
|
||||||
@available_networks.each do |network|
|
@available_networks.each do |network|
|
||||||
return network if network[:name] == network_name
|
if network[:name] == network_name
|
||||||
|
@logger.debug "found existing network by name: #{network}"
|
||||||
|
return network
|
||||||
|
end
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
@ -116,32 +127,38 @@ module VagrantPlugins
|
|||||||
# @available_networks should be filled before calling this function.
|
# @available_networks should be filled before calling this function.
|
||||||
def handle_ip_option(env)
|
def handle_ip_option(env)
|
||||||
return if !@options[:ip]
|
return if !@options[:ip]
|
||||||
|
net_address = nil
|
||||||
|
if @options[:forward_mode] != 'veryisolated'
|
||||||
|
net_address = network_address(@options[:ip], @options[:netmask])
|
||||||
|
# Set IP address of network (actually bridge). It will be used as
|
||||||
|
# gateway address for machines connected to this network.
|
||||||
|
net = IPAddr.new(net_address)
|
||||||
|
|
||||||
|
# Default to first address (after network name)
|
||||||
|
@interface_network[:ip_address] = @options[:host_ip].nil? ? \
|
||||||
|
net.to_range.begin.succ : \
|
||||||
|
IPAddr.new(@options[:host_ip])
|
||||||
|
end
|
||||||
|
|
||||||
net_address = network_address(@options[:ip], @options[:netmask])
|
|
||||||
@interface_network[:network_address] = net_address
|
@interface_network[:network_address] = net_address
|
||||||
|
|
||||||
# Set IP address of network (actually bridge). It will be used as
|
# if network is veryisolated, search by name
|
||||||
# gateway address for machines connected to this network.
|
if @options[:libvirt__forward_mode] == "veryisolated"
|
||||||
net = IPAddr.new(net_address)
|
network = lookup_network_by_name(@options[:network_name])
|
||||||
# Default to first address (after network name)
|
elsif net_address
|
||||||
@interface_network[:ip_address] = @options[:host_ip].nil? ? net.to_range.begin.succ : IPAddr.new(@options[:host_ip])
|
# otherwise, search by ip (if set)
|
||||||
|
network = lookup_network_by_ip(net_address)
|
||||||
# Is there an available network matching to configured ip
|
else
|
||||||
# address?
|
# leaving this here to mimic prior behavior. If we get
|
||||||
@available_networks.each do |available_network|
|
# here, something's probably broken.
|
||||||
if available_network[:network_address] == \
|
network = lookup_network_by_name(@options[:network_name])
|
||||||
@interface_network[:network_address]
|
|
||||||
@interface_network = available_network
|
|
||||||
@logger.debug "found existing network by ip, values are"
|
|
||||||
@logger.debug @interface_network
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
@interface_network = network if network
|
||||||
|
|
||||||
if @interface_network[:created]
|
if @interface_network[:created]
|
||||||
verify_dhcp
|
verify_dhcp
|
||||||
end
|
end
|
||||||
|
|
||||||
if @options[:network_name]
|
if @options[:network_name]
|
||||||
@logger.debug "Checking that network name does not clash with ip"
|
@logger.debug "Checking that network name does not clash with ip"
|
||||||
if @interface_network[:created]
|
if @interface_network[:created]
|
||||||
@ -160,13 +177,13 @@ module VagrantPlugins
|
|||||||
ip_address: @options[:ip],
|
ip_address: @options[:ip],
|
||||||
network_name: @options[:network_name]
|
network_name: @options[:network_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Network with 'name' doesn't exist. Set it as name for new
|
# Network with 'name' doesn't exist. Set it as name for new
|
||||||
# network.
|
# network.
|
||||||
@interface_network[:name] = @options[:network_name]
|
@interface_network[:name] = @options[:network_name]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Do we need to create new network?
|
# Do we need to create new network?
|
||||||
if !@interface_network[:created]
|
if !@interface_network[:created]
|
||||||
|
|
||||||
@ -209,15 +226,39 @@ module VagrantPlugins
|
|||||||
# Handle network_name option, if ip was not specified. Variables
|
# Handle network_name option, if ip was not specified. Variables
|
||||||
# @options and @available_networks should be filled before calling this
|
# @options and @available_networks should be filled before calling this
|
||||||
# function.
|
# function.
|
||||||
def handle_network_name_option
|
def handle_network_name_option(env)
|
||||||
return if @options[:ip] || !@options[:network_name]
|
return if @options[:ip] || \
|
||||||
|
!@options[:network_name] || \
|
||||||
|
!@options[:libvirt__forward_mode] == "veryisolated"
|
||||||
|
|
||||||
@interface_network = lookup_network_by_name(@options[:network_name])
|
network = lookup_network_by_name(@options[:network_name])
|
||||||
if !@interface_network
|
@interface_network = network if network
|
||||||
|
|
||||||
|
# if this interface has a network address, something's wrong.
|
||||||
|
if @interface_network[:network_address]
|
||||||
raise Errors::NetworkNotAvailableError,
|
raise Errors::NetworkNotAvailableError,
|
||||||
network_name: @options[:network_name]
|
network_name: @options[:network_name]
|
||||||
else
|
end
|
||||||
verify_dhcp
|
|
||||||
|
# Do we need to create new network?
|
||||||
|
if !@interface_network[:created]
|
||||||
|
@interface_network[:name] = @options[:network_name]
|
||||||
|
|
||||||
|
# Generate a unique name for network bridge.
|
||||||
|
count = 0
|
||||||
|
while @interface_network[:bridge_name].nil?
|
||||||
|
@logger.debug "generating name for bridge"
|
||||||
|
bridge_name = 'virbr'
|
||||||
|
bridge_name << count.to_s
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
next if lookup_bridge_by_name(bridge_name)
|
||||||
|
|
||||||
|
@interface_network[:bridge_name] = bridge_name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create a private network.
|
||||||
|
create_private_network(env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -236,7 +277,7 @@ module VagrantPlugins
|
|||||||
# Find out DHCP addresses pool range.
|
# Find out DHCP addresses pool range.
|
||||||
network_address = "#{@interface_network[:network_address]}/"
|
network_address = "#{@interface_network[:network_address]}/"
|
||||||
network_address << "#{@interface_network[:netmask]}"
|
network_address << "#{@interface_network[:netmask]}"
|
||||||
net = IPAddr.new(network_address)
|
net = @interface_network[:network_address] ? IPAddr.new(network_address) : nil
|
||||||
|
|
||||||
# First is address of network, second is gateway (by default).
|
# First is address of network, second is gateway (by default).
|
||||||
# So start the range two addresses after network address by default.
|
# So start the range two addresses after network address by default.
|
||||||
|
Loading…
Reference in New Issue
Block a user