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 authored and evgeni committed Aug 20, 2024
1 parent 0878bbb commit ab88247
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 120 deletions.
4 changes: 4 additions & 0 deletions definitions/features/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def current_version
version.to_s[/^\d+\.\d+\.\d+/]
end

def current_major_version
current_version.to_s[/^\d+\.\d+/]
end

def target_version
if feature(:instance).downstream
Features::Satellite.new.target_version
Expand Down
2 changes: 1 addition & 1 deletion definitions/scenarios/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def self.update_metadata(&block)
tags :update_scenario

confine do
feature(:instance).target_version == feature(:instance).current_version.to_s[/^\d+\.\d+/]
feature(:instance).target_version == feature(:instance).current_major_version
end

instance_eval(&block)
Expand Down
79 changes: 33 additions & 46 deletions lib/foreman_maintain/cli/upgrade_command.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,72 @@
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

def try_upgrade
if upgrade_runner.available?
yield
else
instance = ForemanMaintain.detector.feature(:instance)
msg = <<~BANNER
There are no upgrades available.
The current version of #{instance.product_name} is #{instance.current_major_version}.
Consider using the update command.
BANNER

puts msg
ForemanMaintain::UpgradeRunner::WARNING_EXIT_CODE
end
end

subcommand 'check', 'Run pre-upgrade checks before upgrading to specified version' do
target_version_option
interactive_option
disable_self_upgrade_option

def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
upgrade_runner.run_phase(:pre_upgrade_checks)
exit upgrade_runner.exit_code

exit_code = try_upgrade do
upgrade_runner.run_phase(:pre_upgrade_checks)
upgrade_runner.exit_code
end

exit exit_code
end
end

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

def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
upgrade_runner.run
upgrade_runner.save
exit upgrade_runner.exit_code

exit_code = try_upgrade do
upgrade_runner.run
upgrade_runner.save
upgrade_runner.exit_code
end

exit exit_code
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/foreman_maintain/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Runner
include Concerns::Logger
attr_reader :reporter, :exit_code

WARNING_EXIT_CODE = 78
FAILURE_EXIT_CODE = 1

require 'foreman_maintain/runner/execution'
require 'foreman_maintain/runner/stored_execution'
def initialize(reporter, scenarios, options = {})
Expand Down Expand Up @@ -61,8 +64,8 @@ def run_scenario(scenario)
@last_scenario = scenario
@last_scenario_continuation_confirmed = false
end
@exit_code = 78 if scenario.warning?
@exit_code = 1 if scenario.failed?
@exit_code = WARNING_EXIT_CODE if scenario.warning?
@exit_code = FAILURE_EXIT_CODE if scenario.failed?
end

def whitelisted_step?(step)
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
89 changes: 35 additions & 54 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,8 +70,8 @@ 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)
UpgradeRunner.any_instance.expects(:available?).returns(true)
assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Expand All @@ -83,11 +82,22 @@ 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'])
UpgradeRunner.any_instance.expects(:available?).returns(true)
run_cmd([])
end

it 'should raise UsageError and exit with code 1' do
Cli::MainCommand.any_instance.stubs(:exit!)
it 'throws an error message if no upgrade is available' do
foreman_maintain_update_unavailable
UpgradeRunner.any_instance.expects(:available?).twice.returns(false)

assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
There are no upgrades available.
The current version of FakeyFakeFake is 3.14.
Consider using the update command.
OUTPUT

run_cmd([])
end
Expand All @@ -100,7 +110,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,8 +122,8 @@ 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)
UpgradeRunner.any_instance.expects(:available?).returns(true)
assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Expand All @@ -124,76 +133,48 @@ 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'])
UpgradeRunner.any_instance.expects(:available?).returns(true)
run_cmd([])
end

it 'runs the self upgrade when update available for rubygem-foreman_maintain' do
foreman_maintain_update_available
it 'throws an error message if no upgrade is available' do
foreman_maintain_update_unavailable
UpgradeRunner.any_instance.expects(:available?).twice.returns(false)

assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Updating rubygem-foreman_maintain package.
The rubygem-foreman_maintain package successfully updated.
Re-run foreman-maintain with required options!
There are no upgrades available.
The current version of FakeyFakeFake is 3.14.
Consider using the update command.
OUTPUT

UpgradeRunner.current_target_version = '1.15'

run_cmd
run_cmd([])
end

assert_cmd(<<~OUTPUT, ['--target-version', '1.16'])
it 'runs the self upgrade when update available for rubygem-foreman_maintain' do
foreman_maintain_update_available
assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Updating rubygem-foreman_maintain package.
The rubygem-foreman_maintain package successfully updated.
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'])
assert_cmd(<<~OUTPUT, [])
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!)
Updating rubygem-foreman_maintain package.
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
The rubygem-foreman_maintain package successfully updated.
Re-run foreman-maintain with required options!
OUTPUT
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/lib/support/definitions/features/fake_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ class Features::FakeInstance < ForemanMaintain::Feature
def downstream
false
end

def product_name
"FakeyFakeFake"
end

def current_version
'3.14.2'
end

def current_major_version
current_version.to_s[/^\d+\.\d+/]
end
end
Loading

0 comments on commit ab88247

Please sign in to comment.