class Dapp::Dimg::Dimg

Attributes

config[R]
dapp[R]
ignore_signature_auto_calculation[R]
should_be_built[R]

Public Class Methods

new(config:, dapp:, should_be_built: false, ignore_signature_auto_calculation: false) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 23
def initialize(config:, dapp:, should_be_built: false, ignore_signature_auto_calculation: false)
  @config = config
  @dapp = dapp

  @ignore_signature_auto_calculation = ignore_signature_auto_calculation

  @dapp._terminate_dimg_on_terminate(self)

  enable_should_be_built if should_be_built
  should_be_built!
end

Public Instance Methods

after_stages_build!() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 64
def after_stages_build!
  return unless last_stage.image.built? || dev_mode? || force_save_cache?
  last_stage.save_in_cache!
  artifacts.each { |artifact| artifact.last_stage.save_in_cache! }
end
artifact?() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 236
def artifact?
  false
end
artifacts() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 232
def artifacts
  @artifacts ||= artifacts_stages.map { |stage| stage.artifacts.map { |artifact| artifact[:dimg] } }.flatten
end
build!() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 51
def build!
  dapp.lock("#{dapp.name}.images", readonly: true) do
    last_stage.build_lock! do
      begin
        builder.before_build_check
        last_stage.build!
      ensure
        after_stages_build!
      end
    end
  end
end
build_cache_version() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 256
def build_cache_version
  [::Dapp::BUILD_CACHE_VERSION, dev_mode? ? 1 : 0]
end
build_export_image!(image_name, scheme_name:) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 152
def build_export_image!(image_name, scheme_name:)
  Image::Dimg.image_by_name(name: image_name, from: last_stage.image, dapp: dapp).tap do |export_image|
    export_image.add_service_change_label(:'dapp-tag-scheme' => scheme_name)
    export_image.add_service_change_label(:'dapp-dimg' => true)
    export_image.build!
  end
end
builder() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 228
def builder
  @builder ||= Builder.const_get(config._builder.capitalize).new(self)
end
cleanup_tmp() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 260
def cleanup_tmp
  return unless tmp_dir_exists?

  # В tmp-директории могли остаться файлы, владельцами которых мы не являемся.
  # Такие файлы могут попасть туда при экспорте файлов артефакта.
  # Чтобы от них избавиться — запускаем docker-контейнер под root-пользователем
  # и удаляем примонтированную tmp-директорию.
  args = [
    "--rm",
    "--volume=#{dapp.tmp_base_dir}:#{dapp.tmp_base_dir}",
    "--label=dapp=#{dapp.name}",
    "alpine:3.6",
    "rm", "-rf", tmp_path
  ]
  Image::Stage.ruby2go_command(dapp, command: :container_run, options: { args: args })
end
dev_mode?() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 244
def dev_mode?
  dapp.dev_mode?
end
dimg_export_base!(repo, export_format:, push: false) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 97
def dimg_export_base!(repo, export_format:, push: false)
  dapp.lock("#{dapp.name}.images", readonly: true) do
    dapp.tags_by_scheme.each do |tag_scheme_name, tags|
      dapp.log_step_with_indent(tag_scheme_name) do
        tags.each do |tag|
          image_name = format(export_format, repo: repo, dimg_name: name, tag: tag)

          if push && tag_should_not_be_pushed?(tag.to_s)
            dapp.log_state(image_name, state: dapp.t(code: 'state.exist'))
            next
          end

          export_base!(image_name, push: push) do
            export_image = build_export_image!(image_name, scheme_name: tag_scheme_name)
            if push
              export_image.export!
            else
              export_image.tag!
            end
          end
        end
      end unless tags.empty?
    end
  end
end
dimgstage_should_not_be_pushed?(signature) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 123
def dimgstage_should_not_be_pushed?(signature)
  registry_dimgstages_tags.include?(signature)
end
enable_should_be_built() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 35
def enable_should_be_built
  @should_be_built = true
end
export!(repo, format:) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 74
def export!(repo, format:)
  dimg_export_base!(repo, export_format: format, push: true)
end
export_base!(image_name, push: true) { || ... } click to toggle source
# File lib/dapp/dimg/dimg.rb, line 160
def export_base!(image_name, push: true)
  if dapp.dry_run?
    dapp.log_state(image_name, state: dapp.t(code: push ? 'state.push' : 'state.export'), styles: { status: :success })
  else
    dapp.lock("image.#{hashsum image_name}") do
      dapp.log_process(image_name, process: dapp.t(code: push ? 'status.process.pushing' : 'status.process.exporting')) { yield }
    end
  end
end
export_stages!(repo, format:) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 78
def export_stages!(repo, format:)
  dapp.lock("#{dapp.name}.images", readonly: true) do
    export_images.each do |stage_image|
      signature = stage_image.name.split(':').last
      image_name = format(format, repo: repo, signature: signature)

      if dimgstage_should_not_be_pushed?(format(dapp.dimgstage_push_tag_format, signature: signature))
        dapp.log_state(image_name, state: dapp.t(code: 'state.exist'))
        next
      end

      export_base!(image_name, push: true) do
        stage_image.export!(image_name)
      end
    end
    artifacts.each { |artifact| artifact.export_stages!(repo, format: format) }
  end
end
force_save_cache?() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 248
def force_save_cache?
  if ENV.key? "DAPP_FORCE_SAVE_CACHE"
    %w(yes 1 true).include? ENV["DAPP_FORCE_SAVE_CACHE"].to_s
  else
    !!dapp.options[:force_save_cache]
  end
end
get_ruby2go_state_hash() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 16
def get_ruby2go_state_hash
  {
    "Dapp" => dapp.get_ruby2go_state_hash,
    "TmpPath" => tmp_path.to_s,
  }
end
import_base!(image, image_name) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 193
def import_base!(image, image_name)
  if dapp.dry_run?
    dapp.log_state(image_name, state: dapp.t(code: 'state.pull'), styles: { status: :success })
  else
    dapp.lock("image.#{hashsum image_name}") do
      dapp.log_process(image_name,
                       process: dapp.t(code: 'status.process.pulling'),
                       status: { failed: dapp.t(code: 'status.failed.not_pulled') },
                       style: { failed: :secondary }) do
        image.import!(image_name)
      end
    end
  end
end
import_stages!(repo, format:) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 170
def import_stages!(repo, format:)
  dapp.lock("#{dapp.name}.images", readonly: true) do
    import_images.each do |image|
      signature = image.name.split(':').last
      image_name = format(format, repo: repo, signature:signature )

      unless dimgstage_should_not_be_pushed?(format(dapp.dimgstage_push_tag_format, signature: signature))
        dapp.log_state(image_name, state: dapp.t(code: 'state.not_exist'))
        next
      end

      begin
        import_base!(image, image_name)
      rescue ::Dapp::Error::Shellout => e
        dapp.log_info ::Dapp::Helper::NetStatus.message(e)
        next
      end
      break unless !!dapp.options[:pull_all_stages]
    end
    artifacts.each { |artifact| artifact.import_stages!(repo, format: format) }
  end
end
name() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 43
def name
  config._name
end
registry() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 148
def registry
  @registry ||= dapp.dimg_registry
end
registry_dimgstages_tags() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 144
def registry_dimgstages_tags
  @registry_dimgstages_tags ||= registry.dimgstages_tags
end
registry_tags() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 134
def registry_tags
  @registry_tags ||= begin
    if name.nil?
      registry.nameless_dimg_tags
    else
      registry.dimg_tags(name)
    end
  end
end
run(docker_options, command) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 208
def run(docker_options, command)
  run_stage(nil, docker_options, command)
end
run_stage(stage_name, docker_options, command) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 212
def run_stage(stage_name, docker_options, command)
  stage_image = (stage_name.nil? ? last_stage : stage_by_name(stage_name)).image
  raise Error::Dimg, code: :dimg_stage_not_built, data: { stage_name: stage_name } unless stage_image.built?

  args = [docker_options, stage_image.built_id, command].flatten.compact
  if dapp.dry_run?
    dapp.log("docker run #{args.join(' ')}")
  else
    Image::Stage.ruby2go_command(dapp, command: :container_run, options: { args: args })
  end
end
scratch?() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 240
def scratch?
  config._docker._from.nil? && config._from_dimg.nil? && config._from_dimg_artifact.nil?
end
should_be_built!() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 39
def should_be_built!
  raise Error::Dimg, code: :dimg_not_built if should_be_built?
end
stage_image_name(stage_name) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 224
def stage_image_name(stage_name)
  stages.find { |stage| stage.name == stage_name }.image.name
end
stage_should_be_introspected_after_build?(name) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 281
def stage_should_be_introspected_after_build?(name)
  dapp.options[:introspect_stage] == name
end
stage_should_be_introspected_before_build?(name) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 277
def stage_should_be_introspected_before_build?(name)
  dapp.options[:introspect_before] == name
end
tag!(repo, format:) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 70
def tag!(repo, format:)
  dimg_export_base!(repo, export_format: format)
end
tag_should_not_be_pushed?(tag) click to toggle source
# File lib/dapp/dimg/dimg.rb, line 127
def tag_should_not_be_pushed?(tag)
  registry_tags.include?(tag) && begin
    registry_tag_parent = registry.image_parent_id(tag, name)
    registry_tag_parent == last_stage.image.built_id
  end
end
terminate() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 47
def terminate
  cleanup_tmp
end

Protected Instance Methods

should_be_built?() click to toggle source
# File lib/dapp/dimg/dimg.rb, line 287
def should_be_built?
  should_be_built && begin
    builder.before_dimg_should_be_built_check
    !last_stage.image.tagged?
  end
end