From e1d0ce19cec63cdba3ff06b580f0304c5d2de80c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 21 Dec 2011 16:25:58 -0800 Subject: [PATCH] Customizations are back in! --- CHANGELOG.md | 4 ++++ lib/vagrant/action.rb | 1 + lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/customize.rb | 30 ++++++++++++++++++++++++++++++ lib/vagrant/config/vm.rb | 25 +++++++++++++++++++++---- lib/vagrant/driver/virtualbox.rb | 7 ++++++- lib/vagrant/errors.rb | 5 +++++ templates/locales/en.yml | 6 ++++++ 8 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 lib/vagrant/action/vm/customize.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5321b4b..1c9d914c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 0.9.0 (unreleased) + - `config.vm.customize` now takes a command to send to `VBoxManage`, so any + arbitrary command can be sent. The older style of passing a block no longer + works and Vagrant will give a proper error message if it notices this old-style + being used. - Logging. The entire Vagrant source has had logging sprinkled throughout to make debugging issues easier. To enable logging, set the VAGRANT_LOG environmental variable to the log level you wish to see. By default, diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 342044335..a931f3e97 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -33,6 +33,7 @@ module Vagrant autoload :ClearForwardedPorts, 'vagrant/action/vm/clear_forwarded_ports' autoload :ClearNFSExports, 'vagrant/action/vm/clear_nfs_exports' autoload :ClearSharedFolders, 'vagrant/action/vm/clear_shared_folders' + autoload :Customize, 'vagrant/action/vm/customize' 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 a45274c88..1048b97a9 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -29,6 +29,7 @@ module Vagrant use VM::ShareFolders use VM::HostName use VM::Network + use VM::Customize use VM::Boot end end diff --git a/lib/vagrant/action/vm/customize.rb b/lib/vagrant/action/vm/customize.rb new file mode 100644 index 000000000..5469fb576 --- /dev/null +++ b/lib/vagrant/action/vm/customize.rb @@ -0,0 +1,30 @@ +module Vagrant + module Action + module VM + class Customize + def initialize(app, env) + @app = app + end + + def call(env) + customizations = env[:vm].config.vm.customizations + if !customizations.empty? + env[:ui].info I18n.t("vagrant.actions.vm.customize.running") + + # Execute each customization command. + customizations.each do |command| + processed_command = command.collect do |arg| + arg = env[:vm].uuid if arg == :id + arg + end + + env[:vm].driver.execute_command(processed_command) + end + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index 52b3f0f50..92e3cb29e 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -4,8 +4,6 @@ require 'vagrant/config/vm/provisioner' module Vagrant module Config class VMConfig < Base - include Util::StackedProcRunner - attr_accessor :name attr_accessor :auto_port_range attr_accessor :box @@ -17,6 +15,7 @@ module Vagrant attr_reader :shared_folders attr_reader :network_options attr_reader :provisioners + attr_reader :customizations attr_accessor :guest def initialize @@ -24,6 +23,7 @@ module Vagrant @shared_folders = {} @network_options = [] @provisioners = [] + @customizations = [] end def forward_port(name, guestport, hostport, options=nil) @@ -64,8 +64,25 @@ module Vagrant @provisioners << Provisioner.new(name, options, &block) end - def customize(&block) - push_proc(&block) + # TODO: This argument should not be `nil` in the future. + # It is only defaulted to nil so that the deprecation error + # can be properly shown. + def customize(command=nil) + if block_given? + raise Errors::DeprecationError, :message => <<-MESSAGE +`config.vm.customize` now takes an array of arguments to send to +`VBoxManage` instead of having a block which gets a virtual machine +object. Example of the new usage: + + config.vm.customize ["modifyvm", :id, "--memory", "1024"] + +The above will run `VBoxManage modifyvm 1234 --memory 1024` where +"1234" is the ID of your current virtual machine. Anything you could +do before is certainly still possible with `VBoxManage` as well. + MESSAGE + end + + @customizations << command if command end def defined_vms diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index 4647bf8c9..6d43bdffc 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -74,6 +74,11 @@ module Vagrant end end + # Executes a raw command. + def execute_command(command) + execute(*command) + end + # Forwards a set of ports for a VM. # # This will not affect any previously set forwarded ports, @@ -171,7 +176,7 @@ module Vagrant # This reads the folder where VirtualBox places it's VMs. def read_machine_folder execute("list", "systemproperties").split("\n").each do |line| - if line =~ /^Default machine folder:\s+(.+?)$/ + if line =~ /^Default machine folder:\s+(.+?)$/i return $1.to_s end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 274f2ce1f..0fad2ec7f 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -138,6 +138,11 @@ module Vagrant error_key(:config_validation) end + class DeprecationError < VagrantError + status_code(60) + error_key(:deprecation) + end + class DotfileIsDirectory < VagrantError status_code(46) error_key(:dotfile_is_directory) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 20ab73928..abfce1148 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -31,6 +31,12 @@ en: are printed below: %{messages} + deprecation: |- + You are using a feature that has been removed in this version. Explanation: + + %{message} + + Note that this error message will not appear in the next version of Vagrant. dotfile_is_directory: |- The local file Vagrant uses to store data ".vagrant" already exists and is a directory! If you are in your home directory, then please run