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

Simplify builders config #905

Merged
merged 17 commits into from
Aug 29, 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
16 changes: 5 additions & 11 deletions lib/kamal/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,9 @@ def push
say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow
end

# Get the command here to ensure the Dir.chdir doesn't interfere with it
push = KAMAL.builder.push

run_locally do
begin
context_hosts = capture_with_info(*KAMAL.builder.context_hosts).split("\n")

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
execute *KAMAL.builder.buildx_inspect
rescue SSHKit::Command::Failed => e
if e.message =~ /(context not found|no builder|does not exist)/
warn "Missing compatible builder, so creating a new one first"
Expand All @@ -51,6 +42,9 @@ def push
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
Dir.chdir(KAMAL.config.builder.build_directory) { execute *push }
end
Expand All @@ -72,7 +66,7 @@ def pull

desc "create", "Create a build setup"
def create
if (remote_host = KAMAL.config.builder.remote_host)
if (remote_host = KAMAL.config.builder.remote)
connect_to_remote_host(remote_host)
end

Expand Down
14 changes: 4 additions & 10 deletions lib/kamal/cli/templates/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ registry:
password:
- KAMAL_REGISTRY_PASSWORD

# Configure builder setup.
builder:
arch: amd64

# Inject ENV variables into containers (secrets come from .env).
# Remember to run `kamal env push` after making changes!
# env:
Expand All @@ -30,16 +34,6 @@ registry:
# ssh:
# user: app

# Configure builder setup.
# builder:
# args:
# RUBY_VERSION: 3.2.0
# secrets:
# - GITHUB_TOKEN
# remote:
# arch: amd64
# host: ssh://app@192.168.0.1

# Use accessory services (secrets come from .env).
# accessories:
# db:
Expand Down
42 changes: 13 additions & 29 deletions lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "active_support/core_ext/string/filters"

class Kamal::Commands::Builder < Kamal::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, :context_hosts, :config_context_hosts, :validate_image,
:first_mirror, to: :target
delegate :create, :remove, :push, :clean, :pull, :info, :buildx_inspect, :validate_image, :first_mirror, to: :target
delegate :local?, :remote?, to: "config.builder"

include Clone

Expand All @@ -11,43 +11,27 @@ def name
end

def target
if config.builder.multiarch?
if config.builder.remote?
if config.builder.local?
multiarch_remote
else
native_remote
end
if remote?
if local?
hybrid
else
multiarch
remote
end
else
if config.builder.cached?
native_cached
else
native
end
local
end
end

def native
@native ||= Kamal::Commands::Builder::Native.new(config)
end

def native_cached
@native ||= Kamal::Commands::Builder::Native::Cached.new(config)
end

def native_remote
@native ||= Kamal::Commands::Builder::Native::Remote.new(config)
def remote
@remote ||= Kamal::Commands::Builder::Remote.new(config)
end

def multiarch
@multiarch ||= Kamal::Commands::Builder::Multiarch.new(config)
def local
@local ||= Kamal::Commands::Builder::Local.new(config)
end

def multiarch_remote
@multiarch_remote ||= Kamal::Commands::Builder::Multiarch::Remote.new(config)
def hybrid
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
end


Expand Down
37 changes: 27 additions & 10 deletions lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@

class Kamal::Commands::Builder::Base < Kamal::Commands::Base
class BuilderError < StandardError; end

ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'"

delegate :argumentize, to: Kamal::Utils
delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config
delegate \
:args, :secrets, :dockerfile, :target, :arches, :local_arches, :remote_arches, :remote,
:cache_from, :cache_to, :ssh, :driver, :docker_driver?,
to: :builder_config

def clean
docker :image, :rm, "--force", config.absolute_image
end

def push
docker :buildx, :build,
"--push",
*platform_options(arches),
*([ "--builder", builder_name ] unless docker_driver?),
*build_options,
build_context
end

def pull
docker :pull, config.absolute_image
end

def info
combine \
docker(:context, :ls),
docker(:buildx, :ls)
end

def buildx_inspect
docker :buildx, :inspect, builder_name unless docker_driver?
end

def build_options
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ]
end
Expand All @@ -32,14 +53,6 @@ def validate_image
)
end

def context_hosts
:true
end

def config_context_hosts
[]
end

def first_mirror
docker(:info, "--format '{{index .RegistryConfig.Mirrors 0}}'")
end
Expand Down Expand Up @@ -91,4 +104,8 @@ def builder_config
def context_host(builder_name)
docker :context, :inspect, builder_name, "--format", ENDPOINT_DOCKER_HOST_INSPECT
end

def platform_options(arches)
argumentize "--platform", arches.map { |arch| "linux/#{arch}" }.join(",") if arches.any?
end
end
21 changes: 21 additions & 0 deletions lib/kamal/commands/builder/hybrid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Remote
def create
combine \
create_local_buildx,
create_remote_context,
append_remote_buildx
end

private
def builder_name
"kamal-hybrid-#{driver}-#{remote.gsub(/[^a-z0-9_-]/, "-")}"
end

def create_local_buildx
docker :buildx, :create, *platform_options(local_arches), "--name", builder_name, "--driver=#{driver}"
end

def append_remote_buildx
docker :buildx, :create, *platform_options(remote_arches), "--append", "--name", builder_name, builder_name
end
end
14 changes: 14 additions & 0 deletions lib/kamal/commands/builder/local.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Kamal::Commands::Builder::Local < Kamal::Commands::Builder::Base
def create
docker :buildx, :create, "--name", builder_name, "--driver=#{driver}" unless docker_driver?
end

def remove
docker :buildx, :rm, builder_name unless docker_driver?
end

private
def builder_name
"kamal-local-#{driver}"
end
end
41 changes: 0 additions & 41 deletions lib/kamal/commands/builder/multiarch.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/kamal/commands/builder/multiarch/remote.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/kamal/commands/builder/native.rb

This file was deleted.

25 changes: 0 additions & 25 deletions lib/kamal/commands/builder/native/cached.rb

This file was deleted.

Loading