2011-12-18 01:16:11 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
2010-08-25 06:46:10 +00:00
|
|
|
module Vagrant
|
|
|
|
module Command
|
2011-12-18 01:16:11 +00:00
|
|
|
class SSH < Base
|
2010-08-25 06:46:10 +00:00
|
|
|
def execute
|
2011-12-18 01:16:11 +00:00
|
|
|
options = {}
|
2010-08-25 06:46:10 +00:00
|
|
|
|
2011-12-18 01:16:11 +00:00
|
|
|
opts = OptionParser.new do |opts|
|
2012-01-12 07:49:42 +00:00
|
|
|
opts.banner = "Usage: vagrant ssh [vm-name] [-c command] [-- extra ssh args]"
|
2010-08-25 06:46:10 +00:00
|
|
|
|
2011-12-18 01:16:11 +00:00
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-c", "--command COMMAND", "Execute an SSH command directly.") do |c|
|
|
|
|
options[:command] = c
|
2010-08-25 06:46:10 +00:00
|
|
|
end
|
2012-01-05 05:28:30 +00:00
|
|
|
opts.on("-p", "--plain", "Plain mode, leaves authentication up to user.") do |p|
|
|
|
|
options[:plain_mode] = p
|
2011-05-19 01:04:54 +00:00
|
|
|
end
|
2010-08-25 06:46:10 +00:00
|
|
|
end
|
|
|
|
|
2012-01-12 07:49:42 +00:00
|
|
|
# Parse the options and return if we don't have any target.
|
2011-12-18 01:16:11 +00:00
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
2010-08-25 06:46:10 +00:00
|
|
|
|
2012-01-12 07:49:42 +00:00
|
|
|
# Parse out the extra args to send to SSH, which is everything
|
|
|
|
# after the "--"
|
|
|
|
ssh_args = ARGV.drop_while { |i| i != "--" }
|
|
|
|
ssh_args = ssh_args[1..-1]
|
|
|
|
options[:ssh_args] = ssh_args
|
|
|
|
|
|
|
|
# If the remaining arguments ARE the SSH arguments, then just
|
|
|
|
# clear it out. This happens because optparse returns what is
|
|
|
|
# after the "--" as remaining ARGV, and Vagrant can think it is
|
|
|
|
# a multi-vm name (wrong!)
|
|
|
|
argv = [] if argv == ssh_args
|
|
|
|
|
2011-12-18 01:16:11 +00:00
|
|
|
# Execute the actual SSH
|
2012-02-14 18:42:30 +00:00
|
|
|
with_target_vms(argv[0], :single_target => true) do |vm|
|
2011-08-29 03:38:56 +00:00
|
|
|
# Basic checks that are required for proper SSH
|
|
|
|
raise Errors::VMNotCreatedError if !vm.created?
|
2011-12-22 21:48:22 +00:00
|
|
|
raise Errors::VMInaccessible if !vm.state == :inaccessible
|
|
|
|
raise Errors::VMNotRunningError if vm.state != :running
|
2011-08-29 03:38:56 +00:00
|
|
|
|
2011-12-18 01:16:11 +00:00
|
|
|
if options[:command]
|
|
|
|
ssh_execute(vm, options[:command])
|
|
|
|
else
|
2012-01-12 07:49:42 +00:00
|
|
|
opts = {
|
|
|
|
:plain_mode => options[:plain_mode],
|
|
|
|
:extra_args => options[:ssh_args]
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_connect(vm, opts)
|
2011-12-18 01:16:11 +00:00
|
|
|
end
|
2010-08-25 06:46:10 +00:00
|
|
|
end
|
2012-03-23 15:07:35 +00:00
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
0
|
|
|
|
end
|
2011-12-18 01:16:11 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def ssh_execute(vm, command=nil)
|
2011-12-26 17:58:10 +00:00
|
|
|
exit_status = 0
|
|
|
|
|
2011-12-18 01:16:11 +00:00
|
|
|
@logger.debug("Executing command: #{command}")
|
2012-01-09 07:40:05 +00:00
|
|
|
exit_status = vm.channel.execute(command, :error_check => false) do |type, data|
|
2012-01-07 04:03:56 +00:00
|
|
|
# Determine the proper channel to send the output onto depending
|
|
|
|
# on the type of data we are receiving.
|
|
|
|
channel = type == :stdout ? :out : :error
|
2011-12-26 18:03:12 +00:00
|
|
|
|
2012-01-07 04:03:56 +00:00
|
|
|
# Print the SSH output as it comes in, but don't prefix it and don't
|
|
|
|
# force a new line so that the output is properly preserved
|
|
|
|
vm.ui.info(data.to_s,
|
|
|
|
:prefix => false,
|
|
|
|
:new_line => false,
|
|
|
|
:channel => channel)
|
2011-12-18 01:16:11 +00:00
|
|
|
end
|
2011-12-26 17:58:10 +00:00
|
|
|
|
|
|
|
# Exit with the exit status we got from executing the command
|
|
|
|
exit exit_status
|
2011-12-18 01:16:11 +00:00
|
|
|
end
|
|
|
|
|
2011-05-19 01:04:54 +00:00
|
|
|
def ssh_connect(vm, opts)
|
2011-12-18 01:16:11 +00:00
|
|
|
@logger.debug("`exec` into ssh prompt")
|
2012-01-07 02:01:41 +00:00
|
|
|
vm.ssh.exec(opts)
|
2011-12-18 01:16:11 +00:00
|
|
|
end
|
2010-08-25 06:46:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|