From b5b38051571225499e5c737eb579ae592722a3ab Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 31 Dec 2011 10:55:37 +0900 Subject: [PATCH] Add validation once again for host only networks --- lib/vagrant/action/vm/network.rb | 23 +++++++++++++- lib/vagrant/config/vm.rb | 51 +++++++++++++++++++------------- templates/locales/en.yml | 8 ++++- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/lib/vagrant/action/vm/network.rb b/lib/vagrant/action/vm/network.rb index 30f9ccd67..77a49a085 100644 --- a/lib/vagrant/action/vm/network.rb +++ b/lib/vagrant/action/vm/network.rb @@ -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) diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index c0b7b0748..22e9a3f55 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -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 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 600061804..d1b3104dc 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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.