Module: Gusteau::SSH

Includes:
CompressedTarStream
Included in:
Server
Defined in:
lib/gusteau/ssh.rb

Instance Method Summary collapse

Instance Method Details

#connObject



9
10
11
12
13
14
15
16
17
# File 'lib/gusteau/ssh.rb', line 9

def conn
  @conn ||= begin
    opts = { :port => port }
    opts.update(:password => password) if @password
    log "#setting up ssh connection #{@user}@#{host}, #{opts.inspect})" do
      Net::SSH.start(host, @user, opts)
    end
  end
end

#send_command(cmd) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gusteau/ssh.rb', line 19

def send_command(cmd)
  exit_code = -1
  conn.open_channel do |ch|
    ch.exec(prepared_cmd cmd) do |_, success|
      if success
        ch.on_data { |_,data| puts data }
        ch.on_extended_data { |_,_,data| $stderr.puts data }
        ch.on_request("exit-status") { |_,data| exit_code = data.read_long }
      else
        raise "FAILED to execute command: #{cmd.inspect}"
      end
    end
  end
  conn.loop
  exit_code == 0
end

#send_files(files, dest_dir, strip_c = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/gusteau/ssh.rb', line 36

def send_files(files, dest_dir, strip_c = nil)
  strip_arg = strip_c ? "--strip-components=#{strip_c}" : ''

  conn.open_channel { |ch|
    ch.exec(prepared_cmd "tar zxf - -C #{dest_dir} #{strip_arg}")
    ch.send_data(compressed_tar_stream(files))
    ch.eof!
  }.wait
end