Skip to content

Commit

Permalink
Remove accessory env file from host when accessory is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvogt committed Jun 20, 2024
1 parent 4697f89 commit 77b9ffb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def logs(name)
end
end

desc "remove [NAME]", "Remove accessory container, image and data directory from host (use NAME=all to remove all accessories)"
desc "remove [NAME]", "Remove accessory container, image, data directory and env file from host (use NAME=all to remove all accessories)"
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
def remove(name)
confirming "This will remove all containers, images and data directories for #{name}. Are you sure?" do
Expand Down Expand Up @@ -222,6 +222,17 @@ def remove_service_directory(name)
end
end

desc "remove_env_file [NAME]", "Remove accessory env file from host", hide: true
def remove_env_file(name)
with_lock do
with_accessory(name) do |accessory, hosts|
on(hosts) do
execute *accessory.remove_env_file
end
end
end
end

private
def with_accessory(name)
if KAMAL.config.accessory(name)
Expand Down Expand Up @@ -254,6 +265,7 @@ def remove_accessory(name)
remove_container(name)
remove_image(name)
remove_service_directory(name)
remove_env_file(name)
end
end
end
7 changes: 7 additions & 0 deletions test/cli/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class CliAccessoryTest < CliTestCase
Kamal::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_image).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_service_directory).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_env_file).with("mysql")

run_command("remove", "mysql", "-y")
end
Expand All @@ -165,10 +166,12 @@ class CliAccessoryTest < CliTestCase
Kamal::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_image).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_service_directory).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_env_file).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:stop).with("redis")
Kamal::Cli::Accessory.any_instance.expects(:remove_container).with("redis")
Kamal::Cli::Accessory.any_instance.expects(:remove_image).with("redis")
Kamal::Cli::Accessory.any_instance.expects(:remove_service_directory).with("redis")
Kamal::Cli::Accessory.any_instance.expects(:remove_env_file).with("redis")

run_command("remove", "all", "-y")
end
Expand All @@ -185,6 +188,10 @@ class CliAccessoryTest < CliTestCase
assert_match "rm -rf app-mysql", run_command("remove_service_directory", "mysql")
end

test "remove_env_file" do
assert_match "rm -f .kamal/env/accessories/app-mysql.env", run_command("remove_env_file", "mysql")
end

test "hosts param respected" do
Kamal::Cli::Accessory.any_instance.expects(:directories).with("redis")
Kamal::Cli::Accessory.any_instance.expects(:upload).with("redis")
Expand Down

0 comments on commit 77b9ffb

Please sign in to comment.