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
10 changes: 8 additions & 2 deletions lib/kamal/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def options_with_subcommand_class_options
options.merge(@_initializer.last[:class_options] || {})
end

# Class options given after a subcommand (`kamal app exec --skip-hooks ...`) are only in the
# subcommand class options, so check those too.
def skip_hooks?
options_with_subcommand_class_options[:skip_hooks]
end

def initialize_commander
KAMAL.tap do |commander|
if options[:verbose]
Expand Down Expand Up @@ -222,7 +228,7 @@ def capture_lock_status
end

def run_hook(hook, **extra_details)
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
if !skip_hooks? && KAMAL.hook.hook_exists?(hook)
details = {
hosts: KAMAL.hosts.join(","),
roles: KAMAL.specific_roles&.join(","),
Expand Down Expand Up @@ -261,7 +267,7 @@ def on(*args, &block)

def pre_connect_if_required
if !KAMAL.connected?
run_hook "pre-connect", secrets: true unless options[:skip_hooks]
run_hook "pre-connect", secrets: true unless skip_hooks?
KAMAL.connected = true
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/cli/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ class CliAppTest < CliTestCase
end
end

test "exec through main runs the pre-connect hook" do
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)

run_command_through_main("exec", "ruby -v").tap do |output|
assert_hook_ran "pre-connect", output
end
end

test "exec through main with skip hooks does not run the pre-connect hook" do
Kamal::Commands::Hook.any_instance.stubs(:hook_exists?).returns(true)

run_command_through_main("exec", "--skip-hooks", "ruby -v").tap do |output|
assert_no_match "/usr/bin/env .kamal/hooks/pre-connect", output
assert_match %r{docker run --rm --name app-web-exec-latest-[0-9a-f]{6} .* dhh/app:latest ruby -v}, output
end
end

test "exec without command fails" do
error = assert_raises(ArgumentError, "Exec requires a command to be specified") do
run_command("exec")
Expand Down Expand Up @@ -622,6 +639,13 @@ def run_command(*command, config: :with_accessories, host: "1.1.1.1", allow_exec
end
end

# The way `kamal app ...` is invoked from the command line: Main dispatches to the app subcommand.
def run_command_through_main(*command, config: :with_accessories, host: "1.1.1.1")
stdouted do
Kamal::Cli::Main.start([ "app", *command, "-c", "test/fixtures/deploy_#{config}.yml", *([ "--hosts", host ] if host) ])
end
end

def stub_running
Object.any_instance.stubs(:sleep)

Expand Down