Class: Indocker::DockerApi
- Inherits:
-
Object
- Object
- Indocker::DockerApi
- Includes:
- SmartIoC::Iocify
- Defined in:
- lib/indocker/docker_api.rb
Instance Method Summary collapse
- #add_container_to_network(network_name:, container_name:) ⇒ Object
- #authenticate!(serveraddress:, email:, password:, username:) ⇒ Object
- #build_from_dir(repo:, tag:, build_dir:) ⇒ Object
- #check_docker_installed! ⇒ Object
- #container_exists?(name) ⇒ Boolean
- #container_run(repo:, tag:, command:) ⇒ Object
- #copy_from_container(name:, path:) ⇒ Object
- #create_container(repo:, tag:, name: nil, command: nil, env: nil, exposed_ports: nil, port_bindings: nil) ⇒ Object
-
#create_network(name) ⇒ Object
Networks.
- #delete_container(name) ⇒ Object
- #delete_containers_where(&condition) ⇒ Object
- #delete_image(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Object
- #delete_images_where(&condition) ⇒ Object
- #delete_networks_where(&condition) ⇒ Object
- #exec_container(name:, command:) ⇒ Object
- #get_container_id(name) ⇒ Object
- #get_container_state(name) ⇒ Object
-
#get_image_id(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Object
Images.
- #get_network_id(name) ⇒ Object
- #image_exists?(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Boolean
-
#inspect_container(name) ⇒ Object
Containers.
- #inspect_network(name) ⇒ Object
- #network_exists?(name) ⇒ Boolean
- #pull(opts) ⇒ Object
- #remove_network(name) ⇒ Object
- #start_container(name) ⇒ Object
- #stop_container(name) ⇒ Object
Instance Method Details
#add_container_to_network(network_name:, container_name:) ⇒ Object
32 33 34 35 36 |
# File 'lib/indocker/docker_api.rb', line 32 def add_container_to_network(network_name:, container_name:) container_id = get_container_id(container_name.to_s) Docker::Network.get(network_name.to_s).connect(container_id.to_s) end |
#authenticate!(serveraddress:, email:, password:, username:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/indocker/docker_api.rb', line 15 def authenticate!(serveraddress:, email:, password:, username:) params = { 'serveraddress' => serveraddress, 'email' => email, 'password' => password, 'username' => username }.delete_if {|_, value| value.to_s.empty?} Docker.authenticate!(params) end |
#build_from_dir(repo:, tag:, build_dir:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/indocker/docker_api.rb', line 74 def build_from_dir(repo:, tag:, build_dir:) image = Docker::Image.build_from_dir(build_dir) do |x| x.split("\r\n").each do |y| if (log = JSON.parse(y)) && log.has_key?("stream") yield log["stream"].strip end end end image.tag( repo: repo, tag: tag, force: true ) image.id end |
#check_docker_installed! ⇒ Object
8 9 10 11 12 13 |
# File 'lib/indocker/docker_api.rb', line 8 def check_docker_installed! Docker.info nil rescue Excon::Error::Socket raise Indocker::Errors::DockerDoesNotInstalled end |
#container_exists?(name) ⇒ Boolean
119 120 121 |
# File 'lib/indocker/docker_api.rb', line 119 def container_exists?(name) !Docker::Container.get(name.to_s).nil? rescue false end |
#container_run(repo:, tag:, command:) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/indocker/docker_api.rb', line 168 def container_run(repo:, tag:, command:) container = Docker::Container.create( 'Image' => full_name(repo, tag), 'Cmd' => command ).id end |
#copy_from_container(name:, path:) ⇒ Object
131 132 133 |
# File 'lib/indocker/docker_api.rb', line 131 def copy_from_container(name:, path:) Docker::Container.get(name.to_s).copy(path) { |chunk| yield chunk } end |
#create_container(repo:, tag:, name: nil, command: nil, env: nil, exposed_ports: nil, port_bindings: nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/indocker/docker_api.rb', line 135 def create_container(repo:, tag:, name: nil, command: nil, env: nil, exposed_ports: nil, port_bindings: nil) params = { 'Image' => full_name(repo, tag), 'name' => name.to_s, 'Cmd' => command, 'Env' => env, 'ExposedPorts' => exposed_ports, 'HostConfig' => { 'PortBindings' => port_bindings } }.delete_if { |_, value| value.to_s.empty? } Docker::Container.create(params).id end |
#create_network(name) ⇒ Object
Networks
28 29 30 |
# File 'lib/indocker/docker_api.rb', line 28 def create_network(name) Docker::Network.create(name.to_s).id end |
#delete_container(name) ⇒ Object
157 158 159 |
# File 'lib/indocker/docker_api.rb', line 157 def delete_container(name) Docker::Container.get(name.to_s).delete(:force => true) end |
#delete_containers_where(&condition) ⇒ Object
161 162 163 164 165 166 |
# File 'lib/indocker/docker_api.rb', line 161 def delete_containers_where(&condition) Docker::Container.all(all: true).select(&condition).map do |container| container.stop container.delete(force: true) end end |
#delete_image(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Object
70 71 72 |
# File 'lib/indocker/docker_api.rb', line 70 def delete_image(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) Docker::Image.get(full_name(repo, tag)).remove(force: true) end |
#delete_images_where(&condition) ⇒ Object
98 99 100 101 102 |
# File 'lib/indocker/docker_api.rb', line 98 def delete_images_where(&condition) Docker::Image.all.select(&condition).map do |image| image.remove(force: true) end end |
#delete_networks_where(&condition) ⇒ Object
54 55 56 57 58 |
# File 'lib/indocker/docker_api.rb', line 54 def delete_networks_where(&condition) Docker::Network.all.select(&condition).map do |network| network.remove end end |
#exec_container(name:, command:) ⇒ Object
151 152 153 154 155 |
# File 'lib/indocker/docker_api.rb', line 151 def exec_container(name:, command:) Docker::Container.get(name.to_s).exec(command) do |stream, chunk| yield stream, chunk end end |
#get_container_id(name) ⇒ Object
111 112 113 |
# File 'lib/indocker/docker_api.rb', line 111 def get_container_id(name) Docker::Container.get(name.to_s).id end |
#get_container_state(name) ⇒ Object
115 116 117 |
# File 'lib/indocker/docker_api.rb', line 115 def get_container_state(name) Docker::Container.get(name.to_s).info["State"]["Status"] end |
#get_image_id(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Object
Images
62 63 64 |
# File 'lib/indocker/docker_api.rb', line 62 def get_image_id(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) Docker::Image.get(full_name(repo, tag)).id end |
#get_network_id(name) ⇒ Object
46 47 48 |
# File 'lib/indocker/docker_api.rb', line 46 def get_network_id(name) Docker::Network.get(name.to_s).id end |
#image_exists?(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) ⇒ Boolean
66 67 68 |
# File 'lib/indocker/docker_api.rb', line 66 def image_exists?(repo, tag: Indocker::ImageMetadata::DEFAULT_TAG) Docker::Image.exist?(full_name(repo, tag)) end |
#inspect_container(name) ⇒ Object
Containers
107 108 109 |
# File 'lib/indocker/docker_api.rb', line 107 def inspect_container(name) Docker::Container.get(name.to_s).info end |
#inspect_network(name) ⇒ Object
50 51 52 |
# File 'lib/indocker/docker_api.rb', line 50 def inspect_network(name) Docker::Network.get(name.to_s).info end |
#network_exists?(name) ⇒ Boolean
42 43 44 |
# File 'lib/indocker/docker_api.rb', line 42 def network_exists?(name) !Docker::Network.get(name.to_s).nil? rescue false end |
#pull(opts) ⇒ Object
92 93 94 95 96 |
# File 'lib/indocker/docker_api.rb', line 92 def pull(opts) image = Docker::Image.create(opts) image.id end |
#remove_network(name) ⇒ Object
38 39 40 |
# File 'lib/indocker/docker_api.rb', line 38 def remove_network(name) Docker::Network.get(name.to_s).remove end |
#start_container(name) ⇒ Object
123 124 125 |
# File 'lib/indocker/docker_api.rb', line 123 def start_container(name) Docker::Container.get(name.to_s).start!.id end |
#stop_container(name) ⇒ Object
127 128 129 |
# File 'lib/indocker/docker_api.rb', line 127 def stop_container(name) Docker::Container.get(name.to_s).stop end |