Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #38267 - rake ovirt:drop #10469

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions lib/tasks/ovirt.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
namespace :ovirt do
desc <<~END_DESC
Remove all oVirt / RHV compute resources & profiles from Foreman.
Hosts associated with these compute resources will be disassociated and kept.
Host groups with oVirt compute resources & profiles will have their compute resource and compute profile removed.
Compute profiles, images, and other resources will be removed.

Use OVIRT_PRETEND=true to simulate the operation without making any changes.
Use OVIRT_ASSUME_YES=true to skip the confirmation prompt.

Examples:
# foreman-rake ovirt:drop
# OVIRT_ASSUME_YES=true foreman-rake ovirt:drop
# OVIRT_PRETEND=true foreman-rake ovirt:drop
END_DESC

task :drop => :environment do
compute_resources = ComputeResource.unscoped.where(type: 'Foreman::Model::Ovirt')

nothing_to_do if compute_resources.empty?

pretend_mode = Foreman::Cast.to_bool(ENV['OVIRT_PRETEND'])
assume_yes = Foreman::Cast.to_bool(ENV['OVIRT_ASSUME_YES'])

if pretend_mode
User.as_anonymous_admin do
run_in_pretend_mode(compute_resources)
end
end

ask_user(compute_resources) unless assume_yes

User.as_anonymous_admin do
run_for_real(compute_resources)
end

the_end
end

def run_in_pretend_mode(compute_resources)
puts ''
puts "Running in pretend mode. Following actions would be performed:"

compute_resources.each do |cr|
puts "Remove '#{cr.name} [#{cr.id}]' compute resource"

puts "\nDissacociate the following hosts:"
cr.hosts.each do |host|
puts "- #{host.name} [#{host.id}]"
end

puts "\nUpdate the following host groups:"
cr.hostgroups.each do |hg|
puts "- '#{hg.name}' [#{hg.id}]"
end

puts "\nRemove the following compute profiles:"
cr.compute_profiles.each do |cp|
puts "- '#{cp.name}' [#{cp.id}]"
end

puts "\nRemove the following images:"
cr.images.each do |image|
puts "- '#{image.name}' [#{image.id}]"
end
end

puts ""
exit 0
end

def run_for_real(compute_resources)
puts 'Starting Ovirt drop ...'

compute_resources.each do |cr|
puts "Processing data for '#{cr.name}' compute resource."

cr.hosts.each do |host|
puts "Disassociating host '#{host.name}' from the compute resource."
host.disassociate!
end

puts "Removing compute resources and compute profiles from host groups."
cr.hostgroups.update_all(compute_resource_id: nil, compute_profile_id: nil)

puts "Removing '#{cr.name}' compute resource."
cr.destroy
end
end

def ask_user(compute_resources)
puts "!!! WARNING !!! This operation is irreversible."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you can link them back up so I'm not sure it's really that irreversible. Just quite a bit of work.

puts "Affected compute resource(s): #{compute_resources.map(&:name).join(', ')}."
puts ""
puts " - This task will remove all oVirt / RHV compute resources from Foreman."
puts " - All hosts associated with these compute resources will be disassociated and kept."
puts " - All host groups with compute resources will have their compute resource and compute profile removed."
puts " - Compute profiles, images and other resources associated with the compute resources will be removed."
puts ""
puts "Are you sure you want to continue? (yes/no)"

answer = $stdin.gets.chomp.downcase

unless answer == "yes"
puts "Aborting task."
exit 1
end
end

def nothing_to_do
puts "No 'Foreman::Model::Ovirt' compute resources found."
puts "Nothing to do, exiting."
exit 0
end

def the_end
puts ''
puts 'oVirt cleanup was successfully completed.'
puts 'Exiting.'
exit 0
end
end
Loading