2023-08-10 16:53:25 -04:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
|
# SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2011-12-17 20:16:11 -05:00
|
|
|
require 'optparse'
|
|
|
|
|
|
2012-04-19 16:59:48 -04:00
|
|
|
module VagrantPlugins
|
|
|
|
|
module CommandSSH
|
2012-11-07 00:05:14 -05:00
|
|
|
class Command < Vagrant.plugin("2", :command)
|
2013-11-23 17:00:42 -05:00
|
|
|
def self.synopsis
|
|
|
|
|
"connects to machine via SSH"
|
|
|
|
|
end
|
|
|
|
|
|
2010-08-25 02:46:10 -04:00
|
|
|
def execute
|
2011-12-17 20:16:11 -05:00
|
|
|
options = {}
|
2017-06-02 13:28:50 -04:00
|
|
|
options[:tty] = true
|
2010-08-25 02:46:10 -04:00
|
|
|
|
2013-04-08 01:10:50 -04:00
|
|
|
opts = OptionParser.new do |o|
|
2016-05-29 01:06:51 -04:00
|
|
|
o.banner = "Usage: vagrant ssh [options] [name|id] [-- extra ssh args]"
|
2014-02-08 03:20:50 -05:00
|
|
|
o.separator ""
|
|
|
|
|
o.separator "Options:"
|
2013-04-08 01:10:50 -04:00
|
|
|
o.separator ""
|
2011-12-17 20:16:11 -05:00
|
|
|
|
2014-02-08 03:20:50 -05:00
|
|
|
o.on("-c", "--command COMMAND", "Execute an SSH command directly") do |c|
|
2011-12-17 20:16:11 -05:00
|
|
|
options[:command] = c
|
2010-08-25 02:46:10 -04:00
|
|
|
end
|
2013-04-08 01:10:50 -04:00
|
|
|
|
2014-02-08 03:20:50 -05:00
|
|
|
o.on("-p", "--plain", "Plain mode, leaves authentication up to user") do |p|
|
2012-01-05 00:28:30 -05:00
|
|
|
options[:plain_mode] = p
|
2011-05-18 21:04:54 -04:00
|
|
|
end
|
2017-06-02 13:28:50 -04:00
|
|
|
|
|
|
|
|
o.on("-t", "--[no-]tty", "Enables tty when executing an ssh command (defaults to true)") do |t|
|
|
|
|
|
options[:tty] = t
|
|
|
|
|
end
|
2010-08-25 02:46:10 -04:00
|
|
|
end
|
|
|
|
|
|
2012-01-12 02:49:42 -05:00
|
|
|
# Parse out the extra args to send to SSH, which is everything
|
|
|
|
|
# after the "--"
|
2013-04-08 01:07:55 -04:00
|
|
|
split_index = @argv.index("--")
|
|
|
|
|
if split_index
|
|
|
|
|
options[:ssh_args] = @argv.drop(split_index + 1)
|
|
|
|
|
@argv = @argv.take(split_index)
|
|
|
|
|
end
|
2012-01-12 02:49:42 -05:00
|
|
|
|
2013-04-08 01:07:55 -04:00
|
|
|
# Parse the options and return if we don't have any target.
|
|
|
|
|
argv = parse_options(opts)
|
|
|
|
|
return if !argv
|
2012-01-12 02:49:42 -05:00
|
|
|
|
2011-12-17 20:16:11 -05:00
|
|
|
# Execute the actual SSH
|
2014-05-22 12:35:12 -04:00
|
|
|
with_target_vms(argv, single_target: true) do |vm|
|
2013-12-14 00:07:54 -05:00
|
|
|
ssh_opts = {
|
2014-05-22 12:35:12 -04:00
|
|
|
plain_mode: options[:plain_mode],
|
|
|
|
|
extra_args: options[:ssh_args]
|
2013-12-14 00:07:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 20:16:11 -05:00
|
|
|
if options[:command]
|
2012-08-10 03:57:23 -04:00
|
|
|
@logger.debug("Executing single command on remote machine: #{options[:command]}")
|
2013-12-14 00:07:54 -05:00
|
|
|
env = vm.action(:ssh_run,
|
|
|
|
|
ssh_opts: ssh_opts,
|
2017-06-02 13:28:50 -04:00
|
|
|
ssh_run_command: options[:command],
|
|
|
|
|
tty: options[:tty],)
|
2012-08-11 23:35:34 -04:00
|
|
|
|
|
|
|
|
# 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-17 20:16:11 -05:00
|
|
|
else
|
2016-05-27 19:09:38 -04:00
|
|
|
Vagrant::Bundler.instance.deinit
|
2012-08-07 14:31:55 -04:00
|
|
|
@logger.debug("Invoking `ssh` action on machine")
|
2014-05-22 12:35:12 -04:00
|
|
|
vm.action(:ssh, ssh_opts: ssh_opts)
|
2012-08-11 23:35:34 -04:00
|
|
|
|
|
|
|
|
# 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-17 20:16:11 -05:00
|
|
|
end
|
2010-08-25 02:46:10 -04:00
|
|
|
end
|
2011-12-17 20:16:11 -05:00
|
|
|
end
|
2010-08-25 02:46:10 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|