Skip to content

Commit 0b59c61

Browse files
author
Mark Wilson
committed
(WIP)-CLOUD-2069 Adding support for multiple compose files.
1 parent b71811e commit 0b59c61

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,16 @@ docker_compose { '/tmp/docker-compose.yml':
558558

559559
Give options to the ```docker-compose up``` command, such as ```--remove-orphans```, by using the ```up_args``` option.
560560

561+
To supply multiple overide compose files add the following to the manifest file:
562+
563+
```puppet
564+
class {'docker::compose':
565+
compose_files => ['master-docker-compose.yml', 'override-compose.yml],
566+
}
567+
```
568+
569+
Please note you should supply your master docker-compose file as the first element in the array. As per docker multi compose file support compose files will be merged in the order they are specified in the array.
570+
561571
If you are using a v3.2 compose file or above on a Docker Swarm cluster, use the `docker::stack` class. Include the file resource before you run the stack command.
562572

563573
To deploy the stack, add the following code to the manifest file:

lib/puppet/provider/docker_compose/ruby.rb

+23-18
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@
77

88
def exists?
99
Puppet.info("Checking for compose project #{project}")
10-
compose_file = YAML.safe_load(File.read(name))
1110
compose_services = {}
12-
containers = docker([
13-
'ps',
14-
'--format',
15-
"{{.Label \"com.docker.compose.service\"}}-{{.Image}}",
16-
'--filter',
17-
"label=com.docker.compose.project=#{project}",
18-
]).split("\n")
19-
case compose_file['version']
20-
when %r{^2(\.[0-3])?$}, %r{^3(\.[0-6])?$}
21-
compose_services = compose_file['services']
22-
# in compose v1 "version" parameter is not specified
23-
when nil
24-
compose_services = compose_file
25-
else
26-
raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!")
11+
compose_files.each do |file|
12+
compose_file = YAML.safe_load(File.read(file))
13+
containers = docker([
14+
'ps',
15+
'--format',
16+
"{{.Label \"com.docker.compose.service\"}}-{{.Image}}",
17+
'--filter',
18+
"label=com.docker.compose.project=#{project}",
19+
]).split("\n")
20+
case compose_file['version']
21+
when %r{^2(\.[0-3])?$}, %r{^3(\.[0-6])?$}
22+
compose_services = compose_services.merge(compose_file['services'])
23+
# in compose v1 "version" parameter is not specified
24+
when nil
25+
compose_services = compose_services.merge(compose_file)
26+
else
27+
raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!")
28+
end
2729
end
2830

2931
if compose_services.count != containers.count
@@ -60,14 +62,17 @@ def get_image(service_name, compose_services)
6062
image
6163
end
6264

65+
# Preappend -f to each compose file in the array and flatten to pass compose files in args.
66+
compose_files_cmd = compose_files.collect {|x| ['-f', x ] }.flatten
67+
6368
def create
6469
Puppet.info("Running compose project #{project}")
65-
args = ['-f', name, 'up', '-d', '--remove-orphans'].insert(2, resource[:options]).insert(5, resource[:up_args]).compact
70+
args = [compose_files_cmd, 'up', '-d', '--remove-orphans'].insert(2, resource[:options]).insert(5, resource[:up_args]).compact
6671
dockercompose(args)
6772
return unless resource[:scale]
6873
instructions = resource[:scale].map { |k, v| "#{k}=#{v}" }
6974
Puppet.info("Scaling compose project #{project}: #{instructions.join(' ')}")
70-
args = ['-f', name, 'scale'].insert(2, resource[:options]).compact + instructions
75+
args = [compose_files_cmd, 'scale'].insert(2, resource[:options]).compact + instructions
7176
dockercompose(args)
7277
end
7378

lib/puppet/type/docker_compose.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ def refresh
77
provider.restart
88
end
99

10-
newparam(:name) do
11-
desc 'Docker compose file path.'
12-
end
13-
1410
newparam(:scale) do
1511
desc 'A hash of compose services and number of containers.'
1612
validate do |value|
@@ -38,6 +34,13 @@ def refresh
3834
end
3935
end
4036

37+
newparam(:compose_files, :array_matching => :all) do
38+
desc 'An array of Docker Compose Files paths.'
39+
validate do |value|
40+
raise_('compose files should be an array') unless value.is_a? String
41+
end
42+
end
43+
4144
autorequire(:file) do
4245
self[:name]
4346
end

0 commit comments

Comments
 (0)