Switch the actions over to using the new UI objects.

This commit is contained in:
Mitchell Hashimoto 2010-08-25 22:13:55 -07:00
parent e74bce8b10
commit 9ab5a7c2b1
31 changed files with 75 additions and 68 deletions

View File

@ -7,7 +7,7 @@ env = Vagrant::Environment.load!
begin begin
Vagrant::CLI.start(ARGV, :env => env) Vagrant::CLI.start(ARGV, :env => env)
rescue Vagrant::VagrantError => e rescue Vagrant::VagrantError => e
env.ui.error e.message env.ui.error e.message, false
env.ui.error e.backtrace.join("\n") if ENV["VAGRANT_DEBUG"] env.ui.error e.backtrace.join("\n"), false if ENV["VAGRANT_DEBUG"]
exit e.status_code exit e.status_code
end end

View File

@ -10,7 +10,7 @@ module Vagrant
end end
def call(env) def call(env)
env.logger.info "Deleting box directory..." env.ui.info "Deleting box directory..."
FileUtils.rm_rf(env["box"].directory) FileUtils.rm_rf(env["box"].directory)
@app.call(env) @app.call(env)

View File

@ -29,7 +29,7 @@ module Vagrant
def instantiate_downloader def instantiate_downloader
@env["download.classes"].each do |klass| @env["download.classes"].each do |klass|
if klass.match?(@env["box"].uri) if klass.match?(@env["box"].uri)
@env.logger.info "Downloading with #{klass}..." @env.ui.info "Downloading with #{klass}..."
@downloader = klass.new(@env) @downloader = klass.new(@env)
end end
end end
@ -52,13 +52,13 @@ module Vagrant
def recover(env) def recover(env)
if temp_path && File.exist?(temp_path) if temp_path && File.exist?(temp_path)
env.logger.info "Cleaning up downloaded box..." env.ui.info "Cleaning up downloaded box..."
File.unlink(temp_path) File.unlink(temp_path)
end end
end end
def with_tempfile def with_tempfile
@env.logger.info "Creating tempfile for storing box file..." @env.ui.info "Creating tempfile for storing box file..."
File.open(box_temp_path, Platform.tar_file_options) do |tempfile| File.open(box_temp_path, Platform.tar_file_options) do |tempfile|
yield tempfile yield tempfile
end end
@ -69,7 +69,7 @@ module Vagrant
end end
def download_to(f) def download_to(f)
@env.logger.info "Copying box to temporary location..." @env.ui.info "Copying box to temporary location..."
@downloader.download!(@env["box"].uri, f) @downloader.download!(@env["box"].uri, f)
end end
end end

View File

@ -49,7 +49,7 @@ module Vagrant
def decompress def decompress
Dir.chdir(@env["box"].directory) do Dir.chdir(@env["box"].directory) do
@env.logger.info "Extracting box to #{@env["box"].directory}..." @env.ui.info "Extracting box to #{@env["box"].directory}..."
Archive::Tar::Minitar.unpack(@env["download.temp_path"], @env["box"].directory) Archive::Tar::Minitar.unpack(@env["download.temp_path"], @env["box"].directory)
end end
end end

View File

@ -9,7 +9,7 @@ module Vagrant
def call(env) def call(env)
begin begin
env.logger.info "Verifying box..." env.ui.info "Verifying box..."
VirtualBox::Appliance.new(env["box"].ovf_file) VirtualBox::Appliance.new(env["box"].ovf_file)
rescue Exception rescue Exception
return env.error!(:box_verification_failed) return env.error!(:box_verification_failed)

View File

@ -34,6 +34,11 @@ module Vagrant
env.logger env.logger
end end
# Returns a UI object from the environment
def ui
env.ui
end
# Flags the environment as erroneous. Stores the given key # Flags the environment as erroneous. Stores the given key
# and options until the end of the action sequence. # and options until the end of the action sequence.
# #

View File

@ -60,7 +60,7 @@ module Vagrant
FileUtils.mkdir_p(include_dir) FileUtils.mkdir_p(include_dir)
@env["package.include"].each do |f| @env["package.include"].each do |f|
@env.logger.info "Packaging additional file: #{f}" @env.ui.info "Packaging additional file: #{f}"
FileUtils.cp(f, include_dir) FileUtils.cp(f, include_dir)
end end
end end
@ -68,7 +68,7 @@ module Vagrant
# Compress the exported file into a package # Compress the exported file into a package
def compress def compress
@env.logger.info "Compressing package to #{tar_path}..." @env.ui.info "Compressing package to #{tar_path}..."
File.open(tar_path, Platform.tar_file_options) do |tar| File.open(tar_path, Platform.tar_file_options) do |tar|
Archive::Tar::Minitar::Output.open(tar) do |output| Archive::Tar::Minitar::Output.open(tar) do |output|
begin begin

View File

@ -22,18 +22,18 @@ module Vagrant
end end
def boot def boot
@env.logger.info "Booting VM..." @env.ui.info "Booting VM..."
@env["vm"].vm.start(@env.env.config.vm.boot_mode) @env["vm"].vm.start(@env.env.config.vm.boot_mode)
end end
def wait_for_boot def wait_for_boot
@env.logger.info "Waiting for VM to boot..." @env.ui.info "Waiting for VM to boot..."
@env.env.config.ssh.max_tries.to_i.times do |i| @env.env.config.ssh.max_tries.to_i.times do |i|
@env.logger.info "Trying to connect (attempt ##{i+1} of #{@env.env.config[:ssh][:max_tries]})..." @env.ui.info "Trying to connect (attempt ##{i+1} of #{@env.env.config[:ssh][:max_tries]})..."
if @env["vm"].ssh.up? if @env["vm"].ssh.up?
@env.logger.info "VM booted and ready for use!" @env.ui.info "VM booted and ready for use!"
return true return true
end end
@ -44,7 +44,7 @@ module Vagrant
sleep 5 if !@env["vagrant.test"] sleep 5 if !@env["vagrant.test"]
end end
@env.logger.info "Failed to connect to VM! Failed to boot?" @env.ui.error "Failed to connect to VM! Failed to boot?"
false false
end end
end end

View File

@ -14,7 +14,7 @@ module Vagrant
box_url = env["config"].vm.box_url box_url = env["config"].vm.box_url
return env.error!(:box_specified_doesnt_exist, :box_name => box_name) if !box_url return env.error!(:box_specified_doesnt_exist, :box_name => box_name) if !box_url
env.logger.info "Box #{box_name} not found. Fetching box since URL specified..." env.ui.info "Box #{box_name} not found. Fetching box since URL specified..."
Vagrant::Box.add(env.env, box_name, box_url) Vagrant::Box.add(env.env, box_name, box_url)
env.env.load_box! env.env.load_box!
end end

View File

@ -16,9 +16,9 @@ module Vagrant
# doesn't support guest properties (due to cross platform issues) # doesn't support guest properties (due to cross platform issues)
version = env["vm"].vm.interface.get_guest_property_value("/VirtualBox/GuestAdd/Version") version = env["vm"].vm.interface.get_guest_property_value("/VirtualBox/GuestAdd/Version")
if version.empty? if version.empty?
env.logger.error Translator.t(:vm_additions_not_detected) env.ui.warn Translator.t(:vm_additions_not_detected)
elsif version != VirtualBox.version elsif version != VirtualBox.version
env.logger.error Translator.t(:vm_additions_version_mismatch, env.ui.warn Translator.t(:vm_additions_version_mismatch,
:guest_additions_version => version, :guest_additions_version => version,
:virtualbox_version => VirtualBox.version) :virtualbox_version => VirtualBox.version)
end end

View File

@ -19,7 +19,7 @@ module Vagrant
def clear def clear
if used_ports.length > 0 if used_ports.length > 0
@env.logger.info "Deleting any previously set forwarded ports..." @env.ui.info "Deleting any previously set forwarded ports..."
clear_ports clear_ports
@env["vm"].reload! @env["vm"].reload!
end end

View File

@ -16,7 +16,7 @@ module Vagrant
def clear_shared_folders def clear_shared_folders
if @env["vm"].vm.shared_folders.length > 0 if @env["vm"].vm.shared_folders.length > 0
@env.logger.info "Clearing previously set shared folders..." @env.ui.info "Clearing previously set shared folders..."
folders = @env["vm"].vm.shared_folders.dup folders = @env["vm"].vm.shared_folders.dup
folders.each do |shared_folder| folders.each do |shared_folder|

View File

@ -8,7 +8,7 @@ module Vagrant
def call(env) def call(env)
if !env.env.config.vm.proc_stack.empty? if !env.env.config.vm.proc_stack.empty?
env.logger.info "Running any VM customizations..." env.ui.info "Running any VM customizations..."
env.env.config.vm.run_procs!(env["vm"].vm) env.env.config.vm.run_procs!(env["vm"].vm)
env["vm"].vm.save env["vm"].vm.save
end end

View File

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
env.logger.info "Destroying VM and associated drives..." env.ui.info "Destroying VM and associated drives..."
env["vm"].vm.destroy(:destroy_medium => :delete) env["vm"].vm.destroy(:destroy_medium => :delete)
env["vm"].vm = nil env["vm"].vm = nil
env.env.update_dotfile env.env.update_dotfile

View File

@ -21,7 +21,7 @@ module Vagrant
# Destroy the network interface if there is only one # Destroy the network interface if there is only one
# attached VM (which must be this VM) # attached VM (which must be this VM)
if interface.attached_vms.length == 1 if interface.attached_vms.length == 1
env.logger.info "Destroying unused network interface..." env.ui.info "Destroying unused network interface..."
interface.destroy interface.destroy
end end
end end

View File

@ -16,7 +16,7 @@ module Vagrant
next if adapter.attachment_type != :host_only next if adapter.attachment_type != :host_only
if !logged if !logged
env.logger.info "Disabling host only networks..." env.ui.info "Disabling host only networks..."
logged = true logged = true
end end

View File

@ -10,7 +10,7 @@ module Vagrant
def call(env) def call(env)
if env["vm"].vm.saved? if env["vm"].vm.saved?
env.logger.info "Discarding saved state of VM..." env.ui.info "Discarding saved state of VM..."
env["vm"].vm.discard_state env["vm"].vm.discard_state
end end

View File

@ -31,13 +31,13 @@ module Vagrant
end end
def setup_temp_dir def setup_temp_dir
@env.logger.info "Creating temporary directory for export..." @env.ui.info "Creating temporary directory for export..."
@temp_dir = @env["export.temp_dir"] = File.join(@env.env.tmp_path, Time.now.to_i.to_s) @temp_dir = @env["export.temp_dir"] = File.join(@env.env.tmp_path, Time.now.to_i.to_s)
FileUtils.mkpath(@env["export.temp_dir"]) FileUtils.mkpath(@env["export.temp_dir"])
end end
def export def export
@env.logger.info "Exporting VM to #{ovf_path}..." @env.ui.info "Exporting VM to #{ovf_path}..."
@env["vm"].vm.export(ovf_path) do |progress| @env["vm"].vm.export(ovf_path) do |progress|
@env.logger.report_progress(progress.percent, 100, false) @env.logger.report_progress(progress.percent, 100, false)
end end

View File

@ -67,7 +67,7 @@ module Vagrant
existing_ports << options[:hostport] existing_ports << options[:hostport]
# Notify the user # Notify the user
@env.logger.info "Fixed port collision: #{name} now on port #{options[:hostport]}" @env.ui.info "Fixed port collision: #{name} now on port #{options[:hostport]}"
end end
#-------------------------------------------------------------- #--------------------------------------------------------------
@ -82,7 +82,7 @@ module Vagrant
end end
def forward_ports def forward_ports
@env.logger.info "Forwarding ports..." @env.ui.info "Forwarding ports..."
@env.env.config.vm.forwarded_ports.each do |name, options| @env.env.config.vm.forwarded_ports.each do |name, options|
adapter = options[:adapter] adapter = options[:adapter]
@ -91,11 +91,11 @@ module Vagrant
# Host-only or Bridged networking don't require port-forwarding and establishing forwarded ports on these # Host-only or Bridged networking don't require port-forwarding and establishing forwarded ports on these
# attachment types has uncertain behaviour. # attachment types has uncertain behaviour.
if @env["vm"].vm.network_adapters[adapter].attachment_type == :nat if @env["vm"].vm.network_adapters[adapter].attachment_type == :nat
@env.logger.info "Forwarding \"#{name}\": #{options[:guestport]} on adapter \##{adapter+1} => #{options[:hostport]}" @env.ui.info "Forwarding \"#{name}\": #{options[:guestport]} on adapter \##{adapter+1} => #{options[:hostport]}"
forward_port(name, options) forward_port(name, options)
else else
@env.logger.info "VirtualBox adapter \##{adapter+1} not configured as \"NAT\"." @env.ui.info "VirtualBox adapter \##{adapter+1} not configured as \"NAT\"."
@env.logger.info "Skipped port forwarding \"#{name}\": #{options[:guestport]} on adapter\##{adapter+1} => #{options[:hostport]}" @env.ui.info "Skipped port forwarding \"#{name}\": #{options[:guestport]} on adapter\##{adapter+1} => #{options[:hostport]}"
end end
end end

View File

@ -17,7 +17,7 @@ module Vagrant
end end
if env["vm"].vm.state(true) != :powered_off if env["vm"].vm.state(true) != :powered_off
env.logger.info "Forcing shutdown of VM..." env.ui.info "Forcing shutdown of VM..."
env["vm"].vm.stop env["vm"].vm.stop
end end

View File

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
env.logger.info "Importing base VM (#{env.env.box.ovf_file})" env.ui.info "Importing base VM (#{env.env.box.ovf_file})"
begin begin
# Import the virtual machine # Import the virtual machine

View File

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
env.logger.info "Matching MAC addresses..." env.ui.info "Matching MAC addresses..."
env["vm"].vm.network_adapters.first.mac_address = env.env.config.vm.base_mac env["vm"].vm.network_adapters.first.mac_address = env.env.config.vm.base_mac
env["vm"].vm.save env["vm"].vm.save

View File

@ -26,7 +26,7 @@ module Vagrant
if !env.error? && enable_network? if !env.error? && enable_network?
catch_action_exception(env) do catch_action_exception(env) do
@env.logger.info "Enabling host only network..." @env.ui.info "Enabling host only network..."
@env["vm"].system.prepare_host_only_network @env["vm"].system.prepare_host_only_network
@env.env.config.vm.network_options.compact.each do |network_options| @env.env.config.vm.network_options.compact.each do |network_options|
@env["vm"].system.enable_host_only_network(network_options) @env["vm"].system.enable_host_only_network(network_options)
@ -61,7 +61,7 @@ module Vagrant
# Enables and assigns the host only network to the proper # Enables and assigns the host only network to the proper
# adapter on the VM, and saves the adapter. # adapter on the VM, and saves the adapter.
def assign_network def assign_network
@env.logger.info "Preparing host only network..." @env.ui.info "Preparing host only network..."
@env.env.config.vm.network_options.compact.each do |network_options| @env.env.config.vm.network_options.compact.each do |network_options|
adapter = @env["vm"].vm.network_adapters[network_options[:adapter]] adapter = @env["vm"].vm.network_adapters[network_options[:adapter]]
@ -93,7 +93,7 @@ module Vagrant
return @env.error!(:network_not_found, :name => net_options[:name]) if net_options[:name] return @env.error!(:network_not_found, :name => net_options[:name]) if net_options[:name]
# One doesn't exist, create it. # One doesn't exist, create it.
@env.logger.info "Creating new host only network for environment..." @env.ui.info "Creating new host only network for environment..."
ni = interfaces.create ni = interfaces.create
ni.enable_static(network_ip(net_options[:ip], net_options[:netmask]), ni.enable_static(network_ip(net_options[:ip], net_options[:netmask]),

View File

@ -104,7 +104,7 @@ module Vagrant
# involves adding a line to `/etc/exports` for this VM, but it is # involves adding a line to `/etc/exports` for this VM, but it is
# up to the host class to define the specific behavior. # up to the host class to define the specific behavior.
def export_folders def export_folders
@env.logger.info "Exporting NFS shared folders..." @env.ui.info "Exporting NFS shared folders..."
catch_action_exception(@env) do catch_action_exception(@env) do
@env["host"].nfs_export(guest_ip, folders) @env["host"].nfs_export(guest_ip, folders)
@ -113,7 +113,7 @@ module Vagrant
# Uses the system class to mount the NFS folders. # Uses the system class to mount the NFS folders.
def mount_folders def mount_folders
@env.logger.info "Mounting NFS shared folders..." @env.ui.info "Mounting NFS shared folders..."
catch_action_exception(@env) do catch_action_exception(@env) do
@env["vm"].system.mount_nfs(host_ip, folders) @env["vm"].system.mount_nfs(host_ip, folders)

View File

@ -11,7 +11,7 @@ module Vagrant
end end
def call(env) def call(env)
env.logger.info "Persisting the VM UUID (#{env["vm"].uuid})" env.ui.info "Persisting the VM UUID (#{env["vm"].uuid})"
env.env.update_dotfile env.env.update_dotfile
@app.call(env) @app.call(env)

View File

@ -13,7 +13,7 @@ module Vagrant
@app.call(env) @app.call(env)
if !env.error? && provisioning_enabled? if !env.error? && provisioning_enabled?
@env.logger.info "Beginning provisioning process..." @env.ui.info "Beginning provisioning process..."
@provisioner.provision! @provisioner.provision!
end end
end end
@ -40,7 +40,7 @@ module Vagrant
@provisioner = provisioner_klass.new(@env) @provisioner = provisioner_klass.new(@env)
end end
@env.logger.info "Provisioning enabled with #{@provisioner.class}" @env.ui.info "Provisioning enabled with #{@provisioner.class}"
@provisioner.prepare @provisioner.prepare
@provisioner @provisioner
end end

View File

@ -8,7 +8,7 @@ module Vagrant
def call(env) def call(env)
if env["vm"].vm.saved? if env["vm"].vm.saved?
env.logger.info "Resuming suspended VM..." env.ui.info "Resuming suspended VM..."
env["actions"].run(Boot) env["actions"].run(Boot)
end end

View File

@ -60,7 +60,7 @@ module Vagrant
end end
def create_metadata def create_metadata
@env.logger.info "Creating shared folders metadata..." @env.ui.info "Creating shared folders metadata..."
shared_folders.each do |name, data| shared_folders.each do |name, data|
folder = VirtualBox::SharedFolder.new folder = VirtualBox::SharedFolder.new
@ -73,11 +73,11 @@ module Vagrant
end end
def mount_shared_folders def mount_shared_folders
@env.logger.info "Mounting shared folders..." @env.ui.info "Mounting shared folders..."
@env["vm"].ssh.execute do |ssh| @env["vm"].ssh.execute do |ssh|
shared_folders.each do |name, data| shared_folders.each do |name, data|
@env.logger.info "-- #{name}: #{data[:guestpath]}" @env.ui.info "-- #{name}: #{data[:guestpath]}"
@env["vm"].system.mount_shared_folder(ssh, name, data[:guestpath]) @env["vm"].system.mount_shared_folder(ssh, name, data[:guestpath])
end end
end end
@ -89,7 +89,7 @@ module Vagrant
@env["vm"].ssh.execute do |ssh| @env["vm"].ssh.execute do |ssh|
@env["vm"].system.prepare_unison(ssh) @env["vm"].system.prepare_unison(ssh)
@env.logger.info "Creating unison crontab entries..." @env.ui.info "Creating unison crontab entries..."
unison_folders.each do |name, data| unison_folders.each do |name, data|
@env["vm"].system.create_unison(ssh, data) @env["vm"].system.create_unison(ssh, data)
end end

View File

@ -8,7 +8,7 @@ module Vagrant
def call(env) def call(env)
if env["vm"].vm.running? if env["vm"].vm.running?
env.logger.info "Saving VM state and suspending execution..." env.ui.info "Saving VM state and suspending execution..."
env["vm"].vm.save_state env["vm"].vm.save_state
end end

View File

@ -9,9 +9,9 @@ module Vagrant
@env = env @env = env
end end
[:warn, :error, :info, :confirm].each do |method| [:warn, :error, :info, :confirm, :say_with_vm].each do |method|
# By default these methods don't do anything. A silent UI. # By default these methods don't do anything. A silent UI.
define_method(method) { |message| } define_method(method) { |*args| }
end end
# A shell UI, which uses a `Thor::Shell` object to talk with # A shell UI, which uses a `Thor::Shell` object to talk with
@ -23,20 +23,18 @@ module Vagrant
@shell = shell @shell = shell
end end
def warn(message) [[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color|
@shell.say(message, :yellow) define_method(method) do |message, prepend_vm_name=true|
message = format_message(message) if prepend_vm_name
@shell.say(message, color)
end
end end
def error(message) protected
@shell.say(message, :red)
end
def info(message) def format_message(message)
@shell.say(message) name = env.vm_name || "vagrant"
end "[#{name}] #{message}"
def confirm(message)
@shell.say(message, :green)
end end
end end
end end

View File

@ -15,6 +15,10 @@ class ActionEnvironmentTest < Test::Unit::TestCase
assert_equal @instance.env.logger, @instance.logger assert_equal @instance.env.logger, @instance.logger
end end
should "setup the UI" do
assert_equal @instance.env.ui, @instance.ui
end
should "not be erroneous initially" do should "not be erroneous initially" do
assert !@instance.error? assert !@instance.error?
end end