Class: Capissh::Transfer

Inherits:
Object
  • Object
show all
Includes:
Processable
Defined in:
lib/capissh/transfer.rb

Defined Under Namespace

Classes: SFTPTransferWrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processable

#ensure_each_session, #process_iteration

Constructor Details

#initialize(direction, from, to, sessions, options = {}, &block) ⇒ Transfer

Returns a new instance of Transfer.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capissh/transfer.rb', line 26

def initialize(direction, from, to, sessions, options={}, &block)
  @direction = direction
  @from      = from
  @to        = to
  @sessions  = sessions
  @options   = options
  @callback  = block

  @transport = options.fetch(:via, :sftp)
  @logger    = options.delete(:logger)

  @session_map = {}

  prepare_transfers
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



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

def callback
  @callback
end

#directionObject (readonly)

Returns the value of attribute direction.



19
20
21
# File 'lib/capissh/transfer.rb', line 19

def direction
  @direction
end

#fromObject (readonly)

Returns the value of attribute from.



20
21
22
# File 'lib/capissh/transfer.rb', line 20

def from
  @from
end

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/capissh/transfer.rb', line 23

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/capissh/transfer.rb', line 15

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



14
15
16
# File 'lib/capissh/transfer.rb', line 14

def sessions
  @sessions
end

#toObject (readonly)

Returns the value of attribute to.



21
22
23
# File 'lib/capissh/transfer.rb', line 21

def to
  @to
end

#transfersObject (readonly)

Returns the value of attribute transfers.



24
25
26
# File 'lib/capissh/transfer.rb', line 24

def transfers
  @transfers
end

#transportObject (readonly)

Returns the value of attribute transport.



18
19
20
# File 'lib/capissh/transfer.rb', line 18

def transport
  @transport
end

Class Method Details

.process(direction, from, to, sessions, options = {}, &block) ⇒ Object



10
11
12
# File 'lib/capissh/transfer.rb', line 10

def self.process(direction, from, to, sessions, options={}, &block)
  new(direction, from, to, sessions, options, &block).process!
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/capissh/transfer.rb', line 70

def active?
  transfers.any? { |transfer| transfer.active? }
end

#operationObject



74
75
76
# File 'lib/capissh/transfer.rb', line 74

def operation
  "#{direction}load"
end

#process!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/capissh/transfer.rb', line 42

def process!
  loop do
    begin
      break unless process_iteration { active? }
    rescue Exception => error
      if error.respond_to?(:session)
        handle_error(error)
      else
        raise
      end
    end
  end

  failed = transfers.select { |txfr| txfr[:failed] }
  if failed.any?
    hosts = failed.map { |txfr| txfr[:server] }
    errors = failed.map { |txfr| "#{txfr[:error]} (#{txfr[:error].message})" }.uniq.join(", ")
    error = TransferError.new("#{operation} via #{transport} failed on #{hosts.join(',')}: #{errors}")
    error.hosts = hosts

    logger.important(error.message) if logger
    raise error
  end

  logger.debug "#{transport} #{operation} complete" if logger
  self
end

#sanitized_fromObject



78
79
80
81
82
83
84
# File 'lib/capissh/transfer.rb', line 78

def sanitized_from
  if from.responds_to?(:read)
    "#<#{from.class}>"
  else
    from
  end
end

#sanitized_toObject



86
87
88
89
90
91
92
# File 'lib/capissh/transfer.rb', line 86

def sanitized_to
  if to.responds_to?(:read)
    "#<#{to.class}>"
  else
    to
  end
end