Various code style improvements

This commit is contained in:
Igor Alexandrov
2023-06-18 23:39:44 +04:00
parent aa28ee0f3e
commit dadd8225da
6 changed files with 54 additions and 42 deletions

View File

@@ -7,9 +7,9 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
def target def target
case case
when !config.builder.multiarch? && !config.builder.cache? when !config.builder.multiarch? && !config.builder.cached?
native native
when !config.builder.multiarch? && config.builder.cache? when !config.builder.multiarch? && config.builder.cached?
native_cached native_cached
when config.builder.local? && config.builder.remote? when config.builder.local? && config.builder.remote?
multiarch_remote multiarch_remote

View File

@@ -3,6 +3,7 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
class BuilderError < StandardError; end class BuilderError < StandardError; end
delegate :argumentize, to: Mrsk::Utils delegate :argumentize, to: Mrsk::Utils
delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, to: :builder_config
def clean def clean
docker :image, :rm, "--force", config.absolute_image docker :image, :rm, "--force", config.absolute_image
@@ -27,9 +28,9 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
end end
def build_cache def build_cache
if config.builder.cache? if cache_to && cache_from
["--cache-to", config.builder.cache_to, ["--cache-to", cache_to,
"--cache-from", config.builder.cache_from] "--cache-from", cache_from]
end end
end end
@@ -38,18 +39,22 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
end end
def build_args def build_args
argumentize "--build-arg", config.builder.args, sensitive: true argumentize "--build-arg", args, sensitive: true
end end
def build_secrets def build_secrets
argumentize "--secret", config.builder.secrets.collect { |secret| [ "id", secret ] } argumentize "--secret", secrets.collect { |secret| [ "id", secret ] }
end end
def build_dockerfile def build_dockerfile
if Pathname.new(File.expand_path(config.builder.dockerfile)).exist? if Pathname.new(File.expand_path(dockerfile)).exist?
argumentize "--file", config.builder.dockerfile argumentize "--file", dockerfile
else else
raise BuilderError, "Missing #{config.builder.dockerfile}" raise BuilderError, "Missing #{dockerfile}"
end end
end end
def builder_config
config.builder
end
end end

View File

@@ -22,17 +22,17 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
end end
def create_local_buildx def create_local_buildx
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(config.builder.local_arch), "--platform", "linux/#{config.builder.local_arch}" docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local_arch), "--platform", "linux/#{local_arch}"
end end
def append_remote_buildx def append_remote_buildx
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(config.builder.remote_arch), "--platform", "linux/#{config.builder.remote_arch}" docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(remote_arch), "--platform", "linux/#{remote_arch}"
end end
def create_contexts def create_contexts
combine \ combine \
create_context(config.builder.local_arch, config.builder.local_host), create_context(local_arch, local_host),
create_context(config.builder.remote_arch, config.builder.remote_host) create_context(remote_arch, remote_host)
end end
def create_context(arch, host) def create_context(arch, host)
@@ -41,8 +41,8 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
def remove_contexts def remove_contexts
combine \ combine \
remove_context(config.builder.local_arch), remove_context(local_arch),
remove_context(config.builder.remote_arch) remove_context(remote_arch)
end end
def remove_context(arch) def remove_context(arch)

View File

@@ -33,16 +33,16 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
end end
def builder_name_with_arch def builder_name_with_arch
"#{builder_name}-#{config.builder.remote_arch}" "#{builder_name}-#{remote_arch}"
end end
def platform def platform
"linux/#{config.builder.remote_arch}" "linux/#{remote_arch}"
end end
def create_context def create_context
docker :context, :create, docker :context, :create,
builder_name_with_arch, "--description", "'#{builder_name} #{config.builder.remote_arch} native host'", "--docker", "'host=#{config.builder.remote_host}'" builder_name_with_arch, "--description", "'#{builder_name} #{remote_arch} native host'", "--docker", "'host=#{remote_host}'"
end end
def remove_context def remove_context

View File

@@ -23,7 +23,7 @@ class Mrsk::Configuration::Builder
!!@options["remote"] !!@options["remote"]
end end
def cache? def cached?
!!@options["cache"] !!@options["cache"]
end end
@@ -60,39 +60,28 @@ class Mrsk::Configuration::Builder
end end
def cache_from def cache_from
if cache? if cached?
case @options["cache"]["type"] case @options["cache"]["type"]
when 'gha' when 'gha'
"type=gha" "type=gha"
when 'registry' when 'registry'
[ cache_from_config_for_registry
"type=registry",
"ref=#{@server}/#{cache_image}"
].compact.join(",")
end end
end end
end end
def cache_to def cache_to
if cache? if cached?
case @options["cache"]["type"] case @options["cache"]["type"]
when "gha" when "gha"
[ cache_to_config_for_gha
"type=gha",
@options["cache"]&.fetch("options", nil),
].compact.join(",")
when "registry" when "registry"
[ cache_to_config_for_registry
"type=registry",
@options["cache"]&.fetch("options", nil),
"ref=#{@server}/#{cache_image}"
].compact.join(",")
end end
end end
end end
private private
def valid? def valid?
if @options["local"] && !@options["remote"] if @options["local"] && !@options["remote"]
raise ArgumentError, "You must specify both local and remote builder config for remote multiarch builds" raise ArgumentError, "You must specify both local and remote builder config for remote multiarch builds"
@@ -107,7 +96,25 @@ class Mrsk::Configuration::Builder
@options["cache"]&.fetch("image", nil) || "#{@image}-build-cache" @options["cache"]&.fetch("image", nil) || "#{@image}-build-cache"
end end
def current_branch def cache_from_config_for_registry
`git rev-parse --abbrev-ref HEAD`.strip [
"type=registry",
"ref=#{@server}/#{cache_image}"
].compact.join(",")
end end
end
def cache_to_config_for_gha
[
"type=gha",
@options["cache"]&.fetch("options", nil),
].compact.join(",")
end
def cache_to_config_for_registry
[
"type=registry",
@options["cache"]&.fetch("options", nil),
"ref=#{@server}/#{cache_image}"
].compact.join(",")
end
end

View File

@@ -68,8 +68,8 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", @config_with_builder_option.builder.local_host assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", @config_with_builder_option.builder.local_host
end end
test "cache?" do test "cached?" do
assert_equal false, @config.builder.cache? assert_equal false, @config.builder.cached?
end end
test "invalid cache type specified" do test "invalid cache type specified" do
@@ -148,4 +148,4 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal "..", @config_with_builder_option.builder.context assert_equal "..", @config_with_builder_option.builder.context
end end
end end