Class: Docker::Connection
- Inherits:
-
Object
- Object
- Docker::Connection
- Includes:
- Error
- Defined in:
- lib/docker/connection.rb
Overview
This class represents a Connection to a Docker server. The Connection is immutable in that once the url and options is set they cannot be changed.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#info ⇒ Object
Common attribute requests.
-
#initialize(url, opts) ⇒ Connection
constructor
Create a new Connection.
- #log_request(request) ⇒ Object
- #ping ⇒ Object
- #podman? ⇒ Boolean
-
#request(*args, &block) ⇒ Object
Send a request to the server with the ‘.
- #rootless? ⇒ Boolean
- #to_s ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(url, opts) ⇒ Connection
Create a new Connection. This method takes a url (String) and options (Hash). These are passed to Excon, so any options valid for ‘Excon.new` can be passed here.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/docker/connection.rb', line 16 def initialize(url, opts) case when !url.is_a?(String) raise ArgumentError, "Expected a String, got: '#{url}'" when !opts.is_a?(Hash) raise ArgumentError, "Expected a Hash, got: '#{opts}'" else uri = URI.parse(url) if uri.scheme == "unix" @url, @options = 'unix:///', {:socket => uri.path}.merge(opts) elsif uri.scheme =~ /^(https?|tcp)$/ @url, @options = url, opts else @url, @options = "http://#{uri}", opts end end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/docker/connection.rb', line 11 def @options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/docker/connection.rb', line 11 def url @url end |
Instance Method Details
#info ⇒ Object
Common attribute requests
115 116 117 |
# File 'lib/docker/connection.rb', line 115 def info Docker::Util.parse_json(get('/info')) end |
#log_request(request) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/docker/connection.rb', line 97 def log_request(request) if Docker.logger Docker.logger.debug( [request[:method], request[:path], request[:query], request[:body]] ) end end |
#ping ⇒ Object
119 120 121 |
# File 'lib/docker/connection.rb', line 119 def ping get('/_ping') end |
#podman? ⇒ Boolean
123 124 125 126 127 128 129 |
# File 'lib/docker/connection.rb', line 123 def podman? @podman ||= !( Array(version['Components']).find do |component| component['Name'].include?('Podman') end ).nil? end |
#request(*args, &block) ⇒ Object
Send a request to the server with the ‘
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/docker/connection.rb', line 42 def request(*args, &block) retries ||= 0 request = compile_request_params(*args, &block) log_request(request) begin resource.request(request).body rescue Excon::Errors::BadRequest => ex if retries < 2 response_cause = '' begin response_cause = JSON.parse(ex.response.body)['cause'] rescue JSON::ParserError #noop end if response_cause.is_a?(String) # The error message will tell the application type given and then the # application type that the message should be # # This is not perfect since it relies on processing a message that # could change in the future. However, it should be a good stop-gap # until all methods are updated to pass in the appropriate content # type. # # A current example message is: # * 'Content-Type: application/json is not supported. Should be "application/x-tar"' matches = response_cause.delete('"\'').scan(%r{(application/\S+)}) unless matches.count < 2 Docker.logger.warn( <<~RETRY_WARNING Automatically retrying with content type '#{response_cause}' Original Error: #{ex} RETRY_WARNING ) if Docker.logger request[:headers]['Content-Type'] = matches.last.first retries += 1 retry end end end raise ClientError, ex.response.body rescue Excon::Errors::Unauthorized => ex raise UnauthorizedError, ex.response.body rescue Excon::Errors::NotFound => ex raise NotFoundError, ex.response.body rescue Excon::Errors::Conflict => ex raise ConflictError, ex.response.body rescue Excon::Errors::InternalServerError => ex raise ServerError, ex.response.body rescue Excon::Errors::Timeout => ex raise TimeoutError, ex. end end |
#rootless? ⇒ Boolean
131 132 133 |
# File 'lib/docker/connection.rb', line 131 def rootless? @rootless ||= (info['Rootless'] == true) end |
#to_s ⇒ Object
105 106 107 |
# File 'lib/docker/connection.rb', line 105 def to_s "Docker::Connection { :url => #{url}, :options => #{} }" end |
#version ⇒ Object
135 136 137 |
# File 'lib/docker/connection.rb', line 135 def version @version ||= Docker::Util.parse_json(get('/version')) end |