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.

Parameters:

  • profile (Symbol)

    configuration profile name; connection settings are
    loaded from Heatwave::Configuration under this key

  • options (Hash) (defaults to: {})

    connection overrides (win over the profile values)

Options Hash (options):

  • :logger (Logger)

    logger to use (defaults to Rails.logger)

  • :hostname (String)

    SFTP host

  • :username (String)

    SFTP username

  • :password (String)

    SFTP password

  • :port (Integer)

    SFTP port (defaults to 22)



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/services/transport/sftp_connection.rb', line 108

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/services/transport/sftp_connection.rb', line 122

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



26
27
28
29
30
31
32
33
# File 'app/services/transport/sftp_connection.rb', line 26

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



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

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



57
58
59
# File 'app/services/transport/sftp_connection.rb', line 57

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

#download_data(remote_filepath) ⇒ Object

Download the remote filepath to a data string



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

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



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

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



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

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

#list_files(directory) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/services/transport/sftp_connection.rb', line 61

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

#passwordObject



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

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

#portObject



47
48
49
# File 'app/services/transport/sftp_connection.rb', line 47

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

#rm(file_path) ⇒ Object



140
141
142
143
144
145
146
# File 'app/services/transport/sftp_connection.rb', line 140

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



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

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



99
100
101
102
103
104
# File 'app/services/transport/sftp_connection.rb', line 99

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



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

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