Module: ChefMetalDocker::Helpers::Container::Actions

Included in:
ChefMetalDocker::Helpers::Container
Defined in:
lib/chef_metal_docker/helpers/container/actions.rb

Overview

This is a collection of helper methods that are used to trigger actions in the LWRPs. By putting them in a separate file we can a) keep the LWRPs cleaner and b) unit test them!

Instance Method Summary collapse

Instance Method Details

#commitObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 9

def commit
  commit_args = cli_args(
    'author' => new_resource.author,
    'message' => new_resource.message,
    'run' => new_resource.run
  )
  commit_end_args = ''

  if new_resource.repository
    commit_end_args = new_resource.repository
    commit_end_args += ":#{new_resource.tag}" if new_resource.tag
  end

  docker_cmd!("commit #{commit_args} #{current_resource.id} #{commit_end_args}")
end

#cpObject



25
26
27
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 25

def cp
  docker_cmd!("cp #{current_resource.id}:#{new_resource.source} #{new_resource.destination}")
end

#exportObject



29
30
31
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 29

def export
  docker_cmd!("export #{current_resource.id} > #{new_resource.destination}")
end

#killObject



33
34
35
36
37
38
39
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 33

def kill
  if service?
    service_stop
  else
    docker_cmd!("kill #{current_resource.id}")
  end
end

#redeployObject



50
51
52
53
54
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 50

def redeploy 
  stop if running?
  remove if exists?
  run
end

#removeObject



41
42
43
44
45
46
47
48
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 41

def remove
  rm_args = cli_args(
    'force' => new_resource.force,
    'link' => new_resource.link
  )
  docker_cmd!("rm #{rm_args} #{current_resource.id}")
  service_remove if service?
end

#restartObject



56
57
58
59
60
61
62
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 56

def restart
  if service?
    service_restart
  else
    docker_cmd!("restart #{current_resource.id}")
  end
end

#runObject

rubocop:disable MethodLength



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
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 65

def run
  run_args = cli_args(
    'cpu-shares' => new_resource.cpu_shares,
    'cidfile' => cidfile,
    'detach' => new_resource.detach,
    'dns' => Array(new_resource.dns),
    'dns-search' => Array(new_resource.dns_search),
    'env' => Array(new_resource.env),
    'entrypoint' => new_resource.entrypoint,
    'expose' => Array(new_resource.expose),
    'hostname' => new_resource.hostname,
    'interactive' => new_resource.stdin,
    'label' => new_resource.label,
    'link' => Array(new_resource.link),
    'lxc-conf' => Array(new_resource.lxc_conf),
    'memory' => new_resource.memory,
    'networking' => new_resource.networking,
    'name' => container_name,
    'opt' => Array(new_resource.opt),
    'publish' => Array(port),
    'publish-all' => new_resource.publish_exposed_ports,
    'privileged' => new_resource.privileged,
    'rm' => new_resource.remove_automatically,
    'tty' => new_resource.tty,
    'user' => new_resource.user,
    'volume' => Array(new_resource.volume),
    'volumes-from' => new_resource.volumes_from,
    'workdir' => new_resource.working_directory
  )
  dr = docker_cmd!("run #{run_args} #{new_resource.image} #{new_resource.command}")
  dr.error!
  new_resource.id(dr.stdout.chomp)
  service_create if service?
end

#service_action(actions) ⇒ Object

rubocop:enable MethodLength



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 101

def service_action(actions)
  if new_resource.init_type == 'runit'
    runit_service service_name do
      run_template_name 'docker-container'
      action actions
    end
  else
    service service_name do
      case new_resource.init_type
      when 'systemd'
        provider Chef::Provider::Service::Systemd
      when 'upstart'
        provider Chef::Provider::Service::Upstart
      end
      supports :status => true, :restart => true, :reload => true
      action actions
    end
  end
end

#service_createObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 121

def service_create
  case new_resource.init_type
  when 'runit'
    service_create_runit
  when 'systemd'
    service_create_systemd
  when 'sysv'
    service_create_sysv
  when 'upstart'
    service_create_upstart
  end
end

#service_create_runitObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 134

def service_create_runit
  runit_service service_name do
    cookbook new_resource.cookbook
    default_logger true
    options(
      'service_name' => service_name
    )
    run_template_name service_template
  end
end

#service_create_systemdObject



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
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 145

def service_create_systemd
  template "/usr/lib/systemd/system/#{service_name}.socket" do
    if new_resource.socket_template.nil?
      source 'docker-container.socket.erb'
    else
      source new_resource.socket_template
    end
    cookbook new_resource.cookbook
    mode '0644'
    owner 'root'
    group 'root'
    variables(
      :service_name => service_name,
      :sockets => sockets
    )
    not_if port.empty?
  end

  template "/usr/lib/systemd/system/#{service_name}.service" do
    source service_template
    cookbook new_resource.cookbook
    mode '0644'
    owner 'root'
    group 'root'
    variables(
      :cmd_timeout => new_resource.cmd_timeout,
      :service_name => service_name
    )
  end

  service_action([:start, :enable])
end

#service_create_sysvObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 178

def service_create_sysv
  template "/etc/init.d/#{service_name}" do
    source service_template
    cookbook new_resource.cookbook
    mode '0755'
    owner 'root'
    group 'root'
    variables(
      :cmd_timeout => new_resource.cmd_timeout,
      :service_name => service_name
    )
  end

  service_action([:start, :enable])
end

#service_create_upstartObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 194

def service_create_upstart
  # The upstart init script requires inotifywait, which is in inotify-tools
  package 'inotify-tools'

  template "/etc/init/#{service_name}.conf" do
    source service_template
    cookbook new_resource.cookbook
    mode '0600'
    owner 'root'
    group 'root'
    variables(
      :cmd_timeout => new_resource.cmd_timeout,
      :service_name => service_name
    )
  end

  service_action([:start, :enable])
end

#service_removeObject



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 213

def service_remove
  case new_resource.init_type
  when 'runit'
    service_remove_runit
  when 'systemd'
    service_remove_systemd
  when 'sysv'
    service_remove_sysv
  when 'upstart'
    service_remove_upstart
  end
end

#service_remove_runitObject



226
227
228
229
230
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 226

def service_remove_runit
  runit_service service_name do
    action :disable
  end
end

#service_remove_systemdObject



232
233
234
235
236
237
238
239
240
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 232

def service_remove_systemd
  service_action([:stop, :disable])

  %w(service socket).each do |f|
    file "/usr/lib/systemd/system/#{service_name}.#{f}" do
      action :delete
    end
  end
end

#service_remove_sysvObject



242
243
244
245
246
247
248
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 242

def service_remove_sysv
  service_action([:stop, :disable])

  file "/etc/init.d/#{service_name}" do
    action :delete
  end
end

#service_remove_upstartObject



250
251
252
253
254
255
256
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 250

def service_remove_upstart
  service_action([:stop, :disable])

  file "/etc/init/#{service_name}" do
    action :delete
  end
end

#service_restartObject



258
259
260
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 258

def service_restart
  service_action([:restart])
end

#service_startObject



262
263
264
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 262

def service_start
  service_action([:start])
end

#service_stopObject



266
267
268
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 266

def service_stop
  service_action([:stop])
end

#service_templateObject



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 270

def service_template
  return new_resource.init_template unless new_resource.init_template.nil?
  case new_resource.init_type
  when 'runit'
    'docker-container'
  when 'systemd'
    'docker-container.service.erb'
  when 'upstart'
    'docker-container.conf.erb'
  when 'sysv'
    'docker-container.sysv.erb'
  end
end

#startObject



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 284

def start
  start_args = cli_args(
    'attach' => new_resource.attach,
    'interactive' => new_resource.stdin
  )
  if service?
    service_create
  else
    docker_cmd!("start #{start_args} #{current_resource.id}")
  end
end

#stopObject



296
297
298
299
300
301
302
303
304
305
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 296

def stop
  stop_args = cli_args(
    'time' => new_resource.cmd_timeout
  )
  if service?
    service_stop
  else
    docker_cmd!("stop #{stop_args} #{current_resource.id}", (new_resource.cmd_timeout + 15))
  end
end

#waitObject



307
308
309
# File 'lib/chef_metal_docker/helpers/container/actions.rb', line 307

def wait
  docker_cmd!("wait #{current_resource.id}")
end