Project

General

Profile

« Previous | Next » 

Revision 74e2fc5f

Added by Amit Upadhye over 1 year ago

Fixes #35401 - self upgrade for Foreman and Katello (#637)

  • Fixes #35401 - self upgrade for Foreman and Katello

View differences:

definitions/procedures/packages/uninstall.rb
module Procedures::Packages
class Uninstall < ForemanMaintain::Procedure
metadata do
description 'Uninstall packages'
param :packages, 'List of packages to uninstall', :array => true
param :assumeyes, 'Do not ask for confirmation'
param :warn_on_errors, 'Do not interrupt scenario on failure',
:flag => true, :default => false
end
def run
assumeyes_val = @assumeyes.nil? ? assumeyes? : @assumeyes
packages_action(:remove, @packages, :assumeyes => assumeyes_val)
rescue ForemanMaintain::Error::ExecutionError => e
if @warn_on_errors
set_status(:warning, e.message)
else
raise
end
end
def necessary?
@force || @packages.any? { |package| package_version(package).nil? }
end
def runtime_message
"Uninstalling package(s) #{@packages.join(', ')}"
end
end
end
definitions/scenarios/self_upgrade.rb
[maintenance_repo_id(target_version)]
end
end
def upstream_target_version
if feature(:katello_install)
return foreman_version_by_katello(target_version)
else
target_version
end
end
end
class SelfUpgrade < SelfUpgradeBase
......
manual_detection
end
def compose
def downstream_self_upgrade(pkgs_to_update)
if check_min_version('foreman', '2.5') || check_min_version('foreman-proxy', '2.5')
pkgs_to_update = %w[satellite-maintain rubygem-foreman_maintain]
yum_options = req_repos_to_update_pkgs.map do |id|
"--enablerepo=#{id}"
end
......
yum_options: yum_options))
end
end
def upstream_self_upgrade(pkgs_to_update)
# This method is responsible for
# 1. Setup the repositories of next major version
# 2. Update the foreman-maintain packages from next major version repository
# 3. Rollback the repository to current major version
add_step(Procedures::Repositories::Setup.new(:version => upstream_target_version))
add_step(Procedures::Packages::Update.new(packages: pkgs_to_update, assumeyes: true))
ensure
rollback_repositories
end
def rollback_repositories
installed_release_pkg = package_manager.find_installed_package('foreman-release',
'%{VERSION}')
unless current_version.nil? && installed_release_pkg.nil?
current_major_version = current_version[0..2]
installed_foreman_release_major_version = installed_release_pkg[0..2]
if installed_foreman_release_major_version != current_major_version
add_step(Procedures::Packages::Uninstall.new(packages: %w[foreman-release katello-repos],
assumeyes: true))
add_step(Procedures::Repositories::Setup.new(:version => current_major_version))
end
end
end
def compose
pkgs_to_update = %w[rubygem-foreman_maintain]
if feature(:instance).downstream
pkgs_to_update << 'satellite-maintain'
downstream_self_upgrade(pkgs_to_update)
elsif feature(:instance).upstream_install
upstream_self_upgrade(pkgs_to_update)
end
end
end
end
lib/foreman_maintain/cli/upgrade_command.rb
target_versions.sort.each { |version| puts version }
end
def allow_self_upgrade?
!disable_self_upgrade?
end
subcommand 'list-versions', 'List versions this system is upgradable to' do
disable_self_upgrade_option
def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_upgrade?
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
print_versions(UpgradeRunner.available_targets)
end
end
......
def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_upgrade?
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
upgrade_runner.run_phase(:pre_upgrade_checks)
exit upgrade_runner.exit_code
end
......
def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_upgrade?
ForemanMaintain.perform_self_upgrade if allow_self_upgrade?
if phase
upgrade_runner.run_phase(phase.to_sym)
else
lib/foreman_maintain/package_manager/apt.rb
require 'open3'
module ForemanMaintain::PackageManager
class Apt < Base
def installed?(packages)
......
apt_action('upgrade --dry-run', packages, :with_status => with_status)
end
def update_available?(package)
output, status = Open3.capture2("apt-get install #{package} --dry-run")
status.success? && output.include?("Inst #{package}")
end
def list_installed_packages(queryfm = '${binary:Package}-${VERSION}\n')
# The queryfm should only include valid tag(s) as per `dpkg-query` man page.
# If any special formatting is required with querytag then it should be provided with tag i.e,
test/lib/cli/upgrade_command_test.rb
require 'test_helper'
require 'foreman_maintain/cli'
module ForemanMaintain
......
ForemanMaintain.detector.refresh
UpgradeRunner.clear_current_target_version
end
def foreman_maintain_update_available
PackageManagerTestHelper.mock_package_manager
FakePackageManager.any_instance.stubs(:update).with('rubygem-foreman_maintain',
:assumeyes => true).returns(true)
# rubocop:disable Layout/LineLength
FakePackageManager.any_instance.stubs(:update_available?).with('rubygem-foreman_maintain').returns(true)
# rubocop:enable Layout/LineLength
end
def foreman_maintain_update_unavailable
PackageManagerTestHelper.mock_package_manager
# rubocop:disable Layout/LineLength
FakePackageManager.any_instance.stubs(:update_available?).with('rubygem-foreman_maintain').returns(false)
# rubocop:enable Layout/LineLength
end
let :command do
%w[upgrade]
end
......
describe 'list-versions' do
let :command do
%w[upgrade list-versions --disable-self-upgrade]
%w[upgrade list-versions]
end
it 'run self upgrade if upgrade available for foreman-maintain' do
foreman_maintain_update_available
assert_cmd <<-OUTPUT.strip_heredoc
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 'lists the available versions' do
it 'inform if no updates available for foreman-maintain' do
foreman_maintain_update_unavailable
assert_cmd <<-OUTPUT.strip_heredoc
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
1.15
OUTPUT
end
it 'skip self upgrade and lists the available versions' do
command << '--disable-self-upgrade'
assert_cmd <<-OUTPUT.strip_heredoc
1.15
OUTPUT
......
describe 'check' do
let :command do
%w[upgrade check --disable-self-upgrade]
%w[upgrade check]
end
it 'run self upgrade if upgrade available for foreman-maintain' do
foreman_maintain_update_available
command << '--target-version=1.15'
assert_cmd <<-OUTPUT.strip_heredoc
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 '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.strip_heredoc
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
OUTPUT
end
it 'runs the upgrade checks for version' do
it 'runs the upgrade checks for version with disable-self-upgrade' do
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.expects(:exit!)
Cli::MainCommand.any_instance.stubs(:exit!)
run_cmd([])
end
......
describe 'run' do
let :command do
%w[upgrade run --disable-self-upgrade]
%w[upgrade run]
end
it 'runs the full upgrade for version' do
it 'run self upgrade if upgrade available for foreman-maintain' do
foreman_maintain_update_available
command << '--target-version=1.15'
assert_cmd <<-OUTPUT.strip_heredoc
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 '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.strip_heredoc
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
OUTPUT
end
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'])
end
it 'remembers the current target version' do
it 'runs the self upgrade when update available for rubygem-foreman_maintain' do
foreman_maintain_update_available
assert_cmd <<-OUTPUT.strip_heredoc
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
UpgradeRunner.current_target_version = '1.15'
run_cmd
assert_cmd(<<-OUTPUT.strip_heredoc, ['--target-version', '1.16'])
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.strip_heredoc
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.strip_heredoc, ['--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.strip_heredoc
--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'
UpgradeRunner.any_instance.expects(:run)
Cli::MainCommand.any_instance.expects(:exit!)
run_cmd
assert_cmd(<<-OUTPUT.strip_heredoc, ['--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
test/lib/support/definitions/features/fake_instance.rb
class Features::FakeInstance < ForemanMaintain::Feature
metadata do
label :instance
confine do
true
end
end
def downstream
false
end
end

Also available in: Unified diff