Skip to content

Commit

Permalink
check for running tasks before a backup is done
Browse files Browse the repository at this point in the history
optionally allow waiting for them to complete
  • Loading branch information
evgeni committed Jul 18, 2024
1 parent d3069e3 commit 6320b6c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
9 changes: 9 additions & 0 deletions definitions/scenarios/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class Backup < ForemanMaintain::Scenario
param :proxy_features, 'List of proxy features to backup (default: all)', :array => true
param :skip_pulp_content, 'Skip Pulp content during backup'
param :tar_volume_size, 'Size of tar volume (indicates splitting)'
param :wait_for_tasks, 'Wait for running tasks to complete instead of aborting'
end

# rubocop:disable Metrics/AbcSize
def compose
check_valid_strategy
add_step_with_context(Checks::Backup::IncrementalParentType,
:online_backup => strategy == :online,
:sql_tar => feature(:instance).postgresql_local?)
add_step(Checks::ForemanTasks::NotRunning.new(:wait_for_tasks => wait_for_tasks?))
add_step(Checks::Pulpcore::NoRunningTasks.new(:wait_for_tasks => wait_for_tasks?))
safety_confirmation
add_step_with_context(Procedures::Backup::AccessibilityConfirmation) if strategy == :offline
add_step_with_context(Procedures::Backup::PrepareDirectory,
Expand All @@ -36,6 +40,7 @@ def compose

add_step_with_context(Procedures::Backup::CompressData)
end
# rubocop:enable Metrics/AbcSize

# rubocop:disable Metrics/MethodLength
def set_context_mapping
Expand Down Expand Up @@ -126,6 +131,10 @@ def strategy
def include_db_dumps?
!!context.get(:include_db_dumps)
end

def wait_for_tasks?
!!context.get(:wait_for_tasks)
end
end

class BackupRescueCleanup < ForemanMaintain::Scenario
Expand Down
2 changes: 2 additions & 0 deletions lib/foreman_maintain/cli/backup_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def common_backup_options
option '--features', 'FEATURES',
"#{proxy_name} features to include in the backup. " \
'Valid features are tftp, dns, dhcp, openscap, and all.', :multivalued => true
option '--wait-for-tasks', :flag, 'Wait for running tasks to complete instead of aborting'
end
# rubocop:enable Metrics/MethodLength

Expand Down Expand Up @@ -89,6 +90,7 @@ def backup_scenario(options, strategy)
:tar_volume_size => split_pulp_tar,
:skip_pulp_content => skip_pulp_content?,
:incremental_dir => incremental,
:wait_for_tasks => wait_for_tasks?,
}.merge(options))
end

Expand Down
58 changes: 56 additions & 2 deletions test/definitions/scenarios/backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,30 @@ module Scenarios
end
end


let(:task_checks) do
[Checks::ForemanTasks::NotRunning,
Checks::Pulpcore::NoRunningTasks]
end

let(:checks) do
[Checks::Backup::IncrementalParentType] + task_checks
end

describe 'offline' do
let(:scenario) do
ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :offline)
end

it 'composes all steps' do
assert_scenario_has_step(scenario, Checks::Backup::IncrementalParentType)
task_checks.each do |check|
assert_scenario_has_step(scenario, check) do |step|
refute step.options['wait_for_tasks']
end
end
checks.each do |check|
assert_scenario_has_step(scenario, check)
end
assert_scenario_has_step(scenario, Procedures::Backup::AccessibilityConfirmation)
assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory)
assert_scenario_has_step(scenario, Procedures::Backup::Metadata)
Expand All @@ -37,13 +54,35 @@ module Scenarios
end
end

describe 'offline with wait_for_tasks' do
let(:scenario) do
ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :offline,
:wait_for_tasks => true)
end

it 'composes all steps' do
task_checks.each do |check|
assert_scenario_has_step(scenario, check) do |step|
assert step.options['wait_for_tasks']
end
end
end
end

describe 'online' do
let(:scenario) do
ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :online)
end

it 'composes all steps' do
assert_scenario_has_step(scenario, Checks::Backup::IncrementalParentType)
task_checks.each do |check|
assert_scenario_has_step(scenario, check) do |step|
refute step.options['wait_for_tasks']
end
end
checks.each do |check|
assert_scenario_has_step(scenario, check)
end
assert_scenario_has_step(scenario, Procedures::Backup::Online::SafetyConfirmation)
refute_scenario_has_step(scenario, Procedures::Backup::AccessibilityConfirmation)
assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory)
Expand All @@ -58,6 +97,21 @@ module Scenarios
assert_scenario_has_step(scenario, Procedures::Backup::CompressData)
end
end

describe 'online with wait_for_tasks' do
let(:scenario) do
ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :online,
:wait_for_tasks => true)
end

it 'composes all steps' do
task_checks.each do |check|
assert_scenario_has_step(scenario, check) do |step|
assert step.options['wait_for_tasks']
end
end
end
end
end

describe ForemanMaintain::Scenarios::BackupRescueCleanup do
Expand Down

0 comments on commit 6320b6c

Please sign in to comment.