diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 6ba8908a..601d485e 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -115,14 +115,16 @@ class Kamal::Cli::App < Kamal::Cli::Base say "Get current version of running container...", :magenta unless options[:version] using_version(options[:version] || current_running_version) do |version| say "Launching interactive command with version #{version} via SSH from existing container on #{KAMAL.primary_host}...", :magenta - run_locally { exec KAMAL.app(role: "web").execute_in_existing_container_over_ssh(cmd, host: KAMAL.primary_host) } + run_locally { exec KAMAL.app(role: KAMAL.primary_role).execute_in_existing_container_over_ssh(cmd, host: KAMAL.primary_host) } end when options[:interactive] say "Get most recent version available as an image...", :magenta unless options[:version] using_version(version_or_latest) do |version| say "Launching interactive command with version #{version} via SSH from new container on #{KAMAL.primary_host}...", :magenta - run_locally { exec KAMAL.app(role: KAMAL.roles_on(KAMAL.primary_host).first).execute_in_new_container_over_ssh(cmd, host: KAMAL.primary_host) } + run_locally do + exec KAMAL.app(role: KAMAL.primary_role).execute_in_new_container_over_ssh(cmd, host: KAMAL.primary_host) + end end when options[:reuse] diff --git a/lib/kamal/commander.rb b/lib/kamal/commander.rb index 92717b10..9ba3be12 100644 --- a/lib/kamal/commander.rb +++ b/lib/kamal/commander.rb @@ -39,6 +39,10 @@ class Kamal::Commander specific_hosts&.first || specific_roles&.first&.primary_host || config.primary_web_host end + def primary_role + roles_on(primary_host).first + end + def roles (specific_roles || config.roles).select do |role| ((specific_hosts || config.all_hosts) & role.hosts).any? diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index ba58d607..c70b8ed4 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -170,6 +170,25 @@ class CliAppTest < CliTestCase end end + test "exec interactive" do + SSHKit::Backend::Abstract.any_instance.expects(:exec) + .with("ssh -t root@1.1.1.1 'docker run -it --rm --env-file .kamal/env/roles/app-web.env 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 + end + end + + test "exec interactive with reuse" do + SSHKit::Backend::Abstract.any_instance.expects(:exec) + .with("ssh -t root@1.1.1.1 'docker exec -it app-web-999 ruby -v'") + run_command("exec", "-i", "--reuse", "ruby -v").tap do |output| + assert_match "Get current version of running container...", output + assert_match "Running docker ps --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest --format \"{{.Names}}\" | while read line; do echo ${line#app-web-}; done on 1.1.1.1", output + assert_match "Launching interactive command with version 999 via SSH from existing container on 1.1.1.1...", output + end + end + test "containers" do run_command("containers").tap do |output| assert_match "docker container ls --all --filter label=service=app", output diff --git a/test/commander_test.rb b/test/commander_test.rb index 4b6dee7c..2290e0f0 100644 --- a/test/commander_test.rb +++ b/test/commander_test.rb @@ -49,6 +49,12 @@ class CommanderTest < ActiveSupport::TestCase assert_equal "1.1.1.3", @kamal.primary_host end + test "primary_role" do + assert_equal "web", @kamal.primary_role + @kamal.specific_roles = "workers" + assert_equal "workers", @kamal.primary_role + end + test "roles_on" do assert_equal [ "web" ], @kamal.roles_on("1.1.1.1") assert_equal [ "workers" ], @kamal.roles_on("1.1.1.3")