Class: Indocker::DockerRunArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/docker_run_args.rb

Constant Summary collapse

DEFAULT_LOG_MAX_SIZE =
"400m"
DEFAULT_LOG_MAX_FILE =
"2"

Class Method Summary collapse

Class Method Details

.get(container, configuration, number) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
191
192
193
194
195
196
197
198
199
# File 'lib/indocker/docker_run_args.rb', line 6

def get(container, configuration, number)
  args = []
  restart = container.get_start_option(:restart)

  if restart
    args.push("--restart #{restart}")
  end

  env_files = container.get_start_option(:env_files)

  if env_files
    env_files = env_files.is_a?(Array) ? env_files : [env_files]

    env_files.each do |val|
      env_file = configuration.env_files.fetch(val) do
        Indocker.logger.error("env file :#{val} for container :#{container.name} is not specified in configuration :#{configuration.name}")
        exit 1
      end

      path = Indocker::EnvFileHelper.path(env_file)

      if !File.exist?(path)
        raise ArgumentError.new("env file not found: #{path}")
      end

      args.push("--env-file #{path}")
    end
  end

  expose = container.get_start_option(:expose)

  if expose
    expose = expose.is_a?(Array) ? expose : [expose]

    expose.each do |val|
      args.push("--expose #{val}")
    end
  end

  ports = container.get_start_option(:ports)

  if ports
    ports = ports.is_a?(Array) ? ports : [ports]

    ports.each do |val|
      args.push("--publish #{val}")
    end
  end

  environment = container.get_start_option(:environment)

  if environment
    environment.each do |key, val|
      args.push("--env #{key}=#{val}")
    end
  end

  sysctl = container.get_start_option(:sysctl)

  if sysctl
    Array(sysctl).each do |val|
      args.push("--sysctl #{val}")
    end
  end

  if container.is_daemonized?
    args.push("--detach")
  end

  hostname = Indocker::ContainerHelper.hostname(configuration.name, container, number)
  args.push("--hostname #{hostname}")
  args.push("--name #{hostname}")

  read_only = container.get_start_option(:read_only) || false

  if read_only
    args.push("--read-only")
  end

  labels = container.get_start_option(:labels) || []

  labels += container.tags
  labels.uniq!

  labels.each do |label|
    args.push("--label #{label}")
  end

  stop_timeout = container.get_start_option(:stop_timeout)

  if stop_timeout
    args.push("--stop-timeout #{stop_timeout}")
  end

  add_hosts = container.get_start_option(:add_hosts) || []

  add_hosts.each do |host|
    args.push("--add-host #{host}")
  end

  mounts = container.get_start_option(:mounts) || []

  mounts.each do |mount|
    args.push("--mount #{mount}")
  end

  nets = container.get_start_option(:nets) || []

  nets.each do |net|
    args.push("--net #{net}")
  end

  user = container.get_start_option(:user)

  if user
    args.push("--user #{user}")
  end

  max_size = container.get_start_option(:max_size) || DEFAULT_LOG_MAX_SIZE

  if max_size
    args.push("--log-opt max-size=#{max_size}")
  end

  max_file = container.get_start_option(:max_file) || DEFAULT_LOG_MAX_FILE

  if max_file
    args.push("--log-opt max-file=#{max_file}")
  end

  cap_add = container.get_start_option(:cap_add)

  if cap_add
    args.push("--cap-add=#{cap_add}")
  end

  cap_drop = container.get_start_option(:cap_drop)

  if cap_drop
    args.push("--cap-drop=#{cap_drop}")
  end

  health = container.get_start_option(:health) || {}

  if !health.is_a?(Hash)
    raise ArgumentError.new("health should be a Hash for container :#{container.name}")
  end

  if health.has_key?(:cmd)
    args.push("--health-cmd \"#{health[:cmd]}\"")
  end

  if health.has_key?(:interval)
    args.push("--health-interval #{health[:interval]}")
  end

  if health.has_key?(:retries)
    args.push("--health-retries #{health[:retries]}")
  end

  if health.has_key?(:timeout)
    args.push("--health-timeout #{health[:timeout]}")
  end

  if configuration.deploy_args
    configuration.deploy_args.each do |k, v|
      args.push("--#{k}=#{v}")
    end
  end

  container.volumes.each do |volume|
    if volume.is_a?(Indocker::Volumes::Local)
      args.push("-v #{volume.local_path}:#{volume.path}")
    elsif volume.is_a?(Indocker::Volumes::External)
      name = Indocker::Volumes::VolumeHelper.name(configuration.name, volume)
      args.push("-v #{name}:#{volume.path}")
    elsif volume.is_a?(Indocker::Volumes::Repository)
      repository = configuration.repositories.fetch(volume.repository_name) do
        raise ArgumentError.new("specified repository :#{volume.repository_name} for volume :#{volume.name} was not found")
      end

      args.push("-v #{File.join(repository.clone_path)}:#{volume.path}:cached")
    else
      raise NotImplementedError.new("unsupported volume type: #{volume.inspect}")
    end
  end

  container.networks.each do |network|
    name = Indocker::Networks::NetworkHelper.name(configuration.name, network)
    args.push("--network #{name}")
  end

  args
end