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, self.class.__send__(:finalizer_for, @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



67
68
69
# File 'lib/specinfra/backend/docker.rb', line 67

def commit_container
  @container.commit
end

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



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

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

#send_file(from, to) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/specinfra/backend/docker.rb', line 53

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