diff --git a/lib/foreman_maintain/cli/upgrade_command.rb b/lib/foreman_maintain/cli/upgrade_command.rb index 940ce61bb..75c3fec38 100644 --- a/lib/foreman_maintain/cli/upgrade_command.rb +++ b/lib/foreman_maintain/cli/upgrade_command.rb @@ -1,63 +1,24 @@ module ForemanMaintain module Cli class UpgradeCommand < Base - def self.target_version_option - option '--target-version', 'TARGET_VERSION', 'Target version of the upgrade', - :required => false - end - def self.disable_self_upgrade_option option '--disable-self-upgrade', :flag, 'Disable automatic self upgrade', :default => false end - def current_target_version - current_target_version = ForemanMaintain::UpgradeRunner.current_target_version - if current_target_version && target_version && target_version != current_target_version - raise Error::UsageError, - "Can't set target version #{target_version}, "\ - "#{current_target_version} already in progress" - end - @target_version = current_target_version if current_target_version - return true if current_target_version - end - - def validate_target_version! - return if current_target_version - unless UpgradeRunner.available_targets.include?(target_version) - message_start = if target_version - "Can't upgrade to #{target_version}" - else - '--target-version not specified' - end - message = <<~MESSAGE - #{message_start} - Possible target versions are: - MESSAGE - versions = UpgradeRunner.available_targets.join("\n") - raise Error::UsageError, message + versions - end - end - def upgrade_runner return @upgrade_runner if defined? @upgrade_runner - validate_target_version! - @upgrade_runner = ForemanMaintain::UpgradeRunner.new(target_version, reporter, + @upgrade_runner = ForemanMaintain::UpgradeRunner.new(reporter, :assumeyes => assumeyes?, :whitelist => whitelist || [], :force => force?).tap(&:load) end - def print_versions(target_versions) - target_versions.sort.each { |version| puts version } - end - def allow_self_upgrade? !disable_self_upgrade? end subcommand 'check', 'Run pre-upgrade checks before upgrading to specified version' do - target_version_option interactive_option disable_self_upgrade_option @@ -70,7 +31,6 @@ def execute end subcommand 'run', 'Run full upgrade to a specified version' do - target_version_option interactive_option disable_self_upgrade_option diff --git a/lib/foreman_maintain/upgrade_runner.rb b/lib/foreman_maintain/upgrade_runner.rb index f67b7e63e..e8bd82f6e 100644 --- a/lib/foreman_maintain/upgrade_runner.rb +++ b/lib/foreman_maintain/upgrade_runner.rb @@ -34,32 +34,25 @@ def clear_current_target_version attr_reader :version, :tag, :phase - def initialize(version, reporter, options = {}) + def initialize(reporter, options = {}) super(reporter, [], options) - @version = version - scenarios_present = find_scenarios(:tags => :upgrade_scenario).any?(&matching_version_test) - raise "Unknown version #{version}" unless scenarios_present - @scenario_cache = {} - self.phase = :pre_upgrade_checks + @phase = :pre_upgrade_checks + condition = { :tags => [:upgrade_scenario, phase] } + matching_scenarios = find_scenarios(condition) + @version = matching_scenarios.first.target_version end def scenario(phase) return @scenario_cache[phase] if @scenario_cache.key?(phase) condition = { :tags => [:upgrade_scenario, phase] } - matching_scenarios = find_scenarios(condition).select(&matching_version_test) + matching_scenarios = find_scenarios(condition) raise "Too many scenarios match #{condition.inspect}" if matching_scenarios.size > 1 @scenario_cache[phase] = matching_scenarios.first end - def matching_version_test - proc do |scenario| - scenario.respond_to?(:target_version) && scenario.target_version == @version - end - end - def run self.class.current_target_version = @version PHASES.each do |phase| diff --git a/test/lib/cli/upgrade_command_test.rb b/test/lib/cli/upgrade_command_test.rb index 60839eee1..253880602 100644 --- a/test/lib/cli/upgrade_command_test.rb +++ b/test/lib/cli/upgrade_command_test.rb @@ -58,7 +58,6 @@ def foreman_maintain_update_unavailable it 'run self upgrade if upgrade available for foreman-maintain' do foreman_maintain_update_available - command << '--target-version=1.15' assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -71,7 +70,6 @@ def foreman_maintain_update_unavailable it 'runs the upgrade checks when update is not available for foreman-maintain' do foreman_maintain_update_unavailable - command << '--target-version=1.15' UpgradeRunner.any_instance.expects(:run_phase).with(:pre_upgrade_checks) assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -83,12 +81,6 @@ def foreman_maintain_update_unavailable foreman_maintain_update_available command << '--disable-self-upgrade' UpgradeRunner.any_instance.expects(:run_phase).with(:pre_upgrade_checks) - run_cmd(['--target-version=1.15']) - end - - it 'should raise UsageError and exit with code 1' do - Cli::MainCommand.any_instance.stubs(:exit!) - run_cmd([]) end end @@ -100,7 +92,6 @@ def foreman_maintain_update_unavailable it 'run self upgrade if upgrade available for foreman-maintain' do foreman_maintain_update_available - command << '--target-version=1.15' assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -113,7 +104,6 @@ def foreman_maintain_update_unavailable it 'runs the full upgrade when update is not available for foreman-maintain' do foreman_maintain_update_unavailable - command << '--target-version=1.15' UpgradeRunner.any_instance.expects(:run) assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -124,7 +114,7 @@ def foreman_maintain_update_unavailable it 'skip self upgrade and runs the full upgrade for version' do command << '--disable-self-upgrade' UpgradeRunner.any_instance.expects(:run) - run_cmd(['--target-version=1.15']) + run_cmd([]) end it 'runs the self upgrade when update available for rubygem-foreman_maintain' do @@ -142,7 +132,7 @@ def foreman_maintain_update_unavailable run_cmd - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) + assert_cmd(<<~OUTPUT, []) Checking for new version of rubygem-foreman_maintain... Updating rubygem-foreman_maintain package. @@ -151,51 +141,6 @@ def foreman_maintain_update_unavailable Re-run foreman-maintain with required options! OUTPUT end - - it 'remembers the current target version and informs no update available' do - foreman_maintain_update_unavailable - Cli::MainCommand.any_instance.expects(:exit!).twice - assert_cmd <<~OUTPUT - Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - --target-version not specified - Possible target versions are: - 1.15 - OUTPUT - - UpgradeRunner.current_target_version = '1.15' - UpgradeRunner.any_instance.expects(:run) - - run_cmd - - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) - Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - Can't set target version 1.16, 1.15 already in progress - OUTPUT - end - - it 'remembers the current target version when self upgrade disabled' do - command << '--disable-self-upgrade' - Cli::MainCommand.any_instance.expects(:exit!) - assert_cmd <<~OUTPUT - --target-version not specified - Possible target versions are: - 1.15 - OUTPUT - end - - it 'does not allow the another upgrade when one is going on' do - foreman_maintain_update_unavailable - UpgradeRunner.current_target_version = '1.15' - Cli::MainCommand.any_instance.expects(:exit!) - - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) - Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - Can't set target version 1.16, 1.15 already in progress - OUTPUT - end end end end diff --git a/test/lib/upgrade_runner_test.rb b/test/lib/upgrade_runner_test.rb index 803584022..91a21f80e 100644 --- a/test/lib/upgrade_runner_test.rb +++ b/test/lib/upgrade_runner_test.rb @@ -12,11 +12,11 @@ module ForemanMaintain end let(:upgrade_runner) do - UpgradeRunner.new('1.15', reporter) + UpgradeRunner.new(reporter) end let(:upgrade_runner_with_whitelist) do - UpgradeRunner.new('1.15', reporter, + UpgradeRunner.new(reporter, :whitelist => %w[present-service-is-running service-is-stopped]) end @@ -56,7 +56,7 @@ module ForemanMaintain original_scenario = upgrade_runner_with_whitelist.scenario(:pre_upgrade_checks) ForemanMaintain.detector.refresh - new_upgrade_runner = UpgradeRunner.new('1.15', reporter) + new_upgrade_runner = UpgradeRunner.new(reporter) new_upgrade_runner.load _(new_upgrade_runner.phase).must_equal :migrations restored_scenario = new_upgrade_runner.scenario(:pre_upgrade_checks) @@ -87,7 +87,7 @@ module ForemanMaintain upgrade_runner_with_whitelist.run upgrade_runner_with_whitelist.save - new_upgrade_runner = UpgradeRunner.new('1.15', reporter) + new_upgrade_runner = UpgradeRunner.new(reporter) new_upgrade_runner.load _(new_upgrade_runner.phase).must_equal :pre_upgrade_checks _(UpgradeRunner.current_target_version).must_be_nil