@@ -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
400421end
0 commit comments