docker-compose-api 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8cd19119cd22c6ff9c235e71f0f3b7dda2170cd9
4
+ data.tar.gz: d8a5b668de10c6fecad255cc96f0359f59c1888c
5
+ SHA512:
6
+ metadata.gz: 38ccca06b4af322ed96bab86176f2ded9b61da314cd51b57cb485d4dc445a074cca17a800d20450f6731078fc7c3b36ca484f50baf218ec0db7cccba7703daf6
7
+ data.tar.gz: 169c08e927c55f7544036e2a4c4572300cfc886bd6ae035e02cb3ab1cd93bf95e9b0dbcbbbfd073a85b3f7fb5a81224d90bca6182a3ce5c56714690fa0292d6c
data/README.md CHANGED
@@ -19,7 +19,7 @@ gem install docker-compose-api
19
19
 
20
20
  ```ruby
21
21
  # Add the line below on your Gemfile...
22
- gem 'docker-compose-api', git: 'https://github.com/mauricioklein/docker-compose-api'
22
+ gem 'docker-compose-api'
23
23
 
24
24
  # ... and run bundle install
25
25
  bundle install
@@ -44,8 +44,9 @@ DockerCompose.version
44
44
  compose = DockerCompose.load('[path to docker compose file]')
45
45
 
46
46
  # Accessing containers
47
- compose.containers # access all containers
48
- compose.containers['[container name]'] # access a specific container
47
+ compose.containers # access all containers
48
+ compose.containers['container_label'] # access a container by its label (DEPRECATED)
49
+ compose.get_containers_by(label: 'foo', name: 'bar') # Returns an array of all containers with label = 'foo' and name = bar
49
50
 
50
51
  # Starting containers (and their dependencies)
51
52
  compose.start # start all containers
@@ -67,7 +68,7 @@ compose.delete # delete all containers
67
68
  compose.delete(['container1', 'container2', ...]) # delete a list of specific containers
68
69
 
69
70
  # Checking if a container is running or not
70
- a_container = compose.containers['a_container']
71
+ a_container = compose.get_containers_by(name: 'a_container').first
71
72
  a_container.running?
72
73
 
73
74
  # Accessing container informations
@@ -18,9 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "docker-api", "~> 1.22.2"
21
+ spec.add_dependency "docker-api", "1.22.2"
22
22
 
23
- spec.add_development_dependency "bundler"
24
- spec.add_development_dependency "rspec"
25
- spec.add_development_dependency "simplecov"
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rspec", "~> 3.3"
25
+ spec.add_development_dependency "simplecov", "~> 0.10"
26
26
  end
@@ -38,6 +38,7 @@ module DockerCompose
38
38
  def self.create_container(attributes)
39
39
  ComposeContainer.new({
40
40
  label: attributes[0],
41
+ name: attributes[1]['container_name'],
41
42
  image: attributes[1]['image'],
42
43
  build: attributes[1]['build'],
43
44
  links: attributes[1]['links'],
@@ -19,6 +19,15 @@ class Compose
19
19
  true
20
20
  end
21
21
 
22
+ #
23
+ # Select containers based on attributes given by "params"
24
+ #
25
+ def get_containers_by(params)
26
+ @containers.values.select do |container|
27
+ (params.to_a - container.attributes.to_a).empty?
28
+ end
29
+ end
30
+
22
31
  #
23
32
  # Create link relations among containers
24
33
  #
@@ -9,6 +9,7 @@ class ComposeContainer
9
9
  def initialize(hash_attributes)
10
10
  @attributes = {
11
11
  label: hash_attributes[:label],
12
+ name: hash_attributes[:name].nil? ? hash_attributes[:label] : hash_attributes[:name],
12
13
  image: ComposeUtils.format_image(hash_attributes[:image]),
13
14
  build: hash_attributes[:build],
14
15
  links: ComposeUtils.format_links(hash_attributes[:links]),
@@ -54,6 +55,8 @@ class ComposeContainer
54
55
  # Prepare attributes
55
56
  port_bindings = prepare_port_bindings
56
57
  links = prepare_links
58
+ volumes = prepare_volumes
59
+ volume_binds = @attributes[:volumes] && @attributes[:volumes].reject { |volume| volume.split(':').one? }
57
60
 
58
61
  # Exposed ports are port bindings with an empty hash as value
59
62
  exposed_ports = {}
@@ -63,15 +66,19 @@ class ComposeContainer
63
66
  Image: @internal_image,
64
67
  Cmd: @attributes[:command],
65
68
  Env: @attributes[:environment],
66
- Volumes: @attributes[:volumes],
69
+ Volumes: volumes,
67
70
  ExposedPorts: exposed_ports,
68
71
  HostConfig: {
72
+ Binds: volume_binds,
69
73
  Links: links,
70
74
  PortBindings: port_bindings
71
75
  }
72
76
  }
73
77
 
74
- @container = Docker::Container.create(container_config)
78
+ query_params = { 'name' => @attributes[:name] }
79
+
80
+ params = container_config.merge(query_params)
81
+ @container = Docker::Container.create(params)
75
82
  end
76
83
 
77
84
  #
@@ -108,6 +115,28 @@ class ComposeContainer
108
115
  links
109
116
  end
110
117
 
118
+ #
119
+ # Transforms an array of [(host:)container(:accessmode)] to a hash
120
+ # required by the Docker api.
121
+ #
122
+ def prepare_volumes
123
+ return unless @attributes[:volumes]
124
+
125
+ volumes = {}
126
+
127
+ @attributes[:volumes].each do |volume|
128
+ parts = volume.split(':')
129
+
130
+ if parts.one?
131
+ volumes[parts[0]] = {}
132
+ else
133
+ volumes[parts[1]] = { parts[0] => parts[2] || 'rw' }
134
+ end
135
+ end
136
+
137
+ volumes
138
+ end
139
+
111
140
  #
112
141
  # Process each port entry in docker compose file and
113
142
  # create structure recognized by docker client
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module DockerCompose
2
2
  def self.version
3
- "1.0.4"
3
+ "1.0.5"
4
4
  end
5
5
  end
@@ -205,6 +205,19 @@ describe DockerCompose do
205
205
  container1.stop
206
206
  end
207
207
 
208
+ it 'binds volumes' do
209
+ container1 = @compose.containers.values.first
210
+
211
+ # Start container
212
+ container1.start
213
+
214
+ volumes = container1.stats['HostConfig']['Binds']
215
+ expect(volumes).to match_array(['/tmp/test:/tmp:ro'])
216
+
217
+ # Stop container
218
+ container1.stop
219
+ end
220
+
208
221
  it 'supports setting environment as array' do
209
222
  container1 = @compose.containers.values.first
210
223
 
@@ -231,6 +244,42 @@ describe DockerCompose do
231
244
  container1.stop
232
245
  end
233
246
 
247
+ it 'should assing given name to container' do
248
+ container = @compose.containers.values[0]
249
+
250
+ # Start container
251
+ container.start
252
+
253
+ container_name = container.stats['Name']
254
+ expect(container_name).to eq('/busybox-container')
255
+
256
+ # Stop container
257
+ container.stop
258
+ end
259
+
260
+ it 'should assing a random name to container when name is not given' do
261
+ container = @compose.containers.values[1]
262
+
263
+ # Start container
264
+ container.start
265
+
266
+ container_name = container.stats['Name']
267
+ expect(container_name).to_not be_nil
268
+
269
+ # Stop container
270
+ container.stop
271
+ end
272
+
273
+ it 'should filter containers by its attributes' do
274
+ expect(@compose.get_containers_by(label: 'busybox2')).to eq([@compose.containers['busybox2']])
275
+ expect(@compose.get_containers_by(name: 'busybox-container')).to eq([@compose.containers['busybox1']])
276
+ expect(@compose.get_containers_by(image: 'busybox:latest')).to eq([
277
+ @compose.containers['busybox1'],
278
+ @compose.containers['busybox2'],
279
+ @compose.containers['busybox3']
280
+ ])
281
+ end
282
+
234
283
  after(:all) do
235
284
  @compose.delete
236
285
  end
@@ -1,5 +1,6 @@
1
1
  busybox1:
2
2
  image: busybox
3
+ container_name: busybox-container
3
4
  ports:
4
5
  - "3000"
5
6
  - "8000:8000"
@@ -11,6 +12,8 @@ busybox1:
11
12
  command: ping busybox2
12
13
  environment:
13
14
  - MYENV1=variable1
15
+ volumes:
16
+ - "/tmp/test:/tmp:ro"
14
17
 
15
18
  busybox2:
16
19
  image: busybox
@@ -4,10 +4,12 @@ describe ComposeContainer do
4
4
  context 'Object creation' do
5
5
  before(:all) do
6
6
  @attributes = {
7
+ label: SecureRandom.hex,
7
8
  image: 'busybox:latest',
9
+ name: SecureRandom.hex,
8
10
  links: ['service1:label', 'service2'],
9
11
  ports: ['3000', '8000:8000', '127.0.0.1:8001:8001'],
10
- volumes: {'/tmp' => {}},
12
+ volumes: ['/tmp'],
11
13
  command: 'ping -c 3 localhost',
12
14
  environment: ['ENVIRONMENT']
13
15
  }
@@ -17,6 +19,7 @@ describe ComposeContainer do
17
19
 
18
20
  it 'should prepare attributes correctly' do
19
21
  expect(@entry.attributes[:image]).to eq(@attributes[:image])
22
+ expect(@entry.attributes[:name]).to eq(@attributes[:name])
20
23
  expect(@entry.attributes[:links])
21
24
  .to eq({'service1' => 'label', 'service2' => 'service2'})
22
25
  expect(@entry.attributes[:volumes]).to eq(@attributes[:volumes])
@@ -50,15 +53,18 @@ describe ComposeContainer do
50
53
 
51
54
  context 'From image' do
52
55
  before(:all) do
53
- attributes = {
56
+ @attributes = {
57
+ label: SecureRandom.hex,
54
58
  image: 'busybox:latest',
59
+ name: SecureRandom.hex,
55
60
  links: ['links:links'],
56
- volumes: {'/tmp' => {}},
61
+ volumes: ['/tmp'],
57
62
  command: 'ping -c 3 localhost',
58
63
  environment: ['ENVIRONMENT']
59
64
  }
60
65
 
61
- @entry = ComposeContainer.new(attributes)
66
+ @entry = ComposeContainer.new(@attributes)
67
+ @entry_autogen_name = ComposeContainer.new(@attributes.reject{|key| key == :name})
62
68
  end
63
69
 
64
70
  it 'should start/stop a container' do
@@ -82,6 +88,26 @@ describe ComposeContainer do
82
88
  @entry.stop
83
89
  expect(@entry.running?).to be false
84
90
  end
91
+
92
+ it 'should assign a given name to container' do
93
+ #Start container
94
+ @entry.start
95
+
96
+ expect(@entry.stats['Name']).to eq("/#{@attributes[:name]}")
97
+
98
+ # Stop container
99
+ @entry.stop
100
+ end
101
+
102
+ it 'should assign label to container name when name is not given' do
103
+ #Start container
104
+ @entry_autogen_name.start
105
+
106
+ expect(@entry_autogen_name.stats['Name']).to eq("/#{@entry_autogen_name.attributes[:label]}")
107
+
108
+ # Stop container
109
+ @entry_autogen_name.stop
110
+ end
85
111
  end
86
112
 
87
113
  context 'From Dockerfile' do
@@ -89,7 +115,7 @@ describe ComposeContainer do
89
115
  attributes = {
90
116
  build: File.expand_path('spec/docker-compose/fixtures/'),
91
117
  links: ['links:links'],
92
- volumes: {'/tmp' => {}}
118
+ volumes: ['/tmp']
93
119
  }
94
120
 
95
121
  @entry = ComposeContainer.new(attributes)
@@ -122,7 +148,7 @@ describe ComposeContainer do
122
148
  before(:all) do
123
149
  attributes = {
124
150
  links: ['links:links'],
125
- volumes: {'/tmp' => {}},
151
+ volumes: ['/tmp'],
126
152
  command: 'ps aux',
127
153
  environment: ['ENVIRONMENT']
128
154
  }
@@ -150,4 +176,31 @@ describe ComposeContainer do
150
176
  expect(@entry.attributes[:environment]).to eq(%w(ENVIRONMENT=VALUE))
151
177
  end
152
178
  end
179
+
180
+ describe '#prepare_volumes' do
181
+ let(:attributes) do
182
+ { image: 'busybox:latest' }
183
+ end
184
+
185
+ it 'correctly parses container-only volumes' do
186
+ attributes[:volumes] = ['/tmp']
187
+ entry = ComposeContainer.new(attributes)
188
+ volumes = entry.send(:prepare_volumes)
189
+ expect(volumes).to eq({ '/tmp' => {} })
190
+ end
191
+
192
+ it 'correctly parses host-container mapped volumes' do
193
+ attributes[:volumes] = ['./tmp:/tmp']
194
+ entry = ComposeContainer.new(attributes)
195
+ volumes = entry.send(:prepare_volumes)
196
+ expect(volumes).to eq({ '/tmp' => { './tmp' => 'rw' } })
197
+ end
198
+
199
+ it 'correctly parses host-container mapped volumes with access rights' do
200
+ attributes[:volumes] = ['./tmp:/tmp:ro']
201
+ entry = ComposeContainer.new(attributes)
202
+ volumes = entry.send(:prepare_volumes)
203
+ expect(volumes).to eq({ '/tmp' => { './tmp' => 'ro' } })
204
+ end
205
+ end
153
206
  end
metadata CHANGED
@@ -1,80 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-compose-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
5
- prerelease:
4
+ version: 1.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mauricio S. Klein
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-12-29 00:00:00.000000000 Z
11
+ date: 2016-01-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: docker-api
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.22.2
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - '='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 1.22.2
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '0'
33
+ version: '1.7'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
- version: '0'
40
+ version: '1.7'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '0'
47
+ version: '3.3'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
- version: '0'
54
+ version: '3.3'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: simplecov
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
- version: '0'
61
+ version: '0.10'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
- version: '0'
68
+ version: '0.10'
78
69
  description: A simple ruby client for docker-compose api
79
70
  email:
80
71
  - mauricio.klein.msk@gmail.com
@@ -82,9 +73,9 @@ executables: []
82
73
  extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
- - .gitignore
86
- - .rspec
87
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
88
79
  - Gemfile
89
80
  - LICENSE.txt
90
81
  - README.md
@@ -105,27 +96,26 @@ files:
105
96
  homepage: https://github.com/mauricioklein/docker-compose-api
106
97
  licenses:
107
98
  - MIT
99
+ metadata: {}
108
100
  post_install_message:
109
101
  rdoc_options: []
110
102
  require_paths:
111
103
  - lib
112
104
  required_ruby_version: !ruby/object:Gem::Requirement
113
- none: false
114
105
  requirements:
115
- - - ! '>='
106
+ - - ">="
116
107
  - !ruby/object:Gem::Version
117
108
  version: '0'
118
109
  required_rubygems_version: !ruby/object:Gem::Requirement
119
- none: false
120
110
  requirements:
121
- - - ! '>='
111
+ - - ">="
122
112
  - !ruby/object:Gem::Version
123
113
  version: '0'
124
114
  requirements: []
125
115
  rubyforge_project:
126
- rubygems_version: 1.8.23
116
+ rubygems_version: 2.4.5
127
117
  signing_key:
128
- specification_version: 3
118
+ specification_version: 4
129
119
  summary: A simple ruby client for docker-compose api
130
120
  test_files:
131
121
  - spec/docker-compose/docker_compose_spec.rb