class Dapp::Dimg::Build::Stage::Base

Attributes

dimg[R]
next_stage[RW]
prev_stage[RW]

Public Class Methods

new(dimg, next_stage) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 13
def initialize(dimg, next_stage)
  @dimg = dimg

  @next_stage = next_stage
  @next_stage.prev_stage = self
end

Public Instance Methods

adding_custom_dir_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 169
def adding_custom_dir_mounts
  config_custom_dir_mounts.in_depth_merge(labels_custom_dir_mounts)
end
adding_mounts_by_type(type) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 165
def adding_mounts_by_type(type)
  (config_mounts_by_type(type) + labels_mounts_by_type(type)).uniq
end
artifact?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 250
def artifact?
  false
end
build!() click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity

# File lib/dapp/dimg/build/stage/base.rb, line 86
def build!
  prev_stage.build! if prev_stage
  renew             if should_be_renewed?
  image_build
end
build_lock!() { || ... } click to toggle source

rubocop:disable Metrics/PerceivedComplexity

# File lib/dapp/dimg/build/stage/base.rb, line 54
def build_lock!
  return yield if dimg.dapp.dry_run?

  try_lock = proc do
    next yield unless image_should_be_locked?

    no_lock = false

    dimg.dapp.lock("#{dimg.dapp.name}.image.#{image.name}") do
      image.reset_image_inspect

      if image_should_be_locked?
        yield
      else
        stage = self
        stage.renew until (stage = stage.next_stage).nil?

        no_lock = true
      end
    end

    yield if no_lock
  end

  if prev_stage
    prev_stage.build_lock! { try_lock.call }
  else
    try_lock.call
  end
end
builder_checksum() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 223
def builder_checksum
end
config_custom_dir_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 173
def config_custom_dir_mounts
  dimg.config._custom_dir_mount.reduce({}) do |mounts, mount|
    from_path = File.expand_path(mount._from)
    mounts[from_path] ||= []
    mounts[from_path] << mount._to
    mounts
  end
end
config_mounts_by_type(type) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 182
def config_mounts_by_type(type)
  dimg.config.public_send("_#{type}_mount").map(&:_to)
end
dependencies() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 242
def dependencies
  []
end
dependencies_discard() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 246
def dependencies_discard
  @dependencies = nil
end
dependencies_empty?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 201
def dependencies_empty?
  dependencies.flatten.compact.delete_if { |val| val.respond_to?(:empty?) && val.empty? }.empty?
end
empty?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 197
def empty?
  dependencies_empty?
end
g_a_stage?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 254
def g_a_stage?
  false
end
get_ruby2go_state_hash() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 20
def get_ruby2go_state_hash
  {}.tap {|hash|
    # NOTICE: Delete this when stage has been moved to go.
    # NOTICE: This is data for build.StubStage and build.StubImage.

    hash["Image"] = {
      "Labels" => image.labels
        .map{|k,v| [k.to_s, v.to_s]}
        .to_h,
      "ServiceChangeLabels" => image.send(:service_change_options)
        .fetch(:label, {})
        .map{|k,v| [k.to_s, v.to_s]}
        .to_h,
    }

    if prev_stage
      hash["PrevStage"] = prev_stage.get_ruby2go_state_hash
    end
  }
end
git_artifacts_dependencies() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 226
def git_artifacts_dependencies
  dimg.git_artifacts.map { |git_artifact| git_artifact.stage_dependencies_checksum(self) }
end
image() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 98
def image
  @image ||= begin
    if empty?
      prev_stage.image
    else
      Image::Stage.image_by_name(name: image_name, from: from_image, dapp: dimg.dapp)
    end
  end
end
image_add_custom_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 146
def image_add_custom_mounts
  adding_custom_dir_mounts.each do |from, to_pathes|
    FileUtils.mkdir_p(from) unless File.exist?(from)
    to_pathes.tap(&:uniq!).map { |to_path| image.add_volume "#{from}:#{to_path}" }
    image.add_service_change_label :"dapp-mount-custom-dir-#{from.gsub('/', '--')}" => to_pathes.join(';')
  end
end
image_add_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 127
def image_add_mounts
  image_add_service_mounts
  image_add_custom_mounts

  image_add_mounts_labels
end
image_add_mounts_labels() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 154
def image_add_mounts_labels
  [:tmp_dir, :build_dir].each do |type|
    next if (mounts = adding_mounts_by_type(type)).empty?
    image.add_service_change_label :"dapp-mount-#{type.to_s.tr('_', '-')}" => mounts.join(';')
  end

  adding_custom_dir_mounts.each do |from, to_pathes|
    image.add_service_change_label :"dapp-mount-custom-dir-#{from.gsub('/', '--')}" => to_pathes.join(';')
  end
end
image_add_service_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 134
def image_add_service_mounts
  [:tmp_dir, :build_dir].each do |type|
    next if (mounts = adding_mounts_by_type(type)).empty?

    mounts.each do |path|
      absolute_path = File.expand_path(File.join('/', path))
      tmp_path = dimg.send(type, 'mount', dimg.dapp.consistent_uniq_slugify(absolute_path)).tap(&:mkpath)
      image.add_volume "#{tmp_path}:#{absolute_path}"
    end
  end
end
labels_custom_dir_mounts() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 190
def labels_custom_dir_mounts
  from_image.labels.map do |label, value|
    next unless label =~ /dapp-mount-custom-dir-(?<from>.+)/
    [File.expand_path(Regexp.last_match(:from).gsub('--', '/')), value.split(';')]
  end.compact.to_h
end
labels_mounts_by_type(type) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 186
def labels_mounts_by_type(type)
  from_image.labels.select { |l, _| l == "dapp-mount-#{type.to_s.tr('_', '-')}" }.map { |_, value| value.split(';') }.flatten
end
layer_commit(git_artifact) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 230
def layer_commit(git_artifact)
  commits[git_artifact] ||= begin
    if image.built?
      image.labels[dimg.dapp.dimgstage_g_a_commit_label(git_artifact.paramshash)]
    elsif g_a_stage? && !empty?
      git_artifact.latest_commit
    elsif prev_stage
      prev_stage.layer_commit(git_artifact)
    end
  end
end
name() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 219
def name
  class_to_lowercase.to_sym
end
prepare_image() { || ... } click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 108
def prepare_image
  return if dimg.dapp.dry_run?

  image_add_mounts

  image.add_service_change_label dapp: dimg.stage_dapp_label
  image.add_service_change_label 'dapp-version'.to_sym => ::Dapp::VERSION
  image.add_service_change_label 'dapp-cache-version'.to_sym => ::Dapp::BUILD_CACHE_VERSION
  image.add_service_change_label 'dapp-dimg'.to_sym => false
  image.add_service_change_label 'dapp-dev-mode'.to_sym => true if dimg.dev_mode?

  if dimg.dapp.ssh_auth_sock
    image.add_volume "#{dimg.dapp.ssh_auth_sock}:/tmp/dapp-ssh-agent"
    image.add_env SSH_AUTH_SOCK: '/tmp/dapp-ssh-agent'
  end

  yield if block_given?
end
save_in_cache!() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 92
def save_in_cache!
  prev_stage.save_in_cache! if prev_stage
  return unless should_be_tagged?
  image.save_in_cache! unless dimg.dapp.dry_run?
end
set_ruby2go_state_hash(hash) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 41
def set_ruby2go_state_hash(hash)
  if prev_stage
    prev_stage.set_ruby2go_state_hash(hash["PrevStage"])
  end

  # NOTICE: This is data from build.StubImage.

  hash["Image"]["ServiceChangeLabels"].each do |k, v|
    image.add_service_change_label(k.to_sym => v)
  end
end
signature() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 205
def signature
  if empty?
    prev_stage.signature
  else
    args = []
    args << prev_stage.signature unless prev_stage.nil?
    args << dimg.build_cache_version
    args << builder_checksum
    args.concat(dependencies.flatten)

    hashsum args
  end
end

Protected Instance Methods

change_options() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 360
def change_options
  @change_options ||= begin
    dimg.config._docker._change_options.to_h.delete_if do |_, val|
      val.nil? || (val.respond_to?(:empty?) && val.empty?)
    end
  end
end
commits() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 372
def commits
  @commits ||= {}
end
commits_discard() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 368
def commits_discard
  @commits = nil
end
from_image() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 354
def from_image
  prev_stage.image if prev_stage || begin
    raise Error::Build, code: :from_image_required
  end
end
image_build() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 276
def image_build
  prepare_image if image_should_be_prepared?

  introspect_image_before_build if image_should_be_introspected_before_build?

  log_image_build do
    dimg.dapp.log_process(log_name,
                          process: dimg.dapp.t(code: 'status.process.building'),
                          short: should_not_be_detailed?) { image.build! }
  end unless empty?

  introspect_image_after_build if image_should_be_introspected_after_build?
end
image_name() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 350
def image_name
  format(dimg.stage_cache_format, signature: signature)
end
image_reset() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 266
def image_reset
  Image::Stage.image_reset(image_name)
  @image = nil
end
image_should_be_locked?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 310
def image_should_be_locked?
  !(empty? || image.built? || should_be_not_present?)
end
image_should_be_prepared?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 314
def image_should_be_prepared?
  (!image.built? && !should_be_not_present? || image_should_be_introspected? && image.tagged?) && !dimg.dapp.dry_run?
end
image_should_be_untagged?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 322
def image_should_be_untagged?
  image.tagged? && current_or_related_image_should_be_untagged?
end
image_should_be_untagged_condition() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 338
def image_should_be_untagged_condition
  return false unless image.tagged?
  dimg.git_artifacts.any? do |git_artifact|
    !git_artifact.repo.commit_exists? layer_commit(git_artifact)
  end
end
image_untag!() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 271
def image_untag!
  return if dimg.dapp.dry_run?
  image.untag!
end
introspect_image_after_build() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 294
def introspect_image_after_build
  introspect_image_default(image)
end
introspect_image_before_build() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 290
def introspect_image_before_build
  introspect_image_default(from_image)
end
introspect_image_default(introspected_image) click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 298
def introspect_image_default(introspected_image)
  if introspected_image.built?
    introspected_image.introspect!
  else
    dimg.dapp.log_warning(desc: { code: :introspect_image_impossible, data: { name: name } })
  end
end
renew() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 260
def renew
  commits_discard
  image_untag! if image_should_be_untagged?
  image_reset
end
should_be_not_present?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 345
def should_be_not_present?
  return false if next_stage.nil?
  !current_or_related_image_should_be_untagged? && (next_stage.image.tagged? && !next_stage.image_should_be_untagged_condition || next_stage.should_be_not_present?)
end
should_be_renewed?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 318
def should_be_renewed?
  current_or_related_image_should_be_untagged?
end
should_be_tagged?() click to toggle source
# File lib/dapp/dimg/build/stage/base.rb, line 306
def should_be_tagged?
  image.built? && !image.tagged?
end