|
| 1 | +namespace :ovirt do |
| 2 | + desc <<~END_DESC |
| 3 | + Remove all oVirt / RHV compute resources & profiles from Foreman. |
| 4 | + All hosts associated with these compute resources will be disassociated and kept. |
| 5 | + All host groups with oVirt compute resources & profiles will have their compute resource and compute profile removed. |
| 6 | +
|
| 7 | + Examples: |
| 8 | + # foreman-rake ovirt:drop |
| 9 | + END_DESC |
| 10 | + |
| 11 | + task :drop => :environment do |
| 12 | + compute_resources = ComputeResource.unscoped.where(type: 'Foreman::Model::Ovirt') |
| 13 | + |
| 14 | + nothing_to_do if compute_resources.empty? |
| 15 | + |
| 16 | + ask_user(compute_resources) unless Foreman::Cast.to_bool(ENV['OVIRT_ASSUME_YES']) |
| 17 | + |
| 18 | + puts 'Starting Ovirt integration drop ...' |
| 19 | + compute_resources.each do |cr| |
| 20 | + puts "Processing data for '#{cr.name}' compute resource." |
| 21 | + |
| 22 | + cr.hosts.unscoped.each do |host| |
| 23 | + puts "Disassociating host '#{host.name}' from the compute resource." |
| 24 | + host.disassociate! |
| 25 | + end |
| 26 | + |
| 27 | + puts "Removing compute resources and compute profiles from host groups." |
| 28 | + cr.hostgroups.unscoped.update_all(compute_resource_id: nil, compute_profile_id: nil) |
| 29 | + |
| 30 | + puts "Removing '#{cr.name}' compute resource." |
| 31 | + cr.destroy |
| 32 | + end |
| 33 | + |
| 34 | + the_end |
| 35 | + end |
| 36 | + |
| 37 | + def ask_user(compute_resources) |
| 38 | + puts "!!! WARNING !!! This operation is irreversible." |
| 39 | + puts "Affected compute resource(s): #{compute_resources.map(&:name).join(', ')}." |
| 40 | + puts "" |
| 41 | + puts " - This task will remove all oVirt / RHV compute resources from Foreman." |
| 42 | + puts " - All hosts associated with these compute resources will be disassociated and kept." |
| 43 | + puts " - All host groups with compute resources will have their compute resource and compute profile removed." |
| 44 | + puts " - Compute profiles associated with the compute resources will be removed." |
| 45 | + puts "" |
| 46 | + puts "Are you sure you want to continue? (yes/no)" |
| 47 | + |
| 48 | + answer = $stdin.gets.chomp.downcase |
| 49 | + |
| 50 | + unless answer == "yes" |
| 51 | + puts "Aborting task." |
| 52 | + exit 1 |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def nothing_to_do |
| 57 | + puts "No 'Foreman::Model::Ovirt' compute resources found." |
| 58 | + puts "Nothing to do, exiting." |
| 59 | + exit 0 |
| 60 | + end |
| 61 | + |
| 62 | + def the_end |
| 63 | + puts 'oVirt cleanup was successfully completed.' |
| 64 | + puts 'Exiting.' |
| 65 | + exit 0 |
| 66 | + end |
| 67 | +end |
0 commit comments