Project

General

Profile

« Previous | Next » 

Revision e361f5c2

Added by Ivan Necas almost 7 years ago

Update the satellite upgrade to new code

View differences:

definitions/features/sync_plans.rb
).map { |r| r['id'].to_i }
end
def disabled_plans_count
data[:disabled].length
end
def make_disable(ids)
update_records(ids, false)
end
......
update_records(data[:disabled], true)
end
def load_from_storage(storage)
@data = storage.data.fetch(:sync_plans, :enabled => [], :disabled => [])
end
def save_to_storage(storage)
storage[:sync_plans] = @data
end
private
def update_records(ids, enabled)
......
end
updated_record_ids
ensure
new_data = sync_plan_data(enabled, updated_record_ids)
save_state(new_data)
update_data(enabled, updated_record_ids)
end
def data
upgrade_storage = ForemanMaintain.storage(:upgrade)
@data ||= upgrade_storage.data.fetch(:sync_plans, :enabled => [], :disabled => [])
raise 'Use load_from_storage before accessing the data' unless defined? @data
@data
end
def sync_plan_data(enabled, new_ids)
sync_plan_hash = data
def update_data(enabled, new_ids)
if enabled
sync_plan_hash[:disabled] -= new_ids
sync_plan_hash[:enabled] = new_ids
@data[:disabled] -= new_ids
@data[:enabled] = new_ids
else
sync_plan_hash[:disabled].concat(new_ids)
@data[:disabled].concat(new_ids)
end
sync_plan_hash
end
def save_state(sync_plan_hash = {})
storage = ForemanMaintain.storage(:upgrade)
storage[:sync_plans] = sync_plan_hash
storage.save
end
end
definitions/procedures/sync_plans/disable.rb
private
def disable_all_enabled_sync_plans
feature(:sync_plans).load_from_storage(storage)
with_spinner('disabling sync plans') do |spinner|
ids = feature(:sync_plans).ids_by_status(true)
feature(:sync_plans).make_disable(ids)
spinner.update "Total #{ids.length} sync plans are now disabled."
end
ensure
feature(:sync_plans).save_to_storage(storage)
end
end
end
definitions/procedures/sync_plans/enable.rb
private
def enabled_sync_plans
feature(:sync_plans).load_from_storage(storage)
with_spinner('re-enabling sync plans') do |spinner|
record_ids = feature(:sync_plans).make_enable
spinner.update "Total #{record_ids.length} sync plans are now enabled."
end
ensure
feature(:sync_plans).save_to_storage(storage)
end
end
end
definitions/scenarios/satellite_6_2/migrations.rb
module Scenarios::Satellite_6_2
class Migrations < ForemanMaintain::Scenario
metadata do
description 'migration scripts to Satellite 6.2'
tags :migrations, :satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:label => :installer_upgrade))
end
end
end
definitions/scenarios/satellite_6_2/post_migrations.rb
module Scenarios::Satellite_6_2
class PostMigrations < ForemanMaintain::Scenario
metadata do
description 'procedures after migrating to Satellite 6.2'
tags :post_migrations, :satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:post_migrations))
end
end
end
definitions/scenarios/satellite_6_2/post_upgrade_checks.rb
module Scenarios::Satellite_6_2
class PostUpgradeChecks < ForemanMaintain::Scenario
metadata do
description 'checks after upgrading to Satellite 6.2'
tags :post_upgrade_checks, :satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
run_strategy :fail_slow
end
def compose
add_steps(find_checks(:default))
add_steps(find_checks(:post_upgrade))
end
end
end
definitions/scenarios/satellite_6_2/pre_migrations.rb
module Scenarios::Satellite_6_2
class PreMigrations < ForemanMaintain::Scenario
metadata do
description 'procedures before migrating to Satellite 6.2'
tags :pre_migrations, :satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:pre_migrations))
end
end
end
definitions/scenarios/satellite_6_2/pre_upgrade_checks.rb
module Scenarios::Satellite_6_2
class PreUpgradeCheck < ForemanMaintain::Scenario
metadata do
description 'checks before upgrading to Satellite 6.2'
tags :pre_upgrade_check, :satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
run_strategy :fail_slow
end
def compose
add_steps(find_checks(:default))
add_steps(find_checks(:pre_upgrade))
end
end
end
definitions/scenarios/upgrade_to_satellite_6_2.rb
module Scenarios::Satellite_6_2
class Abstract < ForemanMaintain::Scenario
def self.upgrade_metadata(&block)
metadata do
tags :upgrade_to_satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
instance_eval(&block)
end
end
end
class PreUpgradeCheck < Abstract
upgrade_metadata do
description 'checks before upgrading to Satellite 6.2'
tags :pre_upgrade_checks
run_strategy :fail_slow
end
def compose
add_steps(find_checks(:default))
add_steps(find_checks(:pre_upgrade))
end
end
class PreMigrations < ForemanMaintain::Scenario
metadata do
description 'procedures before migrating to Satellite 6.2'
tags :pre_migrations, :upgrade_to_satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:pre_migrations))
end
end
class Migrations < ForemanMaintain::Scenario
metadata do
description 'migration scripts to Satellite 6.2'
tags :migrations, :upgrade_to_satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:label => :installer_upgrade))
end
end
class PostMigrations < ForemanMaintain::Scenario
metadata do
description 'procedures after migrating to Satellite 6.2'
tags :post_migrations, :upgrade_to_satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
end
def compose
add_steps(find_procedures(:post_migrations))
end
end
class PostUpgradeChecks < ForemanMaintain::Scenario
metadata do
description 'checks after upgrading to Satellite 6.2'
tags :post_upgrade_checks, :upgrade_to_satellite_6_2
confine do
feature(:downstream) && feature(:downstream).current_minor_version == '6.1'
end
run_strategy :fail_slow
end
def compose
add_steps(find_checks(:default))
add_steps(find_checks(:post_upgrade))
end
end
end
ForemanMaintain::UpgradeRunner.register_version('6.2', :upgrade_to_satellite_6_2)
lib/foreman_maintain/cli/upgrade_command.rb
module ForemanMaintain
module Cli
class UpgradeCommand < Base
def tags_to_versions
{ :satellite_6_0_z => '6.0.z',
:satellite_6_1 => '6.1',
:satellite_6_1_z => '6.1.z',
:satellite_6_2 => '6.2',
:satellite_6_2_z => '6.2.z',
:satellite_6_3 => '6.3' }
end
# We search for scenarios available for the system and determine
# user-friendly version numbers for it.
# This method returns a hash of mapping the versions to scenarios to run
# The tag is determining which kind of scenario we're searching for
# (such as pre_upgrade_check)
def available_target_versions(tag)
conditions = { :tags => [tag] }
find_scenarios(conditions).inject({}) do |hash, scenario|
# find tag that represent the version upgrade
version_tag = scenario.tags.find { |t| tags_to_versions.key?(t) }
if version_tag
hash.update(tags_to_versions[version_tag] => scenario)
else
hash
end
def validate_target_version!
unless UpgradeRunner.available_targets.include?(target_version)
puts "The specified version #{target_version} is unavailable"
puts 'Possible target versions are:'
print_versions(UpgradeRunner.available_targets)
exit 1
end
end
def find_scenario(tag)
available_target_versions(tag)[target_version]
def upgrade_runner
validate_target_version!
ForemanMaintain::UpgradeRunner.new(target_version, reporter,
:assumeyes => assumeyes?,
:whitelist => whitelist || [])
end
def print_versions(target_versions)
target_versions.keys.sort.each { |version| puts version }
target_versions.sort.each { |version| puts version }
end
subcommand 'list-versions', 'List versions this system is upgradable to' do
def execute
print_versions(available_target_versions(:pre_upgrade_checks))
print_versions(UpgradeRunner.available_targets)
end
end
......
interactive_option
def execute
versions_to_scenarios = available_target_versions(:pre_upgrade_checks)
scenario = versions_to_scenarios[target_version]
if scenario
run_scenario(scenario)
else
puts "The specified version #{target_version} is unavailable"
puts 'Possible target versions are:'
print_versions(versions_to_scenarios)
end
upgrade_runner.run_phase(:pre_upgrade_checks)
exit upgrade_runner.exit_code
end
end
......
interactive_option
def execute
scenarios = [find_scenario(:pre_upgrade_checks),
find_scenario(:pre_migrations),
find_scenario(:migrations),
find_scenario(:post_migrations),
find_scenario(:post_upgrade_checks)].compact
if scenarios.empty?
puts "The specified version #{target_version} is unavailable"
puts 'Possible target versions are:'
print_versions(available_target_versions(:pre_upgrade_checks))
else
run_scenario(scenarios)
end
upgrade_runner.run
exit upgrade_runner.exit_code
end
end
end
lib/foreman_maintain/executable.rb
def_delegators :execution,
:success?, :fail?, :warning?, :output,
:assumeyes?, :whitelisted?,
:execution, :puts, :print, :with_spinner, :ask
:execution, :puts, :print, :with_spinner, :ask, :storage
attr_accessor :associated_feature
lib/foreman_maintain/runner.rb
end
end
def storage
ForemanMaintain.storage(:default)
end
private
def run_steps(scenario, steps)
@steps_to_run = ForemanMaintain::DependencyGraph.sort(steps)
while !@quit && !@steps_to_run.empty?
step = @steps_to_run.shift
@reporter.puts('Rerunning the check after fix procedure') if rerun_check?(step)
execution = Execution.new(step, @reporter, :whitelisted => whitelisted_step?(step))
execution.run
execution = run_step(@steps_to_run.shift)
post_step_decisions(scenario, execution)
end
end
def run_step(step)
@reporter.puts('Rerunning the check after fix procedure') if rerun_check?(step)
execution = Execution.new(step, @reporter,
:whitelisted => whitelisted_step?(step),
:storage => storage)
execution.run
execution
ensure
storage.save
end
def post_step_decisions(scenario, execution)
step = execution.step
next_steps_decision = ask_about_offered_steps(step)
lib/foreman_maintain/runner/execution.rb
attr_reader :reporter
def initialize(step, reporter, options = {})
options.validate_options!(:whitelisted)
options.validate_options!(:whitelisted, :storage)
@step = step
@reporter = reporter
@status = :pending
@output = ''
@whitelisted = options[:whitelisted]
@storage = options[:storage]
end
def name
......
@status == :warning
end
# yaml storage to preserve key/value pairs between runs.
def storage
@storage || ForemanMaintain.storage(:default)
end
def run
@status = :running
@reporter.before_execution_starts(self)
lib/foreman_maintain/upgrade_runner.rb
end
def storage
# TODO: determine from upgrade scenario
ForemanMaintain.storage(:upgrade)
ForemanMaintain.storage("upgrade_#{version}")
end
# serializes the state of the run to storage
......
load_from_hash(storage[:serialized])
end
private
def run_phase(phase)
next_scenario = scenario(phase)
return if next_scenario.nil? || next_scenario.steps.empty?
......
@ask_to_confirm_upgrade = phase == :pre_upgrade_checks
end
private
def to_hash
ret = { :phase => phase, :scenarios => {} }
@scenario_cache.each do |key, scenario|
test/definitions/scenarios/upgrade_satellite_6_2_test.rb
end
let :scenario do
assert_scenario(:tags => [:pre_upgrade_check, :satellite_6_2])
assert_scenario(:tags => [:pre_upgrade_checks, :upgrade_to_satellite_6_2])
end
it 'is valid only for 6.1.x versions' do
assert_scenario(:tags => [:pre_upgrade_check, :satellite_6_2])
assert_scenario(:tags => [:pre_upgrade_checks, :upgrade_to_satellite_6_2])
assume_feature_present(:downstream) do |feature_class|
feature_class.any_instance.stubs(:current_version => version('6.0.8'))
end
detector.refresh
refute_scenario(:tags => [:pre_upgrade_check, :satellite_6_2])
refute_scenario(:tags => [:pre_upgrade_checks, :upgrade_to_satellite_6_2])
assume_feature_present(:downstream) do |feature_class|
feature_class.any_instance.stubs(:current_version => version('6.2.1'))
end
detector.refresh
refute_scenario(:tags => [:pre_upgrade_check, :satellite_6_2])
refute_scenario(:tags => [:pre_upgrade_checks, :upgrade_to_satellite_6_2])
assume_feature_absent(:downstream)
detector.refresh
refute_scenario(:tags => [:pre_upgrade_check, :satellite_6_2])
refute_scenario(:tags => [:pre_upgrade_checks, :upgrade_to_satellite_6_2])
end
it 'composes the pre upgrade checks for migration from satellite 6.1.x to 6.2' do

Also available in: Unified diff