`vagrant ssh`

This commit is contained in:
Mitchell Hashimoto 2011-12-17 17:16:11 -08:00
parent 19adc3189d
commit e34f0a8af7
3 changed files with 43 additions and 29 deletions

View File

@ -100,6 +100,7 @@ Vagrant.commands.register(:package) { Vagrant::Command::Package }
Vagrant.commands.register(:provision) { Vagrant::Command::Provision }
Vagrant.commands.register(:reload) { Vagrant::Command::Reload }
Vagrant.commands.register(:resume) { Vagrant::Command::Resume }
Vagrant.commands.register(:ssh) { Vagrant::Command::SSH }
Vagrant.commands.register(:suspend) { Vagrant::Command::Suspend }
Vagrant.commands.register(:up) { Vagrant::Command::Up }

View File

@ -8,6 +8,7 @@ module Vagrant
autoload :Provision, 'vagrant/command/provision'
autoload :Reload, 'vagrant/command/reload'
autoload :Resume, 'vagrant/command/resume'
autoload :SSH, 'vagrant/command/ssh'
autoload :Suspend, 'vagrant/command/suspend'
autoload :Up, 'vagrant/command/up'
end

View File

@ -1,49 +1,61 @@
require 'optparse'
module Vagrant
module Command
class SSHCommand < NamedBase
class_option :command, :type => :string, :default => false, :aliases => "-c"
register "ssh", "SSH into the currently running Vagrant environment."
class SSH < Base
def execute
if options[:command]
ssh_execute
else
ssh_connect
end
end
options = {}
protected
opts = OptionParser.new do |opts|
opts.banner = "Usage: vagrant ssh [vm-name] [-c command]"
def ssh_execute
ssh_vm.ssh.execute do |ssh|
ssh.exec!(options[:command]) do |channel, type, data|
if type != :exit_status
# 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
ssh_vm.env.ui.info(data.to_s, :prefix => false, :new_line => false)
end
opts.separator ""
opts.on("-c", "--command COMMAND", "Execute an SSH command directly.") do |c|
options[:command] = c
end
end
end
def ssh_connect
ssh_vm.ssh.connect
end
argv = parse_options(opts)
return if !argv
def ssh_vm
@ssh_vm ||= begin
vm = self.name.nil? && env.multivm? ? env.primary_vm : nil
raise Errors::MultiVMTargetRequired, :command => "ssh" if !vm && target_vms.length > 1
vm = target_vms.first if !vm
# SSH always requires a target VM
raise Errors::MultiVMTargetRequired, :command => "ssh" if @env.multivm? && !argv[0]
# Execute the actual SSH
with_target_vms(argv[0]) do |vm|
# Basic checks that are required for proper SSH
raise Errors::VMNotCreatedError if !vm.created?
raise Errors::VMInaccessible if !vm.vm.accessible?
raise Errors::VMNotRunningError if !vm.vm.running?
vm
if options[:command]
ssh_execute(vm, options[:command])
else
ssh_connect(vm)
end
end
end
protected
def ssh_execute(vm, command=nil)
@logger.debug("Executing command: #{command}")
vm.ssh.execute do |ssh|
ssh.exec!(command) do |channel, type, data|
if type != :exit_status
# 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)
end
end
end
end
def ssh_connect(vm)
@logger.debug("`exec` into ssh prompt")
vm.ssh.connect
end
end
end
end