vagrant/plugins/commands/ssh/command.rb

72 lines
2.2 KiB
Ruby
Raw Normal View History

2011-12-18 01:16:11 +00:00
require 'optparse'
2012-04-19 20:59:48 +00:00
module VagrantPlugins
module CommandSSH
2012-11-07 05:05:14 +00:00
class Command < Vagrant.plugin("2", :command)
def self.synopsis
"connects to machine via SSH"
end
def execute
2011-12-18 01:16:11 +00:00
options = {}
opts = OptionParser.new do |o|
2014-02-08 08:20:50 +00:00
o.banner = "Usage: vagrant ssh [options] [name] [-- extra ssh args]"
o.separator ""
o.separator "Options:"
o.separator ""
2011-12-18 01:16:11 +00:00
2014-02-08 08:20:50 +00:00
o.on("-c", "--command COMMAND", "Execute an SSH command directly") do |c|
2011-12-18 01:16:11 +00:00
options[:command] = c
end
2014-02-08 08:20:50 +00:00
o.on("-p", "--plain", "Plain mode, leaves authentication up to user") do |p|
2012-01-05 05:28:30 +00:00
options[:plain_mode] = p
end
end
# Parse out the extra args to send to SSH, which is everything
# after the "--"
split_index = @argv.index("--")
if split_index
options[:ssh_args] = @argv.drop(split_index + 1)
@argv = @argv.take(split_index)
end
# Parse the options and return if we don't have any target.
argv = parse_options(opts)
return if !argv
2011-12-18 01:16:11 +00:00
# Execute the actual SSH
with_target_vms(argv, single_target: true) do |vm|
ssh_opts = {
plain_mode: options[:plain_mode],
extra_args: options[:ssh_args]
}
2011-12-18 01:16:11 +00:00
if options[:command]
@logger.debug("Executing single command on remote machine: #{options[:command]}")
env = vm.action(:ssh_run,
ssh_opts: ssh_opts,
ssh_run_command: options[:command],)
# Exit with the exit status of the command or a 0 if we didn't
# get one.
exit_status = env[:ssh_run_exit_status] || 0
return exit_status
2011-12-18 01:16:11 +00:00
else
2016-05-27 23:09:38 +00:00
Vagrant::Bundler.instance.deinit
2012-08-07 18:31:55 +00:00
@logger.debug("Invoking `ssh` action on machine")
vm.action(:ssh, ssh_opts: ssh_opts)
# We should never reach this point, since the point of `ssh`
# is to exec into the proper SSH shell, but we'll just return
# an exit status of 0 anyways.
return 0
2011-12-18 01:16:11 +00:00
end
end
2011-12-18 01:16:11 +00:00
end
end
end
end