-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #906 from aliismayilov/detached-run
Allow running detached app commands and follow logs by container ID
- Loading branch information
Showing
7 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
jobs: | ||
rubocop: | ||
name: RuboCop | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
module Kamal::Commands::App::Logging | ||
def logs(version: nil, timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) | ||
def logs(container_id: nil, timestamps: true, since: nil, lines: nil, grep: nil, grep_options: nil) | ||
pipe \ | ||
version ? container_id_for_version(version) : 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 | ||
|
||
def follow_logs(host:, timestamps: true, lines: nil, grep: nil, grep_options: nil) | ||
def follow_logs(host:, container_id: nil, timestamps: true, lines: nil, grep: nil, grep_options: nil) | ||
run_over_ssh \ | ||
pipe( | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,13 +263,37 @@ class CliAppTest < CliTestCase | |
|
||
test "exec" do | ||
run_command("exec", "ruby -v").tap do |output| | ||
assert_match "docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env dhh/app:latest ruby -v", output | ||
assert_match "docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v", output | ||
end | ||
end | ||
|
||
test "exec separate arguments" do | ||
run_command("exec", "ruby", " -v").tap do |output| | ||
assert_match "docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env dhh/app:latest ruby -v", output | ||
assert_match "docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v", output | ||
end | ||
end | ||
|
||
test "exec detach" do | ||
run_command("exec", "--detach", "ruby -v").tap do |output| | ||
assert_match "docker run --detach --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v", output | ||
end | ||
end | ||
|
||
test "exec detach with reuse" do | ||
assert_raises(ArgumentError, "Detach is not compatible with reuse") do | ||
run_command("exec", "--detach", "--reuse", "ruby -v") | ||
end | ||
end | ||
|
||
test "exec detach with interactive" do | ||
assert_raises(ArgumentError, "Detach is not compatible with interactive") do | ||
run_command("exec", "--interactive", "--detach", "ruby -v") | ||
end | ||
end | ||
|
||
test "exec detach with interactive and reuse" do | ||
assert_raises(ArgumentError, "Detach is not compatible with interactive or reuse") do | ||
run_command("exec", "--interactive", "--detach", "--reuse", "ruby -v") | ||
end | ||
end | ||
|
||
|
@@ -282,7 +306,7 @@ class CliAppTest < CliTestCase | |
|
||
test "exec interactive" do | ||
SSHKit::Backend::Abstract.any_instance.expects(:exec) | ||
.with("ssh -t [email protected] -p 22 'docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env dhh/app:latest ruby -v'") | ||
.with("ssh -t [email protected] -p 22 'docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:latest ruby -v'") | ||
run_command("exec", "-i", "ruby -v").tap do |output| | ||
assert_match "Get most recent version available as an image...", output | ||
assert_match "Launching interactive command with version latest via SSH from new container on 1.1.1.1...", output | ||
|
@@ -329,6 +353,13 @@ class CliAppTest < CliTestCase | |
assert_match "sh -c '\\''docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''\\'\\'''\\''{{.ID}}'\\''\\'\\'''\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting'\\'' | head -1 | xargs docker logs --timestamps --tail 10 --follow 2>&1", run_command("logs", "--follow") | ||
end | ||
|
||
test "logs with follow and container_id" do | ||
SSHKit::Backend::Abstract.any_instance.stubs(:exec) | ||
.with("ssh -t [email protected] -p 22 'echo ID123 | xargs docker logs --timestamps --tail 10 --follow 2>&1'") | ||
|
||
assert_match "echo ID123 | xargs docker logs --timestamps --tail 10 --follow 2>&1", run_command("logs", "--follow", "--container-id", "ID123") | ||
end | ||
|
||
test "logs with follow and grep" do | ||
SSHKit::Backend::Abstract.any_instance.stubs(:exec) | ||
.with("ssh -t [email protected] -p 22 'sh -c '\\''docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''\\'\\'''\\''{{.ID}}'\\''\\'\\'''\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting'\\'' | head -1 | xargs docker logs --timestamps --follow 2>&1 | grep \"hey\"'") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,12 @@ class CommandsAppTest < ActiveSupport::TestCase | |
new_command.logs.join(" ") | ||
end | ||
|
||
test "logs with container_id" do | ||
assert_equal \ | ||
"echo C137 | xargs docker logs --timestamps 2>&1", | ||
new_command.logs(container_id: "C137").join(" ") | ||
end | ||
|
||
test "logs with since" do | ||
assert_equal \ | ||
"sh -c 'docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting' | head -1 | xargs docker logs --timestamps --since 5m 2>&1", | ||
|
@@ -208,6 +214,10 @@ class CommandsAppTest < ActiveSupport::TestCase | |
"ssh -t root@app-1 -p 22 'sh -c '\\''docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''\\'\\'''\\''{{.ID}}'\\''\\'\\'''\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting'\\'' | head -1 | xargs docker logs --timestamps --follow 2>&1 | grep \"Completed\"'", | ||
new_command.follow_logs(host: "app-1", grep: "Completed") | ||
|
||
assert_equal \ | ||
"ssh -t root@app-1 -p 22 'echo ID321 | xargs docker logs --timestamps --follow 2>&1'", | ||
new_command.follow_logs(host: "app-1", container_id: "ID321") | ||
|
||
assert_equal \ | ||
"ssh -t root@app-1 -p 22 'sh -c '\\''docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''\\'\\'''\\''{{.ID}}'\\''\\'\\'''\\'') ; docker ps --latest --quiet --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting'\\'' | head -1 | xargs docker logs --timestamps --tail 123 --follow 2>&1'", | ||
new_command.follow_logs(host: "app-1", lines: 123) | ||
|
@@ -224,29 +234,43 @@ class CommandsAppTest < ActiveSupport::TestCase | |
|
||
test "execute in new container" do | ||
assert_equal \ | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env dhh/app:999 bin/rails db:setup", | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", env: {}).join(" ") | ||
end | ||
|
||
test "execute in new container with logging" do | ||
@config[:logging] = { "driver" => "local", "options" => { "max-size" => "100m", "max-file" => "3" } } | ||
|
||
assert_equal \ | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-driver \"local\" --log-opt max-size=\"100m\" --log-opt max-file=\"3\" dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", env: {}).join(" ") | ||
end | ||
|
||
test "execute in new container with env" do | ||
assert_equal \ | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --env foo=\"bar\" dhh/app:999 bin/rails db:setup", | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --env foo=\"bar\" --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", env: { "foo" => "bar" }).join(" ") | ||
end | ||
|
||
test "execute in new detached container" do | ||
assert_equal \ | ||
"docker run --detach --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", detach: true, env: {}).join(" ") | ||
end | ||
|
||
test "execute in new container with tags" do | ||
@config[:servers] = [ { "1.1.1.1" => "tag1" } ] | ||
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } } | ||
|
||
assert_equal \ | ||
"docker run --rm --network kamal --env ENV1=\"value1\" --env-file .kamal/apps/app/env/roles/web.env dhh/app:999 bin/rails db:setup", | ||
"docker run --rm --network kamal --env ENV1=\"value1\" --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", env: {}).join(" ") | ||
end | ||
|
||
test "execute in new container with custom options" do | ||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } } | ||
assert_equal \ | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --mount \"somewhere\" --cap-add dhh/app:999 bin/rails db:setup", | ||
"docker run --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" --mount \"somewhere\" --cap-add dhh/app:999 bin/rails db:setup", | ||
new_command.execute_in_new_container("bin/rails", "db:setup", env: {}).join(" ") | ||
end | ||
|
||
|
@@ -263,21 +287,21 @@ class CommandsAppTest < ActiveSupport::TestCase | |
end | ||
|
||
test "execute in new container over ssh" do | ||
assert_match %r{docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env dhh/app:999 bin/rails c}, | ||
assert_match %r{docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size="10m" dhh/app:999 bin/rails c}, | ||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {}) | ||
end | ||
|
||
test "execute in new container over ssh with tags" do | ||
@config[:servers] = [ { "1.1.1.1" => "tag1" } ] | ||
@config[:env]["tags"] = { "tag1" => { "ENV1" => "value1" } } | ||
|
||
assert_equal "ssh -t [email protected] -p 22 'docker run -it --rm --network kamal --env ENV1=\"value1\" --env-file .kamal/apps/app/env/roles/web.env dhh/app:999 bin/rails c'", | ||
assert_equal "ssh -t [email protected] -p 22 'docker run -it --rm --network kamal --env ENV1=\"value1\" --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" dhh/app:999 bin/rails c'", | ||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {}) | ||
end | ||
|
||
test "execute in new container with custom options over ssh" do | ||
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere", "cap-add" => true } } } | ||
assert_match %r{docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --mount \"somewhere\" --cap-add dhh/app:999 bin/rails c}, | ||
assert_match %r{docker run -it --rm --network kamal --env-file .kamal/apps/app/env/roles/web.env --log-opt max-size=\"10m\" --mount \"somewhere\" --cap-add dhh/app:999 bin/rails c}, | ||
new_command.execute_in_new_container_over_ssh("bin/rails", "c", env: {}) | ||
end | ||
|
||
|