Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image and misc endpoints #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ task :default => :spec
namespace :docker do
desc "Stop and delete all Docker containers"
task :cleanup do
container = Docker::API.new(base_url: 'http://10.0.5.5:4243').containers
container = Docker::API.new(base_url: ENV['DOCKER_BASE_URL']).containers
counter = 0
container.list(all: true).each do |c|
puts "Delete container #{c['Id']}"
79 changes: 79 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

# NEXT TODO

* Describe how to setup dev environment
* beschreiben dass mindestens base image auf dem docker host vorhanden sein muss bevor live tests laufen
-> am besten rake task 'setup_dev_env'
-> setup ist docker pull base
* beschreiben wie tests genau ausgeführt werden können, inkl. live etc.
* Describe .env file for tests and development, add an example .env file

* Resolve merge conflict.
* make image tests working with fixtures! by changing VCR settings
* implement image endpoint:
* search (fix test; impl should be ready)
* push
* pull
* import
* insert
* build (under misc)


* Add version param into URLs



# To Do

* Add Rake task to prepare live/recording test env!
* Error handling depending on status code.
* Document code with YARD
* Add thin layer of models (e.g. to create a new container)
* All connections need to be SSL
* Authentication is required for API (needs to be added on Docker side)
* Implement `export` of a container





# Resources

Faraday

* http://adventuresincoding.com/2010/09/writing-modular-http-client-code-with-faraday/
* http://www.intridea.com/blog/2012/3/12/faraday-one-http-client-to-rule-them-all


# Development Hints

## Streaming

Testing the streaming API is easy with CURL. Run on docker node:

./docker run -d base /bin/sh -c "while true; do echo hello world; sleep 1; done"

Now you can attach via web API to it

curl -X POST "http://10.0.5.5:4243/containers/d1158045962d/attach?stream=1&stdout=1"

Code example with CURB

easy = Curl::Easy.new
easy.url = 'http://10.0.5.5:4243/containers/d1158045962d/attach?stream=1&stdout=1'
# easy.timeout = 60 # to stop attaching after a certain time. Throws Curl::Err::TimeoutError
easy.on_body {|data| puts "rec: #{data}"; data.size }
easy.http('POST') # blocks until connection is closed


## Docker Dev Environment

Start Docker from source

Follow the setup at http://docs.docker.io/en/latest/contributing/devenvironment.html abd execute

sudo $GOPATH/bin/docker -d




1 change: 1 addition & 0 deletions docker.gemspec
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'activesupport', '~> 3.2.13'
spec.add_development_dependency 'guard'
spec.add_development_dependency 'guard-rspec'
spec.add_development_dependency 'dotenv'
spec.add_development_dependency 'awesome_print'

spec.add_runtime_dependency 'multi_json', '~> 1.7'
2 changes: 1 addition & 1 deletion lib/docker/api.rb
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ def images
end

def system

Docker::Resource::System.new(@connection)
end


1 change: 1 addition & 0 deletions lib/docker/resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'docker/resource/base'
require 'docker/resource/container'
require 'docker/resource/image'
require 'docker/resource/system'

module Docker
module Resource
71 changes: 71 additions & 0 deletions lib/docker/resource/image.rb
Original file line number Diff line number Diff line change
@@ -22,6 +22,18 @@ def history(name)
response.body_as_json
end

# Insert file and return ID of new image {'Id' => '<image ID>'}
# Leaves behind a container that was used to download the file
def insert_file(name, destination, url, timeout = nil)
params = {path: destination, url: url}
output = []
response = @connection.stream("/images/#{name}/insert", params, timeout) do |data|
output << data
end

MultiJson.load(output.last)
end


def tag(name, repository, options = {})
options = options.merge(repo: repository)
@@ -42,6 +54,65 @@ def search(term)
@connection.get("/images/search", params).body_as_json
end

# Import an image from the given file
def import

end


# Pull an image from the given registry
def pull(name, repository = nil, tag = nil, timeout = nil, &block)
# TODO set standard repository if nil
params = { fromImage: name }
params[:repo] = repository if repository
params[:tag] = tag if tag
if block.nil?
response = @connection.post("/images/create", params)
else
response = @connection.stream("/images/create", params, timeout, &block)
end
response
end


# Params:
# :registry Registry to use
def push(name, options = {}, timeout = nil, &block)
status = nil
if block.nil?
status = @connection.post("/images/#{name}/push", options).status
else
status = @connection.stream("/images/#{name}/push", options, timeout, &block).status
end
raise_if_image_not_found(status)
status
end

# Build from Dockerfile
# Dockerfile as an IO object (either File IO or StringIO)
# def build(dockerfile, options = {}, timeout = nil, &block)
# docker_cmds = dockerfile.read

# # Content-Type: application/json
# @connection.stream("/build", options, timeout, &block).status
# end

# URL must be either a GIT repository or a URL to a Dockerfile.
#
# Valid options
# t repository name and optionally a tag that is applied to the resulting image
# q suppress verbose build output [1|0]
# nocache do not use the cache when building the image
#
# Last line stream is "Successfully built bfa2cdabeb02"
def build_from_url(url, options = {}, timeout = nil, &block)

# TODO URL muss encoded werden!!! -> sollte eigenltich CURB machen???

params = options.merge({ remote: url })
@connection.stream("/build", params, timeout, &block).status
end

private

def raise_if_image_not_found(status)
33 changes: 33 additions & 0 deletions lib/docker/resource/system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Docker
module Resource
end
end

class Docker::Resource::System < Docker::Resource::Base

def auth(user, email, password)
body = {
'username' => user,
'email' => email,
'password' => password
}
json_body = MultiJson.dump(body)
@connection.post('/auth', {}, json_body).body_as_json
end

def account
@connection.get('/auth').body_as_json
end

def info
@connection.get('/info').body_as_json
end

def version
@connection.get('/version').body_as_json
end


end


159 changes: 9 additions & 150 deletions spec/cassettes/Docker_Connection/returns_a_stream.yml

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions spec/cassettes/Docker_Connection/sets_given_query_parameters.yml
12 changes: 6 additions & 6 deletions spec/cassettes/Docker_Connection/sets_given_request_headers.yml
167 changes: 16 additions & 151 deletions spec/cassettes/Docker_Resource_Container/kill/the_container.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

167 changes: 16 additions & 151 deletions spec/cassettes/Docker_Resource_Container/restarts/the_container.yml
Loading