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
80 81 82 83 84 85 86 87 88 89 90 91 92 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 |
# File 'lib/veewee/ssh.rb', line 80 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}" Net::SSH.start(host, [:user], { :port => [:port], :password => [:password], :paranoid => false }) 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 puts data end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| @stderr+=data puts 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 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/veewee/ssh.rb', line 52 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
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/veewee/ssh.rb', line 37 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 |
# 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 while !connected do begin print "." Net::SSH.start(ip, [:user], { :port => [:port] , :password => [:password], :paranoid => false, :timeout => [:timeout] }) do |ssh| block.call(ip); puts "" return true 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 |