Module: Veewee::Provider::Core::Helper::Ssh
- Included in:
- Box
- Defined in:
- lib/veewee/provider/core/helper/ssh.rb
Instance Method Summary collapse
- #ssh_execute(host, command, options = { :progress => "on"}) ⇒ Object
- #ssh_transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
- #when_ssh_login_works(ip = "127.0.0.1", options = { }, &block) ⇒ Object
Instance Method Details
#ssh_execute(host, command, options = { :progress => "on"}) ⇒ Object
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 160 161 162 163 164 |
# File 'lib/veewee/provider/core/helper/ssh.rb', line 82 def ssh_execute(host,command, = { :progress => "on"} ) defaults= { :port => "22", :exitcode => "0", :user => "root"} =defaults.merge() pid="" stdin=command stdout="" stderr="" status=-99999 unless [:mute] ui.info "Executing command: #{command}" end 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 ui.info(data, :new_line => false) unless [:mute] end # "on_extended_data" is called when the process writes something to stderr # NOTE: When requesting a pty (ch.request_pty), everything goes to stdout ch.on_extended_data do |c, type, data| stderr+=data ui.info(data, :new_line => false) unless [:mute] 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 ui.info "ERROR: exit code #{exit_code}" unless [:mute] else #ui.info "Successfully executed" end end channel.on_request("exit-signal") do |ch, data| ui.info "SIGNAL: #{data.read_long}" unless [:mute] end ch.on_close { #ui.info "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 Veewee::Provider::Core::Helper::SshResult.new(stdout,stderr,status), "Exitcode was not what we expected" end end return Veewee::Provider::Core::Helper::SshResult.new(stdout,stderr,status) end |
#ssh_transfer_file(host, filename, destination = '.', options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/veewee/provider/core/helper/ssh.rb', line 66 def ssh_transfer_file(host,filename,destination = '.' , = {}) defaults={ :paranoid => false } =defaults.merge() Net::SSH.start( host,[:user], ) do |ssh| ui.info "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}%" env.ui.info ".",{:new_line => false , :prefix => false} end end ui.info "", {:prefix => false} end |
#when_ssh_login_works(ip = "127.0.0.1", options = { }, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/veewee/provider/core/helper/ssh.rb', line 23 def when_ssh_login_works(ip="127.0.0.1", = { } , &block) defaults={ :port => '22', :timeout => 20000 } =defaults.merge() timeout = [:timeout] timeout=ENV['VEEWEE_TIMEOUT'].to_i unless ENV['VEEWEE_TIMEOUT'].nil? unless [:mute] ui.info "Waiting for ssh login on #{ip} with user #{options[:user]} to sshd on port => #{options[:port]} to work, timeout=#{timeout} sec" end run_hook(:before_ssh) begin Timeout::timeout(timeout) do connected=false while !connected do begin env.ui.info ".",{:new_line => false , :prefix => false} unless [:mute] Net::SSH.start(ip, [:user], { :port => [:port] , :password => [:password], :paranoid => false , :timeout => timeout }) do |ssh| ui.info "\n", {:prefix => false} unless [:mute] block.call(ip); return true end rescue Net::SSH::Disconnect, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNABORTED, Errno::ECONNRESET, Errno::ENETUNREACH, Errno::ETIMEDOUT sleep 5 end end end rescue IOError ui.info "Received a disconnect; moving on" sleep 5 rescue Timeout::Error raise Veewee::Error, "Ssh timeout #{timeout} sec has been reached." end ui.info "" return false end |