Skip to content

Commit 4819ea7

Browse files
authored
Merge pull request #805 from canihavethisone/main
Fix idempotency when using scaling with docker-compose
2 parents f94b5b8 + c551270 commit 4819ea7

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

lib/puppet/provider/docker_compose/ruby.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
environment(HOME: '/root')
1414
end
1515

16+
has_command(:docker_compose, command(:dockercompose)) do
17+
Dir.mkdir('/tmp_docker') unless Dir.exist?('/tmp_docker')
18+
ENV.store('TMPDIR', '/tmp_docker')
19+
end
20+
1621
def exists?
1722
Puppet.info("Checking for compose project #{name}")
1823
compose_services = {}
@@ -31,18 +36,17 @@ def exists?
3136
"label=com.docker.compose.project=#{name}",
3237
]).split("\n")
3338
compose_containers.push(*containers)
34-
compose_containers.uniq!
3539

3640
compose_services = compose_output['services']
3741

38-
if compose_services.count != compose_containers.count
42+
if compose_services.count != compose_containers.uniq.count
3943
return false
4044
end
4145

4246
counts = Hash[*compose_services.each.map { |key, array|
4347
image = (array['image']) ? array['image'] : get_image(key, compose_services)
4448
Puppet.info("Checking for compose service #{key} #{image}")
45-
["#{key}-#{image}", compose_containers.count("#{key}-#{image}")]
49+
[key, compose_containers.count("#{key}-#{image}")]
4650
}.flatten]
4751

4852
# No containers found for the project

manifests/init.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@
660660
-> Docker::Registry <||>
661661
-> Docker::Image <||>
662662
-> Docker::Run <||>
663+
-> Docker_compose <||>
663664
} else {
664665
contain 'docker::repos'
665666
contain 'docker::install'

spec/acceptance/compose_v3_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ class { 'docker::compose':
3737
let(:install_pp) do
3838
<<-MANIFEST
3939
docker_compose { 'web':
40-
compose_files => ['#{tmp_path}/docker-compose-v3.yml'],
41-
ensure => present,
40+
compose_files => ['#{tmp_path}/docker-compose-v3.yml'],
41+
ensure => present,
42+
scale => {
43+
compose_test => 2
44+
}
4245
}
4346
MANIFEST
4447
end
@@ -62,6 +65,9 @@ class { 'docker::compose':
6265
docker_compose { 'web1':
6366
compose_files => ['#{tmp_path}/docker-compose-v3.yml', '#{tmp_path}/docker-compose-override-v3.yml'],
6467
ensure => present,
68+
scale => {
69+
compose_test => 2
70+
}
6571
}
6672
MANIFEST
6773

spec/classes/init_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@
196196
}
197197
else
198198
it {
199+
# Stub /tmp_docker dir to prevent shelling out during spec test
200+
allow(Dir).to receive(:exist?).and_wrap_original do |original_method, a|
201+
original_method.call(a)
202+
end
203+
allow(Dir).to receive(:exist?).with('/tmp_docker').and_return(true)
204+
199205
is_expected.to contain_class('docker::repos').that_comes_before('Class[docker::install]')
200206
is_expected.to contain_class('docker::install').that_comes_before('Class[docker::config]')
201207
is_expected.to contain_class('docker::config').that_comes_before('Class[docker::service]')

spec/defines/registry_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
require 'spec_helper'
44

5+
# Stub /tmp_docker dir to prevent shelling out during spec test
6+
class Dir
7+
class << self
8+
def exist?(var)
9+
return true if var == '/tmp_docker'
10+
end
11+
end
12+
end
13+
514
tests = {
615
'with ensure => absent' => {
716
'ensure' => 'absent',

spec/unit/lib/puppet/type/docker_compose_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
require 'spec_helper'
44

5+
# Stub /tmp_docker dir to prevent shelling out during spec test
6+
class Dir
7+
class << self
8+
def exist?(var)
9+
return true if var == '/tmp_docker'
10+
end
11+
end
12+
end
13+
514
compose = Puppet::Type.type(:docker_compose)
615

716
describe compose do

0 commit comments

Comments
 (0)