Rip out context_hosts checks

The remote host is now encoded in the builder name so we don't need
to check it. We'll just do an inspect to confirm the builder exists.
This commit is contained in:
Donal McBreen
2024-06-06 12:43:27 +01:00
parent 8567ce9bf9
commit cf81837737
7 changed files with 34 additions and 106 deletions

View File

@@ -30,18 +30,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base
say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow
end end
# Get the command here to ensure the Dir.chdir doesn't interfere with it
push = KAMAL.builder.push
run_locally do run_locally do
begin begin
context_hosts = capture_with_info(*KAMAL.builder.context_hosts).split("\n") execute *KAMAL.builder.buildx_inspect
if context_hosts != KAMAL.builder.config_context_hosts
warn "Context hosts have changed, so re-creating builder, was: #{context_hosts.join(", ")}], now: #{KAMAL.builder.config_context_hosts.join(", ")}"
cli.remove
cli.create
end
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
if e.message =~ /(context not found|no builder|does not exist)/ if e.message =~ /(context not found|no builder|does not exist)/
warn "Missing compatible builder, so creating a new one first" warn "Missing compatible builder, so creating a new one first"
@@ -51,6 +42,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base
end end
end end
# Get the command here to ensure the Dir.chdir doesn't interfere with it
push = KAMAL.builder.push
KAMAL.with_verbosity(:debug) do KAMAL.with_verbosity(:debug) do
Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } Dir.chdir(KAMAL.config.builder.build_directory) { execute *push }
end end

View File

@@ -1,8 +1,7 @@
require "active_support/core_ext/string/filters" require "active_support/core_ext/string/filters"
class Kamal::Commands::Builder < Kamal::Commands::Base class Kamal::Commands::Builder < Kamal::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, :context_hosts, :config_context_hosts, :validate_image, delegate :create, :remove, :push, :clean, :pull, :info, :buildx_inspect, :validate_image, :first_mirror, to: :target
:first_mirror, to: :target
include Clone include Clone

View File

@@ -1,4 +1,3 @@
class Kamal::Commands::Builder::Base < Kamal::Commands::Base class Kamal::Commands::Builder::Base < Kamal::Commands::Base
class BuilderError < StandardError; end class BuilderError < StandardError; end
@@ -14,10 +13,29 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
docker :image, :rm, "--force", config.absolute_image docker :image, :rm, "--force", config.absolute_image
end end
def push
docker :buildx, :build,
"--push",
*platform_options,
"--builder", builder_name,
*build_options,
build_context
end
def pull def pull
docker :pull, config.absolute_image docker :pull, config.absolute_image
end end
def info
combine \
docker(:context, :ls),
docker(:buildx, :ls)
end
def buildx_inspect
docker :buildx, :inspect, builder_name
end
def build_options def build_options
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ] [ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ]
end end
@@ -35,14 +53,6 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
) )
end end
def context_hosts
:true
end
def config_context_hosts
[]
end
def first_mirror def first_mirror
docker(:info, "--format '{{index .RegistryConfig.Mirrors 0}}'") docker(:info, "--format '{{index .RegistryConfig.Mirrors 0}}'")
end end

View File

@@ -1,4 +1,4 @@
class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Remote
def create def create
combine \ combine \
create_local_buildx, create_local_buildx,
@@ -6,18 +6,9 @@ class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base
append_remote_buildx append_remote_buildx
end end
def context_hosts
chain \
context_host(builder_name)
end
def config_context_hosts
[ remote_host ].compact
end
private private
def builder_name def builder_name
"kamal-hybrid-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{local_arch}-#{remote_arch}" "kamal-hybrid-#{local_arch}-#{remote_arch}-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}"
end end
def create_local_buildx def create_local_buildx
@@ -28,7 +19,7 @@ class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base
docker :buildx, :create, "--append", "--name", builder_name, builder_name, "--platform", "linux/#{remote_arch}" docker :buildx, :create, "--append", "--name", builder_name, builder_name, "--platform", "linux/#{remote_arch}"
end end
def platform_options def platform
[ "--platform", "linux/#{local_arch},linux/#{remote_arch}" ] "linux/#{local_arch},linux/#{remote_arch}"
end end
end end

View File

@@ -7,25 +7,6 @@ class Kamal::Commands::Builder::Local < Kamal::Commands::Builder::Base
docker :buildx, :rm, builder_name docker :buildx, :rm, builder_name
end end
def info
combine \
docker(:context, :ls),
docker(:buildx, :ls)
end
def push
docker :buildx, :build,
"--push",
*platform_options,
"--builder", builder_name,
*build_options,
build_context
end
def context_hosts
docker :buildx, :inspect, builder_name, "> /dev/null"
end
private private
def builder_name def builder_name
"kamal-local" "kamal-local"

View File

@@ -26,18 +26,9 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
build_context build_context
end end
def context_hosts
context_host(builder_name)
end
def config_context_hosts
[ remote_host ]
end
private private
def builder_name def builder_name
"kamal-remote-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{remote_arch}" "kamal-remote-#{remote_arch}-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}"
end end
def create_remote_context def create_remote_context
@@ -57,6 +48,10 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
end end
def platform_options def platform_options
[ "--platform", "linux/#{remote_arch}" ] [ "--platform", platform ]
end
def platform
"linux/#{remote_arch}"
end end
end end

View File

@@ -158,48 +158,6 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ") builder.push.join(" ")
end end
test "multiarch context hosts" do
command = new_builder_command
assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ")
assert_equal "", command.config_context_hosts.join(" ")
end
test "native context hosts" do
command = new_builder_command(builder: { "multiarch" => false })
assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ")
assert_equal "", command.config_context_hosts.join(" ")
end
test "native cached context hosts" do
command = new_builder_command(builder: { "multiarch" => false, "cache" => { "type" => "registry" } })
assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ")
assert_equal "", command.config_context_hosts.join(" ")
end
test "native remote context hosts" do
command = new_builder_command(builder: { "remote" => { "arch" => "amd64", "host" => "ssh://host" } })
assert_equal "docker context inspect kamal-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ")
assert_equal [ "ssh://host" ], command.config_context_hosts
end
test "multiarch remote context hosts" do
command = new_builder_command(builder: {
"remote" => { "arch" => "amd64", "host" => "ssh://host" },
"local" => { "arch" => "arm64" }
})
assert_equal "docker context inspect kamal-app-multiarch-remote-arm64 --format '{{.Endpoints.docker.Host}}' ; docker context inspect kamal-app-multiarch-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ")
assert_equal [ "ssh://host" ], command.config_context_hosts
end
test "multiarch remote context hosts with local host" do
command = new_builder_command(builder: {
"remote" => { "arch" => "amd64", "host" => "ssh://host" },
"local" => { "arch" => "arm64", "host" => "unix:///var/run/docker.sock" }
})
assert_equal "docker context inspect kamal-app-multiarch-remote-arm64 --format '{{.Endpoints.docker.Host}}' ; docker context inspect kamal-app-multiarch-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ")
assert_equal [ "unix:///var/run/docker.sock", "ssh://host" ], command.config_context_hosts
end
test "mirror count" do test "mirror count" do
command = new_builder_command command = new_builder_command
assert_equal "docker info --format '{{index .RegistryConfig.Mirrors 0}}'", command.first_mirror.join(" ") assert_equal "docker info --format '{{index .RegistryConfig.Mirrors 0}}'", command.first_mirror.join(" ")