Puppet Function: docker_run_flags

Defined in:
lib/puppet/parser/functions/docker_run_flags.rb
Function type:
Ruby 3.x API

Overview

docker_run_flags()Any

Transforms a hash into a string of docker flags

Returns:

  • (Any)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/parser/functions/docker_run_flags.rb', line 5

newfunction(:docker_run_flags, :type => :rvalue) do |args|
  opts = args[0] || {}
  flags = []

  if opts['username']
    flags << "-u '#{opts['username'].shellescape}'"
  end

  if opts['hostname']
    flags << "-h '#{opts['hostname'].shellescape}'"
  end

  if opts['restart']
    flags << "--restart '#{opts['restart']}'"
  end

  if opts['net']
    flags << "--net #{opts['net']}"
  end

  if opts['memory_limit']
    flags << "-m #{opts['memory_limit']}"
  end

  cpusets = [opts['cpuset']].flatten.compact
  unless cpusets.empty?
    value = cpusets.join(',')
    flags << "--cpuset=#{value}"
  end

  if opts['disable_network']
    flags << '-n false'
  end

  if opts['privileged']
    flags << '--privileged'
  end

  if opts['detach']
    flags << '--detach=true'
  end

  if opts['tty']
    flags << '-t'
  end

  multi_flags = lambda { |values, format|
    filtered = [values].flatten.compact
    filtered.map { |val| sprintf(format + " \\\n", val) }
  }

  [
    ['--dns %s',          'dns'],
    ['--dns-search %s',   'dns_search'],
    ['--expose=%s',       'expose'],
    ['--link %s',         'links'],
    ['--lxc-conf="%s"',   'lxc_conf'],
    ['--volumes-from %s', 'volumes_from'],
    ['-e %s',             'env'],
    ['--env-file %s',     'env_file'],
    ['-p %s',             'ports'],
    ['-l %s',             'labels'],
    ['--add-host %s',     'hostentries'],
    ['-v %s',             'volumes']
  ].each do |(format, key)|
    values    = opts[key]
    new_flags = multi_flags.call(values, format)
    flags.concat(new_flags)
  end

  opts['extra_params'].each do |param|
    flags << param
  end

  flags.flatten.join(" ")
end