Add power to follow logs on app and traefik
This commit is contained in:
@@ -81,18 +81,28 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
|||||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||||
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
||||||
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
||||||
|
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||||
def logs
|
def logs
|
||||||
# FIXME: Catch when app containers aren't running
|
# FIXME: Catch when app containers aren't running
|
||||||
|
|
||||||
since = options[:since]
|
grep = options[:grep]
|
||||||
lines = options[:lines]
|
|
||||||
grep = options[:grep]
|
|
||||||
|
|
||||||
on(MRSK.hosts) do |host|
|
if options[:follow]
|
||||||
begin
|
run_locally do
|
||||||
puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
|
info "Following logs on #{MRSK.primary_host}..."
|
||||||
rescue SSHKit::Command::Failed
|
info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||||
puts_by_host host, "Nothing found"
|
exec MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
since = options[:since]
|
||||||
|
lines = options[:lines]
|
||||||
|
|
||||||
|
on(MRSK.hosts) do |host|
|
||||||
|
begin
|
||||||
|
puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
|
||||||
|
rescue SSHKit::Command::Failed
|
||||||
|
puts_by_host host, "Nothing found"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,11 +37,25 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base
|
|||||||
desc "logs", "Show log lines from Traefik on servers"
|
desc "logs", "Show log lines from Traefik on servers"
|
||||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||||
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
||||||
|
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
||||||
|
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||||
def logs
|
def logs
|
||||||
since = options[:since]
|
grep = options[:grep]
|
||||||
lines = options[:lines]
|
|
||||||
|
|
||||||
on(MRSK.traefik_hosts) { |host| puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines)), type: "Traefik" }
|
if options[:follow]
|
||||||
|
run_locally do
|
||||||
|
info "Following logs on #{MRSK.primary_host}..."
|
||||||
|
info MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||||
|
exec MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
since = options[:since]
|
||||||
|
lines = options[:lines]
|
||||||
|
|
||||||
|
on(MRSK.traefik_hosts) do |host|
|
||||||
|
puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines, grep: grep)), type: "Traefik"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "remove", "Remove Traefik container and image from servers"
|
desc "remove", "Remove Traefik container and image from servers"
|
||||||
|
|||||||
@@ -57,6 +57,14 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
|||||||
*command
|
*command
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def follow_logs(host:, grep: nil)
|
||||||
|
run_over_ssh pipe(
|
||||||
|
current_container_id,
|
||||||
|
"xargs docker logs -t -n 10 -f 2>&1",
|
||||||
|
("grep '#{grep}'" if grep)
|
||||||
|
).join(" "), host: host
|
||||||
|
end
|
||||||
|
|
||||||
def console(host:)
|
def console(host:)
|
||||||
exec_over_ssh "bin/rails", "c", host: host
|
exec_over_ssh "bin/rails", "c", host: host
|
||||||
end
|
end
|
||||||
@@ -79,7 +87,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
def exec_over_ssh(*command, host:)
|
def exec_over_ssh(*command, host:)
|
||||||
"ssh -t #{config.ssh_user}@#{host} '#{run_exec(*command, interactive: true).join(" ")}'"
|
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
|
||||||
end
|
end
|
||||||
|
|
||||||
def service_filter
|
def service_filter
|
||||||
|
|||||||
@@ -23,5 +23,9 @@ module Mrsk::Commands
|
|||||||
def docker(*args)
|
def docker(*args)
|
||||||
args.compact.unshift :docker
|
args.compact.unshift :docker
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run_over_ssh(command, host:)
|
||||||
|
"ssh -t #{config.ssh_user}@#{host} '#{command}'"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,12 +24,17 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
|||||||
docker :ps, "--filter", "name=traefik"
|
docker :ps, "--filter", "name=traefik"
|
||||||
end
|
end
|
||||||
|
|
||||||
def logs(since: nil, lines: nil)
|
def logs(since: nil, lines: nil, grep: nil)
|
||||||
docker :logs, "traefik",
|
pipe \
|
||||||
(" --since #{since}" if since),
|
docker(:logs, "traefik", (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"),
|
||||||
(" -n #{lines}" if lines),
|
("grep '#{grep}'" if grep)
|
||||||
"-t",
|
end
|
||||||
"2>&1"
|
|
||||||
|
def follow_logs(host:, grep: nil)
|
||||||
|
run_over_ssh pipe(
|
||||||
|
docker(:logs, "traefik", "-t", "-n", "10", "-f", "2>&1"),
|
||||||
|
("grep '#{grep}'" if grep)
|
||||||
|
).join(" "), host: host
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_container
|
def remove_container
|
||||||
|
|||||||
Reference in New Issue
Block a user