Skip to content

Commit

Permalink
Add force deletion option to cluster removal process
Browse files Browse the repository at this point in the history
  • Loading branch information
vitobotta committed Jan 29, 2025
1 parent 819f338 commit b9ab220
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
33 changes: 18 additions & 15 deletions src/cluster/delete.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ class Cluster::Delete
private getter settings : Configuration::Main do
configuration.settings
end
private getter force : Bool = false
private property instance_deletors : Array(Hetzner::Instance::Delete) = [] of Hetzner::Instance::Delete

def initialize(@configuration)
def initialize(@configuration, @force)
end

def run
print "Please enter the cluster name to confirm that you want to delete it: "
input = gets

if input.try(&.strip) != settings.cluster_name
puts
puts "Cluster name '#{input.try(&.strip)}' does not match expected '#{settings.cluster_name}'. Aborting deletion.".colorize(:red)
puts
exit 1
end
unless force
print "Please enter the cluster name to confirm that you want to delete it: "
input = gets

if input.try(&.strip) != settings.cluster_name
puts
puts "Cluster name '#{input.try(&.strip)}' does not match expected '#{settings.cluster_name}'. Aborting deletion.".colorize(:red)
puts
exit 1
end

if settings.protect_against_deletion
puts
puts "WARNING: Cluster cannot be deleted. If you are sure about this, disable the protection by setting `protect_against_deletion` to `false` in the config file. Aborting deletion.".colorize(:red)
puts
exit 1
if settings.protect_against_deletion
puts
puts "WARNING: Cluster cannot be deleted. If you are sure about this, disable the protection by setting `protect_against_deletion` to `false` in the config file. Aborting deletion.".colorize(:red)
puts
exit 1
end
end

delete_resources
Expand Down
8 changes: 7 additions & 1 deletion src/hetzner-k3s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ module Hetzner::K3s
short: "c",
required: true

define_flag force : Bool,
description: "Force deletion of the cluster without any prompts",
long: "force",
required: false,
default: false

def run
configuration = Configuration::Loader.new(flags.configuration_file_path, nil)
configuration.validate(:delete)

Cluster::Delete.new(configuration: configuration).run
Cluster::Delete.new(configuration: configuration, force: flags.force).run
end
end

Expand Down

0 comments on commit b9ab220

Please sign in to comment.