Class: Vagrant::Communication::SSH
Overview
Provides communication with the VM via SSH.
Instance Method Summary
collapse
#retryable
#remove_ansi_escape_codes
Methods inherited from Base
#test
Constructor Details
#initialize(vm) ⇒ SSH
Returns a new instance of SSH.
19
20
21
22
23
|
# File 'lib/vagrant/communication/ssh.rb', line 19
def initialize(vm)
@vm = vm
@logger = Log4r::Logger.new("vagrant::communication::ssh")
@connection = nil
end
|
Instance Method Details
#execute(command, opts = nil, &block) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/vagrant/communication/ssh.rb', line 42
def execute(command, opts=nil, &block)
opts = {
:error_check => true,
:error_class => Errors::VagrantError,
:error_key => :ssh_bad_exit_status,
:command => command,
:sudo => false
}.merge(opts || {})
exit_status = connect do |connection|
shell_execute(connection, command, opts[:sudo], &block)
end
if opts[:error_check] && exit_status != 0
error_opts = opts.merge(:_key => opts[:error_key])
raise opts[:error_class], error_opts
end
exit_status
end
|
#ready? ⇒ Boolean
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/vagrant/communication/ssh.rb', line 25
def ready?
@logger.debug("Checking whether SSH is ready...")
Timeout.timeout(@vm.config.ssh.timeout) do
connect
end
@logger.info("SSH is ready!")
true
rescue Timeout::Error, Errors::SSHConnectionRefused, Net::SSH::Disconnect => e
@logger.info("SSH not up: #{e.inspect}")
return false
end
|
#sudo(command, opts = nil, &block) ⇒ Object
69
70
71
72
73
|
# File 'lib/vagrant/communication/ssh.rb', line 69
def sudo(command, opts=nil, &block)
opts = { :sudo => true }.merge(opts || {})
execute(command, opts, &block)
end
|
#upload(from, to) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/vagrant/communication/ssh.rb', line 75
def upload(from, to)
@logger.debug("Uploading: #{from} to #{to}")
connect do |connection|
scp = Net::SCP.new(connection)
scp.upload!(File.open(from, "r"), to)
end
rescue Net::SCP::Error => e
raise Errors::SCPUnavailable if e.message =~ /\(127\)/
raise
end
|