From 6b5c5f06502a4ef8de1a0dba7ef908edb1565e51 Mon Sep 17 00:00:00 2001 From: dhh Date: Sat, 16 Sep 2023 10:03:28 -0700 Subject: [PATCH] Extract Logging too Leave only the core essentials in App --- lib/kamal/commands/app.rb | 24 +++--------------------- lib/kamal/commands/app/logging.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 lib/kamal/commands/app/logging.rb diff --git a/lib/kamal/commands/app.rb b/lib/kamal/commands/app.rb index 9cc1bcfa..f3ebe567 100644 --- a/lib/kamal/commands/app.rb +++ b/lib/kamal/commands/app.rb @@ -1,5 +1,5 @@ class Kamal::Commands::App < Kamal::Commands::Base - include Assets, Containers, Cord, Execution, Images + include Assets, Containers, Cord, Execution, Images, Logging ACTIVE_DOCKER_STATUSES = [ :running, :restarting ] @@ -48,24 +48,6 @@ class Kamal::Commands::App < Kamal::Commands::Base end - def logs(since: nil, lines: nil, grep: nil) - pipe \ - current_running_container_id, - "xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1", - ("grep '#{grep}'" if grep) - end - - def follow_logs(host:, grep: nil) - run_over_ssh \ - pipe( - current_running_container_id, - "xargs docker logs --timestamps --tail 10 --follow 2>&1", - (%(grep "#{grep}") if grep) - ), - host: host - end - - def current_running_container_id docker :ps, "--quiet", *filter_args(statuses: ACTIVE_DOCKER_STATUSES), "--latest" end @@ -90,7 +72,7 @@ class Kamal::Commands::App < Kamal::Commands::Base end def remove_env_file - [:rm, "-f", role_config.host_env_file_path] + [ :rm, "-f", role_config.host_env_file_path ] end @@ -104,7 +86,7 @@ class Kamal::Commands::App < Kamal::Commands::Base end def service_role_dest - [config.service, role, config.destination].compact.join("-") + [ config.service, role, config.destination ].compact.join("-") end def filters(statuses: nil) diff --git a/lib/kamal/commands/app/logging.rb b/lib/kamal/commands/app/logging.rb new file mode 100644 index 00000000..d20eda2c --- /dev/null +++ b/lib/kamal/commands/app/logging.rb @@ -0,0 +1,18 @@ +module Kamal::Commands::App::Logging + def logs(since: nil, lines: nil, grep: nil) + pipe \ + current_running_container_id, + "xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1", + ("grep '#{grep}'" if grep) + end + + def follow_logs(host:, grep: nil) + run_over_ssh \ + pipe( + current_running_container_id, + "xargs docker logs --timestamps --tail 10 --follow 2>&1", + (%(grep "#{grep}") if grep) + ), + host: host + end +end