Class: PicsolveDockerBuilder::Composer::Container

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/composer/container.rb

Overview

Represents an container in an namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#base_dir, #config_file, #config_path, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(name, config, composer) ⇒ Container

Returns a new instance of Container.



11
12
13
14
15
16
# File 'lib/picsolve_docker_builder/composer/container.rb', line 11

def initialize(name, config, composer)
  log.info "Image name=#{name} config=#{config}"
  @config = config
  @name = name
  @composer = composer
end

Instance Attribute Details

#composerObject (readonly)

Returns the value of attribute composer.



10
11
12
# File 'lib/picsolve_docker_builder/composer/container.rb', line 10

def composer
  @composer
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/picsolve_docker_builder/composer/container.rb', line 10

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/picsolve_docker_builder/composer/container.rb', line 10

def name
  @name
end

Instance Method Details

#environment(kubernetes) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/picsolve_docker_builder/composer/container.rb', line 50

def environment(kubernetes)
  env = []
  log.info "evaluate dynamic stage variables #{kubernetes.stage}"
  requirements(kubernetes).each do |req|
    env += req.environment
  end
  env += environment_static
  env
end

#environment_staticObject



60
61
62
63
64
65
66
67
68
# File 'lib/picsolve_docker_builder/composer/container.rb', line 60

def environment_static
  return [] unless config.key? 'environment'
  config['environment'].map do |key, value|
    {
      'name' => key,
      'value' => value
    }
  end
end

#hashObject



126
127
128
# File 'lib/picsolve_docker_builder/composer/container.rb', line 126

def hash
  repo_tag_hash_unique[:hash]
end

#liveness_probeObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/picsolve_docker_builder/composer/container.rb', line 96

def liveness_probe
  # get port with health check
  port = nil
  config['ports'].each do |p|
    next unless p['health_check'] == 'http'
    port = p
    break
  end

  # no health check
  return nil if port.nil?

  {
    'httpGet' => {
      'path' => '/healthcheck',
      'port' => port['container_port']
    },
    'initialDelaySeconds' => 300,
    'timeoutSeconds' => 5
  }
end

#portsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/picsolve_docker_builder/composer/container.rb', line 70

def ports
  config['ports'].map do |port|
    if port.is_a? String
      port_split = port.split(/:/)
      container_port = port_split[1]
    else
      container_port = port['container_port']
    end
    {
      'port' => container_port.to_i
    }
  end
rescue StandardError
  []
end

#ports_rcObject



86
87
88
89
90
91
92
93
94
# File 'lib/picsolve_docker_builder/composer/container.rb', line 86

def ports_rc
  p = []
  ports.each do |port|
    p << {
      'containerPort' => port['port']
    }
  end
  p
end

#rc(kubernetes) ⇒ Object



22
23
24
25
26
27
# File 'lib/picsolve_docker_builder/composer/container.rb', line 22

def rc(kubernetes)
  @rcs = {} if @rcs.nil?
  @rcs[kubernetes] = kubernetes.rc(self) \
    unless @rcs.key?(kubernetes)
  @rcs[kubernetes]
end

#repo_tagObject



18
19
20
# File 'lib/picsolve_docker_builder/composer/container.rb', line 18

def repo_tag
  config['image']
end

#repo_tag_hash_uniqueObject



118
119
120
# File 'lib/picsolve_docker_builder/composer/container.rb', line 118

def repo_tag_hash_unique
  @repo_tag_hash_unique ||= Registry.repo_tag_unique(config['image'])
end

#repo_tag_uniqueObject



122
123
124
# File 'lib/picsolve_docker_builder/composer/container.rb', line 122

def repo_tag_unique
  repo_tag_hash_unique[:tag_unique]
end

#requirements(kubernetes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/picsolve_docker_builder/composer/container.rb', line 36

def requirements(kubernetes)
  return [] unless config.key? 'requirements'
  config['requirements'].map do |name, config|
    PicsolveDockerBuilder::Composer::Requirements.new(
      name,
      config,
      PicsolveDockerBuilder::Composer::Requirements::Context.new(
        kubernetes,
        self
      )
    )
  end
end

#service(kubernetes) ⇒ Object



29
30
31
32
33
34
# File 'lib/picsolve_docker_builder/composer/container.rb', line 29

def service(kubernetes)
  @services = {} if @rcs.nil?
  @services[kubernetes] = kubernetes.service(self) \
    unless @services.key?(kubernetes)
  @services[kubernetes]
end