Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dockerapi (0.22.0)
dockerapi (0.22.1)
base64
excon (>= 0.97, < 2)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
* Update version.
* Run tests.
* Commit changes.
* Release / push version to Rubygems & Github.
* Push a new Github tag with `git tag $(bundle exec rake version | tr -d '"') && git push --tags`.

## Contributing

Expand Down
19 changes: 17 additions & 2 deletions lib/docker/api/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
##
# Base class to provide general methods, helpers and implementations accross classes.
class Docker::API::Base
[:get, :post, :head, :delete, :put].each do | method |
define_method(method) { | path, params = {} | self.request(method: method, path: path, params: params) }
end

##
# Call an Excon request and returns a Docker::API::Response object.
#
# @param params [Hash]: Request parameters.
def request params
params[:path] = build_path(params[:path], params[:params] ||= {})
response = Docker::API::Response.new(@connection.excon.request(params).data)
response.request_params = params
p response if Docker::API.print_response_to_stdout
response
end

##
# Create new object and sets the validation to happen automatically when method parameters include "params" or "body".
Expand Down Expand Up @@ -43,9 +58,9 @@ def default_writer path
# @param url [String]: Endpoint URL where the file is going to be sent.
# @param header [Hash]: Header of the request.
# @param &block: Replace the default output to stdout behavior.
def default_reader path, url, header = {"Content-Type" => "application/x-tar"}, &block
def default_reader path, base_url, params, header = {"Content-Type" => "application/x-tar"}, &block
file = File.open(File.expand_path(path), "r")
response = @connection.request(method: :post, path: url , headers: header, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s}, response_block: block_given? ? block : default_streamer )
response = request(method: :post, path: base_url, params: params , headers: header, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s}, response_block: block_given? ? block : default_streamer )
file.close
response
end
Expand Down
10 changes: 5 additions & 5 deletions lib/docker/api/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Docker::API::Config < Docker::API::Base
#
# @param params [Hash]: Parameters that are appended to the URL.
def list params = {}
@connection.get(build_path("/configs",params))
get("/configs", params)
end

# Create a config
Expand All @@ -24,7 +24,7 @@ def list params = {}
#
# @param body [Hash]: Request body to be sent as json.
def create body = {}
@connection.request(method: :post, path: "/v#{Docker::API::API_VERSION}/configs/create", headers: {"Content-Type": "application/json"}, body: body.to_json)
request(method: :post, path: "/configs/create", headers: {"Content-Type": "application/json"}, body: body.to_json)
end

# Inspect a config
Expand All @@ -35,7 +35,7 @@ def create body = {}
#
# @param name [String]: The ID or name of the config.
def details name
@connection.get("/v#{Docker::API::API_VERSION}/configs/#{name}")
get("/configs/#{name}")
end

# Update a config
Expand All @@ -50,7 +50,7 @@ def details name
#
# @param body [Hash]: Request body to be sent as json.
def update name, params = {}, body = {}
@connection.request(method: :post, path: build_path("/v#{Docker::API::API_VERSION}/configs/#{name}/update",params), headers: {"Content-Type": "application/json"}, body: body.to_json)
request(method: :post, path: "/configs/#{name}/update", params: params, headers: {"Content-Type": "application/json"}, body: body.to_json)
end

# Delete a config
Expand All @@ -61,6 +61,6 @@ def update name, params = {}, body = {}
#
# @param name [String]: The ID or name of the config.
def remove name
@connection.delete("/v#{Docker::API::API_VERSION}/configs/#{name}")
delete("/configs/#{name}")
end
end
19 changes: 3 additions & 16 deletions lib/docker/api/connection.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
##
# Connection class.
class Docker::API::Connection
[:get, :post, :head, :delete, :put].each do | method |
define_method(method) { | path | self.request(method: method, path: path) }
end

##
# Call an Excon request and returns a Docker::API::Response object.
#
# @param params [Hash]: Request parameters.
def request params
response = Docker::API::Response.new(@connection.request(params).data)
response.request_params = params
p response if Docker::API.print_response_to_stdout
response
end
attr_reader(:excon)

##
# Create an Excon connection.
#
# @param url [String]: URL for the connection.
# @param params [String]: Additional parameters.
def initialize url = nil, params = {}
return @connection = Excon.new('unix:///', params.merge({socket: '/var/run/docker.sock'})) unless url
@connection = Excon.new(url, params)
return @excon = Excon.new('unix:///', params.merge({socket: '/var/run/docker.sock'})) unless url
@excon = Excon.new(url, params)
end

end
59 changes: 29 additions & 30 deletions lib/docker/api/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Docker::API::Container < Docker::API::Base
#
# @param params [Hash]: Parameters that are appended to the URL.
def list params = {}
@connection.get(build_path("/containers/json", params))
get("/containers/json", params)
end

##
Expand All @@ -23,7 +23,7 @@ def list params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def details name, params = {}
@connection.get(build_path("/containers/#{name}/json", params))
get("/containers/#{name}/json", params)
end

##
Expand All @@ -35,7 +35,7 @@ def details name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def top name, params = {}
@connection.get(build_path("/containers/#{name}/top", params))
get("/containers/#{name}/top", params)
end

##
Expand All @@ -46,7 +46,7 @@ def top name, params = {}
#
# @param name [String]: The ID or name of the container.
def changes name
@connection.get(build_path("/containers/#{name}/changes"))
get("/containers/#{name}/changes")
end

##
Expand All @@ -58,7 +58,7 @@ def changes name
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def start name, params = {}
@connection.post(build_path("/containers/#{name}/start", params))
post("/containers/#{name}/start", params)
end

##
Expand All @@ -70,7 +70,7 @@ def start name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def stop name, params = {}
@connection.post(build_path("/containers/#{name}/stop", params))
post("/containers/#{name}/stop", params)
end

##
Expand All @@ -82,7 +82,7 @@ def stop name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def restart name, params = {}
@connection.post(build_path("/containers/#{name}/restart", params))
post("/containers/#{name}/restart", params)
end

##
Expand All @@ -94,7 +94,7 @@ def restart name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def kill name, params = {}
@connection.post(build_path("/containers/#{name}/kill", params))
post("/containers/#{name}/kill", params)
end

##
Expand All @@ -106,7 +106,7 @@ def kill name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def wait name, params = {}
@connection.post(build_path("/containers/#{name}/wait", params))
post("/containers/#{name}/wait", params)
end

##
Expand All @@ -118,8 +118,7 @@ def wait name, params = {}
# @param name [String]: The ID or name of the container.
# @param body [Hash]: Request body to be sent as json.
def update name, body = {}

@connection.request(method: :post, path: build_path("/containers/#{name}/update"), headers: {"Content-Type": "application/json"}, body: body.to_json)
request(method: :post, path: "/containers/#{name}/update", headers: {"Content-Type": "application/json"}, body: body.to_json)
end

##
Expand All @@ -131,7 +130,7 @@ def update name, body = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def rename name, params = {}
@connection.post(build_path("/containers/#{name}/rename", params))
post("/containers/#{name}/rename", params)
end

##
Expand All @@ -143,7 +142,7 @@ def rename name, params = {}
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def resize name, params = {}
@connection.post(build_path("/containers/#{name}/resize", params))
post("/containers/#{name}/resize", params)
end

##
Expand All @@ -154,7 +153,7 @@ def resize name, params = {}
#
# @param params [Hash]: Parameters that are appended to the URL.
def prune params = {}
@connection.post(build_path("/containers/prune", params))
post("/containers/prune", params)
end

##
Expand All @@ -165,7 +164,7 @@ def prune params = {}
#
# @param name [String]: The ID or name of the container.
def pause name
@connection.post(build_path("/containers/#{name}/pause"))
post("/containers/#{name}/pause")
end

##
Expand All @@ -176,7 +175,7 @@ def pause name
#
# @param name [String]: The ID or name of the container.
def unpause name
@connection.post(build_path("/containers/#{name}/unpause"))
post("/containers/#{name}/unpause")
end

##
Expand All @@ -188,7 +187,7 @@ def unpause name
# @param name [String]: The ID or name of the container.
# @param params [Hash]: Parameters that are appended to the URL.
def remove name, params = {}
@connection.delete(build_path("/containers/#{name}", params))
delete("/containers/#{name}", params)
end

##
Expand All @@ -201,13 +200,12 @@ def remove name, params = {}
# @param params [Hash]: Parameters that are appended to the URL.
# @param &block: Replace the default output to stdout behavior.
def logs name, params = {}, &block

path = build_path("/containers/#{name}/logs", params)
path = "/containers/#{name}/logs"

if [true, 1 ].include? params[:follow]
@connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
request(method: :get, path: path, params: params, response_block: block_given? ? block : default_streamer)
else
@connection.get(path)
get(path, params)
end
end

Expand All @@ -221,7 +219,7 @@ def logs name, params = {}, &block
# @param params [Hash]: Parameters that are appended to the URL.
# @param &block: Replace the default output to stdout behavior.
def attach name, params = {}, &block
@connection.request(method: :post, path: build_path("/containers/#{name}/attach", params) , response_block: block_given? ? block : default_streamer)
request(method: :post, path: "/containers/#{name}/attach", params: params , response_block: block_given? ? block : default_streamer)
end

##
Expand All @@ -233,7 +231,7 @@ def attach name, params = {}, &block
# @param params [Hash]: Parameters that are appended to the URL.
# @param body [Hash]: Request body to be sent as json.
def create params = {}, body = {}
@connection.request(method: :post, path: build_path("/containers/create", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
request(method: :post, path: "/containers/create", params: params, headers: {"Content-Type": "application/json"}, body: body.to_json)
end

##
Expand All @@ -246,11 +244,12 @@ def create params = {}, body = {}
# @param params [Hash]: Parameters that are appended to the URL.
# @param &block: Replace the default output to stdout behavior.
def stats name, params = {}, &block
path = build_path("/containers/#{name}/stats", params)
path = "/containers/#{name}/stats"

if [true, 1 ].include? params[:stream]
@connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
request(method: :get, path: path, params: params, response_block: block_given? ? block : default_streamer)
else
@connection.get(path)
get(path, params)
end
end

Expand All @@ -266,7 +265,7 @@ def stats name, params = {}, &block
def export name, path, &block
response = self.details(name)
return response unless response.status == 200
@connection.request(method: :get, path: "/containers/#{name}/export" , response_block: block_given? ? block : default_writer(path))
request(method: :get, path: "/containers/#{name}/export", response_block: block_given? ? block : default_writer(path))
end

##
Expand All @@ -282,11 +281,11 @@ def export name, path, &block
# @param params [Hash]: Parameters that are appended to the URL.
# @param &block: Replace the default file writing behavior.
def get_archive name, path, params = {}, &block
response = @connection.head(build_path("/containers/#{name}/archive", params))
response = head("/containers/#{name}/archive", params)
return response unless response.status == 200

file = File.open( File.expand_path( path ) , "wb")
response = @connection.request(method: :get, path: build_path("/containers/#{name}/archive", params) , response_block: block_given? ? block : lambda { |chunk, remaining_bytes, total_bytes| file.write(chunk) })
response = request(method: :get, path: "/containers/#{name}/archive", params: params , response_block: block_given? ? block : lambda { |chunk, remaining_bytes, total_bytes| file.write(chunk) })
file.close
response
end
Expand All @@ -304,7 +303,7 @@ def get_archive name, path, params = {}, &block
# @param params [Hash]: Parameters that are appended to the URL.
def put_archive name, path, params = {}
file = File.open( File.expand_path( path ) , "r")
response = @connection.request(method: :put, path: build_path("/containers/#{name}/archive", params) , request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
response = request(method: :put, path: "/containers/#{name}/archive", params: params , request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
file.close
response
end
Expand Down
8 changes: 4 additions & 4 deletions lib/docker/api/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Docker::API::Exec < Docker::API::Base
# @param name [String]: The ID or name of the container.
# @param body [Hash]: Request body to be sent as json.
def create name, body = {}
@connection.request(method: :post, path: build_path("/containers/#{name}/exec"), headers: {"Content-Type": "application/json"}, body: body.to_json )
request(method: :post, path: "/containers/#{name}/exec", headers: {"Content-Type": "application/json"}, body: body.to_json )
end

##
Expand All @@ -25,7 +25,7 @@ def create name, body = {}
# @param body [Hash]: Request body to be sent as json.
# @param &block: Replace the default output to stdout behavior.
def start name, body = {}, &block
@connection.request(method: :post, path: build_path("/exec/#{name}/start"), headers: {"Content-Type": "application/json"}, body: body.to_json,
request(method: :post, path: "/exec/#{name}/start", headers: {"Content-Type": "application/json"}, body: body.to_json,
response_block: block_given? ? block : default_streamer )
end

Expand All @@ -38,7 +38,7 @@ def start name, body = {}, &block
# @param name [String]: Exec instance ID.
# @param body [Hash]: Request body to be sent as json.
def resize name, params = {}
@connection.post(build_path("/exec/#{name}/resize", params))
post("/exec/#{name}/resize", params)
end

##
Expand All @@ -49,7 +49,7 @@ def resize name, params = {}
#
# @param name [String]: Exec instance ID.
def details name
@connection.get(build_path("/exec/#{name}/json"))
get("/exec/#{name}/json")
end

end
Loading