From 8e2184d65e2bcbbf092699aae024083eeabfc850 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Wed, 6 Mar 2024 14:59:26 +0000 Subject: [PATCH] Ensure `kamal remove` completes without setup If `kamal setup` has not run or errored out part way through, `kamal remove` should still complete. Fixes: https://github.com/basecamp/kamal/issues/629 --- lib/kamal/cli/traefik.rb | 4 ++-- test/integration/main_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/kamal/cli/traefik.rb b/lib/kamal/cli/traefik.rb index e835b1d8..c67dc69e 100644 --- a/lib/kamal/cli/traefik.rb +++ b/lib/kamal/cli/traefik.rb @@ -20,7 +20,7 @@ class Kamal::Cli::Traefik < Kamal::Cli::Base on(hosts) do execute *KAMAL.auditor.record("Rebooted traefik"), verbosity: :debug execute *KAMAL.registry.login - execute *KAMAL.traefik.stop + execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false execute *KAMAL.traefik.remove_container execute *KAMAL.traefik.run end @@ -44,7 +44,7 @@ class Kamal::Cli::Traefik < Kamal::Cli::Base mutating do on(KAMAL.traefik_hosts) do execute *KAMAL.auditor.record("Stopped traefik"), verbosity: :debug - execute *KAMAL.traefik.stop + execute *KAMAL.traefik.stop, raise_on_non_zero_exit: false end end end diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index 856781cb..22f60860 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -60,6 +60,19 @@ class MainTest < IntegrationTest assert_equal({ "path" => "/up", "port" => 3000, "max_attempts" => 7, "exposed_port" => 3999, "cord"=>"/tmp/kamal-cord", "log_lines" => 50, "cmd"=>"wget -qO- http://localhost > /dev/null || exit 1" }, config[:healthcheck]) end + test "setup and remove" do + # Check remove completes when nothing has been setup yet + kamal :remove, "-y" + assert_no_images_or_containers + + kamal :envify + kamal :setup + assert_images_and_containers + + kamal :remove, "-y" + assert_no_images_or_containers + end + private def assert_local_env_file(contents) assert_equal contents, deployer_exec("cat .env", capture: true) @@ -84,4 +97,22 @@ class MainTest < IntegrationTest assert_equal "200", Net::HTTP.get_response(URI.parse("http://localhost:12345/versions/.hidden")).code end + + def vm1_image_ids + docker_compose("exec vm1 docker image ls -q", capture: true).strip.split("\n") + end + + def vm1_container_ids + docker_compose("exec vm1 docker ps -a -q", capture: true).strip.split("\n") + end + + def assert_no_images_or_containers + assert vm1_image_ids.empty? + assert vm1_container_ids.empty? + end + + def assert_images_and_containers + assert vm1_image_ids.any? + assert vm1_container_ids.any? + end end