|
| 1 | +namespace :ovirt do |
| 2 | + desc <<~END_DESC |
| 3 | + Remove all Ovirt / RHEV 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 |
| 17 | + |
| 18 | + puts 'Starting Ovirt integration drop ...' |
| 19 | + compute_resources.each do |cr| |
| 20 | + hosts = cr.hosts.unscoped |
| 21 | + host_groups = cr.hostgroups.unscoped |
| 22 | + |
| 23 | + hosts.each do |host| |
| 24 | + puts "Disassociating host '#{host.name}' from the compute resource" |
| 25 | + host.disassociate! |
| 26 | + end |
| 27 | + |
| 28 | + puts "Removing Compute Resource and Compute Profile from host groups." |
| 29 | + host_groups.update_all(compute_resource_id: nil, compute_profile_id: nil) |
| 30 | + |
| 31 | + puts "Removing compute resource '#{cr.name}'" |
| 32 | + cr.destroy |
| 33 | + end |
| 34 | + |
| 35 | + the_end |
| 36 | + end |
| 37 | + |
| 38 | + def ask_user |
| 39 | + puts "!!! WARNING !!! This operation is irreversible." |
| 40 | + puts " - This task will remove all Ovirt / RHEV compute resources from Foreman." |
| 41 | + puts " - All hosts associated with these compute resources will be disassociated and kept." |
| 42 | + puts " - All host groups with compute resources will have their compute resource and compute profile removed." |
| 43 | + puts " - Compute profiles associated with the compute resources will be removed.." |
| 44 | + puts "" |
| 45 | + puts "Are you sure you want to continue? (yes/no)" |
| 46 | + |
| 47 | + answer = $stdin.gets.chomp.downcase |
| 48 | + |
| 49 | + unless answer == "yes" |
| 50 | + puts "Aborting task." |
| 51 | + exit 1 |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + def nothing_to_do |
| 56 | + puts "No 'Foreman::Model::Ovirt' compute resources found." |
| 57 | + puts "Nothing to do, exiting." |
| 58 | + exit 0 |
| 59 | + end |
| 60 | + |
| 61 | + def the_end |
| 62 | + puts 'Ovirt cleanup was successfully completed.' |
| 63 | + puts 'Exiting.' |
| 64 | + exit 0 |
| 65 | + end |
| 66 | +end |
0 commit comments