Class: Wip::ComposeFile

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/compose_file.rb

Overview

Parses compose.yml/docker-compose.yml into the shape mode: compose-native needs to drive wslc directly, in place of an external compose-for-wslc binary (see ComposeBridge/mode: compose).

This exists only because wslc has no native Compose support yet (tracked upstream in microsoft/WSL#40948). Delete this file — and its hooks in config.rb, cli.rb, doctor.rb and initializer.rb (see README "Compose mode (native)") — once wslc ships that support, or a compose-for-wslc tool reliably supports run.

Defined Under Namespace

Classes: Service

Constant Summary collapse

SERVICE_KEYS =
%w[image build command environment ports volumes working_dir depends_on].freeze
BUILD_KEYS =
%w[context dockerfile].freeze
SUPPORTED_CONDITIONS =
%w[service_started].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services, path:) ⇒ ComposeFile

Returns a new instance of ComposeFile.



35
36
37
38
39
40
# File 'lib/wip/compose_file.rb', line 35

def initialize(services, path:)
  @path = path
  @services = services.to_h { |name, entry| [name.to_s, build_service(name.to_s, entry)] }
  validate_depends_on!
  @order = topological_order
end

Class Method Details

.load(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/wip/compose_file.rb', line 24

def self.load(path)
  raw = YAML.safe_load_file(path, aliases: true)
  raise ConfigError, "#{path}: services: must be a mapping" unless raw.is_a?(Hash) && raw['services'].is_a?(Hash)

  new(raw['services'], path: path)
rescue Psych::Exception => e
  raise ConfigError, "Could not parse #{path}: #{e.message}"
rescue Errno::ENOENT
  raise ConfigError, "Compose file not found: #{path}"
end

Instance Method Details

#build_specsObject

name => dockerfile:, tag: for every service with a build:.



45
46
47
48
49
50
51
52
# File 'lib/wip/compose_file.rb', line 45

def build_specs
  @order.filter_map do |name|
    build = @services.fetch(name).build
    next unless build

    [name, build.merge('tag' => image_tag(name))]
  end.to_h
end

#service_names_in_dependency_orderObject



42
# File 'lib/wip/compose_file.rb', line 42

def service_names_in_dependency_order = @order.dup

#to_dependencies_hashObject

Shaped like Config::DEPENDENCY_DEFAULTS expects: image/command/env/ports/volumes/workdir, in dependency order so callers iterating sidecars start them before their dependents.



56
57
58
59
60
61
62
63
# File 'lib/wip/compose_file.rb', line 56

def to_dependencies_hash
  @order.to_h do |name|
    service = @services.fetch(name)
    [name, { 'image' => service.build ? image_tag(name) : service.image, 'command' => service.command,
             'env' => service.env, 'ports' => service.ports, 'volumes' => service.volumes,
             'workdir' => service.workdir }]
  end
end