From 8d6a33584f52a1c57cdcdd2c4c707e2afc60e04f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Jan 2012 21:12:08 -0800 Subject: [PATCH] Set the name as a separate step. [GH-669] --- CHANGELOG.md | 2 ++ lib/vagrant/action.rb | 1 + lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/default_name.rb | 22 ++++++++++++++++++++++ lib/vagrant/action/vm/import.rb | 3 +-- lib/vagrant/driver/virtualbox.rb | 1 + lib/vagrant/driver/virtualbox_4_0.rb | 26 +++++++++++++++++++++----- lib/vagrant/driver/virtualbox_4_1.rb | 22 +++++++++++++++++++--- lib/vagrant/driver/virtualbox_base.rb | 3 +-- 9 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 lib/vagrant/action/vm/default_name.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index def25b3e0..71d3bfde8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ properly created if they don't exist. [GH-667] - Fix the precedence for Arch, Ubuntu, and FreeBSD host classes so they are properly detected. [GH-683] + - Fix issue where VM import sometimes made strange VirtualBox folder + layouts. [GH-669] ## 0.9.3 (January 24, 2012) diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index a94dcf43d..8d5821ffe 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -35,6 +35,7 @@ module Vagrant autoload :ClearNetworkInterfaces, 'vagrant/action/vm/clear_network_interfaces' autoload :ClearSharedFolders, 'vagrant/action/vm/clear_shared_folders' autoload :Customize, 'vagrant/action/vm/customize' + autoload :DefaultName, 'vagrant/action/vm/default_name' autoload :Destroy, 'vagrant/action/vm/destroy' autoload :DestroyUnusedNetworkInterfaces, 'vagrant/action/vm/destroy_unused_network_interfaces' autoload :DiscardState, 'vagrant/action/vm/discard_state' diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 539255b49..14b31d0dc 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -85,6 +85,7 @@ module Vagrant use VM::CheckBox use VM::Import use VM::CheckGuestAdditions + use VM::DefaultName use VM::MatchMACAddress use registry.get(:start) end diff --git a/lib/vagrant/action/vm/default_name.rb b/lib/vagrant/action/vm/default_name.rb new file mode 100644 index 000000000..3ae69851d --- /dev/null +++ b/lib/vagrant/action/vm/default_name.rb @@ -0,0 +1,22 @@ +require 'log4r' + +module Vagrant + module Action + module VM + class DefaultName + def initialize(app, env) + @logger = Log4r::Logger.new("vagrant::action::vm::defaultname") + @app = app + end + + def call(env) + @logger.info("Setting the default name of the VM") + name = env[:root_path].basename.to_s + "_#{Time.now.to_i}" + env[:vm].driver.set_name(name) + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/vm/import.rb b/lib/vagrant/action/vm/import.rb index 01b4ff640..7d2f385fe 100644 --- a/lib/vagrant/action/vm/import.rb +++ b/lib/vagrant/action/vm/import.rb @@ -10,9 +10,8 @@ module Vagrant env[:ui].info I18n.t("vagrant.actions.vm.import.importing", :name => env[:vm].box.name) # Import the virtual machine - name = File.basename(env[:vm].env.cwd) + "_#{Time.now.to_i}" ovf_file = env[:vm].box.directory.join("box.ovf").to_s - env[:vm].uuid = env[:vm].driver.import(ovf_file, name) do |progress| + env[:vm].uuid = env[:vm].driver.import(ovf_file) do |progress| env[:ui].clear_line env[:ui].report_progress(progress, 100, false) end diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index 8a37f4e0b..e689de0ee 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -94,6 +94,7 @@ module Vagrant :read_used_ports, :read_vms, :set_mac_address, + :set_name, :share_folders, :ssh_port, :start, diff --git a/lib/vagrant/driver/virtualbox_4_0.rb b/lib/vagrant/driver/virtualbox_4_0.rb index 10d0f969c..9fbee2a79 100644 --- a/lib/vagrant/driver/virtualbox_4_0.rb +++ b/lib/vagrant/driver/virtualbox_4_0.rb @@ -9,7 +9,7 @@ module Vagrant def initialize(uuid) super() - @logger = Log4r::Logger.new("vagrant::driver::virtualbox_4_1") + @logger = Log4r::Logger.new("vagrant::driver::virtualbox_4_0") @uuid = uuid end @@ -147,11 +147,15 @@ module Vagrant execute("controlvm", @uuid, "poweroff") end - def import(ovf, name) + def import(ovf) + output = "" total = "" last = 0 - execute("import", ovf, "--vsys", "0", "--vmname", name) do |type, data| - if type == :stderr + execute("import", ovf) do |type, data| + if type == :stdout + # Keep track of the stdout so that we can get the VM name + output << data + elsif type == :stderr # Append the data so we can see the full view total << data @@ -171,6 +175,14 @@ module Vagrant end end + # Find the name of the VM name + if output !~ /Suggested VM name "(.+?)"/ + @logger.error("Couldn't find VM name in the output.") + return nil + end + + name = $1.to_s + output = execute("list", "vms") if output =~ /^"#{Regexp.escape(name)}" \{(.+?)\}$/ return $1.to_s @@ -274,7 +286,7 @@ module Vagrant elsif line =~ /^NetworkMask:\s+(.+?)$/ info[:netmask] = $1.to_s elsif line =~ /^Status:\s+(.+?)$/ - info[:status] = $1.to_s + info[:status] = $1.to_s end end @@ -374,6 +386,10 @@ module Vagrant execute("modifyvm", @uuid, "--macaddress1", mac) end + def set_name(name) + execute("modifyvm", @uuid, "--name", name) + end + def share_folders(folders) folders.each do |folder| execute("sharedfolder", "add", @uuid, "--name", diff --git a/lib/vagrant/driver/virtualbox_4_1.rb b/lib/vagrant/driver/virtualbox_4_1.rb index e63a16443..bb2bd324a 100644 --- a/lib/vagrant/driver/virtualbox_4_1.rb +++ b/lib/vagrant/driver/virtualbox_4_1.rb @@ -147,11 +147,15 @@ module Vagrant execute("controlvm", @uuid, "poweroff") end - def import(ovf, name) + def import(ovf) + output = "" total = "" last = 0 - execute("import", ovf, "--vsys", "0", "--vmname", name) do |type, data| - if type == :stderr + execute("import", ovf) do |type, data| + if type == :stdout + # Keep track of the stdout so that we can get the VM name + output << data + elsif type == :stderr # Append the data so we can see the full view total << data @@ -171,6 +175,14 @@ module Vagrant end end + # Find the name of the VM name + if output !~ /Suggested VM name "(.+?)"/ + @logger.error("Couldn't find VM name in the output.") + return nil + end + + name = $1.to_s + output = execute("list", "vms") if output =~ /^"#{Regexp.escape(name)}" \{(.+?)\}$/ return $1.to_s @@ -374,6 +386,10 @@ module Vagrant execute("modifyvm", @uuid, "--macaddress1", mac) end + def set_name(name) + execute("modifyvm", @uuid, "--name", name) + end + def share_folders(folders) folders.each do |folder| execute("sharedfolder", "add", @uuid, "--name", diff --git a/lib/vagrant/driver/virtualbox_base.rb b/lib/vagrant/driver/virtualbox_base.rb index a3f72eee0..ed1227e2e 100644 --- a/lib/vagrant/driver/virtualbox_base.rb +++ b/lib/vagrant/driver/virtualbox_base.rb @@ -133,9 +133,8 @@ module Vagrant # Imports the VM from an OVF file. # # @param [String] ovf Path to the OVF file. - # @param [String] name Name of the VM. # @return [String] UUID of the imported VM. - def import(ovf, name) + def import(ovf) end # Returns a list of forwarded ports for a VM.