Skip to content

Commit

Permalink
Capture logs for specific container_id
Browse files Browse the repository at this point in the history
  • Loading branch information
aliismayilov committed Oct 26, 2024
1 parent 9896acc commit bc049e7
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/kamal/cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def logs
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
Expand All @@ -226,7 +226,7 @@ def logs

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
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/cli/app/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def close_barrier
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}"
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/app/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def execute_in_new_container(*command, interactive: false, detach: false, env:)
docker :run,
("-it" if interactive),
("--detach" if detach),
"--rm",
("--rm" unless detach),
"--network", "kamal",
*role&.env_args(host),
*argumentize("--env", env),
Expand Down
6 changes: 3 additions & 3 deletions lib/kamal/commands/app/logging.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/cli/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion test/commands/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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=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=role=web --filter status=running --filter status=restarting' | head -1 | xargs docker logs --timestamps --since 5m 2>&1",
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bc049e7

Please sign in to comment.