Class: Bigrig::DockerAdapter
- Inherits:
-
Object
- Object
- Bigrig::DockerAdapter
- Defined in:
- lib/bigrig/docker_adapter.rb
Class Method Summary collapse
- .build(path, &block) ⇒ Object
- .container_exists?(name) ⇒ Boolean
- .hosts(arr) ⇒ Object
- .image_id_by_tag(tag) ⇒ Object
- .kill(name) ⇒ Object
- .logs(name, &block) ⇒ Object
- .pull(tag, &block) ⇒ Object
- .push(tag, credentials = nil, &block) ⇒ Object
- .remove_container(name) ⇒ Object
- .remove_image(tag) ⇒ Object
- .run(args) ⇒ Object
- .running?(name) ⇒ Boolean
- .tag(id, tag) ⇒ Object
Class Method Details
.build(path, &block) ⇒ Object
14 15 16 |
# File 'lib/bigrig/docker_adapter.rb', line 14 def build(path, &block) Docker::Image.build_from_dir(path, {}, connection, &block).id end |
.container_exists?(name) ⇒ Boolean
18 19 20 21 22 |
# File 'lib/bigrig/docker_adapter.rb', line 18 def container_exists?(name) !Docker::Container.get(name).nil? rescue Docker::Error::NotFoundError false end |
.hosts(arr) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/bigrig/docker_adapter.rb', line 24 def hosts(arr) (arr || []).map do |line| parts = line.split ':' "#{Resolv.getaddress parts.first}:#{parts[1]}" end end |
.image_id_by_tag(tag) ⇒ Object
31 32 33 34 35 |
# File 'lib/bigrig/docker_adapter.rb', line 31 def image_id_by_tag(tag) Docker::Image.get(tag).id rescue Docker::Error::NotFoundError raise ImageNotFoundError end |
.kill(name) ⇒ Object
37 38 39 40 41 |
# File 'lib/bigrig/docker_adapter.rb', line 37 def kill(name) Docker::Container.get(name).kill rescue Docker::Error::NotFoundError raise ContainerNotFoundError end |
.logs(name, &block) ⇒ Object
43 44 45 46 |
# File 'lib/bigrig/docker_adapter.rb', line 43 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
48 49 50 51 52 53 |
# File 'lib/bigrig/docker_adapter.rb', line 48 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
55 56 57 58 |
# File 'lib/bigrig/docker_adapter.rb', line 55 def push(tag, credentials = nil, &block) puts "Pushing #{tag}" Docker::Image.get(tag).push credentials, {}, &block end |
.remove_container(name) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/bigrig/docker_adapter.rb', line 66 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
74 75 76 77 78 79 |
# File 'lib/bigrig/docker_adapter.rb', line 74 def remove_image(tag) Docker::Image.get(tag).remove 'force' => true true rescue Docker::Error::NotFoundError raise ImageNotFoundError end |
.run(args) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bigrig/docker_adapter.rb', line 81 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
93 94 95 96 97 |
# File 'lib/bigrig/docker_adapter.rb', line 93 def running?(name) Docker::Container.get(name).info['State']['Running'] rescue Docker::Error::NotFoundError false end |
.tag(id, tag) ⇒ Object
60 61 62 63 64 |
# File 'lib/bigrig/docker_adapter.rb', line 60 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 |