Class: Specinfra::Backend::Docker

Inherits:
Exec
  • Object
show all
Defined in:
lib/specinfra/backend/docker.rb

Instance Method Summary collapse

Methods inherited from Exec

#build_command, #send_directory

Methods inherited from Base

clear, #command, #get_config, #host_inventory, instance, #os_info, #set_config, #set_example, #stderr_handler=, #stdout_handler=

Constructor Details

#initialize(config = {}) ⇒ Docker

Returns a new instance of Docker.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/specinfra/backend/docker.rb', line 4

def initialize(config = {})
  super

  begin
    require 'docker' unless defined?(::Docker)
  rescue LoadError
    fail "Docker client library is not available. Try installing `docker-api' gem."
  end

  ::Docker.url = get_config(:docker_url)

  if image = get_config(:docker_image)
    @images = []
    @base_image = get_or_pull_image(image)

    create_and_start_container
    ObjectSpace.define_finalizer(self, proc { cleanup_container })
  elsif container = get_config(:docker_container)
    @container = ::Docker::Container.get(container)
  else
    fail 'Please specify docker_image or docker_container.'
  end
end

Instance Method Details

#commit_containerObject



48
49
50
# File 'lib/specinfra/backend/docker.rb', line 48

def commit_container
  @container.commit
end

#run_command(cmd, opts = {}) ⇒ Object



28
29
30
31
32
# File 'lib/specinfra/backend/docker.rb', line 28

def run_command(cmd, opts={})
  cmd = build_command(cmd)
  run_pre_command(opts)
  docker_run!(cmd, opts)
end

#send_file(from, to) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/specinfra/backend/docker.rb', line 34

def send_file(from, to)
  if @base_image
    @images << commit_container if @container
    @images << current_image.insert_local('localPath' => from, 'outputPath' => to)
    cleanup_container
    create_and_start_container
  elsif @container
    # This needs Docker >= 1.8
    @container.archive_in(from, to)
  else
    fail 'Cannot call send_file without docker_image or docker_container.'
  end
end