-
Notifications
You must be signed in to change notification settings - Fork 999
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
stejskalleos
wants to merge
1
commit into
theforeman:develop
Choose a base branch
from
stejskalleos:ls/ovirt-rake
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
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)" | ||
stejskalleos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.