Class: Buildr::Transports::SFTP

Inherits:
Transport show all
Defined in:
lib/core/transports.rb

Instance Attribute Summary collapse

Attributes inherited from Transport

#base_path, #options, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Transport

#download, perform

Constructor Details

#initialize(url, options) ⇒ SFTP

Returns a new instance of SFTP.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/core/transports.rb', line 311

def initialize(url, options)
  super
  # SSH options are based on the username/password from the URI.
  ssh_options = { :port=>@uri.port, :username=>@uri.user }.merge(options || {})
  ssh_options[:password] ||= SFTP.passwords[@uri.host]
  begin
    puts "Connecting to #{@uri.host}" if Rake.application.options.trace
    session = Net::SSH.start(@uri.host, ssh_options)
    SFTP.passwords[@uri.host] = ssh_options[:password]
  rescue Net::SSH::AuthenticationFailed=>ex
    # Only if running with console, prompt for password.
    if !ssh_options[:password] && $stdout.isatty
      password = HighLine.new.ask("Password for #{@uri.host}:") { |q| q.echo = "*" }
      ssh_options[:password] = password
      retry
    end
    raise
  end
  @sftp = session.sftp.connect
  puts "connected" if Rake.application.options.trace
end

Instance Attribute Details

#sftpObject (readonly)

Returns the value of attribute sftp.



309
310
311
# File 'lib/core/transports.rb', line 309

def sftp
  @sftp
end

Class Method Details

.passwordsObject



304
305
306
# File 'lib/core/transports.rb', line 304

def passwords()
  @passwords ||= {}
end

Instance Method Details

#closeObject



375
376
377
378
# File 'lib/core/transports.rb', line 375

def close()
  @sftp.close
  @sftp = nil
end

#mkpath(path) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'lib/core/transports.rb', line 364

def mkpath(path)
  # To create a path, we need to create all its parent.
  # We use realpath to determine if the path already exists,
  # otherwise mkdir fails.
  puts "Creating path #{@base_path}" if Rake.application.options.trace
  path.split("/").inject(@base_path) do |base, part|
    @sftp.realpath(base+part) rescue @sftp.mkdir base + part
    "#{base}#{part}/"
  end
end

#upload(source, path) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/core/transports.rb', line 333

def upload(source, path)
  File.open(source) do |file|
    with_progress_bar path.split("/").last, File.size(source) do |progress|
      with_digests(@options[:digests]) do |digester|
        puts "Uploading to #{@base_path}#{path}" if Rake.application.options.trace
        @sftp.open_handle(@base_path + path, "w") do |handle|
          # Writing in chunks gives us the benefit of a progress bar,
          # but also require that we maintain a position in the file,
          # since write() with two arguments always writes at position 0.
          pos = 0
          while chunk = file.read(32 * 4096) 
            @sftp.write(handle, chunk, pos)
            pos += chunk.size
            digester << chunk
            progress << chunk
          end
        end

        # Upload all the digests.
        digester.each do |type, hexdigest|
          puts "Uploading signature to #{@base_path}#{path}.#{type}" if Rake.application.options.trace
          @sftp.open_handle("#{@base_path}#{path}.#{type}", "w") do |handle|
            @sftp.write(handle, "#{hexdigest}  #{path}")
          end
        end
      end

    end 
  end
end