Skip to content

Commit

Permalink
Drop --target-version parameter
Browse files Browse the repository at this point in the history
This parameter is no longer used by the upgrade runner as it is
defined internally by the scenarios.
  • Loading branch information
ehelms committed Aug 19, 2024
1 parent 0878bbb commit f34dd75
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 115 deletions.
51 changes: 10 additions & 41 deletions lib/foreman_maintain/cli/upgrade_command.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -70,11 +31,19 @@ def execute
end

subcommand 'run', 'Run full upgrade to a specified version' do
target_version_option
interactive_option
disable_self_upgrade_option

def execute
unless upgrade_runner.available?
msg = <<~BANNER
There are no upgrades available.
BANNER

puts msg
exit upgrade_runner.exit_code
end

ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
upgrade_runner.run
Expand Down
25 changes: 12 additions & 13 deletions lib/foreman_maintain/upgrade_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,31 @@ 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 available?
condition = { :tags => [:upgrade_scenario, :pre_upgrade_check] }
matching_scenarios = find_scenarios(condition)
!matching_scenarios.empty?
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|
Expand Down
59 changes: 2 additions & 57 deletions test/lib/cli/upgrade_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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...
Expand All @@ -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
Expand All @@ -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...
Expand All @@ -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...
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
8 changes: 4 additions & 4 deletions test/lib/upgrade_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f34dd75

Please sign in to comment.