Skip to content

Commit

Permalink
Pipe app container id
Browse files Browse the repository at this point in the history
  • Loading branch information
aliismayilov committed Nov 23, 2024
1 parent ac90ee0 commit 1cc5406
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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(container_id: "$(#{app.container_id_for_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
14 changes: 12 additions & 2 deletions lib/kamal/commands/app/logging.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
module Kamal::Commands::App::Logging
def logs(container_id: nil, timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil)
pipe \
container_id ? "echo #{container_id}" : current_running_container_id,
container_id_command(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:, 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,
container_id_command(container_id),
"xargs docker logs#{" --timestamps" if timestamps}#{" --tail #{lines}" if lines} --follow 2>&1",
(%(grep "#{grep}"#{" #{grep_options}" if grep_options}) if grep)
),
host: host
end

private

def container_id_command(container_id)
case container_id
when Array then container_id
when String, Symbol then "echo #{container_id}"
else current_running_container_id
end
end
end

0 comments on commit 1cc5406

Please sign in to comment.