2011-12-18 01:16:11 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
2012-04-19 20:59:48 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommandSSH
|
2012-06-26 23:18:02 +00:00
|
|
|
class Command < Vagrant.plugin("1", :command)
|
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-03-11 22:32:17 +00:00
|
|
|
with_target_vms(argv, :single_target => true) do |vm|
|
2011-12-18 01:16:11 +00:00
|
|
|
if options[:command]
|
2012-08-10 07:57:23 +00:00
|
|
|
# XXX: Exit with proper exit status
|
|
|
|
@logger.debug("Executing single command on remote machine: #{options[:command]}")
|
|
|
|
vm.action(:ssh_run, :ssh_run_command => options[:command])
|
2011-12-18 01:16:11 +00:00
|
|
|
else
|
2012-01-12 07:49:42 +00:00
|
|
|
opts = {
|
|
|
|
:plain_mode => options[:plain_mode],
|
|
|
|
:extra_args => options[:ssh_args]
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:31:55 +00:00
|
|
|
@logger.debug("Invoking `ssh` action on machine")
|
2012-08-05 20:45:24 +00:00
|
|
|
vm.action(:ssh, :ssh_opts => 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
|
2011-12-18 01:16:11 +00:00
|
|
|
end
|
2010-08-25 06:46:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|