Class: Transport::SftpConnection

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

Overview

Service object: sftp connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SftpConnection.



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

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.



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

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/services/transport/sftp_connection.rb', line 100

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



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

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



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

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



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

def config
  raise "Missing profile in secrets file: #{profile}" unless (profile_config = Heatwave::Configuration.fetch(profile&.to_sym))

  @config ||= profile_config.symbolize_keys
end

#connectionObject



49
50
51
# File 'app/services/transport/sftp_connection.rb', line 49

def connection(&)
  Net::SFTP.start(hostname, username, build_options, &)
end

#download_data(remote_filepath) ⇒ Object

Download the remote filepath to a data string



75
76
77
78
79
80
81
# File 'app/services/transport/sftp_connection.rb', line 75

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



64
65
66
67
68
69
70
71
72
# File 'app/services/transport/sftp_connection.rb', line 64

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



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

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

#list_files(directory) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/services/transport/sftp_connection.rb', line 53

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

#passwordObject



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

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

#portObject



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

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

#rm(file_path) ⇒ Object



132
133
134
135
136
137
138
# File 'app/services/transport/sftp_connection.rb', line 132

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



83
84
85
86
87
88
89
# File 'app/services/transport/sftp_connection.rb', line 83

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



91
92
93
94
95
96
# File 'app/services/transport/sftp_connection.rb', line 91

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



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

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