dockerc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d31aaeeb7a0afc460b89d17ed826a6653ec51deb
4
+ data.tar.gz: ae300a20f6114ccd4b425d646db435cbe9027717
5
+ SHA512:
6
+ metadata.gz: 8107b5860b6f30cd4f620a1f3d9420c58f97e07b4169ff9c456f416608b3fde4799a81b234de2c8da49c499a93c66c8df961818310f629147d0d2749ceb8a348
7
+ data.tar.gz: 0ecb78a29669c9ac87647aa840164d4f6a1af0de2ddec5f69b0aab284cb0ef4cecb90f86811fee470d1691f668411f9371bd82e7ba29d79f10a5bc7e5dbf3c8a
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dockerc.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Gorsuch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Dockerc
2
+
3
+ **Note: this is alpha software. The interfaces are not stable.**
4
+
5
+ A lightweight docker client that focuses on:
6
+
7
+ * returning simple data structures instead of any defined object model
8
+ * normalization of parameters
9
+ * exceptions thrown in exceptional cases
10
+ * explicit code to reduce confusion
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'dockerc'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install dockerc
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ # connect to unix:///var/run/docker.sock
30
+ c = Dockerc::Client.new
31
+ # connect to an alternate docker endpoint
32
+ c = Dockerc::Client.new(docker_url: 'http://localhost:4243')
33
+
34
+ # pull an image from the public repository
35
+ c.pull_image 'gorsuch/litterbox'
36
+
37
+ # launch a container
38
+ c.create_container :image => 'gorsuch/litterbox', :cmd => 'date'
39
+
40
+ # list all containers
41
+ c.containers
42
+ #=> [{:command=>"date ", :created=>1392044336, :id=>"ea1c87570244276041caafb69ab2fd102c974b0b214e885304de00222b9f6bd0", :image=>"gorsuch/litterbox:latest", :names=>["/desperate_hawking"], :ports=>[], :status=>"Exit 0"}]
43
+
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( http://github.com/<my-github-username>/dockerc/fork )
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push 'lib'
6
+ t.libs.push 'test'
7
+ t.test_files = FileList['test/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dockerc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dockerc'
8
+ spec.version = Dockerc::VERSION
9
+ spec.authors = ['Michael Gorsuch']
10
+ spec.email = ['michael.gorsuch@gmail.com']
11
+ spec.summary = %q{Yet another docker client.}
12
+ spec.description = spec.summary
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'excon'
22
+ spec.add_dependency 'multi_json'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.5'
25
+ spec.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'uri'
2
+
3
+ require 'excon'
4
+ require 'multi_json'
5
+
6
+ require 'dockerc/client'
7
+ require 'dockerc/errors'
8
+ require 'dockerc/normalizer'
9
+ require 'dockerc/version'
@@ -0,0 +1,83 @@
1
+ module Dockerc
2
+ class Client
3
+ attr_reader :docker_url
4
+
5
+ def initialize(args={})
6
+ @docker_url = args.fetch(:docker_url, ENV['DOCKER_URL'] || 'unix:///var/run/docker.sock')
7
+ end
8
+
9
+ def connection
10
+ @connection ||= create_connection!
11
+ end
12
+
13
+ def create_connection!
14
+ uri = URI.parse docker_url
15
+ if uri.scheme == 'unix'
16
+ Excon.new('unix:///', socket: uri.path, persistent: true)
17
+ else
18
+ Excon.new(docker_url, persistent: true)
19
+ end
20
+ end
21
+
22
+ def containers
23
+ json = connection.get({
24
+ path: '/containers/json',
25
+ query: { all: 1 },
26
+ expects: [ 200 ]
27
+ }).body
28
+
29
+ normalizer.handle_response_data(MultiJson.load(json))
30
+ end
31
+
32
+ def create_container(params)
33
+ req = {
34
+ path: '/containers/create',
35
+ body: normalizer.handle_request_data(params).to_json,
36
+ expects: [ 201 ]
37
+ }
38
+ begin
39
+ res = connection.post(req)
40
+ rescue Excon::Errors::InternalServerError => e
41
+ raise Dockerc::Errors::ContainerCreationError, e.response.body
42
+ rescue Excon::Errors::NotFound
43
+ raise Dockerc::Errors::ImageNotFound
44
+ end
45
+
46
+ normalizer.handle_response_data(MultiJson.load(res.body))
47
+ end
48
+
49
+ def images
50
+ json = connection.get({
51
+ path: '/images/json',
52
+ query: { all: 1 },
53
+ expects: [ 200 ]
54
+ }).body
55
+
56
+ normalizer.handle_response_data(MultiJson.load(json))
57
+ end
58
+
59
+ def pull_image(name)
60
+ parts = []
61
+ streamer = lambda do |chunk, remaining_bytes, total_bytes|
62
+ data = MultiJson.load(chunk)
63
+ parts << normalizer.handle_response_data(data)
64
+ end
65
+ body = connection.post({
66
+ path: '/images/create',
67
+ query: normalizer.handle_request_query(from_image: name),
68
+ expects: [ 200 ],
69
+ response_block: streamer
70
+ }).body
71
+ unless parts.last.has_key?(:id)
72
+ raise Dockerc::Errors::ImagePullFailed, parts
73
+ end
74
+ parts
75
+ end
76
+
77
+ private
78
+
79
+ def normalizer
80
+ @normalizer ||= Dockerc::Normalizer.new
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ module Dockerc
2
+ module Errors
3
+ class ContainerCreationError < StandardError ; end
4
+ class ImageNotFound < StandardError ; end
5
+ class ImagePullFailed < StandardError ; end
6
+ end
7
+ end
@@ -0,0 +1,60 @@
1
+ module Dockerc
2
+ class Normalizer
3
+ def handle_request_query(h)
4
+ h.inject({}) do |memo,(k,v)|
5
+ memo[handle_request_query_key(k)] = handle_request_query_value(v)
6
+ memo
7
+ end
8
+ end
9
+
10
+ def handle_request_query_key(s)
11
+ parts = s.to_s.split('_')
12
+ parts.map!(&:capitalize)
13
+ parts.first.downcase!
14
+ parts.join
15
+ end
16
+
17
+ def handle_request_query_value(s)
18
+ case s
19
+ when TrueClass
20
+ 1
21
+ when FalseClass
22
+ 0
23
+ else
24
+ s
25
+ end
26
+ end
27
+
28
+ def handle_request_data(h)
29
+ h.inject({}) do |memo, (k,v)|
30
+ memo[handle_request_key(k)] = v
31
+ memo
32
+ end
33
+ end
34
+
35
+ def handle_request_key(s)
36
+ parts = s.to_s.split('_')
37
+ parts.map(&:capitalize).join
38
+ end
39
+
40
+ def handle_response_hash(h)
41
+ h.inject({}) do |memo, (k,v)|
42
+ memo[handle_response_hash_param(k)] = v
43
+ memo
44
+ end
45
+ end
46
+
47
+ def handle_response_hash_param(s)
48
+ s.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase.to_sym
49
+ end
50
+
51
+ def handle_response_data(data)
52
+ case data
53
+ when Hash
54
+ handle_response_hash(data)
55
+ when Array
56
+ data.map { |h| handle_response_hash h }
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Dockerc
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class NormalizerTest < MiniTest::Unit::TestCase
4
+ def test_create
5
+ assert Dockerc::Normalizer.new
6
+ end
7
+
8
+ def test_query
9
+ n = Dockerc::Normalizer.new
10
+ assert_equal({'command' => 'foo'}, n.handle_request_query({:command => 'foo'}))
11
+ assert_equal({'fromImage' => 'gorsuch/litterbox'}, n.handle_request_query({:from_image => 'gorsuch/litterbox'}))
12
+ assert_equal({'all' => 1}, n.handle_request_query({:all => true}))
13
+ end
14
+
15
+ def test_request
16
+ n = Dockerc::Normalizer.new
17
+ assert_equal({'AttachStdin' => false}, n.handle_request_data({:attach_stdin => false}))
18
+ end
19
+
20
+ def test_response
21
+ n = Dockerc::Normalizer.new
22
+ assert_equal({:id => '12345', :warnings => []}, n.handle_response_data({'Id' => '12345', 'Warnings' => []}))
23
+ assert_equal([{:id => '12345'}, {:id => '6789'}], n.handle_response_data([{'Id' => '12345'}, {'Id' => '6789'}]))
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require 'dockerc'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dockerc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Gorsuch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Yet another docker client.
70
+ email:
71
+ - michael.gorsuch@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - dockerc.gemspec
82
+ - lib/dockerc.rb
83
+ - lib/dockerc/client.rb
84
+ - lib/dockerc/errors.rb
85
+ - lib/dockerc/normalizer.rb
86
+ - lib/dockerc/version.rb
87
+ - test/normalizer_test.rb
88
+ - test/test_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Yet another docker client.
113
+ test_files:
114
+ - test/normalizer_test.rb
115
+ - test/test_helper.rb