Project

General

Profile

Bug #16499 » clean_update_repos.rb

Ivan Necas, 09/12/2016 03:22 AM

 
# to run:
#
# foreman-rake console < clean_update_repos.rb
#

require 'set'
def remove_from_planning(plan_step, removed_step, execution_plan)
if plan_step.children.delete(removed_step.id)
plan_step.save
else
plan_step.planned_steps(execution_plan).each do |sub_plan_step|
remove_from_planning(sub_plan_step, removed_step, execution_plan)
end
end
end


def plan_action_ids(step, execution_plan)
step.planned_steps(execution_plan).inject([step.action_id]) do |action_ids, sub_plan_step|
action_ids.concat(plan_action_ids(sub_plan_step, execution_plan))
end
end

def all_sub_step_ids(step, execution_plan)
step.planned_steps(execution_plan).inject([step.id]) do |step_ids, sub_step|
step_ids.concat(all_sub_step_ids(sub_step, execution_plan))
end
end

def remove_from_flow(flow, step_ids)
if flow.respond_to?(:flows)
flow.flows.delete_if do |sub_flow|
if sub_flow.respond_to?(:flows)
remove_from_flow(sub_flow, step_ids)
sub_flow.flows.empty?
else
step_ids.include?(sub_flow.step_id)
end
end
end
end

def delete_steps_actions(execution_plan, step_ids, action_ids)
adapter = ForemanTasks.dynflow.world.persistence.adapter
action_ids.each_slice(1000) do |ids|
adapter.send(:table, :step).where(execution_plan_uuid: execution_plan.id, action_id: ids).delete
end
step_ids.each_slice(1000) do |ids|
adapter.send(:table, :step).where(execution_plan_uuid: execution_plan.id, id: ids).delete
end
action_ids.each_slice(1000) do |ids|
adapter.send(:table, :action).where(execution_plan_uuid: execution_plan.id, id: ids).delete
end
end

def clean_content_view_version(cvv)
return unless cvv.last_event
task = cvv.last_event.task
execution_plan = task.execution_plan
action_ids_to_delete = Set.new

plan_steps_to_delete = execution_plan.steps.values.find_all do |step|
step.is_a?(Dynflow::ExecutionPlan::Steps::PlanStep) &&
step.action_class.name == "Actions::Pulp::Repos::Update"
end

all_step_ids_to_delete = []

plan_steps_to_delete.each do |plan_step|
plan_action_ids(plan_step, execution_plan).each do |action_id|
action_ids_to_delete << action_id
end
remove_from_planning(execution_plan.root_plan_step, plan_step, execution_plan)
all_step_ids_to_delete.concat(all_sub_step_ids(plan_step, execution_plan))
end

step_ids_to_delete = Set.new(execution_plan.steps.values.find_all do |step|
action_ids_to_delete.include?(step.action_id)
end.map(&:id))

remove_from_flow(execution_plan.run_flow, step_ids_to_delete)
remove_from_flow(execution_plan.finalize_flow, step_ids_to_delete)

all_step_ids_to_delete.concat(step_ids_to_delete.to_a)

all_step_ids_to_delete.each do |step_id|
execution_plan.steps.delete(step_id)
end

execution_plan.save
delete_steps_actions(execution_plan, all_step_ids_to_delete, action_ids_to_delete)
puts "Content view version #{cvv.to_s} cleaned}"
end

cvvs = Katello::ContentView.all.map { |cv| cv.content_view_versions }.flatten
cvvs.each do |cvv|
clean_content_view_version(cvv)
end;"Finished"
    (1-1/1)