Module: DockerComposeEnv

Defined in:
lib/docker_compose_env.rb,
lib/docker_compose_env/railtie.rb,
lib/docker_compose_env/version.rb

Overview

The main module for DockerComposeEnv, this provides a single “easy mode” method for setting up the environment.

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
'0.2.1'.freeze

Class Method Summary collapse

Class Method Details

.setup!(env: ENV, file: 'docker-compose.yml', process_env: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docker_compose_env.rb', line 8

def self.setup!(env: ENV, file: 'docker-compose.yml', process_env: {})
  config = YAML.safe_load(File.read(file))

  config.fetch('services', {}).keys.each do |service_name|
    (config['services'][service_name]['ports'] || []).each do |container_port|
      compose_port_info = nil

      if /(\d+)\/udp$/ =~ container_port
        parsed_port = /(?<port>\d+)\/udp$/.match(container_port).named_captures['port']

        command = "docker-compose --file=#{file} port --protocol=udp #{service_name} #{parsed_port}"
        Open3.popen3(process_env, command) do |stdin, stdout, stderr, wait_thr|
          compose_port_info = stdout.read
          wait_thr.value
        end

        if (compose_port_info =~ /^0\.0\.0\.0\:\d+\s*$/) == 0
          if env["#{service_name.upcase}_HOST"].nil?
            env["#{service_name.upcase}_HOST"] = '0.0.0.0'
          end

          if env["#{service_name.upcase}_PORT_#{parsed_port}_UDP"].nil?
            env["#{service_name.upcase}_PORT_#{parsed_port}_UDP"] = compose_port_info.gsub(/0\.0\.0\.0:(\d+)\s*/, '\1')
          end
        end
      else
        command = "docker-compose --file=#{file} port #{service_name} #{container_port}"
        Open3.popen3(process_env, command) do |stdin, stdout, stderr, wait_thr|
          compose_port_info = stdout.read
          wait_thr.value
        end

        if (compose_port_info =~ /^0\.0\.0\.0\:\d+\s*$/) == 0
          if env["#{service_name.upcase}_HOST"].nil?
            env["#{service_name.upcase}_HOST"] = '0.0.0.0'
          end

          if env["#{service_name.upcase}_PORT_#{container_port}"].nil?
            env["#{service_name.upcase}_PORT_#{container_port}"] = compose_port_info.gsub(/0\.0\.0\.0:(\d+)\s*/, '\1')
          end
        end
      end
    end
  end
rescue Errno::ENOENT => exception
  raise exception unless exception.message == 'No such file or directory - docker-compose'
end