docker_helper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # docker_helper -- Helper methods to interact with Docker #
5
+ # #
6
+ # Copyright (C) 2014 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # docker_helper is free software; you can redistribute it and/or modify it #
12
+ # under the terms of the GNU Affero General Public License as published by #
13
+ # the Free Software Foundation; either version 3 of the License, or (at your #
14
+ # option) any later version. #
15
+ # #
16
+ # docker_helper is distributed in the hope that it will be useful, but #
17
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
18
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
19
+ # License for more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with docker_helper. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module DockerHelper
28
+
29
+ # Generic proxy class that allows to call prefixed methods with or without
30
+ # that prefix via #method_missing.
31
+
32
+ class Proxy
33
+
34
+ def initialize(prefix = 'docker')
35
+ self.proxy_prefix = prefix
36
+ end
37
+
38
+ attr_accessor :proxy_prefix
39
+
40
+ def method_missing(method, *args, &block)
41
+ respond_to_missing?(method) ?
42
+ send(prefix_method(method), *args, &block) : super
43
+ end
44
+
45
+ def respond_to_missing?(method, _ = false)
46
+ respond_to?(prefix_method(method))
47
+ end
48
+
49
+ private
50
+
51
+ def prefix_method(method)
52
+ "#{proxy_prefix}_#{method}"
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,27 @@
1
+ module DockerHelper
2
+
3
+ module Version
4
+
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ TINY = 1
8
+
9
+ class << self
10
+
11
+ # Returns array representation.
12
+ def to_a
13
+ [MAJOR, MINOR, TINY]
14
+ end
15
+
16
+ # Short-cut for version string.
17
+ def to_s
18
+ to_a.join('.')
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ VERSION = Version.to_s
26
+
27
+ end
@@ -0,0 +1,203 @@
1
+ describe DockerHelper do
2
+
3
+ subject { Object.new.extend(described_class) }
4
+
5
+ describe '#version' do
6
+
7
+ it 'should return the client version' do
8
+ expect_pipe('Client version: 1.2.3', 'version', {})
9
+ expect(subject.docker_version).to eq('1.2.3')
10
+ end
11
+
12
+ end
13
+
14
+ describe '#tags' do
15
+
16
+ it 'should return the image tags' do
17
+ expect_pipe(<<-EOT, 'images', {})
18
+ REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
19
+ foo/bar baz 123 now 0.0
20
+ quix 1.1 456 yesterday 0.0
21
+ quix 1.0 789 a week ago 0.0
22
+ EOT
23
+
24
+ expect(subject.docker_tags('quix')).to eq(%w[1.0 1.1])
25
+ end
26
+
27
+ end
28
+
29
+ describe '#build' do
30
+
31
+ it 'should build the image' do
32
+ expect_system(true, 'build', '-t', 'foo', 'bar', {})
33
+ expect(subject.docker_build('bar', 'foo')).to eq(true)
34
+ end
35
+
36
+ end
37
+
38
+ describe '#volume' do
39
+
40
+ it 'should return the volume path' do
41
+ expect_pipe('path', 'inspect', '-f', '{{index .Volumes "volume"}}', 'foo', {})
42
+ expect(subject.docker_volume('volume', 'foo')).to eq('path')
43
+ end
44
+
45
+ end
46
+
47
+ describe '#url' do
48
+
49
+ it 'should return the URL' do
50
+ expect_pipe('0.0.0.0:42', 'port', 'foo', '23', {})
51
+ expect(subject.docker_url(23, 'foo')).to eq('http://0.0.0.0:42')
52
+ end
53
+
54
+ end
55
+
56
+ describe '#start' do
57
+
58
+ it 'should start the container' do
59
+ expect_system(true, 'run', '-d', '-P', '--name', 'foo', 'bar', {})
60
+ expect(subject.docker_start('foo', 'bar')).to eq(true)
61
+ end
62
+
63
+ end
64
+
65
+ describe '#start!' do
66
+
67
+ describe 'when already running' do
68
+
69
+ before do
70
+ expect_system(true, 'stop', 'foo', err: :close)
71
+ expect_system(true, 'start', 'foo', {})
72
+ end
73
+
74
+ describe 'without a block' do
75
+
76
+ it 'should restart the container' do
77
+ expect(subject.docker_start!('foo', 'bar')).to eq(true)
78
+ end
79
+
80
+ end
81
+
82
+ describe 'with a block' do
83
+
84
+ it 'should ignore the block' do
85
+ expect { |b| subject.docker_start!('foo', 'bar', &b) }.not_to yield_control
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ describe 'when not running' do
93
+
94
+ before do
95
+ expect_system(false, 'stop', 'foo', err: :close)
96
+ expect_system(false, 'start', 'foo', {})
97
+ expect_system(true, 'run', '-d', '-P', '--name', 'foo', 'bar', {})
98
+ end
99
+
100
+ describe 'without a block' do
101
+
102
+ it 'should start the container' do
103
+ expect(subject.docker_start!('foo', 'bar')).to eq(true)
104
+ end
105
+
106
+ end
107
+
108
+ describe 'with a block' do
109
+
110
+ it 'should start the container and yield control' do
111
+ expect { |b| subject.docker_start!('foo', 'bar', &b) }.to yield_with_args('foo', 'bar')
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ describe '#stop' do
121
+
122
+ it 'should stop the container' do
123
+ expect_system(true, 'stop', 'foo', err: :close)
124
+ expect(subject.docker_stop('foo')).to eq(true)
125
+ end
126
+
127
+ end
128
+
129
+ describe '#restart' do
130
+
131
+ describe 'when already running' do
132
+
133
+ it 'should restart the container' do
134
+ expect_system(true, 'stop', 'foo', err: :close)
135
+ expect_system(true, 'start', 'foo', {})
136
+ expect(subject.docker_restart('foo')).to eq(true)
137
+ end
138
+
139
+ end
140
+
141
+ describe 'when not running' do
142
+
143
+ it 'should restart the container' do
144
+ expect_system(false, 'stop', 'foo', err: :close)
145
+ expect_system(true, 'start', 'foo', {})
146
+ expect(subject.docker_restart('foo')).to eq(true)
147
+ end
148
+
149
+ end
150
+
151
+ end
152
+
153
+ describe '#clean' do
154
+
155
+ it 'should remove the container' do
156
+ expect_system(true, 'stop', 'foo', err: :close)
157
+ expect_system(true, 'rm', '-v', '-f', 'foo', err: :close)
158
+ expect(subject.docker_clean('foo')).to eq(true)
159
+ end
160
+
161
+ end
162
+
163
+ describe '#clobber' do
164
+
165
+ it 'should remove the container and the image' do
166
+ expect_system(true, 'stop', 'foo', err: :close)
167
+ expect_system(true, 'rm', '-v', '-f', 'foo', err: :close)
168
+ expect_system(true, 'rmi', 'bar', err: :close)
169
+ expect(subject.docker_clobber('foo', 'bar')).to eq(true)
170
+ end
171
+
172
+ end
173
+
174
+ describe '#reset' do
175
+
176
+ it 'should reset the container' do
177
+ expect_system(true, 'stop', 'foo', err: :close)
178
+ expect_system(true, 'rm', '-v', '-f', 'foo', err: :close)
179
+ expect_system(true, 'run', '-d', '-P', '--name', 'foo', 'bar', {})
180
+ expect(subject.docker_reset('foo', 'bar')).to eq(true)
181
+ end
182
+
183
+ end
184
+
185
+ end
186
+
187
+ describe DockerHelper, '::proxy' do
188
+
189
+ subject { described_class.proxy }
190
+
191
+ before do
192
+ expect(subject).to receive(:docker_version).with(no_args).and_return('1.2.3')
193
+ end
194
+
195
+ it 'should respond to unabbreviated method' do
196
+ expect(subject.docker_version).to eq('1.2.3')
197
+ end
198
+
199
+ it 'should respond to abbreviated method' do
200
+ expect(subject.version).to eq('1.2.3')
201
+ end
202
+
203
+ end
@@ -0,0 +1,19 @@
1
+ $:.unshift('lib') unless $:.first == 'lib'
2
+
3
+ require 'docker_helper'
4
+
5
+ RSpec.configure { |config|
6
+ config.mock_with(:rspec) { |c| c.verify_partial_doubles = true }
7
+
8
+ config.include(Module.new {
9
+ def expect_pipe(result, *args)
10
+ expect(subject).to receive(:docker_pipe).with('sudo', /\Adocker(?:\.io)?\z/, *args).and_return(result)
11
+ expect(subject).not_to receive(:docker_system)
12
+ end
13
+
14
+ def expect_system(result, *args)
15
+ expect(subject).to receive(:docker_system).with('sudo', /\Adocker(?:\.io)?\z/, *args).and_return(result)
16
+ expect(subject).not_to receive(:docker_pipe)
17
+ end
18
+ })
19
+ }
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docker_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jens Wille
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Control the Docker command-line client from Ruby.
56
+ email: jens.wille@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - README
61
+ - COPYING
62
+ - ChangeLog
63
+ files:
64
+ - COPYING
65
+ - ChangeLog
66
+ - README
67
+ - Rakefile
68
+ - lib/docker_helper.rb
69
+ - lib/docker_helper/proxy.rb
70
+ - lib/docker_helper/version.rb
71
+ - spec/docker_helper_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/blackwinter/docker_helper
74
+ licenses:
75
+ - AGPL-3.0
76
+ metadata: {}
77
+ post_install_message: |2+
78
+
79
+ docker_helper-0.0.1 [2014-08-05]:
80
+
81
+ * First release.
82
+
83
+ rdoc_options:
84
+ - "--title"
85
+ - docker_helper Application documentation (v0.0.1)
86
+ - "--charset"
87
+ - UTF-8
88
+ - "--line-numbers"
89
+ - "--all"
90
+ - "--main"
91
+ - README
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.9.3
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.1
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Helper methods to interact with Docker.
110
+ test_files: []