Skip to content

Commit 956230b

Browse files
committed
Stop app container by container id
1 parent 4b88852 commit 956230b

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

lib/kamal/cli/app.rb

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,13 @@ def start
6666
end
6767

6868
desc "stop", "Stop app container on servers"
69+
option :container_id, desc: "Docker container ID to stop (instead of stopping all containers)"
6970
def stop
7071
with_lock do
71-
on(KAMAL.app_hosts) do |host|
72-
roles = KAMAL.roles_on(host)
73-
74-
roles.each do |role|
75-
app = KAMAL.app(role: role, host: host)
76-
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
77-
78-
if role.running_proxy?
79-
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
80-
endpoint = capture_with_info(*app.container_id_for_version(version)).strip
81-
if endpoint.present?
82-
execute *app.remove, raise_on_non_zero_exit: false
83-
end
84-
end
85-
86-
execute *app.stop, raise_on_non_zero_exit: false
87-
end
72+
if options[:container_id]
73+
stop_container(options[:container_id])
74+
else
75+
stop_all_containers
8876
end
8977
end
9078
end
@@ -397,4 +385,37 @@ def with_lock_if_stopping
397385
def host_boot_groups
398386
KAMAL.config.boot.limit ? KAMAL.app_hosts.each_slice(KAMAL.config.boot.limit).to_a : [ KAMAL.app_hosts ]
399387
end
388+
389+
def stop_container(container_id)
390+
on(KAMAL.app_hosts) do |host|
391+
roles = KAMAL.roles_on(host)
392+
393+
roles.each do |role|
394+
app = KAMAL.app(role: role, host: host)
395+
execute *KAMAL.auditor.record("Stopped container #{container_id}", role: role), verbosity: :debug
396+
execute *app.stop_by_container_id(container_id), raise_on_non_zero_exit: false
397+
end
398+
end
399+
end
400+
401+
def stop_all_containers
402+
on(KAMAL.app_hosts) do |host|
403+
roles = KAMAL.roles_on(host)
404+
405+
roles.each do |role|
406+
app = KAMAL.app(role: role, host: host)
407+
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
408+
409+
if role.running_proxy?
410+
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
411+
endpoint = capture_with_info(*app.container_id_for_version(version)).strip
412+
if endpoint.present?
413+
execute *app.remove, raise_on_non_zero_exit: false
414+
end
415+
end
416+
417+
execute *app.stop, raise_on_non_zero_exit: false
418+
end
419+
end
420+
end
400421
end

lib/kamal/commands/app.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def stop(version: nil)
4747
xargs(docker(:stop, *role.stop_args))
4848
end
4949

50+
def stop_by_container_id(container_id)
51+
docker(:stop, *role.stop_args, container_id)
52+
end
53+
5054
def info
5155
docker :ps, *container_filter_args
5256
end

test/cli/app_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ class CliAppTest < CliTestCase
250250
end
251251
end
252252

253+
test "stop with container id" do
254+
run_command("stop", "--container-id", "abcd1234").tap do |output|
255+
assert_match "docker stop abcd1234", output
256+
end
257+
end
258+
253259
test "stale_containers" do
254260
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
255261
.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)

test/commands/app_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ class CommandsAppTest < ActiveSupport::TestCase
100100
new_command.stop(version: "123").join(" ")
101101
end
102102

103+
test "stop by container id" do
104+
assert_equal \
105+
"docker stop abcd1234",
106+
new_command.stop_by_container_id("abcd1234").join(" ")
107+
end
108+
103109
test "info" do
104110
assert_equal \
105111
"docker ps --filter label=service=app --filter label=destination= --filter label=role=web",

0 commit comments

Comments
 (0)