Skip to content

Commit

Permalink
Avoid race condition between Cobbler and tar
Browse files Browse the repository at this point in the history
Also find errors in cobbler.log
  • Loading branch information
Bischoff authored and admd committed Jun 7, 2024
1 parent c081cd6 commit cb6f2f7
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions testsuite/features/step_definitions/cobbler_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,39 @@
end

Then(/^the local logs for Cobbler should not contain errors$/) do
node = get_target('server')

# normal log file
cobbler_log_file = '/var/log/cobbler/cobbler.log'
local_file = '/tmp/cobbler.log'
# to avoid a race condition with "tar" as called by "mgrctl cp", we need to work on a copy:
node.run("cp #{cobbler_log_file} #{local_file}")
return_code = file_extract(node, local_file, local_file)
raise ScriptError, 'File extraction failed' unless return_code.zero?

output = File.read(local_file).each_line.select { |line| line.include? 'ERROR' }
unless output.empty?
node.run("cp #{local_file} #{cobbler_log_file}-$(date +\"%Y_%m_%d_%I_%M_%p\")")
log "Error in Cobbler log:\n#{output}"
log ''
end

# debug log file
cobbler_log_file = '/var/log/cobbler/cobbler_debug.log'
local_file = '/tmp/cobbler_debug.log'
return_code = file_extract(get_target('server'), cobbler_log_file, local_file)
# to avoid a race condition with "tar" as called by "mgrctl cp", we need to work on a copy:
node.run("cp #{cobbler_log_file} #{local_file}")
return_code = file_extract(node, local_file, local_file)
raise ScriptError, 'File extraction failed' unless return_code.zero?

file_data = File.read(local_file).gsub!("\n", ',').chop.gsub('"', ' \' ').gsub('\\\'\'', '"')
file_data = "[#{file_data}]"
data_hash = JSON.parse(file_data)
output = data_hash.select { |key, _hash| key['levelname'] == 'ERROR' }
get_target('server').run("cp #{cobbler_log_file} #{cobbler_log_file}$(date +\"%Y_%m_%d_%I_%M_%p\")") unless output.empty?
raise ScriptError, "Errors in Cobbler logs:\n #{output}" unless output.empty?
file_data = File.read(local_file).gsub("\n", ',').chop.gsub('"', ' \' ').gsub('\\\'\'', '"')
data_hash = JSON.parse("[#{file_data}]")
output_debug = data_hash.select { |key, _hash| key['levelname'] == 'ERROR' }
unless output_debug.empty?
node.run("cp #{local_file} #{cobbler_log_file}-$(date +\"%Y_%m_%d_%I_%M_%p\")")
log "Error in Cobbler debug log:\n#{output_debug}"
log ''
end

raise ScriptError, 'Errors in Cobbler logs' unless output.empty? && output_debug.empty?
end

0 comments on commit cb6f2f7

Please sign in to comment.