Skip to content

Commit

Permalink
Refactor log_header into a method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrafanie committed Feb 6, 2015
1 parent 31210b8 commit 13ca633
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions vmdb/app/models/file_depot_ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 13ca633

Please sign in to comment.