Show container logs and healthcheck status on failure

This commit is contained in:
Donal McBreen
2024-05-07 17:07:21 +01:00
parent 5be6fa3b4e
commit 773ba3a5ab
7 changed files with 31 additions and 14 deletions

View File

@@ -15,7 +15,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
end
#  Primary hosts and roles are returned first, so they can open the barrier
barrier = Kamal::Cli::Healthcheck::Barrier.new if KAMAL.roles.many?
barrier = Kamal::Cli::Healthcheck::Barrier.new
on(KAMAL.hosts, **KAMAL.boot_strategy) do |host|
KAMAL.roles_on(host).each do |role|

View File

@@ -1,6 +1,6 @@
class Kamal::Cli::App::Boot
attr_reader :host, :role, :version, :barrier, :sshkit
delegate :execute, :capture_with_info, :info, to: :sshkit
delegate :execute, :capture_with_info, :capture_with_pretty_json, :info, :error, to: :sshkit
delegate :uses_cord?, :assets?, :running_traefik?, to: :role
def initialize(host, role, sshkit, version, barrier)
@@ -55,7 +55,11 @@ class Kamal::Cli::App::Boot
reach_barrier
rescue => e
close_barrier if barrier_role?
if barrier_role? && barrier.close
info "Deploy failed, so closed barrier (#{host})"
error capture_with_info(*app.logs(version: version))
error capture_with_info(*app.container_health_log(version: version))
end
execute *app.stop(version: version), raise_on_non_zero_exit: false
raise
@@ -92,14 +96,10 @@ class Kamal::Cli::App::Boot
barrier.wait
info "Barrier opened (#{host})"
rescue Kamal::Cli::Healthcheck::Error
info "Barrier closed, shutting down new container... (#{host})"
info "Barrier closed, shutting down new container (#{host})..."
raise
end
def close_barrier
barrier&.close
end
def barrier_role?
role == KAMAL.primary_role
end

View File

@@ -1,4 +1,6 @@
module Kamal::Commands::App::Containers
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"
def list_containers
docker :container, :ls, "--all", *filter_args
end
@@ -20,4 +22,10 @@ module Kamal::Commands::App::Containers
def remove_containers
docker :container, :prune, "--force", *filter_args
end
def container_health_log(version:)
pipe \
container_id_for(container_name: container_name(version)),
xargs(docker(:inspect, "--format", DOCKER_HEALTH_LOG_FORMAT))
end
end

View File

@@ -1,7 +1,7 @@
module Kamal::Commands::App::Logging
def logs(since: nil, lines: nil, grep: nil)
def logs(version: nil, since: nil, lines: nil, grep: nil)
pipe \
current_running_container_id,
version ? container_id_for_version(version) : current_running_container_id,
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1",
("grep '#{grep}'" if grep)
end

View File

@@ -3,7 +3,6 @@ module Kamal::Commands
delegate :sensitive, :argumentize, to: Kamal::Utils
DOCKER_HEALTH_STATUS_FORMAT = "'{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}'"
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"
attr_accessor :config