Class: Sidedock::Ports

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sidedock/ports.rb

Instance Method Summary collapse

Methods inherited from Base

#machine, machine

Constructor Details

#initialize(machine_id, port_mapping) ⇒ Ports

Returns a new instance of Ports.



5
6
7
8
9
# File 'lib/sidedock/ports.rb', line 5

def initialize(machine_id, port_mapping)
  @port_mapping = port_mapping
  @machine_id = machine_id
  define_port_accessors if @port_mapping.present?
end

Instance Method Details

#define_port_accessorsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sidedock/ports.rb', line 11

def define_port_accessors
  @port_mapping.each do |name, port_number|
    raise "#{name} cannot be used as port mapping key" if respond_to? :name

    port = find do |port|
      port.internal == port_number
    end

    raise "Port #{port_number} not exposed by Dockerfile, " \
          "change or remove it in the `port_mapping` option. "\
          "Available: #{each.to_h}" unless port.present?

    define_singleton_method name do
      port.external
    end
  end
end

#each(&block) ⇒ Object



29
30
31
# File 'lib/sidedock/ports.rb', line 29

def each(&block)
  ports.each &block
end

#portsObject



33
34
35
36
37
# File 'lib/sidedock/ports.rb', line 33

def ports
  @ports ||= raw_configuration_lines.map do |raw_configuration|
    PortConfiguration.new raw_configuration
  end
end

#raw_configuration_linesObject



39
40
41
# File 'lib/sidedock/ports.rb', line 39

def raw_configuration_lines
  @raw_configuration_lines ||= machine.execute("port #{@machine_id}").strip.split("\n")
end