Class: Chef::Provisioning::DockerDriver::DockerRunOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/docker_driver/docker_run_options.rb

Overview

HostConfig/Ulimits

Class Method Summary collapse

Class Method Details

.cli_option(option_name, type = nil, aliases: nil, api: nil, &block) ⇒ Object



271
272
273
274
275
276
277
# File 'lib/chef/provisioning/docker_driver/docker_run_options.rb', line 271

def self.cli_option(option_name, type=nil, aliases: nil, api: nil, &block)
  api = Array(api)
  cli_options[option_name] = { type: type, api: Array(api), block: block }
  Array(aliases).each do |option_name|
    cli_options[option_name] = { type: type, api: Array(api), block: block }
  end
end

.cli_optionsObject



267
268
269
# File 'lib/chef/provisioning/docker_driver/docker_run_options.rb', line 267

def self.cli_options
  @cli_options ||= {}
end

.include_command_line_options_in_container_config(config, docker_options) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/chef/provisioning/docker_driver/docker_run_options.rb', line 209

def self.include_command_line_options_in_container_config(config, docker_options)

  # Grab the command line options we've begun supporting
  # The following are command line equivalents for `docker run`
  docker_options.each do |key, value|
    # Remove -, symbolize key
    key = key.to_s.gsub('-', '_').to_sym
    option = cli_options[key]
    if !option
      raise "Unknown option in docker_options: #{key.inspect}"
    end

    # Figure out the new value
    if option[:type] == :boolean
      value == !!value
    elsif option[:type] == Array
      value = Array(value)
    elsif option[:type] == Integer
      value = parse_int(value) if value.is_a?(String)
    elsif option[:type].is_a?(String)
      # If it's A:B:C, translate [ "a:b:c", "d:e:f" ] -> [ { "A" => "a", "B" => "b", "C" => "c" }, { "A" => "d", "B" => "e", "C" => "f" } ]
      names = option[:type].split(":")
      if names.size == 2 && value.is_a?(Hash)
        value.map { |key,value| { names[0] => key, names[1] => value } }
      else
        Array(value).map do |item|
          item_values = item.split(":", names.size)
          item = Hash[names.zip(item_values)]
        end
      end
    end

    option[:api].each do |api|
      # Grab the parent API key so we know what we're setting
      api_parent = config
      api.split("/")[0..-2].each do |api_key|
        api_parent[api_key] = {} if !api_parent[api_key]
        api_parent = api_parent[api_key]
      end
      api_key = api.split("/")[-1] if api

      # Bring in the current value
      if option[:type] == Array || option[:type].is_a?(String)
        api_parent[api_key] ||= []
        api_parent[api_key] += value
      else
        api_parent[api_key] = value
      end
    end

    # Call the block (if any)
    if option[:block]
      option[:block].call(config, value)
    end
  end
  config
end