From 1cc5406b00048775dce4b75aabd94512ee821abb Mon Sep 17 00:00:00 2001 From: Ali Ismayilov <993934+aliismayilov@users.noreply.github.com> Date: Sat, 23 Nov 2024 21:37:58 +0100 Subject: [PATCH] Pipe app container id --- lib/kamal/cli/app/boot.rb | 2 +- lib/kamal/commands/app/logging.rb | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/kamal/cli/app/boot.rb b/lib/kamal/cli/app/boot.rb index b98e0472..41db39a6 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(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}" diff --git a/lib/kamal/commands/app/logging.rb b/lib/kamal/commands/app/logging.rb index 56e0679c..d5c8d5c7 100644 --- a/lib/kamal/commands/app/logging.rb +++ b/lib/kamal/commands/app/logging.rb @@ -1,7 +1,7 @@ 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 @@ -9,10 +9,20 @@ module Kamal::Commands::App::Logging 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