From 13ca633e6c4485f6b4b784b3ba324ec1796e79b6 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Tue, 3 Feb 2015 16:02:42 -0500 Subject: [PATCH] Refactor log_header into a method. https://bugzilla.redhat.com/show_bug.cgi?id=1184465 --- vmdb/app/models/file_depot_ftp.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/vmdb/app/models/file_depot_ftp.rb b/vmdb/app/models/file_depot_ftp.rb index 471a3a3c810..5d8176ae977 100644 --- a/vmdb/app/models/file_depot_ftp.rb +++ b/vmdb/app/models/file_depot_ftp.rb @@ -5,44 +5,45 @@ def self.uri_prefix "ftp" end + def log_header(method = nil) + "MIQ(#{self.class.name}##{method})" + end + def self.validate_settings(settings) depot = new(:uri => settings[:uri]) depot.with_connection(:username => settings[:username], :password => settings[:password]) { |c| c.last_response } end def upload_file(file) - log_header = "MIQ(#{self.class.name}##{__method__})" super with_connection do |ftp| begin return if destination_file_exists?(ftp, destination_file) create_directory_structure(ftp) - $log.info("#{log_header} Uploading file: #{destination_file} to File Depot: #{name}...") ftp.putbinaryfile(file.local_file, destination_file) rescue => err msg = "Error '#{err.message.chomp}', writing to FTP: [#{uri}], Username: [#{authentication_userid}]" - $log.error("#{log_header} #{msg}") + $log.error("#{log_header(__method__)} #{msg}") raise msg else file.update_attributes( :state => "available", :log_uri => destination_file ) - $log.info("#{log_header} Uploading file: #{destination_file}... Complete") + $log.info("#{log_header(__method__)} Uploading file: #{destination_file}... Complete") file.post_upload_tasks end end end def remove_file(file) - log_header = "MIQ(#{self.class.name}##{__method__})" @file = file - $log.info("#{log_header} Removing log file [#{destination_file}]...") + $log.info("#{log_header(__method__)} Removing log file [#{destination_file}]...") with_connection do |ftp| ftp.delete(destination_file) end - $log.info("#{log_header} Removing log file [#{destination_file}]...complete") + $log.info("#{log_header(__method__)} Removing log file [#{destination_file}]...complete") end def verify_credentials(_auth_type = nil) @@ -63,22 +64,21 @@ def with_connection(cred_hash = nil) end def connect(cred_hash = nil) - log_header = "MIQ(#{self.class.name}##{__method__})" host = URI.split(URI.encode(uri))[2] begin - $log.info("#{log_header} 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 + $log.info("#{log_header(__method__)} Connecting to #{self.class.name}: #{name} host: #{host}...") creds = cred_hash ? [cred_hash[:username], cred_hash[:password]] : login_credentials ftp.login(*creds) - $log.info("#{log_header} Connected to #{self.class.name}: #{name} host: #{host}") + $log.info("#{log_header(__method__)} Connected to #{self.class.name}: #{name} host: #{host}") rescue SocketError => err - $log.error("#{log_header} Failed to connect. #{err.message}") + $log.error("#{log_header(__method__)} Failed to connect. #{err.message}") raise rescue Net::FTPPermError => err - $log.error("#{log_header} Failed to login. #{err.message}") + $log.error("#{log_header(__method__)} Failed to login. #{err.message}") raise else ftp