Skip to content

Commit

Permalink
(WIP)-CLOUD-2069 Adding support for multiple compose files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Wilson committed Sep 18, 2018
1 parent b71811e commit 0b59c61
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,16 @@ docker_compose { '/tmp/docker-compose.yml':

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

To supply multiple overide compose files add the following to the manifest file:

```puppet
class {'docker::compose':
compose_files => ['master-docker-compose.yml', 'override-compose.yml],
}
```

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.

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.

To deploy the stack, add the following code to the manifest file:
Expand Down
41 changes: 23 additions & 18 deletions lib/puppet/provider/docker_compose/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@

def exists?
Puppet.info("Checking for compose project #{project}")
compose_file = YAML.safe_load(File.read(name))
compose_services = {}
containers = docker([
'ps',
'--format',
"{{.Label \"com.docker.compose.service\"}}-{{.Image}}",
'--filter',
"label=com.docker.compose.project=#{project}",
]).split("\n")
case compose_file['version']
when %r{^2(\.[0-3])?$}, %r{^3(\.[0-6])?$}
compose_services = compose_file['services']
# in compose v1 "version" parameter is not specified
when nil
compose_services = compose_file
else
raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!")
compose_files.each do |file|
compose_file = YAML.safe_load(File.read(file))
containers = docker([
'ps',
'--format',
"{{.Label \"com.docker.compose.service\"}}-{{.Image}}",
'--filter',
"label=com.docker.compose.project=#{project}",
]).split("\n")
case compose_file['version']
when %r{^2(\.[0-3])?$}, %r{^3(\.[0-6])?$}
compose_services = compose_services.merge(compose_file['services'])
# in compose v1 "version" parameter is not specified
when nil
compose_services = compose_services.merge(compose_file)
else
raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!")
end
end

if compose_services.count != containers.count
Expand Down Expand Up @@ -60,14 +62,17 @@ def get_image(service_name, compose_services)
image
end

# Preappend -f to each compose file in the array and flatten to pass compose files in args.
compose_files_cmd = compose_files.collect {|x| ['-f', x ] }.flatten

def create
Puppet.info("Running compose project #{project}")
args = ['-f', name, 'up', '-d', '--remove-orphans'].insert(2, resource[:options]).insert(5, resource[:up_args]).compact
args = [compose_files_cmd, 'up', '-d', '--remove-orphans'].insert(2, resource[:options]).insert(5, resource[:up_args]).compact
dockercompose(args)
return unless resource[:scale]
instructions = resource[:scale].map { |k, v| "#{k}=#{v}" }
Puppet.info("Scaling compose project #{project}: #{instructions.join(' ')}")
args = ['-f', name, 'scale'].insert(2, resource[:options]).compact + instructions
args = [compose_files_cmd, 'scale'].insert(2, resource[:options]).compact + instructions
dockercompose(args)
end

Expand Down
11 changes: 7 additions & 4 deletions lib/puppet/type/docker_compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ def refresh
provider.restart
end

newparam(:name) do
desc 'Docker compose file path.'
end

newparam(:scale) do
desc 'A hash of compose services and number of containers.'
validate do |value|
Expand Down Expand Up @@ -38,6 +34,13 @@ def refresh
end
end

newparam(:compose_files, :array_matching => :all) do
desc 'An array of Docker Compose Files paths.'
validate do |value|
raise_('compose files should be an array') unless value.is_a? String
end
end

autorequire(:file) do
self[:name]
end
Expand Down

0 comments on commit 0b59c61

Please sign in to comment.