Skip to content

Commit

Permalink
Add ManageIQ::Util::FtpLib
Browse files Browse the repository at this point in the history
A library for shared Net::FTP functions that can be used throughout the
ManageIQ project.  Extracted from FileDepotFtp.
  • Loading branch information
NickLaMuro committed Jul 15, 2018
1 parent 5a2ec6b commit 0e842f5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
42 changes: 2 additions & 40 deletions app/models/file_depot_ftp.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'net/ftp'

class FileDepotFtp < FileDepot
include ManageIQ::Util::FtpLib

attr_accessor :ftp

def self.uri_prefix
Expand Down Expand Up @@ -62,52 +64,12 @@ def with_connection(cred_hash = nil)
end
end

def connect(cred_hash = nil)
host = URI(uri).hostname

begin
_log.info("Connecting to #{self.class.name}: #{name} host: #{host}...")
@ftp = Net::FTP.new(host)
@ftp.passive = true # Use passive mode to avoid firewall issues see http://slacksite.com/other/ftp.html#passive
# @ftp.debug_mode = true if settings[:debug] # TODO: add debug option
creds = cred_hash ? [cred_hash[:username], cred_hash[:password]] : login_credentials
@ftp.login(*creds)
_log.info("Connected to #{self.class.name}: #{name} host: #{host}")
rescue SocketError => err
_log.error("Failed to connect. #{err.message}")
raise
rescue Net::FTPPermError => err
_log.error("Failed to login. #{err.message}")
raise
else
@ftp
end
end

def file_exists?(file_or_directory)
!ftp.nlst(file_or_directory.to_s).empty?
rescue Net::FTPPermError
false
end

def self.display_name(number = 1)
n_('FTP', 'FTPs', number)
end

private

def create_directory_structure(directory_path)
pwd = ftp.pwd
directory_path.to_s.split('/').each do |directory|
unless ftp.nlst.include?(directory)
_log.info("creating #{directory}")
ftp.mkdir(directory)
end
ftp.chdir(directory)
end
ftp.chdir(pwd)
end

def upload(source, destination)
create_directory_structure(destination_path)
_log.info("Uploading file: #{destination} to File Depot: #{name}...")
Expand Down
72 changes: 72 additions & 0 deletions lib/manageiq/util/ftp_lib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'net/ftp'
require 'logger'

# Helper methods for net/ftp based classes and files.
#
# Will setup a `@ftp` attr_accessor to be used as the return value for
# `.connect`, the main method being provided in this class. Will also setup
# logging if not already done for the particular class (that follows the
# VmdbLogger conventions, and setup a `uri` attr_accessor if that doesn't
# already exist.
module ManageIQ
module Util
module FtpLib
def self.included(klass)
klass.send(:attr_accessor, :ftp)

klass.send(:attr_accessor, :uri) unless klass.instance_methods.include?(:uri=)

unless klass.instance_methods.include?(:_log)
klass.send(:define_method, :_log) do
self.class.instance_logger
end
end
end

def connect(cred_hash = nil)
host = URI(uri).hostname

begin
host_url = host
host_url << " (#{name})" if respond_to?(:name)
_log.info("Connecting to FTP host #{host_url}...")
@ftp = Net::FTP.new(host)
# Use passive mode to avoid firewall issues see http://slacksite.com/other/ftp.html#passive
@ftp.passive = true
# @ftp.debug_mode = true if settings[:debug] # TODO: add debug option
creds = cred_hash ? [cred_hash[:username], cred_hash[:password]] : login_credentials
@ftp.login(*creds)
_log.info("Successfully connected FTP host #{host_url}...")
rescue SocketError => err
_log.error("Failed to connect. #{err.message}")
raise
rescue Net::FTPPermError => err
_log.error("Failed to login. #{err.message}")
raise
else
@ftp
end
end

def file_exists?(file_or_directory)
!ftp.nlst(file_or_directory.to_s).empty?
rescue Net::FTPPermError
false
end

private

def create_directory_structure(directory_path)
pwd = ftp.pwd
directory_path.to_s.split('/').each do |directory|
unless ftp.nlst.include?(directory)
_log.info("creating #{directory}")
ftp.mkdir(directory)
end
ftp.chdir(directory)
end
ftp.chdir(pwd)
end
end
end
end

0 comments on commit 0e842f5

Please sign in to comment.