diff --git a/CHANGELOG.md b/CHANGELOG.md index 445214605..29c402812 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ FEATURES: false will not attempt to automatically manage NAT DNS proxy settings with VirtualBox. [GH-1313] - `vagrant provision` accepts the `--provision-with` flag [GH-1167] + - Set the name of VirtualBox machines with `virtualbox.name` in the + VirtualBox provider config. [GH-1126] IMPROVEMENTS / BUG FIXES: diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index b7a2f5550..1506de024 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -17,7 +17,6 @@ module VagrantPlugins autoload :ClearSharedFolders, File.expand_path("../action/clear_shared_folders", __FILE__) autoload :Created, File.expand_path("../action/created", __FILE__) autoload :Customize, File.expand_path("../action/customize", __FILE__) - autoload :DefaultName, File.expand_path("../action/default_name", __FILE__) autoload :Destroy, File.expand_path("../action/destroy", __FILE__) autoload :DestroyConfirm, File.expand_path("../action/destroy_confirm", __FILE__) autoload :DestroyUnusedNetworkInterfaces, File.expand_path("../action/destroy_unused_network_interfaces", __FILE__) @@ -41,6 +40,7 @@ module VagrantPlugins autoload :PruneNFSExports, File.expand_path("../action/prune_nfs_exports", __FILE__) autoload :Resume, File.expand_path("../action/resume", __FILE__) autoload :SaneDefaults, File.expand_path("../action/sane_defaults", __FILE__) + autoload :SetName, File.expand_path("../action/set_name", __FILE__) autoload :SetupPackageFiles, File.expand_path("../action/setup_package_files", __FILE__) autoload :ShareFolders, File.expand_path("../action/share_folders", __FILE__) autoload :Suspend, File.expand_path("../action/suspend", __FILE__) @@ -295,7 +295,7 @@ module VagrantPlugins b2.use CheckBox b2.use Import b2.use CheckGuestAdditions - b2.use DefaultName + b2.use SetName b2.use MatchMACAddress end end diff --git a/plugins/providers/virtualbox/action/default_name.rb b/plugins/providers/virtualbox/action/default_name.rb deleted file mode 100644 index f1b0565dd..000000000 --- a/plugins/providers/virtualbox/action/default_name.rb +++ /dev/null @@ -1,26 +0,0 @@ -require "log4r" - -module VagrantPlugins - module ProviderVirtualBox - module Action - class DefaultName - def initialize(app, env) - @logger = Log4r::Logger.new("vagrant::action::vm::defaultname") - @app = app - end - - def call(env) - # Figure out the name and sanitize the default - prefix = env[:root_path].basename.to_s - prefix.gsub!(/[^-a-z0-9_]/i, "") - name = prefix + "_#{Time.now.to_i}" - @logger.info("Setting the default name of the VM: #{name}") - - env[:machine].provider.driver.set_name(name) - - @app.call(env) - end - end - end - end -end diff --git a/plugins/providers/virtualbox/action/set_name.rb b/plugins/providers/virtualbox/action/set_name.rb new file mode 100644 index 000000000..adfaa1b0e --- /dev/null +++ b/plugins/providers/virtualbox/action/set_name.rb @@ -0,0 +1,30 @@ +require "log4r" + +module VagrantPlugins + module ProviderVirtualBox + module Action + class SetName + def initialize(app, env) + @logger = Log4r::Logger.new("vagrant::action::vm::setname") + @app = app + end + + def call(env) + name = env[:machine].provider_config.name + + # If no name was manually set, then use a default + if !name + prefix = env[:root_path].basename.to_s + prefix.gsub!(/[^-a-z0-9_]/i, "") + name = prefix + "_#{Time.now.to_i}" + end + + @logger.info("Setting the name of the VM: #{name}") + env[:machine].provider.driver.set_name(name) + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/virtualbox/config.rb b/plugins/providers/virtualbox/config.rb index 4df7e834b..846bdc08e 100644 --- a/plugins/providers/virtualbox/config.rb +++ b/plugins/providers/virtualbox/config.rb @@ -18,6 +18,12 @@ module VagrantPlugins # @return [Boolean] attr_accessor :gui + # This should be set to the name of the machine in the VirtualBox + # GUI. + # + # @return [String] + attr_accessor :name + # The defined network adapters. # # @return [Hash] @@ -26,6 +32,7 @@ module VagrantPlugins def initialize @auto_nat_dns_proxy = UNSET_VALUE @customizations = [] + @name = UNSET_VALUE @network_adapters = {} @gui = UNSET_VALUE @@ -66,6 +73,9 @@ module VagrantPlugins # Default is to not show a GUI @gui = false if @gui == UNSET_VALUE + + # The default name is just nothing, and we default it + @name = nil if @name == UNSET_VALUE end end end