Class: Mccloud::Util::Ssh
- Inherits:
-
Object
- Object
- Mccloud::Util::Ssh
- Defined in:
- lib/mccloud/util/ssh.rb
Class Method Summary collapse
-
.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.
- .ssh(host, user, options = { :progress => "on" }, command = nil, exitcode = 0) ⇒ Object
- .transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
- .when_ssh_login_works(ip = "localhost", options = { }, &block) ⇒ Object
Class Method Details
.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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mccloud/util/ssh.rb', line 58 def self.execute_when_tcp_available(ip="localhost", = { } , &block) defaults={ :port => 22, :timeout => 20000 , :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,Errno::ETIMEDOUT sleep [:pollrate] end end end rescue Timeout::Error raise 'timeout connecting to port' end return false end |
.ssh(host, user, options = { :progress => "on" }, command = nil, exitcode = 0) ⇒ Object
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 160 161 162 163 164 165 166 167 |
# File 'lib/mccloud/util/ssh.rb', line 86 def self.ssh(host ,user , = { :progress => "on" } ,command=nil ,exitcode=0) defaults= { :port => "22", :user => "root", :paranoid => false } =defaults.merge() @pid="" @stdin=command @stdout="" @stderr="" @status=-99999 puts "Executing command: #{command}" 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 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.to_s ) if (exitcode=="*") #its a test so we don't need to worry else puts "Exitcode = #{exitcode} - #{@status.to_s}" raise "Exitcode was not what we expected" end end end |
.transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mccloud/util/ssh.rb', line 38 def self.transfer_file(host,filename,destination = '.' , = {}) unless File.exists?(filename) raise Mccloud::Error,"Can't transfer #{filename}: does not exist" end 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
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 |
# File 'lib/mccloud/util/ssh.rb', line 5 def self.when_ssh_login_works(ip="localhost", = { } , &block) defaults={ :port => '22', :timeout => 20000 , :user => 'vagrant', :password => 'vagrant'} =defaults.merge() puts puts "Waiting for ssh login on #{ip} with user #{[:user]} to sshd on port => #{[:port]} to work (Timeout in #{[:timeout]} seconds)" 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] ,:keys => [:keys] }) do |ssh| block.call(ip); puts "" return true end rescue Net::SSH::AuthenticationFailed,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 |