class ScalingoBackupsManager::SftpTools

Attributes

ftp_host[RW]

Public Class Methods

new(ftp_host) click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 8
def initialize(ftp_host)
  @ftp_host = ftp_host
end

Public Instance Methods

list_files(path) click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 18
def list_files(path)
  files = []
  start do |sftp|
    sftp.dir.glob("#{path}", "*.tar.gz").each do |file|
      files << file
    end
  end
  files
end
mkdir!(path) click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 34
def mkdir!(path)
  start do |sftp|
    folder_tree = []
    path.split("/").each do |folder_name|
      next if folder_name.blank?

      folder_tree << folder_name
      begin
        sftp.mkdir!(folder_tree.join("/"))
      rescue
      end
    end
  end
end
remove!(path) click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 28
def remove!(path)
  start do |sftp|
    sftp.remove!(path)
  end
end
start() { |sftp| ... } click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 12
def start
  Net::SFTP.start(@ftp_host[:host], @ftp_host[:user], password: @ftp_host[:password], port: @ftp_host[:port]) do |sftp|
    yield(sftp) if block_given?
  end
end
upload_file(filepath, remote_dir, options: {}) click to toggle source
# File lib/scalingo_backups_manager/sftp_tools.rb, line 49
def upload_file(filepath, remote_dir, options: {})
  filename = filepath.split('/').last
  start do |sftp|
    begin
      sftp.upload!(filepath, "#{remote_dir}/#{filename}")
    rescue
      if options.dig(:webhooks, :slack_webhook_url)
        ScalingoBackupsManager::Notification.send_slack_notification(options.dig(:webhooks, :slack_webhook_url), "An error has occured while uploading backup, see the logs for more information")
      end
      if options.dig(:webhooks, :discord_webhook_url)
        ScalingoBackupsManager::Notification.send_discord_notification(options.dig(:webhooks, :discord_webhook_url), "An error has occured while uploading backup, see the logs for more information")
      end
    end
  end
end