Class: Docker::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/docker/connection.rb', line 14

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

#optionsObject (readonly)

Returns the value of attribute options


9
10
11
# File 'lib/docker/connection.rb', line 9

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url


9
10
11
# File 'lib/docker/connection.rb', line 9

def url
  @url
end

Instance Method Details

#infoObject

Common attribute requests


113
114
115
# File 'lib/docker/connection.rb', line 113

def info
  Docker::Util.parse_json(get('/info'))
end

#log_request(request) ⇒ Object


95
96
97
98
99
100
101
# File 'lib/docker/connection.rb', line 95

def log_request(request)
  if Docker.logger
    Docker.logger.debug(
      [request[:method], request[:path], request[:query], request[:body]]
    )
  end
end

#pingObject


117
118
119
# File 'lib/docker/connection.rb', line 117

def ping
  get('/_ping')
end

#podman?Boolean

Returns:

  • (Boolean)

121
122
123
124
125
126
127
# File 'lib/docker/connection.rb', line 121

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 `


40
41
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
# File 'lib/docker/connection.rb', line 40

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.message
  end
end

#rootless?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'lib/docker/connection.rb', line 129

def rootless?
  @rootless ||= (info['Rootless'] == true)
end

#to_sObject


103
104
105
# File 'lib/docker/connection.rb', line 103

def to_s
  "Docker::Connection { :url => #{url}, :options => #{options} }"
end

#versionObject


133
134
135
# File 'lib/docker/connection.rb', line 133

def version
  @version ||= Docker::Util.parse_json(get('/version'))
end