Class: Bigrig::DockerAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/bigrig/docker_adapter.rb

Class Method Summary collapse

Class Method Details

.build(path, &block) ⇒ Object



15
16
17
# File 'lib/bigrig/docker_adapter.rb', line 15

def build(path, &block)
  Docker::Image.build_from_tar(Tar.create_dir_tar(path), {}, connection, &block).id
end

.container_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/bigrig/docker_adapter.rb', line 19

def container_exists?(name)
  !Docker::Container.get(name).nil?
rescue Docker::Error::NotFoundError
  false
end

.exec(name, command) ⇒ Object



25
26
27
# File 'lib/bigrig/docker_adapter.rb', line 25

def exec(name, command)
  Docker::Container.get(name).exec(command)
end

.hosts(arr) ⇒ Object



29
30
31
32
33
34
# File 'lib/bigrig/docker_adapter.rb', line 29

def hosts(arr)
  (arr || []).map do |line|
    parts = line.split ':'
    "#{Resolv.getaddress parts.first}:#{parts[1]}"
  end
end

.image_id_by_tag(tag) ⇒ Object



36
37
38
39
40
# File 'lib/bigrig/docker_adapter.rb', line 36

def image_id_by_tag(tag)
  Docker::Image.get(tag).id
rescue Docker::Error::NotFoundError
  raise ImageNotFoundError
end

.kill(name) ⇒ Object



42
43
44
45
46
# File 'lib/bigrig/docker_adapter.rb', line 42

def kill(name)
  Docker::Container.get(name).kill
rescue Docker::Error::NotFoundError
  raise ContainerNotFoundError
end

.logs(name, &block) ⇒ Object



48
49
50
51
# File 'lib/bigrig/docker_adapter.rb', line 48

def logs(name, &block)
  container = Docker::Container.get name, {}, connection
  container.streaming_logs follow: true, stdout: true, stderr: true, &block
end

.pull(tag, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/bigrig/docker_adapter.rb', line 53

def pull(tag, &block)
  Docker::Image.get(Docker::Image.create('fromImage' => tag, &block).id).id
rescue Docker::Error::ArgumentError => e
  e.to_s =~ /"id"=>nil/ && raise(RepoNotFoundError)
  raise
end

.push(tag, credentials = nil, &block) ⇒ Object



60
61
62
63
# File 'lib/bigrig/docker_adapter.rb', line 60

def push(tag, credentials = nil, &block)
  puts "Pushing #{tag}"
  Docker::Image.get(tag).push credentials, {}, &block
end

.remove_container(name) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/bigrig/docker_adapter.rb', line 71

def remove_container(name)
  fail ContainerRunningError, 'You cannot remove a running container' if running?(name)
  Docker::Container.get(name).delete
  true
rescue Docker::Error::NotFoundError
  raise ContainerNotFoundError
end

.remove_image(tag) ⇒ Object



79
80
81
82
83
84
# File 'lib/bigrig/docker_adapter.rb', line 79

def remove_image(tag)
  Docker::Image.get(tag).remove 'force' => true
  true
rescue Docker::Error::NotFoundError
  raise ImageNotFoundError
end

.run(args) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/bigrig/docker_adapter.rb', line 86

def run(args)
  container = create_container args
  container.start('Links' => args[:links], 'ExtraHosts' => hosts(args[:hosts]),
                  'PortBindings' => port_bindings(args[:ports]),
                  'VolumesFrom' => args[:volumes_from], 'Binds' => args[:volumes])
  container.id
end

.running?(name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/bigrig/docker_adapter.rb', line 94

def running?(name)
  Docker::Container.get(name).info['State']['Running']
rescue Docker::Error::NotFoundError
  false
end

.tag(id, tag) ⇒ Object



65
66
67
68
69
# File 'lib/bigrig/docker_adapter.rb', line 65

def tag(id, tag)
  i = tag.rindex ':'
  repo, version = [tag[0...i], tag[i + 1..-1]]
  Docker::Image.get(id).tag 'repo' => repo, 'tag' => version, 'force' => true
end