diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 22f888174..23035130a 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -66,13 +66,16 @@ def start end desc "stop", "Stop app container on servers" + option :container_id, desc: "Docker container ID to stop (instead of stopping all containers)" def stop + container_id = options[:container_id] + with_lock do on(KAMAL.app_hosts) do |host| roles = KAMAL.roles_on(host) roles.each do |role| - app = KAMAL.app(role: role, host: host) + app = KAMAL.app(role: role, host: host, container_id: container_id) execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug if role.running_proxy? diff --git a/lib/kamal/commander.rb b/lib/kamal/commander.rb index 3931304ae..33317c515 100644 --- a/lib/kamal/commander.rb +++ b/lib/kamal/commander.rb @@ -81,8 +81,8 @@ def accessory_names config.accessories&.collect(&:name) || [] end - def app(role: nil, host: nil) - Kamal::Commands::App.new(config, role: role, host: host) + def app(role: nil, host: nil, container_id: nil) + Kamal::Commands::App.new(config, role: role, host: host, container_id: container_id) end def accessory(name) diff --git a/lib/kamal/commands/app.rb b/lib/kamal/commands/app.rb index 715371f5c..374a6f80d 100644 --- a/lib/kamal/commands/app.rb +++ b/lib/kamal/commands/app.rb @@ -3,14 +3,15 @@ class Kamal::Commands::App < Kamal::Commands::Base ACTIVE_DOCKER_STATUSES = [ :running, :restarting ] - attr_reader :role, :host + attr_reader :role, :host, :container_id delegate :container_name, to: :role - def initialize(config, role: nil, host: nil) + def initialize(config, role: nil, host: nil, container_id: nil) super(config) @role = role @host = host + @container_id = container_id end def run(hostname: nil) @@ -54,7 +55,7 @@ def info def current_running_container_id - current_running_container(format: "--quiet") + container_id || current_running_container(format: "--quiet") end def container_id_for_version(version, only_running: false) diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 882a14fe8..06cd97c1a 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -250,6 +250,12 @@ class CliAppTest < CliTestCase end end + test "stop with container id" do + run_command("stop", "--container-id", "abcd1234").tap do |output| + assert_match "abcd1234 | xargs docker stop", output + end + end + test "stale_containers" do SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info) .with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=destination=", "--filter", "label=role=web", "--format", "\"{{.Names}}\"", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)