From 75b44cd328dac23013e2ee48e8847164a5291d9a Mon Sep 17 00:00:00 2001 From: Ali Ismayilov <993934+aliismayilov@users.noreply.github.com> Date: Sat, 26 Oct 2024 14:40:35 +0200 Subject: [PATCH] Capture logs for specific container_id --- lib/kamal/cli/app.rb | 6 +++--- lib/kamal/cli/app/boot.rb | 2 +- lib/kamal/commands/app/execution.rb | 2 +- lib/kamal/commands/app/logging.rb | 6 +++--- test/cli/app_test.rb | 2 +- test/commands/app_test.rb | 8 +++++++- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 3ed9a3b2..2378fa03 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -215,8 +215,8 @@ class Kamal::Cli::App < Kamal::Cli::Base role = KAMAL.roles_on(KAMAL.primary_host).first app = KAMAL.app(role: role, host: host) - info app.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, lines: lines, grep: grep, grep_options: grep_options, container_id: container_id) - exec app.follow_logs(host: KAMAL.primary_host, timestamps: timestamps, lines: lines, grep: grep, grep_options: grep_options, container_id: container_id) + info app.follow_logs(host: KAMAL.primary_host, container_id: container_id, timestamps: timestamps, lines: lines, grep: grep, grep_options: grep_options) + exec app.follow_logs(host: KAMAL.primary_host, container_id: container_id, timestamps: timestamps, lines: lines, grep: grep, grep_options: grep_options) end else lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set @@ -226,7 +226,7 @@ class Kamal::Cli::App < Kamal::Cli::Base roles.each do |role| begin - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(timestamps: timestamps, since: since, lines: lines, grep: grep, grep_options: grep_options)) + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(container_id: container_id, timestamps: timestamps, since: since, lines: lines, grep: grep, grep_options: grep_options)) rescue SSHKit::Command::Failed puts_by_host host, "Nothing found" end diff --git a/lib/kamal/cli/app/boot.rb b/lib/kamal/cli/app/boot.rb index fd330c71..b98e0472 100644 --- a/lib/kamal/cli/app/boot.rb +++ b/lib/kamal/cli/app/boot.rb @@ -91,7 +91,7 @@ class Kamal::Cli::App::Boot if barrier.close info "First #{KAMAL.primary_role} container is unhealthy on #{host}, not booting any other roles" begin - error capture_with_info(*app.logs(version: version)) + error capture_with_info(*app.logs(container_id: "$(#{app.container_id_for_version(version)})")) error capture_with_info(*app.container_health_log(version: version)) rescue SSHKit::Command::Failed error "Could not fetch logs for #{version}" diff --git a/lib/kamal/commands/app/execution.rb b/lib/kamal/commands/app/execution.rb index 3d5ff756..e1289fd8 100644 --- a/lib/kamal/commands/app/execution.rb +++ b/lib/kamal/commands/app/execution.rb @@ -11,7 +11,7 @@ module Kamal::Commands::App::Execution docker :run, ("-it" if interactive), ("--detach" if detach), - "--rm", + ("--rm" unless detach), "--network", "kamal", *role&.env_args(host), *argumentize("--env", env), diff --git a/lib/kamal/commands/app/logging.rb b/lib/kamal/commands/app/logging.rb index 4a45a533..56e0679c 100644 --- a/lib/kamal/commands/app/logging.rb +++ b/lib/kamal/commands/app/logging.rb @@ -1,12 +1,12 @@ module Kamal::Commands::App::Logging - def logs(version: nil, timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) + def logs(container_id: nil, timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) pipe \ - version ? container_id_for_version(version) : current_running_container_id, + container_id ? "echo #{container_id}" : current_running_container_id, "xargs docker logs#{" --timestamps" if timestamps}#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1", ("grep '#{grep}'#{" #{grep_options}" if grep_options}" if grep) end - def follow_logs(host:, timestamps: true, lines: nil, grep: nil, grep_options: nil, container_id: nil) + def follow_logs(host:, container_id: nil, timestamps: true, lines: nil, grep: nil, grep_options: nil) run_over_ssh \ pipe( container_id ? "echo #{container_id}" : current_running_container_id, diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 63937ea2..7c71d05e 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -275,7 +275,7 @@ class CliAppTest < CliTestCase test "exec detach" do run_command("exec", "--detach", "ruby -v").tap do |output| - assert_match "docker run --detach --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v", output + assert_match "docker run --detach --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v", output end end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index c9934629..c42f535a 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -157,6 +157,12 @@ class CommandsAppTest < ActiveSupport::TestCase new_command.logs.join(" ") end + test "logs with container_id" do + assert_equal \ + "echo C137 | xargs docker logs --timestamps 2>&1", + new_command.logs(container_id: "C137").join(" ") + end + test "logs with since" do assert_equal \ "sh -c 'docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting' | head -1 | xargs docker logs --timestamps --since 5m 2>&1", @@ -248,7 +254,7 @@ class CommandsAppTest < ActiveSupport::TestCase test "execute in new detached container" do assert_equal \ - "docker run --detach --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", + "docker run --detach --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", new_command.execute_in_new_container("bin/rails", "db:setup", detach: true, env: {}).join(" ") end