Class: Hako::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/hako/container.rb

Direct Known Subclasses

AppContainer

Constant Summary collapse

PROVIDERS_KEY =
'$providers'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, definition, dry_run:) ⇒ Container

Returns a new instance of Container.

Parameters:

  • app (Application)
  • definition (Hash)
  • dry_run (Boolean)


15
16
17
18
19
20
# File 'lib/hako/container.rb', line 15

def initialize(app, definition, dry_run:)
  @app = app
  @definition = default_config.merge(definition)
  @definition['docker_labels'].merge!(default_labels)
  @dry_run = dry_run
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



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

def definition
  @definition
end

Instance Method Details

#default_configHash (private)

Returns:

  • (Hash)


244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/hako/container.rb', line 244

def default_config
  {
    'cpu' => 0,
    'env' => {},
    'docker_labels' => {},
    'links' => [],
    'essential' => true,
    'mount_points' => [],
    'port_mappings' => [],
    'volumes_from' => [],
    'privileged' => false,
  }
end

#default_labelsHash<String, String> (private)

Returns:

  • (Hash<String, String>)


259
260
261
262
263
# File 'lib/hako/container.rb', line 259

def default_labels
  {
    'cc.wanko.hako.version' => VERSION,
  }
end

#depends_onObject



205
206
207
208
209
210
211
212
213
214
# File 'lib/hako/container.rb', line 205

def depends_on
  if @definition.key?('depends_on')
    @definition['depends_on'].map do |depends_on|
      {
        container_name: depends_on.fetch('container_name'),
        condition: depends_on.fetch('condition'),
      }
    end
  end
end

#envHash<String, String>

Returns:

  • (Hash<String, String>)


48
49
50
# File 'lib/hako/container.rb', line 48

def env
  @expanded_env ||= expand_env(@definition.fetch('env'))
end

#expand_env(env) ⇒ Hash<String, String> (private)

Parameters:

  • env (Hash<String, String>)

Returns:

  • (Hash<String, String>)


222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/hako/container.rb', line 222

def expand_env(env)
  env = env.dup
  provider_types = env.delete(PROVIDERS_KEY) || []
  providers = load_providers(provider_types)
  expander = EnvExpander.new(providers)
  if @dry_run
    expander.validate!(env)
    env
  else
    expander.expand(env)
  end
end

#extra_hostsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


140
141
142
143
144
145
146
147
148
149
# File 'lib/hako/container.rb', line 140

def extra_hosts
  if @definition.key?('extra_hosts')
    @definition['extra_hosts'].map do |extra_host|
      {
        hostname: extra_host.fetch('hostname'),
        ip_address: extra_host.fetch('ip_address'),
      }
    end
  end
end

#health_checkObject

@return[Hash, nil]



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hako/container.rb', line 108

def health_check
  if @definition.key?('health_check')
    conf = @definition['health_check']
    ret = {
      command: conf.fetch('command'),
      interval: conf.fetch('interval', 30),
      retries: conf.fetch('retries', 3),
      timeout: conf.fetch('timeout', 5),
    }

    if conf.key?('start_period')
      ret[:start_period] = conf.fetch('start_period')
    end

    ret
  end
end

#linux_parametersHash?

Returns:

  • (Hash, nil)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/hako/container.rb', line 152

def linux_parameters
  if @definition.key?('linux_parameters')
    ret = {}
    conf = @definition['linux_parameters']

    if conf.key?('capabilities')
      cap = conf['capabilities']
      ret[:capabilities] = {
        add: cap.fetch('add', []),
        drop: cap.fetch('drop', [])
      }
    end

    if conf.key?('devices')
      ret[:devices] = conf['devices'].map do |d|
        {
          host_path: d.fetch('host_path'),
          container_path: d.fetch('container_path', nil),
          permissions: d.fetch('permissions', [])
        }
      end
    end

    ret[:init_process_enabled] = conf.fetch('init_process_enabled', nil)

    ret[:shared_memory_size] = conf.fetch('shared_memory_size', nil)

    if conf.key?('tmpfs')
      ret[:tmpfs] = conf['tmpfs'].map do |t|
        {
          container_path: t.fetch('container_path'),
          mount_options: t.fetch('mount_options', []),
          size: t.fetch('size')
        }
      end
    end

    ret
  end
end

#load_providers(provider_configs) ⇒ Array<EnvProvider> (private)

Parameters:

  • provider_configs (Array<Hash>)

Returns:



237
238
239
240
241
# File 'lib/hako/container.rb', line 237

def load_providers(provider_configs)
  provider_configs.map do |provider_config|
    Loader.new(Hako::EnvProviders, 'hako/env_providers').load(provider_config.fetch('type')).new(@app.root_path, provider_config)
  end
end

#log_configurationHash?

Returns:

  • (Hash, nil)


97
98
99
100
101
102
103
104
105
# File 'lib/hako/container.rb', line 97

def log_configuration
  if @definition.key?('log_configuration')
    conf = @definition['log_configuration']
    {
      log_driver: conf.fetch('log_driver'),
      options: conf.fetch('options'),
    }
  end
end

#mount_pointsArray<Hash>

Returns:

  • (Array<Hash>)


76
77
78
79
80
81
82
83
84
# File 'lib/hako/container.rb', line 76

def mount_points
  @definition['mount_points'].map do |mount_point|
    {
      source_volume: mount_point.fetch('source_volume'),
      container_path: mount_point.fetch('container_path'),
      read_only: mount_point.fetch('read_only', false),
    }
  end
end

#port_mappingsArray<Hash>

Returns:

  • (Array<Hash>)


65
66
67
68
69
70
71
72
73
# File 'lib/hako/container.rb', line 65

def port_mappings
  @definition['port_mappings'].map do |port_mapping|
    {
      container_port: port_mapping.fetch('container_port'),
      host_port: port_mapping.fetch('host_port'),
      protocol: port_mapping.fetch('protocol', 'tcp'),
    }
  end
end

#secretsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


53
54
55
56
57
58
59
60
61
62
# File 'lib/hako/container.rb', line 53

def secrets
  if @definition['secrets']
    @definition['secrets'].map do |secret|
      {
        name: secret.fetch('name'),
        value_from: secret.fetch('value_from'),
      }
    end
  end
end

#system_controlsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


194
195
196
197
198
199
200
201
202
203
# File 'lib/hako/container.rb', line 194

def system_controls
  if @definition.key?('system_controls')
    @definition['system_controls'].map do |system_control|
      {
        namespace: system_control.fetch('namespace'),
        value: system_control.fetch('value'),
      }
    end
  end
end

#ulimitsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hako/container.rb', line 127

def ulimits
  if @definition.key?('ulimits')
    @definition['ulimits'].map do |ulimit|
      {
        name: ulimit.fetch('name'),
        soft_limit: ulimit.fetch('soft_limit'),
        hard_limit: ulimit.fetch('hard_limit'),
      }
    end
  end
end

#volumes_fromArray<Hash>

Returns:

  • (Array<Hash>)


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

def volumes_from
  @definition['volumes_from'].map do |volumes_from|
    {
      source_container: volumes_from.fetch('source_container'),
      read_only: volumes_from.fetch('read_only', false),
    }
  end
end