Class: Sffftp::Sftp

Inherits:
Object
  • Object
show all
Defined in:
lib/sffftp/sftp.rb

Constant Summary collapse

ATTRS =
[
  :remote_host,
  :remote_user_name,
  :remote_user_password,
  :remote_path,
  :remote_port,
  :queue_folder,
  :ok_folder,
  :ng_folder,
  :timeout,
  :logger2ssh,
  :logger
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sftp

Returns a new instance of Sftp.



25
26
27
28
29
30
31
# File 'lib/sffftp/sftp.rb', line 25

def initialize(opts={})
  self.remote_port = 22
  self.logger      = Logger.new(STDOUT)
  opts.each do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



23
24
25
# File 'lib/sffftp/sftp.rb', line 23

def connection
  @connection
end

Instance Method Details

#attrs_ok?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sffftp/sftp.rb', line 33

def attrs_ok?
  ATTRS.each do |attr|
    #v = instance_variable_get("@#{attr.to_s}")
    v = send(attr)
    case attr
    when :remote_user_name,:remote_user_password,:timeout,:logger2ssh
      next
    when :queue_folder,:ok_folder,:ng_folder
      unless File.directory?(v)
        raise "#{attr}:#{v} is not folder"
      end
    else
      unless v
        raise %|must set "#{attr}" at least|
      end
    end
  end
  true
end

#closeObject



81
82
83
84
85
86
87
88
# File 'lib/sffftp/sftp.rb', line 81

def close
  begin
    if connection && connection.open?
      connection.close
    end
  rescue
  end
end

#connect(close_connected_instance = true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sffftp/sftp.rb', line 57

def connect(close_connected_instance=true)
  if close_connected_instance
    close if connection && connection.open?
  end
  if connection && connection.open?
    unless close_connected_instance
      return self.connection
    end
  end

  opt = {}
  opt[:port] = remote_port
  if remote_user_password
    opt[:password] = remote_user_password
  end
  if timeout
    opt[:timeout] = timeout
  end
  if logger2ssh
    opt[:logger] = logger
  end
  self.connection = Net::SFTP.start(remote_host, remote_user_name, opt)
end

#proceedObject



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
# File 'lib/sffftp/sftp.rb', line 90

def proceed
  attrs_ok?

  files = Dir::entries(queue_folder).map{|o|"#{queue_folder}/#{o}"}
  files = files.select do |o|
    File::ftype(o) == "file" &&
      !(o =~ /\.(tmp|ok)$/)
  end

  unless @__not_first_time_to_proceed
    logger.info("queue_folder: #{queue_folder}")
    logger.info("target: #{remote_user_name}@#{remote_host}:#{remote_port}:#{remote_path}")
    @__not_first_time_to_proceed = true
  end
  uploaded_count = 0

  begin
    connect
    files.each do |file|
      break if @killing
      if upload!(file)
        uploaded_count += 1
      end
    end
  # ハンドルできないエラーは取り敢えず現状維持をモットーとする
  rescue => e
    logger.error e.message
    logger.error e.backtrace.join("\n")
  ensure
    close
  end

  uploaded_count
end

#receive_signal(signal) ⇒ Object



53
54
55
# File 'lib/sffftp/sftp.rb', line 53

def receive_signal(signal)
  @killing = true
end