Move multiarch remote builder to hybrid builder
Include the host name in the builder name, so we can have one builder per host/arch across all kamal projects. Inherit from the remote builder. The difference in the hybrid builder is that we create a local buildx instance and append the remote context to it.
This commit is contained in:
committed by
Donal McBreen
parent
c048c097ed
commit
2aeabda455
@@ -17,7 +17,7 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
|
|||||||
when !config.builder.multiarch? && config.builder.cached?
|
when !config.builder.multiarch? && config.builder.cached?
|
||||||
local
|
local
|
||||||
when config.builder.local? && config.builder.remote?
|
when config.builder.local? && config.builder.remote?
|
||||||
multiarch_remote
|
hybrid
|
||||||
when config.builder.remote?
|
when config.builder.remote?
|
||||||
remote
|
remote
|
||||||
else
|
else
|
||||||
@@ -33,8 +33,8 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
|
|||||||
@local ||= Kamal::Commands::Builder::Local.new(config)
|
@local ||= Kamal::Commands::Builder::Local.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
def multiarch_remote
|
def hybrid
|
||||||
@multiarch_remote ||= Kamal::Commands::Builder::Multiarch::Remote.new(config)
|
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
34
lib/kamal/commands/builder/hybrid.rb
Normal file
34
lib/kamal/commands/builder/hybrid.rb
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base
|
||||||
|
def create
|
||||||
|
combine \
|
||||||
|
create_local_buildx,
|
||||||
|
create_remote_context,
|
||||||
|
append_remote_buildx
|
||||||
|
end
|
||||||
|
|
||||||
|
def context_hosts
|
||||||
|
chain \
|
||||||
|
context_host(builder_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def config_context_hosts
|
||||||
|
[ remote_host ].compact
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def builder_name
|
||||||
|
"kamal-hybrid-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{local_arch}-#{remote_arch}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_local_buildx
|
||||||
|
docker :buildx, :create, "--name", builder_name, "--platform", "linux/#{local_arch}", "--driver=docker-container"
|
||||||
|
end
|
||||||
|
|
||||||
|
def append_remote_buildx
|
||||||
|
docker :buildx, :create, "--append", "--name", builder_name, builder_name, "--platform", "linux/#{remote_arch}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def platform_options
|
||||||
|
[ "--platform", "linux/#{local_arch},linux/#{remote_arch}" ]
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
class Kamal::Commands::Builder::Multiarch::Remote < Kamal::Commands::Builder::Base
|
|
||||||
def create
|
|
||||||
combine \
|
|
||||||
create_contexts,
|
|
||||||
create_local_buildx,
|
|
||||||
append_remote_buildx
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove
|
|
||||||
combine \
|
|
||||||
remove_contexts,
|
|
||||||
super
|
|
||||||
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
|
|
||||||
chain \
|
|
||||||
context_host(builder_name_with_arch(local_arch)),
|
|
||||||
context_host(builder_name_with_arch(remote_arch))
|
|
||||||
end
|
|
||||||
|
|
||||||
def config_context_hosts
|
|
||||||
[ local_host, remote_host ].compact
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def builder_name
|
|
||||||
"kamal-#{config.service}-multiarch-remote"
|
|
||||||
end
|
|
||||||
|
|
||||||
def builder_name_with_arch(arch)
|
|
||||||
"#{builder_name}-#{arch}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_local_buildx
|
|
||||||
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local_arch), "--platform", "linux/#{local_arch}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def append_remote_buildx
|
|
||||||
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(remote_arch), "--platform", "linux/#{remote_arch}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_contexts
|
|
||||||
combine \
|
|
||||||
create_context(local_arch, local_host),
|
|
||||||
create_context(remote_arch, remote_host)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_context(arch, host)
|
|
||||||
docker :context, :create, builder_name_with_arch(arch), "--description", "'#{builder_name} #{arch} native host'", "--docker", "'host=#{host}'"
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_contexts
|
|
||||||
combine \
|
|
||||||
remove_context(local_arch),
|
|
||||||
remove_context(remote_arch)
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_context(arch)
|
|
||||||
docker :context, :rm, builder_name_with_arch(arch)
|
|
||||||
end
|
|
||||||
|
|
||||||
def platform_options
|
|
||||||
if local_arch
|
|
||||||
[ "--platform", "linux/#{local_arch}" ]
|
|
||||||
else
|
|
||||||
[ "--platform", "linux/amd64,linux/arm64" ]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
|
class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
|
||||||
def create
|
def create
|
||||||
chain \
|
chain \
|
||||||
create_context,
|
create_remote_context,
|
||||||
create_buildx
|
create_buildx
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
chain \
|
chain \
|
||||||
remove_context,
|
remove_remote_context,
|
||||||
remove_buildx
|
remove_buildx
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
|
|||||||
def push
|
def push
|
||||||
docker :buildx, :build,
|
docker :buildx, :build,
|
||||||
"--push",
|
"--push",
|
||||||
"--platform", platform,
|
*platform_options,
|
||||||
"--builder", builder_name,
|
"--builder", builder_name,
|
||||||
*build_options,
|
*build_options,
|
||||||
build_context
|
build_context
|
||||||
@@ -37,18 +37,14 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
def builder_name
|
def builder_name
|
||||||
"kamal-remote-#{remote_arch}"
|
"kamal-remote-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{remote_arch}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def platform
|
def create_remote_context
|
||||||
"linux/#{remote_arch}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_context
|
|
||||||
docker :context, :create, builder_name, "--description", "'#{builder_name} host'", "--docker", "'host=#{remote_host}'"
|
docker :context, :create, builder_name, "--description", "'#{builder_name} host'", "--docker", "'host=#{remote_host}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_context
|
def remove_remote_context
|
||||||
docker :context, :rm, builder_name
|
docker :context, :rm, builder_name
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -59,4 +55,8 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base
|
|||||||
def remove_buildx
|
def remove_buildx
|
||||||
docker :buildx, :rm, builder_name
|
docker :buildx, :rm, builder_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def platform_options
|
||||||
|
[ "--platform", "linux/#{remote_arch}" ]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user