Class: Centurion::DockerViaApi
- Inherits:
-
Object
- Object
- Centurion::DockerViaApi
- Defined in:
- lib/centurion/docker_via_api.rb
Instance Method Summary collapse
- #create_container(configuration, name = nil) ⇒ Object
-
#initialize(hostname, port, tls_args = {}, api_version = nil) ⇒ DockerViaApi
constructor
A new instance of DockerViaApi.
- #inspect_container(container_id) ⇒ Object
- #inspect_image(image, tag = "latest") ⇒ Object
- #ps(options = {}) ⇒ Object
- #remove_container(container_id) ⇒ Object
- #restart_container(container_id, timeout = 30) ⇒ Object
- #start_container(container_id, configuration) ⇒ Object
- #stop_container(container_id, timeout = 30) ⇒ Object
Constructor Details
#initialize(hostname, port, tls_args = {}, api_version = nil) ⇒ DockerViaApi
Returns a new instance of DockerViaApi.
9 10 11 12 13 14 15 |
# File 'lib/centurion/docker_via_api.rb', line 9 def initialize(hostname, port, tls_args = {}, api_version = nil) @tls_args = default_tls_args(tls_args[:tls]).merge(tls_args.reject { |k, v| v.nil? }) # Required by tls_enable? @base_uri = "http#{'s' if tls_enable?}://#{hostname}:#{port}" api_version ||= "/v1.12" @docker_api_version = api_version configure_excon_globally end |
Instance Method Details
#create_container(configuration, name = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/centurion/docker_via_api.rb', line 58 def create_container(configuration, name = nil) path = @docker_api_version + "/containers/create" response = Excon.post( @base_uri + path, tls_excon_arguments.merge( query: name ? {name: "#{name}-#{SecureRandom.hex(7)}"} : nil, body: configuration.to_json, headers: { "Content-Type" => "application/json" } ) ) raise response.inspect unless response.status == 201 JSON.load(response.body) end |
#inspect_container(container_id) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/centurion/docker_via_api.rb', line 109 def inspect_container(container_id) path = @docker_api_version + "/containers/#{container_id}/json" response = Excon.get( @base_uri + path, tls_excon_arguments ) raise response.inspect unless response.status == 200 JSON.load(response.body) end |
#inspect_image(image, tag = "latest") ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/centurion/docker_via_api.rb', line 26 def inspect_image(image, tag = "latest") repository = "#{image}:#{tag}" path = @docker_api_version + "/images/#{repository}/json" response = Excon.get( @base_uri + path, tls_excon_arguments.merge(headers: {'Accept' => 'application/json'}) ) raise response.inspect unless response.status == 200 JSON.load(response.body) end |
#ps(options = {}) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/centurion/docker_via_api.rb', line 17 def ps(={}) path = @docker_api_version + "/containers/json" path += "?all=1" if [:all] response = Excon.get(@base_uri + path, tls_excon_arguments) raise unless response.status == 200 JSON.load(response.body) end |
#remove_container(container_id) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/centurion/docker_via_api.rb', line 38 def remove_container(container_id) path = @docker_api_version + "/containers/#{container_id}" response = Excon.delete( @base_uri + path, tls_excon_arguments ) raise response.inspect unless response.status == 204 true end |
#restart_container(container_id, timeout = 30) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/centurion/docker_via_api.rb', line 91 def restart_container(container_id, timeout = 30) path = @docker_api_version + "/containers/#{container_id}/restart?t=#{timeout}" response = Excon.post( @base_uri + path, tls_excon_arguments ) case response.status when 204 true when 404 fail "Failed to start missing container! \"#{response.body}\"" when 500 fail "Failed to start existing container! \"#{response.body}\"" else raise response.inspect end end |
#start_container(container_id, configuration) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/centurion/docker_via_api.rb', line 72 def start_container(container_id, configuration) path = @docker_api_version + "/containers/#{container_id}/start" response = Excon.post( @base_uri + path, tls_excon_arguments.merge( body: configuration.to_json, headers: { "Content-Type" => "application/json" } ) ) case response.status when 204 true when 500 fail "Failed to start container! \"#{response.body}\"" else raise response.inspect end end |
#stop_container(container_id, timeout = 30) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/centurion/docker_via_api.rb', line 48 def stop_container(container_id, timeout = 30) path = @docker_api_version + "/containers/#{container_id}/stop?t=#{timeout}" response = Excon.post( @base_uri + path, tls_excon_arguments ) raise response.inspect unless response.status == 204 true end |