adding purge unconfigured method

This commit is contained in:
Samuel Beaulieu 2021-12-07 13:08:21 -06:00
parent c85fed771f
commit 761c96fd38
No known key found for this signature in database
GPG key ID: 12030F74136D0F34
2 changed files with 39 additions and 20 deletions

View file

@ -444,18 +444,44 @@ module Vmpooler
filter[zone(pool)] = [] if filter[zone(pool)].nil?
filter[zone(pool)] << "(labels.pool != #{pool} OR -labels.pool:*)"
end
vm_to_purge = []
filter.keys.each do |zone|
#VMs
instance_list = connection.list_instances(project, zone, filter: filter[zone].join(" AND "))
next if instance_list.items.nil?
result_list = []
unless instance_list.items.nil?
instance_list.items.each do |vm|
next if !vm.labels.nil? && whitelist&.include?(vm.labels['pool'])
next if whitelist&.include?("") && vm.labels.nil?
vm_to_purge << vm.name
result = connection.delete_instance(project, zone, vm.name)
result_list << result
end
end
#now check they are done
result_list.each do |result|
wait_for_operation(project, pool_name, result, connection)
end
#Disks
disks_list = connection.list_disks(project, zone, filter: filter[zone].join(" AND "))
unless disks_list.items.nil?
disks_list.items.each do |disk|
next if !disk.labels.nil? && whitelist&.include?(disk.labels['pool'])
next if whitelist&.include?("") && disk.labels.nil?
result = connection.delete_disk(project, zone, disk.name)
end
end
#Snapshots
snapshot_list = connection.list_snapshots(project, filter: filter[zone].join(" AND "))
unless snapshot_list.items.nil?
snapshot_list.items.each do |sn|
next if !sn.labels.nil? && whitelist&.include?(sn.labels['pool'])
next if whitelist&.include?("") && sn.labels.nil?
result = connection.delete_snapshot(project, sn.name)
end
end
end
puts vm_to_purge
end
end

View file

@ -13,14 +13,8 @@
# (optional: will default to it's parent :key: name eg. 'gce')
#
# - purge_unconfigured_resources
# Enable purging of VMs (folders or resources) detected
# vSphere: within the base folder path that are not configured for the provider.
# Only a single layer of folders and their child VMs are evaluated from detected base folder paths
# Nested child folders will not be destroyed.
# A base folder path for 'vmpooler/redhat-7' would be 'vmpooler'
#
# gce: in the project but without a pool label, or a pool label for an nonexistant pool
#
# Enable purging of VMs detected
# For GCE: by default any VM in the project but without a "pool" label, or a "pool" label with the value for an unconfigured pool
# An optional allowlist can be provided to ignore purging certain folders or pool labels
#
# Setting this on the provider will enable purging for the provider
@ -28,12 +22,11 @@
# (optional; default: false)
#
# - recources_allowlist
# vSphere: Specify folders that are within the base folder path, not in the configuration, and should not be destroyed
# To exclude 'vmpooler/myfolder' from being destroyed when the base path is 'vmpooler' you would specify 'myfolder' in the allowlist
# gce: Specify pool labels that should be ignored when purging VMs, for pools that are not configured. For example if the pool label is
# set to 'donotdelete' and there is no pool with that name configured, adding that value to the allowlist would not purge the VM.
# For GCE: Specify pool labels that should be ignored when purging VMs, for pools that are not configured. For example if the label is
# set to 'pool=donotdelete' and there is no pool with that name configured, adding "donotdelete" to the allowlist would not purge the VM.
# adding "" (empty string) to the allowlist has a special meaning whereas VMs that do not have the "pool" label are also not purged.
# This option is only evaluated when 'purge_unconfigured_folders' is enabled
# Expects an array of strings specifying the allowlisted folders by name
# Expects an array of strings specifying the allowlisted labels by name
# (optional; default: nil)
#
# If you want to support more than one provider with different parameters (server, username or passwords) you have to specify the