Class: Docker::Compose::Mapper
- Inherits:
-
Object
- Object
- Docker::Compose::Mapper
- Defined in:
- lib/docker/compose/mapper.rb
Overview
Uses a Session to discover information about services’ IP addresses and ports as reachable from the host, then
Constant Summary collapse
- ELIDED =
Pattern that matches an “elided” host or port that should be omitted from output, but is needed to identify a specific container and port.
/^\[.+\]$/.freeze
- REMOVE_ELIDED =
Regexp that can be used with gsub to strip elision marks
/[\[\]]/.freeze
- BadSubstitution =
Class.new(StandardError)
- NoService =
Class.new(RuntimeError)
Class Method Summary collapse
-
.map(env, strict: true, session: Session.new, net_info: NetInfo.new) { ... } ⇒ Object
Instantiate a mapper; map some environment variables; yield to caller for additional processing.
Instance Method Summary collapse
-
#initialize(session, docker_host, strict: true) ⇒ Mapper
constructor
Create an instance of Mapper.
-
#map(value) ⇒ String, Array
Substitute service hostnames and ports that appear in a URL or a host:port string.
-
#published_port(service, port) ⇒ Integer
Figure out which host port a given service’s port has been published to, and/or whether that service is running.
Constructor Details
#initialize(session, docker_host, strict: true) ⇒ Mapper
Create an instance of Mapper
42 43 44 45 46 |
# File 'lib/docker/compose/mapper.rb', line 42 def initialize(session, docker_host, strict:true) @session = session @docker_host = docker_host @strict = strict end |
Class Method Details
.map(env, strict: true, session: Session.new, net_info: NetInfo.new) { ... } ⇒ Object
Instantiate a mapper; map some environment variables; yield to caller for additional processing.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/docker/compose/mapper.rb', line 22 def self.map(env, strict:true, session:Session.new, net_info:NetInfo.new) mapper = self.new(session, net_info.docker_routable_ip, strict:strict) env.each_pair do |k, v| begin v = mapper.map(v) yield(k, v) rescue NoService yield(k, nil) end end end |
Instance Method Details
#map(value) ⇒ String, Array
Substitute service hostnames and ports that appear in a URL or a host:port string. If either component of a host:port string is surrounded by square brackets, “elide” that component, removing it from the result but using it to find the correct service and port.
71 72 73 74 75 76 77 |
# File 'lib/docker/compose/mapper.rb', line 71 def map(value) if value.respond_to?(:map) value.map { |e| map_scalar(e) } else map_scalar(value) end end |
#published_port(service, port) ⇒ Integer
Figure out which host port a given service’s port has been published to, and/or whether that service is running. Cannot distinguish between the “service not running” case and the “container port not published” case!
85 86 87 88 89 90 |
# File 'lib/docker/compose/mapper.rb', line 85 def published_port(service, port) result = @session.port(service, port) Integer(result.split(':').last.gsub("\n", "")) rescue RuntimeError raise NoService, "Service '#{service}' not running, or does not publish port '#{port}'" end |