Easy operations `run` and `sudo` now echo stdout/stderr

This commit is contained in:
Mitchell Hashimoto 2012-06-01 16:33:41 +02:00
parent f133f39bcc
commit ca6d49bc94
2 changed files with 23 additions and 7 deletions

View File

@ -1,6 +1,8 @@
require "delegate" require "delegate"
require "optparse" require "optparse"
require "log4r"
require "vagrant/easy/operations" require "vagrant/easy/operations"
module Vagrant module Vagrant
@ -13,6 +15,7 @@ module Vagrant
def initialize(vm, argv) def initialize(vm, argv)
super(Operations.new(vm)) super(Operations.new(vm))
@logger = Log4r::Logger.new("vagrant::easy::command_api")
@argv = argv @argv = argv
@vm = vm @vm = vm
end end
@ -22,6 +25,8 @@ module Vagrant
# #
# @return [String] # @return [String]
def arg(*names) def arg(*names)
@logger.info("reading args: #{names.inspect}")
# Mangle the names a bit to add "=VALUE" to every flag. # Mangle the names a bit to add "=VALUE" to every flag.
names = names.map do |name| names = names.map do |name|
"#{name}=VALUE" "#{name}=VALUE"

View File

@ -58,18 +58,22 @@ module Vagrant
# puts "Output was #{output.stdout}" # puts "Output was #{output.stdout}"
# #
# @param [String] command Command to run # @param [String] command Command to run
def run(command) # @param [Hash] options Additional options
def run(command, options=nil)
@logger.info("run: #{command}") @logger.info("run: #{command}")
remote_command(:execute, command) options = { :echo => true }.merge(options || {})
remote_command(:execute, command, options)
end end
# Same as {run} except runs the command with superuser privileges # Same as {run} except runs the command with superuser privileges
# via `sudo`. # via `sudo`.
# #
# @param [String] command Command # @param [String] command Command
def sudo(command) # @param [Hash] options Additional options
def sudo(command, options=nil)
@logger.info("sudo: #{command}") @logger.info("sudo: #{command}")
remote_command(:sudo, command) options = { :echo => true }.merge(options || {})
remote_command(:sudo, command, options)
end end
# Uploads a file to the virtual machine. # Uploads a file to the virtual machine.
@ -105,7 +109,7 @@ module Vagrant
protected protected
# Runs a command on the remote host. # Runs a command on the remote host.
def remote_command(type, command) def remote_command(type, command, options)
# If the VM is not running, then we can't run SSH commands... # If the VM is not running, then we can't run SSH commands...
raise Errors::VMNotRunningError if @vm.state != :running raise Errors::VMNotRunningError if @vm.state != :running
@ -120,6 +124,13 @@ module Vagrant
elsif type == :stderr elsif type == :stderr
result.stderr += data result.stderr += data
end end
# If we're echoing data, then echo it!
if options[:echo]
@vm.ui.info(data.to_s,
:prefix => false,
:new_line => false)
end
end end
# Return the result # Return the result