Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/kamal/cli/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ def logs(name)

if options[:follow]
run_locally do
info "Following logs on #{hosts}..."
info accessory.follow_logs(timestamps: timestamps, grep: grep, grep_options: grep_options)
exec accessory.follow_logs(timestamps: timestamps, grep: grep, grep_options: grep_options)
first_host = hosts.first
info "Following logs on #{first_host}..."
info accessory.follow_logs(host: first_host, timestamps: timestamps, grep: grep, grep_options: grep_options)
exec accessory.follow_logs(host: first_host, timestamps: timestamps, grep: grep, grep_options: grep_options)
end
else
since = options[:since]
Expand Down
10 changes: 6 additions & 4 deletions lib/kamal/commands/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def logs(timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil)
("grep '#{grep}'#{" #{grep_options}" if grep_options}" if grep)
end

def follow_logs(timestamps: true, grep: nil, grep_options: nil)
def follow_logs(host: hosts.first, timestamps: true, grep: nil, grep_options: nil)
run_over_ssh \
pipe \
pipe(
docker(:logs, service_name, ("--timestamps" if timestamps), "--tail", "10", "--follow", "2>&1"),
(%(grep "#{grep}"#{" #{grep_options}" if grep_options}) if grep)
),
host: host
end

def execute_in_existing_container(*command, interactive: false)
Expand Down Expand Up @@ -80,8 +82,8 @@ def execute_in_new_container_over_ssh(*command)
run_over_ssh execute_in_new_container(*command, interactive: true)
end

def run_over_ssh(command)
super command, host: hosts.first
def run_over_ssh(command, host: hosts.first)
super command, host: host
end

def ensure_local_file_present(local_file)
Expand Down
10 changes: 10 additions & 0 deletions test/cli/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ class CliAccessoryTest < CliTestCase
assert_match "docker logs app-mysql --timestamps --tail 10 --follow 2>&1 | grep \"hey\" -C 2", run_command("logs", "mysql", "--follow", "--grep", "hey", "--grep-options", "-C 2")
end

test "logs with follow respects hosts param" do
SSHKit::Backend::Abstract.any_instance.stubs(:exec)
.with("ssh -t [email protected] -p 22 'docker logs app-redis --timestamps --tail 10 --follow 2>&1'")

run_command("logs", "redis", "--follow", "--hosts", "1.1.1.2").tap do |output|
assert_match "Following logs on 1.1.1.2...", output
assert_match "ssh -t [email protected] -p 22 'docker logs app-redis --timestamps --tail 10 --follow 2>&1'", output
end
end

test "remove with confirmation" do
Kamal::Cli::Accessory.any_instance.expects(:stop).with("mysql")
Kamal::Cli::Accessory.any_instance.expects(:remove_container).with("mysql")
Expand Down