Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Kamal to run without traefik #580

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/kamal/cli/healthcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Kamal::Cli::Healthcheck < Kamal::Cli::Base

desc "perform", "Health check current app version"
def perform
return unless KAMAL.primary_role.running_traefik?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we raise an exception here instead and protect the callers with if statements? If someone managed to call this without a web role, I think it would be better to fail then to silently pass the check.

on(KAMAL.primary_host) do
begin
execute *KAMAL.healthcheck.run
Expand Down
4 changes: 2 additions & 2 deletions lib/kamal/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def configure(**kwargs)
attr_reader :specific_roles, :specific_hosts

def specific_primary!
self.specific_hosts = [ config.primary_web_host ]
self.specific_hosts = [ config.primary_host ]
end

def specific_roles=(role_names)
Expand All @@ -36,7 +36,7 @@ def specific_hosts=(hosts)
end

def primary_host
specific_hosts&.first || specific_roles&.first&.primary_host || config.primary_web_host
specific_hosts&.first || specific_roles&.first&.primary_host || config.primary_host
end

def primary_role
Expand Down
1 change: 1 addition & 0 deletions lib/kamal/commands/healthcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Kamal::Commands::Healthcheck < Kamal::Commands::Base

def run
web = config.role(:web)
return unless web.present?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again could we raise here instead of returning?


docker :run,
"--detach",
Expand Down
10 changes: 7 additions & 3 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ def all_hosts
roles.flat_map(&:hosts).uniq
end

def primary_web_host
role(:web).primary_host
def primary_host
primary_role.primary_host
end

def primary_role
role(:web) || roles.first
end

def traefik_hosts
Expand Down Expand Up @@ -208,7 +212,7 @@ def to_h
{
roles: role_names,
hosts: all_hosts,
primary_host: primary_web_host,
primary_host: primary_host,
version: version,
repository: repository,
absolute_image: absolute_image,
Expand Down
9 changes: 7 additions & 2 deletions test/cli/healthcheck_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ class CliHealthcheckTest < CliTestCase
end
assert_match "container not ready (unhealthy)", exception.message
end

test "does not perform if primary does not have traefik" do
SSHKit::Backend::Abstract.any_instance.expects(:execute).never
run_command("perform", config_file: "test/fixtures/deploy_workers_only.yml")
end

private
def run_command(*command)
stdouted { Kamal::Cli::Healthcheck.start([*command, "-c", "test/fixtures/deploy_with_accessories.yml"]) }
def run_command(*command, config_file: "test/fixtures/deploy_with_accessories.yml")
stdouted { Kamal::Cli::Healthcheck.start([*command, "-c", config_file]) }
end
end
6 changes: 3 additions & 3 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3" ], @config_with_roles.all_hosts
end

test "primary web host" do
assert_equal "1.1.1.1", @config.primary_web_host
assert_equal "1.1.1.1", @config_with_roles.primary_web_host
test "primary host" do
assert_equal "1.1.1.1", @config.primary_host
assert_equal "1.1.1.1", @config_with_roles.primary_host
end

test "traefik hosts" do
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/deploy_workers_only.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
service: app
image: dhh/app
servers:
workers:
hosts:
- 1.1.1.1
- 1.1.1.2
registry:
username: user
password: pw