class Dapp::Dimg::Builder::Chef

Constants

CHEFDK_VERSION

Public Instance Methods

before_build_check() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 46
def before_build_check
  %i(before_install install before_setup setup build_artifact).tap do |stages|
    (builder_cookbook.enabled_recipes -
     stages.map {|stage| builder_cookbook.stage_enabled_recipes(stage)}.flatten.uniq).each do |recipe|
      dimg.dapp.log_warning(desc: {code: :recipe_does_not_used, data: {recipe: recipe}})
    end
  end
end
before_dimg_should_be_built_check() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 36
def before_dimg_should_be_built_check
  super

  %i(before_install install before_setup setup).each do |stage|
    unless stage_empty?(stage) || stage_cookbooks_checksum_path(stage).exist?
      raise Error::Chef, code: :stage_checksum_not_calculated, data: {stage: stage}
    end
  end
end
berks_bin() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 10
def berks_bin
  "/.dapp/deps/chefdk/#{CHEFDK_VERSION}/bin/berks"
end
builder_cookbook() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 83
def builder_cookbook
  @builder_cookbook ||= begin
    unless dimg.dapp.builder_cookbook_path.exist?
      raise Error::Chef,
            code: :builder_cookbook_not_found,
            data: {path: dimg.dapp.builder_cookbook_path.to_s}
    end

    cookbooks = Marshal.load Marshal.dump(dimg.config._chef._cookbook)

    cookbooks.each do |_name, desc|
      # Получение относительного пути из директории .dapp_chef до указанной зависимости.
      # В Dappfile указываются пути относительно самого Dappfile либо абсолютные пути.
      # В объекте конфига должны лежать абсолютные пути по ключу :path.
      if desc[:path]
        relative_from_cookbook_path = Pathname.new(desc[:path]).relative_path_from(dimg.dapp.builder_cookbook_path).to_s
        desc[:path] = relative_from_cookbook_path
      end
    end

    # Добавление самого cookbook'а builder'а.
    cookbooks[dimg.dapp.name] = {
      name: dimg.dapp.name,
      path: '.'
    }

    berksfile = Berksfile.from_conf(cookbook_path: dimg.dapp.builder_cookbook_path.to_s, cookbooks: cookbooks)
    metadata = CookbookMetadata.from_conf(name: dimg.dapp.name, version: '1.0.0', cookbooks: cookbooks)

    Cookbook.new(self,
                 path: dimg.dapp.builder_cookbook_path,
                 berksfile: berksfile,
                 metadata: metadata,
                 enabled_recipes: dimg.config._chef._recipe,
                 enabled_modules: dimg.config._chef._dimod)
  end
end
chef_solo_bin() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 6
def chef_solo_bin
  "/.dapp/deps/chefdk/#{CHEFDK_VERSION}/bin/chef-solo"
end
chefdk_container() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 63
def chefdk_container
  @chefdk_container ||= begin
    is_container_exist = proc{dimg.dapp.shellout("#{dimg.dapp.host_docker} inspect #{chefdk_container_name}").exitstatus.zero?}
    if !is_container_exist.call
      dimg.dapp.lock("dappdeps.container.#{chefdk_container_name}", default_timeout: 600) do
        if !is_container_exist.call
          dimg.dapp.log_secondary_process(dimg.dapp.t(code: 'process.chefdk_container_creating', data: {name: chefdk_container_name}), short: true) do
            dimg.dapp.shellout!(
              ["#{dimg.dapp.host_docker} create",
              "--name #{chefdk_container_name}",
              "--volume /.dapp/deps/chefdk/#{CHEFDK_VERSION} #{chefdk_image}"].join(' ')
            )
          end
        end
      end
    end
    chefdk_container_name
  end
end
chefdk_container_name() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 59
def chefdk_container_name # FIXME: hashsum(image) or dockersafe()
  "dappdeps_chefdk_#{CHEFDK_VERSION}"
end
chefdk_image() click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 55
def chefdk_image
  "dappdeps/chefdk:#{CHEFDK_VERSION}" # TODO: config, DSL, DEFAULT_CHEFDK_IMAGE
end
container_stage_build_path(_stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 222
def container_stage_build_path(_stage)
  Pathname.new('/.dapp/chef/build')
end
container_stage_config_path(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 201
def container_stage_config_path(stage)
  install_chef_solo_stage_config(stage)
  container_stage_build_path(stage).join('config.rb')
end
container_stage_json_attributes_path(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 213
def container_stage_json_attributes_path(stage)
  install_json_attributes(stage)
  container_stage_build_path(stage).join('attributes.json')
end
install_chef_solo_stage_config(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 190
def install_chef_solo_stage_config(stage)
  @install_chef_solo_stage_config ||= {}
  @install_chef_solo_stage_config[stage] ||= true.tap do
    stage_build_path(stage).join('config.rb').write [
      "file_cache_path \"/.dapp/chef/cache\"\n",
      "cookbook_path \"#{container_stage_build_path(stage).join('cookbooks')}\"\n",
      ("log_level :debug\n" if ENV["DAPP_CHEF_DEBUG"]),
    ].compact.join
  end
end
install_json_attributes(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 206
def install_json_attributes(stage)
  @install_json_attributes ||= {}
  @install_json_attributes[stage] ||= true.tap do
    stage_build_path(stage).join('attributes.json').write "#{stage_attributes_raw(stage)}\n"
  end
end
stage_attributes(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 121
def stage_attributes(stage)
  dimg.config._chef.send("__#{stage}_attributes")
end
stage_attributes_raw(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 125
def stage_attributes_raw(stage)
  JSON.dump stage_attributes(stage)
end
stage_build_path(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 218
def stage_build_path(stage)
  dimg.tmp_path(dimg.name).join(stage.to_s)
end
stage_cookbooks_checksum(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 133
def stage_cookbooks_checksum(stage)
  if stage_cookbooks_checksum_path(stage).exist?
    stage_cookbooks_checksum_path(stage).read.strip
  else
    checksum = builder_cookbook.stage_checksum(stage)

    stage_cookbooks_checksum_path(stage).tap do |path|
      path.parent.mkpath
      path.write "#{checksum}\n"
    end

    checksum
  end
end
stage_cookbooks_checksum_path(stage) click to toggle source
# File lib/dapp/dimg/builder/chef.rb, line 129
def stage_cookbooks_checksum_path(stage)
  dimg.build_path.join("#{builder_cookbook.checksum}.#{dimg.name}.#{stage}.checksum")
end
stage_cookbooks_runlist(stage) click to toggle source

rubocop:disable Metrics/PerceivedComplexity

# File lib/dapp/dimg/builder/chef.rb, line 149
def stage_cookbooks_runlist(stage)
  @stage_cookbooks_runlist ||= {}
  @stage_cookbooks_runlist[stage] ||= begin
    res = []

    format_entry = proc do |cookbook, entrypoint|
      entrypoint = 'void' if entrypoint.nil?
      "#{cookbook}::#{entrypoint}"
    end

    builder_cookbook.enabled_modules.map do |cookbook|
      if builder_cookbook.stage_enabled_modules(stage).include? cookbook
        [cookbook, stage]
      else
        [cookbook, nil]
      end
    end.tap {|entries| res.concat entries}

    builder_cookbook.stage_enabled_recipes(stage)
                    .map {|recipe| [builder_cookbook.name, recipe]}
                    .tap do |entries|
      if entries.any?
        res.concat entries
      else
        res << [builder_cookbook.name, nil]
      end
    end

    if res.all? {|_, entrypoint| entrypoint.nil?}
      []
    else
      res.map(&format_entry)
    end
  end
end
stage_empty?(stage) click to toggle source

rubocop:enable Metrics/PerceivedComplexity

# File lib/dapp/dimg/builder/chef.rb, line 186
def stage_empty?(stage)
  stage_cookbooks_runlist(stage).empty?
end