From 5d629d0600fa6806cd6e809b3a9277c6f5829599 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 20 Jan 2023 16:27:05 +0100 Subject: [PATCH] Extract puts_by_host --- lib/mrsk/cli/app.rb | 14 +++++++------- lib/mrsk/cli/base.rb | 3 +-- lib/mrsk/commands/base.rb | 2 -- lib/mrsk/sshkit_with_ext.rb | 8 ++++++++ 4 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 lib/mrsk/sshkit_with_ext.rb diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index 431608ee..02a88f48 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -36,7 +36,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base desc "details", "Display details about app containers" def details - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.info, verbosity: Logger::INFO) + "\n\n" } + on(MRSK.config.hosts) { |host| puts_by_host host, capture(*MRSK.app.info, verbosity: Logger::INFO) } end desc "exec [CMD]", "Execute a custom task on servers passed in as CMD='bin/rake some:task'" @@ -48,7 +48,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base if options[:once] on(MRSK.config.primary_host) { puts capture(*MRSK.app.send(runner, cmd), verbosity: Logger::INFO) } else - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.send(runner, cmd), verbosity: Logger::INFO) + "\n\n" } + on(MRSK.config.hosts) { |host| puts_by_host host, capture(*MRSK.app.send(runner, cmd), verbosity: Logger::INFO) } end end @@ -80,18 +80,18 @@ class Mrsk::Cli::App < Mrsk::Cli::Base if options[:once] on(MRSK.config.primary_host) { puts capture(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'"), verbosity: Logger::INFO) } else - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'"), verbosity: Logger::INFO) + "\n\n" } + on(MRSK.config.hosts) { |host| puts_by_host host, capture(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'"), verbosity: Logger::INFO) } end end desc "containers", "List all the app containers currently on servers" def containers - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.list_containers, verbosity: Logger::INFO) + "\n\n" } + on(MRSK.config.hosts) { |host| puts_by_host host, capture(*MRSK.app.list_containers, verbosity: Logger::INFO) } end desc "current", "Return the current running container ID" def current - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.current_container_id, verbosity: Logger::INFO) + "\n\n" } + on(MRSK.config.hosts) { |host| puts_by_host host, capture(*MRSK.app.current_container_id, verbosity: Logger::INFO) } end desc "logs", "Show last 100 log lines from app on servers" @@ -107,9 +107,9 @@ class Mrsk::Cli::App < Mrsk::Cli::Base on(MRSK.config.hosts) do |host| begin - puts "App Host: #{host}\n" + capture(*MRSK.app.logs(since: since, lines: lines, grep: grep), verbosity: Logger::INFO) + "\n\n" + puts_by_host host, capture(*MRSK.app.logs(since: since, lines: lines, grep: grep), verbosity: Logger::INFO) rescue SSHKit::Command::Failed - puts "App Host: #{host}\nNothing found\n\n" + puts_by_host host, "Nothing found" end end end diff --git a/lib/mrsk/cli/base.rb b/lib/mrsk/cli/base.rb index 87ffbafb..885449d6 100644 --- a/lib/mrsk/cli/base.rb +++ b/lib/mrsk/cli/base.rb @@ -1,6 +1,5 @@ require "thor" -require "sshkit" -require "sshkit/dsl" +require "mrsk/sshkit_with_ext" module Mrsk::Cli class Base < Thor diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb index 87f917da..d2a0ae89 100644 --- a/lib/mrsk/commands/base.rb +++ b/lib/mrsk/commands/base.rb @@ -1,5 +1,3 @@ -require "sshkit" - module Mrsk::Commands class Base delegate :redact, to: Mrsk::Utils diff --git a/lib/mrsk/sshkit_with_ext.rb b/lib/mrsk/sshkit_with_ext.rb new file mode 100644 index 00000000..7fd9ff7c --- /dev/null +++ b/lib/mrsk/sshkit_with_ext.rb @@ -0,0 +1,8 @@ +require "sshkit" +require "sshkit/dsl" + +class SSHKit::Backend::Abstract + def puts_by_host(host, output) + puts "App Host: #{host}\n#{output}\n\n" + end +end