Skip to content

Commit

Permalink
Fixes #34937 - Simplify self-upgrade mechanism
Browse files Browse the repository at this point in the history
With this change self-upgrade enables required repositories in yum/dnf
command. This means it does not change the state of system repositories,
which simplifies the code and improves time it takes to complete the update.

(cherry picked from commit c9ae892)
  • Loading branch information
upadhyeammit committed Jul 8, 2022
1 parent 35a6aff commit 928fe74
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 118 deletions.
4 changes: 3 additions & 1 deletion definitions/procedures/packages/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class Update < ForemanMaintain::Procedure
param :force, 'Do not skip if package is installed', :flag => true, :default => false
param :warn_on_errors, 'Do not interrupt scenario on failure',
:flag => true, :default => false
param :yum_options, 'Extra yum options if any', :array => true, :default => []
end

def run
assumeyes_val = @assumeyes.nil? ? assumeyes? : @assumeyes
package_manager.clean_cache(:assumeyes => assumeyes_val)
packages_action(:update, @packages, :assumeyes => assumeyes_val)
opts = { :assumeyes => assumeyes_val, :yum_options => @yum_options }
packages_action(:update, @packages, opts)
rescue ForemanMaintain::Error::ExecutionError => e
if @warn_on_errors
set_status(:warning, e.message)
Expand Down
71 changes: 5 additions & 66 deletions definitions/scenarios/self_upgrade.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
module ForemanMaintain::Scenarios
class SelfUpgradeBase < ForemanMaintain::Scenario
include ForemanMaintain::Concerns::Downstream
def enabled_system_repos_id
repository_manager.enabled_repos.keys
end

def enable_repos(repo_ids = stored_enabled_repos_ids)
add_step(Procedures::Repositories::Enable.new(repos: repo_ids))
end

def disable_repos(repo_ids = stored_enabled_repos_ids)
add_step(Procedures::Repositories::Disable.new(repos: repo_ids))
end

def target_version
current_full_version = feature(:instance).downstream.current_version
@target_version ||= current_full_version.bump
Expand Down Expand Up @@ -44,36 +32,6 @@ def maintenance_repo(version)
end
end

def maintenance_repo_version
return '6' if current_version == '6.10'

current_version
end

def stored_enabled_repos_ids
@stored_enabled_repos_ids ||= begin
path = File.expand_path('enabled_repos.yml', ForemanMaintain.config.backup_dir)
@stored_enabled_repos_ids = File.file?(path) ? YAML.load(File.read(path)) : []
end
end

def all_maintenance_repos
repo_regex = if el7?
/rhel-\d-server-satellite-maintenance-\d.\d-rpms/
else
/satellite-maintenance-\d.\d-for-rhel-\d-x86_64-rpms/
end
stored_enabled_repos_ids.select { |id| !id.match(repo_regex).nil? }
end

def repos_ids_to_reenable
repos_ids_to_reenable = stored_enabled_repos_ids - all_maintenance_repos
if use_rhsm?
repos_ids_to_reenable << maintenance_repo(maintenance_repo_version)
end
repos_ids_to_reenable
end

def use_rhsm?
return false if maintenance_repo_label

Expand All @@ -100,30 +58,11 @@ class SelfUpgrade < SelfUpgradeBase
def compose
if check_min_version('foreman', '2.5') || check_min_version('foreman-proxy', '2.5')
pkgs_to_update = %w[satellite-maintain rubygem-foreman_maintain]
add_step(Procedures::Repositories::BackupEnabledRepos.new)
disable_repos
add_step(Procedures::Repositories::Enable.new(repos: req_repos_to_update_pkgs,
use_rhsm: use_rhsm?))
add_step(Procedures::Packages::Update.new(packages: pkgs_to_update, assumeyes: true))
disable_repos('*')
enable_repos(repos_ids_to_reenable)
end
end
end

class SelfUpgradeRescue < SelfUpgradeBase
metadata do
label :rescue_self_upgrade
description 'Disables all version specific maintenance repositories and,'\
"\nenables the repositories which were configured prior to self upgrade"
manual_detection
run_strategy :fail_slow
end

def compose
if check_min_version('foreman', '2.5') || check_min_version('foreman-proxy', '2.5')
disable_repos('*')
enable_repos(repos_ids_to_reenable)
yum_options = req_repos_to_update_pkgs.map do |id|
"--enablerepo=#{id}"
end
add_step(Procedures::Packages::Update.new(packages: pkgs_to_update, assumeyes: true,
yum_options: yum_options))
end
end
end
Expand Down
8 changes: 1 addition & 7 deletions lib/foreman_maintain/cli/self_upgrade_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ class SelfUpgradeCommand < Base
'Repository label from which packages should be updated.'\
'This can be used when standard CDN repositories are unavailable.'
def execute
run_scenario(upgrade_scenario, upgrade_rescue_scenario)
run_scenario(upgrade_scenario)
end

def upgrade_scenario
Scenarios::SelfUpgrade.new(
maintenance_repo_label: maintenance_repo_label
)
end

def upgrade_rescue_scenario
Scenarios::SelfUpgradeRescue.new(
maintenance_repo_label: maintenance_repo_label
)
end
end
end
end
5 changes: 3 additions & 2 deletions lib/foreman_maintain/concerns/system_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ def server?
end

def packages_action(action, packages, options = {})
options.validate_options!(:assumeyes)
options.validate_options!(:assumeyes, :yum_options)
case action
when :install
package_manager.install(packages, :assumeyes => options[:assumeyes])
when :update
package_manager.update(packages, :assumeyes => options[:assumeyes])
package_manager.update(packages, :assumeyes => options[:assumeyes],
:yum_options => options[:yum_options])
when :remove
package_manager.remove(packages, :assumeyes => options[:assumeyes])
else
Expand Down
12 changes: 8 additions & 4 deletions lib/foreman_maintain/package_manager/yum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def remove(packages, assumeyes: false)
yum_action('remove', packages, :assumeyes => assumeyes)
end

def update(packages = [], assumeyes: false)
yum_action('update', packages, :assumeyes => assumeyes)
def update(packages = [], assumeyes: false, yum_options: [])
yum_action('update', packages, :assumeyes => assumeyes, :yum_options => yum_options)
end

def clean_cache(assumeyes: false)
Expand Down Expand Up @@ -116,8 +116,12 @@ def setup_protector(enabled)
File.open(protector_config_file, 'w') { |file| file.puts config }
end

def yum_action(action, packages, with_status: false, assumeyes: false, valid_exit_statuses: [0])
yum_options = []
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def yum_action(action, packages, options)
with_status = options.fetch(:with_status, false)
assumeyes = options.fetch(:assumeyes, false)
valid_exit_statuses = options.fetch(:valid_exit_statuses, [0])
yum_options = options.fetch(:yum_options, [])
packages = [packages].flatten(1)
yum_options << '-y' if assumeyes
yum_options << '--disableplugin=foreman-protector'
Expand Down
38 changes: 0 additions & 38 deletions test/definitions/scenarios/self_upgrade_test.rb

This file was deleted.

0 comments on commit 928fe74

Please sign in to comment.