Skip to content

Commit

Permalink
Fixes #37175: Add dedicated update command
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Feb 16, 2024
1 parent df2e834 commit 9293128
Show file tree
Hide file tree
Showing 13 changed files with 508 additions and 7 deletions.
4 changes: 3 additions & 1 deletion definitions/checks/repositories/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Validate < ForemanMaintain::Check

param :version,
'Version for which repositories needs to be validated',
:required => true
:required => false

manual_detection
end
Expand All @@ -21,6 +21,8 @@ def run
if feature(:instance).downstream.subscribed_using_activation_key?
skip 'Your system is subscribed using custom activation key'
else
@version ||= package_manager.package_version(feature(:instance).downstream.package_name)

with_spinner("Validating availability of repositories for #{@version}") do |spinner|
find_absent_repos(spinner)
end
Expand Down
126 changes: 126 additions & 0 deletions definitions/scenarios/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
module Scenarios::Update
class Abstract < ForemanMaintain::Scenario
def self.update_metadata(&block)
metadata do
tags :update_scenario
instance_eval(&block)
end
end
end

class PreUpdateCheck < Abstract
update_metadata do
description 'Checks before updating'
tags :pre_update_checks
run_strategy :fail_slow
end

# rubocop:disable Metrics/MethodLength
def compose
add_steps(
Checks::Foreman::FactsNames, # if Foreman database present
Checks::ForemanProxy::CheckTftpStorage, # if Satellite with foreman-proxy+tftp
Checks::ForemanProxy::VerifyDhcpConfigSyntax, # if foreman-proxy+dhcp-isc
Checks::ForemanTasks::NotPaused, # if foreman-tasks present
Checks::Puppet::VerifyNoEmptyCacertRequests, # if puppetserver
Checks::ServerPing,
Checks::ServicesUp,
Checks::SystemRegistration,
Checks::CheckHotfixInstalled,
Checks::CheckTmout,
Checks::CheckUpstreamRepository,
Checks::Disk::AvailableSpace,
Checks::Disk::AvailableSpaceCandlepin, # if candlepin
Checks::Foreman::ValidateExternalDbVersion, # if external database
Checks::Foreman::CheckCorruptedRoles,
Checks::Foreman::CheckDuplicatePermissions,
Checks::Foreman::TuningRequirements, # if katello present
Checks::ForemanOpenscap::InvalidReportAssociations, # if foreman-openscap
Checks::ForemanTasks::Invalid::CheckOld, # if foreman-tasks
Checks::ForemanTasks::Invalid::CheckPendingState, # if foreman-tasks
Checks::ForemanTasks::Invalid::CheckPlanningState, # if foreman-tasks
Checks::ForemanTasks::NotRunning, # if foreman-tasks
Checks::NonRhPackages,
Checks::PackageManager::Dnf::ValidateDnfConfig,
Checks::Repositories::CheckNonRhRepository,
Checks::Repositories::Validate
)
end
# rubocop:enable Metrics/MethodLength
end

class PreMigrations < Abstract
update_metadata do
description 'Procedures before migrating'
tags :pre_migrations
end

def compose
add_steps(
Procedures::Packages::Update.new(
:assumeyes => true,
:dnf_options => ['--downloadonly']
),
Procedures::MaintenanceMode::EnableMaintenanceMode,
Procedures::Crond::Stop,
Procedures::SyncPlans::Disable
)
end
end

class Migrations < Abstract
update_metadata do
description 'Migration scripts'
tags :migrations
run_strategy :fail_fast
end

def compose
add_steps(
Procedures::Service::Stop,
Procedures::Packages::Update.new(:assumeyes => true, :clean_cache => false),
Procedures::Installer::Run.new(:assumeyes => true),
Procedures::Installer::UpgradeRakeTask
)
end
end

class PostMigrations < Abstract
update_metadata do
description 'Procedures after migrating'
tags :post_migrations
end

def compose
add_steps(
Procedures::RefreshFeatures,
Procedures::Service::Start,
Procedures::Crond::Start,
Procedures::SyncPlans::Enable,
Procedures::MaintenanceMode::DisableMaintenanceMode
)
end
end

class PostUpdateChecks < Abstract
update_metadata do
description 'Checks after update'
tags :post_update_checks
run_strategy :fail_slow
end

def compose
add_steps(
Checks::Foreman::FactsNames, # if Foreman database present
Checks::ForemanProxy::CheckTftpStorage, # if Satellite with foreman-proxy+tftp
Checks::ForemanProxy::VerifyDhcpConfigSyntax, # if foreman-proxy+dhcp-isc
Checks::ForemanTasks::NotPaused, # if foreman-tasks present
Checks::Puppet::VerifyNoEmptyCacertRequests, # if puppetserver
Checks::ServerPing,
Checks::ServicesUp,
Checks::SystemRegistration,
Procedures::Packages::CheckForReboot
)
end
end
end
2 changes: 2 additions & 0 deletions lib/foreman_maintain/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'foreman_maintain/cli/packages_command'
require 'foreman_maintain/cli/plugin_command'
require 'foreman_maintain/cli/self_upgrade_command'
require 'foreman_maintain/cli/update_command'

module ForemanMaintain
module Cli
Expand All @@ -20,6 +21,7 @@ class MainCommand < Base

subcommand 'health', 'Health related commands', HealthCommand
subcommand 'upgrade', 'Upgrade related commands', UpgradeCommand
subcommand 'update', 'Update related commands', UpdateCommand
subcommand 'service', 'Control applicable services', ServiceCommand
subcommand 'backup', 'Backup server', BackupCommand
subcommand 'restore', 'Restore a backup', RestoreCommand
Expand Down
48 changes: 48 additions & 0 deletions lib/foreman_maintain/cli/update_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'foreman_maintain/update_runner'

module ForemanMaintain
module Cli
class UpdateCommand < Base
def self.disable_self_update_option
option '--disable-self-update', :flag, 'Disable automatic self update',
:default => false
end

def update_runner
update_runner = ForemanMaintain::UpdateRunner.new(
reporter,
:assumeyes => assumeyes?,
:whitelist => whitelist || [],
:force => force?
)
update_runner.load
update_runner
end

subcommand 'check', 'Run pre-update checks before updating' do
interactive_option
disable_self_update_option

def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_update?
update_runner.run_phase(:pre_update_checks)
exit update_runner.exit_code
end
end

subcommand 'run', 'Run an update' do
interactive_option
disable_self_update_option

def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_update?
update_runner.run
update_runner.save
exit update_runner.exit_code
end
end
end
end
end
6 changes: 5 additions & 1 deletion lib/foreman_maintain/executable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ def say(message)
def __run__(execution)
setup_execution_state(execution)
unless skipped?
run
if defined?(self.class.present?)
run if self.class.present?
else
run
end
end
rescue Error::Skip => e
set_skip(e.message)
Expand Down
4 changes: 4 additions & 0 deletions lib/foreman_maintain/package_manager/dnf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def info(name)
dnf_action('module info', name, with_status: true, assumeyes: true)
end

def package_version(package_name)
sys.execute!("rpm -q #{package_name} --qf '%{version}'").strip
end

private

# rubocop:disable Metrics/LineLength, Metrics/ParameterLists
Expand Down
6 changes: 3 additions & 3 deletions lib/foreman_maintain/scenario.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ def before_scenarios
scenarios
end

def add_steps(steps)
steps.each do |step|
def add_steps(*steps)
steps.flatten.each do |step|
self.steps << step.ensure_instance
end
end

def add_step(step)
add_steps([step]) unless step.nil?
add_steps(step) unless step.nil?
end

def add_step_with_context(definition, extra_params = {})
Expand Down
Loading

0 comments on commit 9293128

Please sign in to comment.