Class: Dockerc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dockerc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/dockerc/client.rb', line 5

def initialize(args={})
  @docker_url = args.fetch(:docker_url, ENV['DOCKER_URL'] || 'unix:///var/run/docker.sock')
end

Instance Attribute Details

#docker_urlObject (readonly)

Returns the value of attribute docker_url.



3
4
5
# File 'lib/dockerc/client.rb', line 3

def docker_url
  @docker_url
end

Instance Method Details

#connectionObject



9
10
11
# File 'lib/dockerc/client.rb', line 9

def connection
  @connection ||= create_connection!
end

#containersObject



22
23
24
25
26
27
28
29
30
# File 'lib/dockerc/client.rb', line 22

def containers
  json = connection.get({
    path: '/containers/json',
    query: { all: 1 },
    expects: [ 200 ]
  }).body

  normalizer.handle_response_data(MultiJson.load(json))
end

#create_connection!Object



13
14
15
16
17
18
19
20
# File 'lib/dockerc/client.rb', line 13

def create_connection!
  uri = URI.parse docker_url
  if uri.scheme == 'unix'
    Excon.new('unix:///', socket: uri.path, persistent: true)
  else
    Excon.new(docker_url, persistent: true)
  end
end

#create_container(params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dockerc/client.rb', line 32

def create_container(params)
  req = {
    path:    '/containers/create',
    body:    normalizer.handle_request_data(params).to_json,
    expects: [ 201 ]
  }
  begin
    res = connection.post(req)
  rescue Excon::Errors::InternalServerError => e
    raise Dockerc::Errors::ContainerCreationError, e.response.body
  rescue Excon::Errors::NotFound
    raise Dockerc::Errors::ImageNotFound
  end

  normalizer.handle_response_data(MultiJson.load(res.body))
end

#imagesObject



49
50
51
52
53
54
55
56
57
# File 'lib/dockerc/client.rb', line 49

def images
  json = connection.get({
    path:    '/images/json',
    query:   { all: 1 },
    expects: [ 200 ]
  }).body

  normalizer.handle_response_data(MultiJson.load(json))
end

#pull_image(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dockerc/client.rb', line 59

def pull_image(name)
  parts = []
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
    data = MultiJson.load(chunk)
    parts << normalizer.handle_response_data(data)
  end
  body = connection.post({
    path:    '/images/create',
    query:   normalizer.handle_request_query(from_image: name),
    expects: [ 200 ],
    response_block: streamer
  }).body
  unless parts.last.has_key?(:id)
    raise Dockerc::Errors::ImagePullFailed, parts
  end
  parts
end