Class: DockerApi

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-plugins-docker/client_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil) ⇒ DockerApi

Returns a new instance of DockerApi.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sensu-plugins-docker/client_helpers.rb', line 5

def initialize(uri = nil)
  @client = nil
  @docker_uri = uri || ENV['DOCKER_URL'] || ENV['DOCKER_HOST'] || '/var/run/docker.sock'
  if @docker_uri.sub!(%r{^(unix://)?/}, '')
    @docker_uri = 'unix:///' + @docker_uri
    @client = NetX::HTTPUnix.new(@docker_uri)
  else
    protocol = %r{^(https?|tcp)://}.match(@docker_uri) || 'http://'
    @docker_uri.sub!(protocol.to_s, '')
    split_host = @docker_uri.split ':'
    @client = if split_host.length == 2
                NetX::HTTPUnix.new("#{protocol}#{split_host[0]}", split_host[1])
              else
                NetX::HTTPUnix.new("#{protocol}#{@docker_uri}", 2375)
              end
  end
  @client.start
end

Instance Method Details

#call(path, halt = true, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sensu-plugins-docker/client_helpers.rb', line 28

def call(path, halt = true, limit = 10)
  raise ArgumentError, "HTTP redirect too deep. Last url called : #{path}" if limit.zero?
  if %r{^unix:///} =~ @docker_uri
    request = Net::HTTP::Get.new path.to_s
  else
    uri = URI("#{@docker_uri}#{path}")
    request = Net::HTTP::Get.new uri.request_uri
  end
  response = @client.request(request)
  case response
  when Net::HTTPSuccess     then response
  when Net::HTTPRedirection then call(response['location'], true, limit - 1)
  else
    return response.error! unless halt == false
    return response
  end
end

#parse(path, halt = true, limit = 10) ⇒ Object



46
47
48
49
# File 'lib/sensu-plugins-docker/client_helpers.rb', line 46

def parse(path, halt = true, limit = 10)
  parsed = parse_json(call(path, halt, limit))
  parsed
end

#uriObject



24
25
26
# File 'lib/sensu-plugins-docker/client_helpers.rb', line 24

def uri
  @docker_uri
end