Class: Veewee::Ssh
- Inherits:
-
Object
- Object
- Veewee::Ssh
- Defined in:
- lib/veewee/ssh.rb
Class Method Summary collapse
- .execute(host, command, options = { :progress => "on"}) ⇒ Object
-
.execute_when_tcp_available(ip = "localhost", options = { }, &block) ⇒ Object
we need to try the actual login because vbox gives us a connect after the machine boots.
- .transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
- .when_ssh_login_works(ip = "localhost", options = { }, &block) ⇒ Object
Class Method Details
.execute(host, command, options = { :progress => "on"}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/veewee/ssh.rb', line 93 def self.execute(host,command, = { :progress => "on"} ) defaults= { :port => "22", :exitcode => "0", :user => "root"} =defaults.merge() @pid="" @stdin=command @stdout="" @stderr="" @status=-99999 puts "Executing command: #{command}" @key_auth_tried = false = { :port => [:port], :password => [:password], :paranoid => false} # ssh_options[:verbose] => :debug begin Net::SSH.start(host, [:user], ) do |ssh| # open a new channel and configure a minimal set of callbacks, then run # the event loop until the channel finishes (closes) channel = ssh.open_channel do |ch| #request pty for sudo stuff and so ch.request_pty do |ch, success| raise "Error requesting pty" unless success end ch.exec "#{command}" do |ch, success| raise "could not execute command" unless success # "on_data" is called when the process writes something to stdout ch.on_data do |c, data| @stdout+=data print data end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| @stderr+=data print data end #exit code #http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/a806b0f5dae4e1e2 channel.on_request("exit-status") do |ch, data| exit_code = data.read_long @status=exit_code if exit_code > 0 puts "ERROR: exit code #{exit_code}" else #puts "Successfully executed" end end channel.on_request("exit-signal") do |ch, data| puts "SIGNAL: #{data.read_long}" end ch.on_close { #puts "done!" } #status=ch.exec "echo $?" end end channel.wait end rescue Net::SSH::AuthenticationFailed [:keys] = Array.new([File.join(File.dirname(__FILE__),'./../../validation/vagrant')]) .delete(:password) [:auth_methods] = ['publickey'] if @key_auth_tried raise else @key_auth_tried = true retry end rescue Net::SSH::Disconnect, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNABORTED, Errno::ECONNRESET, Errno::ENETUNREACH sleep 1 end if (@status.to_s != [:exitcode] ) if ([:exitcode]=="*") #its a test so we don't need to worry else raise "Exitcode was not what we expected" end end end |
.execute_when_tcp_available(ip = "localhost", options = { }, &block) ⇒ Object
we need to try the actual login because vbox gives us a connect after the machine boots
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/veewee/ssh.rb', line 65 def self.execute_when_tcp_available(ip="localhost", = { } , &block) defaults={ :port => 22, :timeout => 2 , :pollrate => 5} =defaults.merge() begin Timeout::timeout([:timeout]) do connected=false while !connected do begin puts "trying connection" s = TCPSocket.new(ip, [:port]) s.close block.call(ip); return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH sleep [:pollrate] end end end rescue Timeout::Error raise 'timeout connecting to port' end return false end |
.transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/veewee/ssh.rb', line 49 def self.transfer_file(host,filename,destination = '.' , = {}) Net::SSH.start( host,[:user], ) do |ssh| puts "Transferring #{filename} to #{destination} " ssh.scp.upload!( filename, destination ) do |ch, name, sent, total| # print "\r#{destination}: #{(sent.to_f * 100 / total.to_f).to_i}%" print "." end end puts end |
.when_ssh_login_works(ip = "localhost", options = { }, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/veewee/ssh.rb', line 4 def self.when_ssh_login_works(ip="localhost", = { } , &block) defaults={ :port => '22', :timeout => 200 , :user => 'vagrant', :password => 'vagrant'} =defaults.merge() puts puts "Waiting for ssh login with user #{[:user]} to sshd on port => #{[:port]} to work" begin Timeout::timeout([:timeout]) do connected=false @key_auth_tried = false = { :port => [:port] , :password => [:password], :paranoid => false, :timeout => [:timeout] } while !connected do begin print "." Net::SSH.start(ip, [:user], ) do |ssh| block.call(ip); puts "" return true end rescue Net::SSH::AuthenticationFailed [:keys] = File.join(File.dirname(__FILE__),'./../../validation/vagrant') .delete(:password) = if @key_auth_tried raise else @key_auth_tried = true retry end rescue Net::SSH::Disconnect,Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNABORTED, Errno::ECONNRESET, Errno::ENETUNREACH sleep 5 end end end rescue Timeout::Error raise 'ssh timeout' end puts "" return false end |