From 5f34a694f50edd910c1d8e1adc69f5c6dcf775a5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Jul 2013 12:56:40 -0700 Subject: [PATCH] VBoxManage customizations can run at different events [GH-1247] --- CHANGELOG.md | 3 +++ plugins/providers/virtualbox/action.rb | 4 +++- .../providers/virtualbox/action/customize.rb | 13 ++++++++--- plugins/providers/virtualbox/config.rb | 22 +++++++++++++++++-- templates/locales/en.yml | 8 ++++++- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d07ad30b..48d470123 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ FEATURES: - `vagrant help ` now works. [GH-1578] - Added `config.vm.box_download_insecure` to allor the box_url setting to point to an https site that won't be validated. [GH-1712] + - VirtualBox VBoxManage customizations can now be specified to run + pre-boot (the default and existing functionality, pre-import, + or post-boot. [GH-1247] IMPROVEMENTS: diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 972c5c8c4..c94a08421 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -69,8 +69,9 @@ module VagrantPlugins b.use ForwardPorts b.use SetHostname b.use SaneDefaults - b.use Customize + b.use Customize, "pre-boot" b.use Boot + b.use Customize, "post-boot" b.use CheckGuestAdditions end end @@ -308,6 +309,7 @@ module VagrantPlugins # If the VM is NOT created yet, then do the setup steps if !env[:result] b2.use CheckAccessible + b2.use Customize, "pre-import" b2.use Import b2.use MatchMACAddress end diff --git a/plugins/providers/virtualbox/action/customize.rb b/plugins/providers/virtualbox/action/customize.rb index eb39ab962..0bcb84a6d 100644 --- a/plugins/providers/virtualbox/action/customize.rb +++ b/plugins/providers/virtualbox/action/customize.rb @@ -2,14 +2,21 @@ module VagrantPlugins module ProviderVirtualBox module Action class Customize - def initialize(app, env) + def initialize(app, env, event) @app = app + @event = event end def call(env) - customizations = env[:machine].provider_config.customizations + customizations = [] + env[:machine].provider_config.customizations.each do |event, command| + if event == @event + customizations << command + end + end + if !customizations.empty? - env[:ui].info I18n.t("vagrant.actions.vm.customize.running") + env[:ui].info I18n.t("vagrant.actions.vm.customize.running", event: @event) # Execute each customization command. customizations.each do |command| diff --git a/plugins/providers/virtualbox/config.rb b/plugins/providers/virtualbox/config.rb index b14d262d0..f7a62f64b 100644 --- a/plugins/providers/virtualbox/config.rb +++ b/plugins/providers/virtualbox/config.rb @@ -52,8 +52,10 @@ module VagrantPlugins # # @param [Array] command An array of arguments to pass to # VBoxManage. - def customize(command) - @customizations << command + def customize(*command) + event = command.first.is_a?(String) ? command.shift : "pre-boot" + command = command[0] + @customizations << [event, command] end # This defines a network adapter that will be added to the VirtualBox @@ -78,6 +80,22 @@ module VagrantPlugins @name = nil if @name == UNSET_VALUE end + def validate(machine) + errors = [] + + valid_events = ["pre-import", "pre-boot", "post-boot"] + @customizations.each do |event, _| + if !valid_events.include?(event) + errors << I18n.t( + "vagrant.virtualbox.config.invalid_event", + event: event.to_s, + valid_events: valid_events.join(", ")) + end + end + + { "VitualBox Provider" => errors } + end + def to_s "VirtualBox" end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index c43b18a05..d9b5fed08 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -40,6 +40,12 @@ en: run_file_not_found: |- The 'run_file' specified could not be found. + virtualbox: + config: + invalid_event: |- + %{event} is not a valid event for customization. Valid events + are: %{valid_events} + general: batch_unexpected_error: |- An unexpected error ocurred when executing the action on the @@ -862,7 +868,7 @@ en: %{error} Please fix this customization and try again. - running: Running any VM customizations... + running: Running '%{event}' VM customizations... destroy: destroying: Destroying VM and associated drives... destroy_network: