Add validation once again for host only networks

This commit is contained in:
Mitchell Hashimoto 2011-12-31 10:55:37 +09:00
parent c64f5e8d05
commit b5b3805157
3 changed files with 59 additions and 23 deletions

View File

@ -11,7 +11,7 @@ module Vagrant
def call(env)
@env = env
networks = env[:vm].config.vm.network_options.compact
networks = host_only_networks
# Verify that none of the networks collide with a bridged
# interface, because this will cause problems.
@ -40,6 +40,27 @@ module Vagrant
end
end
# Returns an array of the network options for host only networks.
def host_only_networks
results = []
@env[:vm].config.vm.networks.each do |type, args|
if type == :hostonly
ip = args[0]
options = args[1] || {}
results << {
:ip => ip,
:netmask => "255.255.255.0",
:adapter => 1,
:mac => nil,
:name => nil
}.merge(options)
end
end
results
end
# Verifies that there is no collision with a bridged network interface
# for the given network options.
def verify_no_bridge_collision(net_options)

View File

@ -13,7 +13,7 @@ module Vagrant
attr_accessor :host_name
attr_reader :forwarded_ports
attr_reader :shared_folders
attr_reader :network_options
attr_reader :networks
attr_reader :provisioners
attr_reader :customizations
attr_accessor :guest
@ -21,7 +21,7 @@ module Vagrant
def initialize
@forwarded_ports = {}
@shared_folders = {}
@network_options = []
@networks = []
@provisioners = []
@customizations = []
end
@ -48,16 +48,8 @@ module Vagrant
}.merge(opts || {})
end
def network(ip, options=nil)
options = {
:ip => ip,
:netmask => "255.255.255.0",
:adapter => 1,
:mac => nil,
:name => nil
}.merge(options || {})
@network_options[options[:adapter]] = options
def network(type, *args)
@networks << [type, args]
end
def provision(name, options=nil, &block)
@ -130,17 +122,34 @@ do before is certainly still possible with `VBoxManage` as well.
end
# Validate some basic networking
network_options.each do |options|
next if !options
#
# TODO: One day we need to abstract this out, since in the future
# providers other than VirtualBox will not be able to satisfy
# all types of networks.
networks.each do |type, args|
if type == :hostonly
# Validate the host-only network
ip = args[0]
options = args[1] || {}
ip = options[:ip].split(".")
if !ip
errors.add(I18n.t("vagrant.config.vm.network_ip_required"))
else
ip_parts = ip.split(".")
if ip.length != 4
errors.add(I18n.t("vagrant.config.vm.network_ip_invalid",
:ip => options[:ip]))
elsif ip.last == "1"
errors.add(I18n.t("vagrant.config.vm.network_ip_ends_one",
:ip => options[:ip]))
if ip_parts.length != 4
errors.add(I18n.t("vagrant.config.vm.network_ip_invalid",
:ip => ip))
elsif ip_parts.last == "1"
errors.add(I18n.t("vagrant.config.vm.network_ip_ends_one",
:ip => ip))
end
end
elsif type == :bridged
else
# Invalid network type
errors.add(I18n.t("vagrant.config.vm.network_invalid",
:type => type.to_s))
end
end

View File

@ -191,7 +191,13 @@ en:
boot_mode_invalid: "Boot mode must be one of: headless or gui"
box_missing: "A box must be specified."
box_not_found: "The box '%{name}' could not be found."
network_ip_invalid: "The host only network IP '%{ip}' is invalid."
network_invalid: |-
The network type '%{type}' is not valid. Please use
'hostonly' or 'bridged'.
network_ip_required: |-
Host only networks require an IP as an argument.
network_ip_invalid: |-
The host only network IP '%{ip}' is invalid.
network_ip_ends_one: |-
The host only network IP '%{ip}' must not end in a 1, as this
is reserved for the host machine.