Skip to content

Commit

Permalink
Refactor how we check if a channel is synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
srbarrios committed Jan 31, 2025
1 parent 258ef73 commit fd8fa69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
19 changes: 11 additions & 8 deletions testsuite/features/step_definitions/command_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
raise ScriptError, "Synchronization error, channel #{channel} or #{channel}-#{architecture} in #{product} product not found" if channels_to_synchronize.nil? || channels_to_synchronize.empty?

channels_to_synchronize.each do |os_product_version_channel|
command = "spacewalk-common-channels -u admin -p admin -a #{architecture} #{os_product_version_channel}"
command = "spacewalk-common-channels -u admin -p admin -a #{architecture} #{os_product_version_channel.gsub("-#{architecture}", '')}"
get_target('server').run(command, verbose: true)
log "Channel #{os_product_version_channel} added"
end
Expand Down Expand Up @@ -350,7 +350,7 @@

# Remove from the list to kill those channels that are already synced
channels_to_kill.each do |remaining_channel|
if channel_is_synced(remaining_channel)
if channel_sync_completed?(remaining_channel)
log "Channel '#{remaining_channel}' is already synced, so there is no need to kill repo-sync process."
channels_to_kill.delete(remaining_channel)
end
Expand All @@ -363,17 +363,20 @@
time_spent = 0
checking_rate = 5
repeat_until_timeout(timeout: 60, message: 'Some reposync processes were not killed properly', dont_raise: true) do
command_output, _code = get_target('server').run("ps axo pid,cmd | grep /usr/bin/spacewalk-repo-sync --channel #{channel} | grep -v grep", check_errors: false)
command_output, _code = get_target('server').run('ps axo pid,cmd | grep spacewalk-repo-sync | grep -v grep', verbose: true, check_errors: false)
process = command_output.split("\n")[0]
channel_synchronizing = process.split[5].strip
if process.nil?
log "#{time_spent / 60.to_i} minutes waiting for '#{channel}' channel to start its repo-sync processes." if ((time_spent += checking_rate) % 60).zero?
sleep checking_rate
next
else
elsif channel_synchronizing == channel
pid = process.split[0]
get_target('server').run("kill #{pid}", check_errors: false)
get_target('server').run("kill #{pid}", verbose: true, check_errors: false)
log "Reposync of channel #{channel} killed"
break
else
log "Warning: Repo-sync process for channel '#{channel_synchronizing}' running."
end
end
end
Expand Down Expand Up @@ -419,7 +422,7 @@
end
begin
repeat_until_timeout(timeout: timeout, message: 'Channel not fully synced') do
break if channel_is_synced(channel)
break if channel_sync_completed?(channel)

log "#{time_spent / 60.to_i} minutes out of #{timeout / 60.to_i} waiting for '#{channel}' channel to be synchronized" if ((time_spent += checking_rate) % 60).zero?
sleep checking_rate
Expand All @@ -441,7 +444,7 @@
time_spent = 0
checking_rate = 10
# Let's start with a timeout margin aside from the sum of the timeouts for each channel
timeout = 600
timeout = 900
channels_to_wait.each do |channel|
if TIMEOUT_BY_CHANNEL_NAME[channel].nil?
log "Unknown timeout for channel #{channel}, assuming one minute"
Expand All @@ -453,7 +456,7 @@
begin
repeat_until_timeout(timeout: timeout, message: 'Product not fully synced') do
channels_to_wait.each do |channel|
if channel_is_synced(channel)
if channel_sync_completed?(channel)
channels_to_wait.delete(channel)
log "Channel #{channel} finished syncing"
end
Expand Down
36 changes: 32 additions & 4 deletions testsuite/features/support/commonlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,35 @@ def update_controller_ca
certutil -d sql:/root/.pki/nssdb -A -t TC -n "susemanager" -i /etc/pki/trust/anchors/#{server_name}.cert`
end

# This method checks if the synchronization for the given channel is completed
#
# @param channel_name [String] the channel to check
# @return [Boolean] true if the synchronization is completed, false otherwise
def channel_sync_completed?(channel_name)
log_tmp_file = '/tmp/reposync.log'
get_target('server').extract('/var/log/rhn/reposync.log', log_tmp_file)
raise ScriptError, 'The file with repository synchronization logs doesn\'t exist or is empty' if !File.exist?(log_tmp_file) || File.empty?(log_tmp_file)

log_content = File.readlines(log_tmp_file)
channel_found = false
log_content.each do |line|
if line.include?('Channel: ') && line.include?(channel_name)
channel_found = true
elsif line.include?('Channel: ') && !line.include?(channel_name)
channel_found = false
elsif line.include?('Sync of channel completed.') && channel_found
return true
end
end

false
end

# Determines whether a channel is synchronized on the server.
#
# @param channel [String] The name of the channel to check.
# @return [Boolean] Returns true if the channel is synchronized, false otherwise.
def channel_is_synced(channel)
def channel_is_synced?(channel)
sync_status = false
# solv is the last file to be written when the server synchronizes a channel, therefore we wait until it exist
result, code = get_target('server').run("dumpsolv /var/cache/rhn/repodata/#{channel}/solv", verbose: false, check_errors: false)
Expand Down Expand Up @@ -736,9 +760,13 @@ def wait_action_complete(actionid, timeout: DEFAULT_TIMEOUT)
# @param channels [Array<String>] The list of channels to filter.
# @param filters [Array<String>] The list of filters to apply.
def filter_channels(channels, filters = [])
filtered_channels = channels.clone
filters.each do |filter|
filtered_channels.delete_if { |channel| channel.include? filter }
if channels.nil? || channels.empty?
puts 'Warning: No channels to filter'
else
filtered_channels = channels.clone
filters.each do |filter|
filtered_channels.delete_if { |channel| channel.include? filter }
end
end
filtered_channels
end
Expand Down

0 comments on commit fd8fa69

Please sign in to comment.