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

Trim long hostnames #762

Merged
merged 1 commit into from
May 10, 2024
Merged
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
3 changes: 2 additions & 1 deletion lib/kamal/cli/app/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def old_version_renamed_if_clashing
def start_new_version
audit "Booted app version #{version}"
execute *app.tie_cord(role.cord_host_file) if uses_cord?
execute *app.run(hostname: "#{host}-#{SecureRandom.hex(6)}")
hostname = "#{host.to_s[0...51].gsub(/\.+$/, '')}-#{SecureRandom.hex(6)}"
execute *app.run(hostname: hostname)
Kamal::Cli::Healthcheck::Poller.wait_for_healthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }
end

Expand Down
20 changes: 20 additions & 0 deletions test/cli/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ class CliAppTest < CliTestCase
end
end

test "long hostname" do
stub_running

hostname = "this-hostname-is-really-unacceptably-long-to-be-honest.example.com"

stdouted { Kamal::Cli::App.start([ "boot", "-c", "test/fixtures/deploy_with_uncommon_hostnames.yml", "--hosts", hostname ]) }.tap do |output|
assert_match /docker run --detach --restart unless-stopped --name app-web-latest --hostname this-hostname-is-really-unacceptably-long-to-be-hon-[0-9a-f]{12} /, output
end
end

test "hostname is trimmed if will end with a period" do
stub_running

hostname = "this-hostname-with-random-part-is-too-long.example.com"

stdouted { Kamal::Cli::App.start([ "boot", "-c", "test/fixtures/deploy_with_uncommon_hostnames.yml", "--hosts", hostname ]) }.tap do |output|
assert_match /docker run --detach --restart unless-stopped --name app-web-latest --hostname this-hostname-with-random-part-is-too-long.example-[0-9a-f]{12} /, output
end
end

private
def run_command(*command, config: :with_accessories)
stdouted { Kamal::Cli::App.start([ *command, "-c", "test/fixtures/deploy_#{config}.yml", "--hosts", "1.1.1.1" ]) }
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/deploy_with_uncommon_hostnames.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
service: app
image: dhh/app
servers:
- "this-hostname-with-random-part-is-too-long.example.com"
- "this-hostname-is-really-unacceptably-long-to-be-honest.example.com"
registry:
username: user
password: pw