Class: Transport::SftpConnection

Inherits:
Object
  • Object
show all
Defined in:
app/services/transport/sftp_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, options = {}) ⇒ SftpConnection

Returns a new instance of SftpConnection.



8
9
10
11
12
# File 'app/services/transport/sftp_connection.rb', line 8

def initialize(profile, options={})
  @profile = profile
  @options = options
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'app/services/transport/sftp_connection.rb', line 6

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



6
7
8
# File 'app/services/transport/sftp_connection.rb', line 6

def profile
  @profile
end

Instance Method Details

#batch_download_data(directory, pattern = nil) ⇒ Object

In one shot returns a hash, hash key is filename, hash value is content of file
optional pattern as regexp to match on filename



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/transport/sftp_connection.rb', line 103

def batch_download_data(directory, pattern = nil)
  data = {}
  logger.info "Attempting connection to host: #{hostname} with user: #{username} directory #{directory}, pattern: #{pattern}"
  connection do |c|
    c.dir.foreach(directory) do |entry|
      if pattern.nil? || entry.name =~ pattern
        logger.info "SFTP downloading file #{directory}/#{entry.name}"
        data[entry.name] = c.download!("#{directory}/#{entry.name}")
      end
    end
  end
  data
end

#batch_download_to_file(directory, local_directory = nil, pattern = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/services/transport/sftp_connection.rb', line 117

def batch_download_to_file(directory, local_directory = nil, pattern = nil)
  data = {}
  local_directory ||= Dir.tmpdir
  logger.info "Attempting connection to host: #{hostname} with user: #{username} and directory #{directory}, pattern: #{pattern}, local download dir: #{local_directory}"
  connection do |c|
    c.dir.foreach(directory) do |entry|
      if pattern.nil? || !!(entry.name =~ pattern)
        remote_file_path = "#{directory}/#{entry.name}"
        local_file_path = "#{local_directory}/#{entry.name}"
        logger.info "SFTP downloading file #{remote_file_path}"
        c.download!(remote_file_path, local_file_path)
        data[entry.name] = local_file_path
      end
    end
  end
  data
end

#build_optionsObject

Here we build an option hash for the SSH connection, any of these value
can be added
http://net-ssh.github.io/net-ssh/Net/SSH.html



17
18
19
20
21
22
23
24
# File 'app/services/transport/sftp_connection.rb', line 17

def build_options
  {
    password: password,
    port: port,
    auth_methods: ['password'], #Use password explicitly, don't do public/private key
    config: false #Don't read local options
  }
end

#configObject



42
43
44
45
46
47
48
# File 'app/services/transport/sftp_connection.rb', line 42

def config
  if profile_config = Heatwave::Configuration.fetch(profile&.to_sym)
    @config ||= profile_config.symbolize_keys
  else
    raise "Missing profile in secrets file: #{profile}"
  end
end

#connectionObject



50
51
52
53
54
# File 'app/services/transport/sftp_connection.rb', line 50

def connection
  Net::SFTP.start(hostname, username, build_options) do |sftp|
    yield(sftp)
  end
end

#download_data(remote_filepath) ⇒ Object

Download the remote filepath to a data string



78
79
80
81
82
83
84
# File 'app/services/transport/sftp_connection.rb', line 78

def download_data(remote_filepath)
  data = nil
  connection do |c|
    data = c.download!(remote_filepath)
  end
  data
end

#download_to_file(remote_filepath, local_directory) ⇒ Object

Download the remote filepath to a local directory



67
68
69
70
71
72
73
74
75
# File 'app/services/transport/sftp_connection.rb', line 67

def download_to_file(remote_filepath, local_directory)
  # filename locally
  basename = File.basename remote_filepath
  local_filepath = "#{local_directory}/#{basename}"
  connection do |c|
    c.download!(remote_filepath, local_filepath)
  end
  local_filepath
end

#hostnameObject



26
27
28
# File 'app/services/transport/sftp_connection.rb', line 26

def hostname
  @options[:hostname] || config[:hostname]
end

#list_files(directory) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/services/transport/sftp_connection.rb', line 56

def list_files(directory)
  files = []
  connection do |c|
    c.dir.foreach(directory) do |entry|
      files << entry.name
    end
  end
  files
end

#passwordObject



34
35
36
# File 'app/services/transport/sftp_connection.rb', line 34

def password
  @options[:password] || config[:password]
end

#portObject



38
39
40
# File 'app/services/transport/sftp_connection.rb', line 38

def port
  @options[:port] || config[:port] || 22
end

#rm(file_path) ⇒ Object



135
136
137
138
139
140
141
# File 'app/services/transport/sftp_connection.rb', line 135

def rm(file_path)
  connection do |c|
    logger.info "SFTP deleting file #{file_path}"
    c.remove!(file_path)
  end
  :deleted
end

#send_data(data, remote_filepath) ⇒ Object



86
87
88
89
90
91
92
# File 'app/services/transport/sftp_connection.rb', line 86

def send_data(data, remote_filepath)
  io = StringIO.new(data)
  connection do |c|
    c.upload!(io, remote_filepath)
  end
  true # return success, and here for backward compatibility, we just return true, unless there is a more rigourous status check
end

#send_file(local_filepath, remote_filepath) ⇒ Object



94
95
96
97
98
99
# File 'app/services/transport/sftp_connection.rb', line 94

def send_file(local_filepath, remote_filepath)
  connection do |c|
    c.upload!(local_filepath, remote_filepath)
  end
  true # return success, and here for backward compatibility, we just return true, unless there is a more rigourous status check
end

#usernameObject



30
31
32
# File 'app/services/transport/sftp_connection.rb', line 30

def username
  @options[:username] || config[:username]
end