Sha256: 46d49c356acc32b709fda7ddbdfd42ba345ed67f9dcba556744a03183e10f838

Contents?: true

Size: 1.85 KB

Versions: 14

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'set'
require 'hako/app_container'
require 'hako/container'
require 'hako/loader'

module Hako
  class DefinitionLoader
    # @param [Application] app
    # @param [Boolean] dry_run
    def initialize(app, dry_run:)
      @app = app
      @dry_run = dry_run
    end

    # @param [String, nil] tag
    # @param [Array<String>, nil] with
    # @return [Hash<String, Container>]
    def load(tag: nil, with: nil)
      # XXX: Load additional_containers for compatibility
      sidecars = @app.definition.fetch('sidecars', @app.definition.fetch('additional_containers', {}))
      container_names = ['app']
      if with
        container_names.concat(with)
      else
        container_names.concat(sidecars.keys)
      end

      load_containers_from_name(tag, container_names, sidecars)
    end

    private

    # @param [String, nil] tag
    # @param [Array<String>] container_names
    # @param [Hash<String, Hash>] sidecars
    # @return [Hash<String, Container>]
    def load_containers_from_name(tag, container_names, sidecars)
      names = Set.new(container_names)
      containers = {}
      while containers.size < names.size
        names.difference(containers.keys).each do |name|
          containers[name] =
            case name
            when 'app'
              @app.definition['app']['tag'] = tag if tag
              AppContainer.new(@app, @app.definition['app'], dry_run: @dry_run)
            else
              Container.new(@app, sidecars.fetch(name), dry_run: @dry_run)
            end

          containers[name].links.each do |link|
            m = link.match(/\A([^:]+):([^:]+)\z/)
            names << (m ? m[1] : link)
          end

          containers[name].volumes_from.each do |volumes_from|
            names << volumes_from[:source_container]
          end
        end
      end
      containers
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hako-2.17.0 lib/hako/definition_loader.rb
hako-2.16.0 lib/hako/definition_loader.rb
hako-2.15.1 lib/hako/definition_loader.rb
hako-2.15.0 lib/hako/definition_loader.rb
hako-2.14.0 lib/hako/definition_loader.rb
hako-2.13.0 lib/hako/definition_loader.rb
hako-2.12.0 lib/hako/definition_loader.rb
hako-2.11.1 lib/hako/definition_loader.rb
hako-2.11.0 lib/hako/definition_loader.rb
hako-2.10.0 lib/hako/definition_loader.rb
hako-2.9.2 lib/hako/definition_loader.rb
hako-2.9.1 lib/hako/definition_loader.rb
hako-2.9.0 lib/hako/definition_loader.rb
hako-2.8.0 lib/hako/definition_loader.rb