From b4c5b854e2ba697d719a2c7bf6a1282b7b084d72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 10 Dec 2011 12:49:35 -0800 Subject: [PATCH] SSH working with new changes --- lib/vagrant/config/ssh.rb | 6 +---- lib/vagrant/ssh.rb | 48 +++++++++++++++++++------------------- lib/vagrant/ssh/session.rb | 11 ++++----- lib/vagrant/vm.rb | 2 +- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/vagrant/config/ssh.rb b/lib/vagrant/config/ssh.rb index 5af7caebd..e9a067d5e 100644 --- a/lib/vagrant/config/ssh.rb +++ b/lib/vagrant/config/ssh.rb @@ -9,7 +9,7 @@ module Vagrant attr_accessor :forwarded_port_destination attr_accessor :max_tries attr_accessor :timeout - attr_writer :private_key_path + attr_accessor :private_key_path attr_accessor :forward_agent attr_accessor :forward_x11 attr_accessor :shell @@ -22,10 +22,6 @@ module Vagrant @forward_x11 = false end - def private_key_path - File.expand_path(@private_key_path, env.root_path) - end - def validate(errors) [:username, :host, :forwarded_port_key, :max_tries, :timeout, :private_key_path].each do |field| errors.add(I18n.t("vagrant.config.common.error_empty", :field => field)) if !instance_variable_get("@#{field}".to_sym) diff --git a/lib/vagrant/ssh.rb b/lib/vagrant/ssh.rb index a08645688..bb4e31da6 100644 --- a/lib/vagrant/ssh.rb +++ b/lib/vagrant/ssh.rb @@ -14,12 +14,8 @@ module Vagrant include Util::Retryable include Util::SafeExec - # Reference back up to the environment which this SSH object belongs - # to - attr_accessor :env - - def initialize(environment) - @env = environment + def initialize(vm) + @vm = vm @logger = Log4r::Logger.new("vagrant::ssh") end @@ -28,7 +24,7 @@ module Vagrant # of options which override the configuration values. def connect(opts={}) if Util::Platform.windows? - raise Errors::SSHUnavailableWindows, :key_path => env.config.ssh.private_key_path, + raise Errors::SSHUnavailableWindows, :key_path => private_key_path, :ssh_port => port(opts) end @@ -37,7 +33,7 @@ module Vagrant options = {} options[:port] = port(opts) [:host, :username, :private_key_path].each do |param| - options[param] = opts[param] || env.config.ssh.send(param) + options[param] = opts[param] || @vm.config.ssh.send(param) end check_key_permissions(options[:private_key_path]) @@ -46,9 +42,9 @@ module Vagrant command_options = ["-p #{options[:port]}", "-o UserKnownHostsFile=/dev/null", "-o StrictHostKeyChecking=no", "-o IdentitiesOnly=yes", "-i #{options[:private_key_path]}", "-o LogLevel=ERROR"] - command_options << "-o ForwardAgent=yes" if env.config.ssh.forward_agent + command_options << "-o ForwardAgent=yes" if @vm.config.ssh.forward_agent - if env.config.ssh.forward_x11 + if @vm.config.ssh.forward_x11 # Both are required so that no warnings are shown regarding X11 command_options << "-o ForwardX11=yes" command_options << "-o ForwardX11Trusted=yes" @@ -63,29 +59,29 @@ module Vagrant # a Net::SSH object which can be used to execute remote commands. def execute(opts={}) # Check the key permissions to avoid SSH hangs - check_key_permissions(env.config.ssh.private_key_path) + check_key_permissions(private_key_path) # Merge in any additional options opts = opts.dup - opts[:forward_agent] = true if env.config.ssh.forward_agent + opts[:forward_agent] = true if @vm.config.ssh.forward_agent opts[:port] ||= port - @logger.info("Connecting to SSH: #{env.config.ssh.host} #{opts[:port]}") + @logger.info("Connecting to SSH: #{@vm.config.ssh.host} #{opts[:port]}") # The exceptions which are acceptable to retry on during # attempts to connect to SSH exceptions = [Errno::ECONNREFUSED, Net::SSH::Disconnect] # Connect to SSH and gather the session - session = retryable(:tries => env.config.ssh.max_tries, :on => exceptions) do - connection = Net::SSH.start(env.config.ssh.host, - env.config.ssh.username, - opts.merge( :keys => [env.config.ssh.private_key_path], + session = retryable(:tries => @vm.config.ssh.max_tries, :on => exceptions) do + connection = Net::SSH.start(@vm.config.ssh.host, + @vm.config.ssh.username, + opts.merge( :keys => [private_key_path], :keys_only => true, :user_known_hosts_file => [], :paranoid => false, :config => false)) - SSH::Session.new(connection, env) + SSH::Session.new(connection, @vm) end # Yield our session for executing @@ -116,8 +112,8 @@ module Vagrant ssh_port = port require 'timeout' - Timeout.timeout(env.config.ssh.timeout) do - execute(:timeout => env.config.ssh.timeout, :port => ssh_port) do |ssh| + Timeout.timeout(@vm.config.ssh.timeout) do + execute(:timeout => @vm.config.ssh.timeout, :port => ssh_port) do |ssh| # We run a basic command to test that the shell is up and # ready to receive commands. Only then is our SSH connection # truly "up" @@ -170,20 +166,20 @@ module Vagrant return opts[:port] if opts[:port] # Check if a port was specified in the config - return env.config.ssh.port if env.config.ssh.port + return @vm.config.ssh.port if @vm.config.ssh.port # Check if we have an SSH forwarded port pnum_by_name = nil pnum_by_destination = nil - env.vm.vm.network_adapters.each do |na| + @vm.vm.network_adapters.each do |na| # Look for the port number by name... pnum_by_name = na.nat_driver.forwarded_ports.detect do |fp| - fp.name == env.config.ssh.forwarded_port_key + fp.name == @vm.config.ssh.forwarded_port_key end # Look for the port number by destination... pnum_by_destination = na.nat_driver.forwarded_ports.detect do |fp| - fp.guestport == env.config.ssh.forwarded_port_destination + fp.guestport == @vm.config.ssh.forwarded_port_destination end # pnum_by_name is what we're looking for here, so break early @@ -197,5 +193,9 @@ module Vagrant # This should NEVER happen. raise Errors::SSHPortNotDetected end + + def private_key_path + File.expand_path(@vm.config.ssh.private_key_path, @vm.env.root_path) + end end end diff --git a/lib/vagrant/ssh/session.rb b/lib/vagrant/ssh/session.rb index d68862a1e..4fc88d1a8 100644 --- a/lib/vagrant/ssh/session.rb +++ b/lib/vagrant/ssh/session.rb @@ -7,11 +7,10 @@ module Vagrant include Util::Retryable attr_reader :session - attr_reader :env - def initialize(session, env) + def initialize(session, vm) @session = session - @env = env + @vm = vm end # Executes a given command and simply returns true/false if the @@ -34,7 +33,7 @@ module Vagrant # of `sudo`. def sudo!(commands, options=nil, &block) channel = session.open_channel do |ch| - ch.exec("sudo -H #{env.config.ssh.shell} -l") do |ch2, success| + ch.exec("sudo -H #{@vm.config.ssh.shell} -l") do |ch2, success| # Set the terminal ch2.send_data "export TERM=vt100\n" @@ -61,9 +60,9 @@ module Vagrant # the actual `exec!` implementation, except that this # implementation also reports `:exit_status` to the block if given. def exec!(commands, options=nil, &block) - retryable(:tries => env.config.ssh.max_tries, :on => [IOError, Net::SSH::Disconnect], :sleep => 1.0) do + retryable(:tries => @vm.config.ssh.max_tries, :on => [IOError, Net::SSH::Disconnect], :sleep => 1.0) do metach = session.open_channel do |ch| - ch.exec("#{env.config.ssh.shell} -l") do |ch2, success| + ch.exec("#{@vm.config.ssh.shell} -l") do |ch2, success| # Set the terminal ch2.send_data "export TERM=vt100\n" diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index ebc276142..667fa2a72 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -75,7 +75,7 @@ module Vagrant # On the initial call, this will initialize the object. On # subsequent calls it will reuse the existing object. def ssh - @ssh ||= SSH.new(env) + @ssh ||= SSH.new(self) end # Returns a boolean true if the VM has been created, otherwise