Class: Capissh::FileTransfers

Inherits:
Object
  • Object
show all
Defined in:
lib/capissh/file_transfers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, connection_manager, options = {}) ⇒ FileTransfers

Returns a new instance of FileTransfers.



7
8
9
10
11
# File 'lib/capissh/file_transfers.rb', line 7

def initialize(configuration, connection_manager, options={})
  @configuration = configuration
  @connection_manager = connection_manager
  @logger  = options[:logger]
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/capissh/file_transfers.rb', line 5

def configuration
  @configuration
end

#connection_managerObject (readonly)

Returns the value of attribute connection_manager.



5
6
7
# File 'lib/capissh/file_transfers.rb', line 5

def connection_manager
  @connection_manager
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/capissh/file_transfers.rb', line 5

def logger
  @logger
end

Instance Method Details

#download(servers, from, to, options = {}, &block) ⇒ Object



40
41
42
# File 'lib/capissh/file_transfers.rb', line 40

def download(servers, from, to, options={}, &block)
  transfer(servers, :down, from, to, options, &block)
end

#get(servers, remote_path, path, options = {}, &block) ⇒ Object

Get file remote_path from FIRST server targeted by the current task and transfer it to local machine as path.

Pass only one server, or the first of the set of servers will be used.

get server, “#deploy_to/current/log/production.log”, “log/production.log.web”



26
27
28
# File 'lib/capissh/file_transfers.rb', line 26

def get(servers, remote_path, path, options={}, &block)
  download(Array(servers).slice(0,1), remote_path, path, options, &block)
end

#put(servers, data, path, options = {}) ⇒ Object

Store the given data at the given location on all servers targetted by the current task. If :mode is specified it is used to set the mode on the file.



16
17
18
# File 'lib/capissh/file_transfers.rb', line 16

def put(servers, data, path, options={})
  upload(servers, StringIO.new(data), path, options)
end

#transfer(servers, direction, from, to, options = {}, &block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/capissh/file_transfers.rb', line 44

def transfer(servers, direction, from, to, options={}, &block)
  if configuration.dry_run
    return logger.debug "transfering: #{[direction, from, to] * ', '}"
  end
  connection_manager.execute_on_servers(servers, options) do |sessions|
    Transfer.process(direction, from, to, sessions, options.merge(:logger => logger), &block)
  end
end

#upload(servers, from, to, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/capissh/file_transfers.rb', line 30

def upload(servers, from, to, options={}, &block)
  opts = options.dup
  mode = opts.delete(:mode)
  transfer(servers, :up, from, to, opts, &block)
  if mode
    mode = mode.is_a?(Numeric) ? mode.to_s(8) : mode.to_s
    configuration.run servers, "chmod #{mode} #{to}", opts
  end
end