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)


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

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>)


235
236
237
238
239
# File 'lib/hako/container.rb', line 235

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

#envHash<String, String>

Returns:

  • (Hash<String, String>)


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

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>)


198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/hako/container.rb', line 198

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)


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

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]



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

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)


151
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
# File 'lib/hako/container.rb', line 151

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:



213
214
215
216
217
# File 'lib/hako/container.rb', line 213

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)


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

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>)


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

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>)


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

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)


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

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

#ulimitsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


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

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>)


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

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