From 761c96fd3829674a6762ddaa74a873f98ee7d4bf Mon Sep 17 00:00:00 2001 From: Samuel Beaulieu Date: Tue, 7 Dec 2021 13:08:21 -0600 Subject: [PATCH] adding purge unconfigured method --- lib/vmpooler/providers/gce.rb | 40 +++++++++++++++++++++++++++++------ vmpooler.yaml.example | 19 ++++++----------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/vmpooler/providers/gce.rb b/lib/vmpooler/providers/gce.rb index df9589e..8741ac0 100644 --- a/lib/vmpooler/providers/gce.rb +++ b/lib/vmpooler/providers/gce.rb @@ -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? - 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_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? + 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 diff --git a/vmpooler.yaml.example b/vmpooler.yaml.example index e0fe749..0f7cbb5 100644 --- a/vmpooler.yaml.example +++ b/vmpooler.yaml.example @@ -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