Skip to content

Commit 0ea0efd

Browse files
committed
Fixes #38267 - rake ovirt:drop
Rake task for removal of Ovirt compute resources and its data - Disassociate all hosts from the CR - Update host groups - Remove Compute resource & its profiles
1 parent 8b00ca2 commit 0ea0efd

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

lib/tasks/ovirt.rake

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
namespace :ovirt do
2+
desc <<~END_DESC
3+
Remove all oVirt / RHV compute resources & profiles from Foreman.
4+
Hosts associated with these compute resources will be disassociated and kept.
5+
Host groups with oVirt compute resources & profiles will have their compute resource and compute profile removed.
6+
Compute profiles, images, and other resources will be removed.
7+
8+
Use OVIRT_PRETEND=true to simulate the operation without making any changes.
9+
Use OVIRT_ASSUME_YES=true to skip the confirmation prompt.
10+
11+
Examples:
12+
# foreman-rake ovirt:drop
13+
# OVIRT_ASSUME_YES=true foreman-rake ovirt:drop
14+
# OVIRT_PRETEND=true foreman-rake ovirt:drop
15+
END_DESC
16+
17+
task :drop => :environment do
18+
compute_resources = ComputeResource.unscoped.where(type: 'Foreman::Model::Ovirt')
19+
20+
nothing_to_do if compute_resources.empty?
21+
22+
pretend_mode = Foreman::Cast.to_bool(ENV['OVIRT_PRETEND'])
23+
assume_yes = Foreman::Cast.to_bool(ENV['OVIRT_ASSUME_YES'])
24+
25+
if pretend_mode
26+
User.as_anonymous_admin do
27+
run_in_pretend_mode(compute_resources)
28+
end
29+
end
30+
31+
ask_user(compute_resources) unless assume_yes
32+
33+
User.as_anonymous_admin do
34+
run_for_real(compute_resources)
35+
end
36+
37+
the_end
38+
end
39+
40+
def run_in_pretend_mode(compute_resources)
41+
puts ''
42+
puts "Running in pretend mode. Following actions would be performed:"
43+
44+
compute_resources.each do |cr|
45+
puts "Remove '#{cr.name} [#{cr.id}]' compute resource"
46+
47+
puts "\nDissacociate the following hosts:"
48+
cr.hosts.each do |host|
49+
puts "- #{host.name} [#{host.id}]"
50+
end
51+
52+
puts "\nUpdate the following host groups:"
53+
cr.hostgroups.each do |hg|
54+
puts "- '#{hg.name}' [#{hg.id}]"
55+
end
56+
57+
puts "\nRemove the following compute profiles:"
58+
cr.compute_profiles.each do |cp|
59+
puts "- '#{cp.name}' [#{cp.id}]"
60+
end
61+
62+
puts "\nRemove the following images:"
63+
cr.images.each do |image|
64+
puts "- '#{image.name}' [#{image.id}]"
65+
end
66+
end
67+
68+
puts ""
69+
exit 0
70+
end
71+
72+
def run_for_real(compute_resources)
73+
puts 'Starting Ovirt drop ...'
74+
75+
compute_resources.each do |cr|
76+
puts "Processing data for '#{cr.name}' compute resource."
77+
78+
cr.hosts.each do |host|
79+
puts "Disassociating host '#{host.name}' from the compute resource."
80+
host.disassociate!
81+
end
82+
83+
puts "Removing compute resources and compute profiles from host groups."
84+
cr.hostgroups.update_all(compute_resource_id: nil, compute_profile_id: nil)
85+
86+
puts "Removing '#{cr.name}' compute resource."
87+
cr.destroy
88+
end
89+
end
90+
91+
def ask_user(compute_resources)
92+
puts "!!! WARNING !!! This operation is irreversible."
93+
puts "Affected compute resource(s): #{compute_resources.map(&:name).join(', ')}."
94+
puts ""
95+
puts " - This task will remove all oVirt / RHV compute resources from Foreman."
96+
puts " - All hosts associated with these compute resources will be disassociated and kept."
97+
puts " - All host groups with compute resources will have their compute resource and compute profile removed."
98+
puts " - Compute profiles, images and other resources associated with the compute resources will be removed."
99+
puts ""
100+
puts "Are you sure you want to continue? (yes/no)"
101+
102+
answer = $stdin.gets.chomp.downcase
103+
104+
unless answer == "yes"
105+
puts "Aborting task."
106+
exit 1
107+
end
108+
end
109+
110+
def nothing_to_do
111+
puts "No 'Foreman::Model::Ovirt' compute resources found."
112+
puts "Nothing to do, exiting."
113+
exit 0
114+
end
115+
116+
def the_end
117+
puts ''
118+
puts 'oVirt cleanup was successfully completed.'
119+
puts 'Exiting.'
120+
exit 0
121+
end
122+
end

0 commit comments

Comments
 (0)