Class: Specinfra::Backend::Docker

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

Instance Method Summary collapse

Methods inherited from Exec

#send_directory

Methods inherited from Base

#command, #get_config, #host_inventory, instance, #os_info, #set_config, #set_example

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

#add_pre_command(cmd) ⇒ Object


38
39
40
# File 'lib/specinfra/backend/docker.rb', line 38

def add_pre_command(cmd)
  cmd
end

#build_command(cmd) ⇒ Object


34
35
36
# File 'lib/specinfra/backend/docker.rb', line 34

def build_command(cmd)
  cmd
end

#commit_containerObject


52
53
54
# File 'lib/specinfra/backend/docker.rb', line 52

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)
  cmd = add_pre_command(cmd)
  docker_run!(cmd)
end

#send_file(from, to) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/specinfra/backend/docker.rb', line 42

def send_file(from, to)
  if @base_image.nil?
    fail 'Cannot call send_file without docker_image.'
  end

  @images << current_image.insert_local('localPath' => from, 'outputPath' => to)
  cleanup_container
  create_and_start_container
end