-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding in type/provider for docker stack (#433)
- Loading branch information
1 parent
593d234
commit 686e2ae
Showing
6 changed files
with
188 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'deep_merge' | ||
|
||
Puppet::Type.type(:docker_stack).provide(:ruby) do | ||
desc 'Support for Puppet running Docker Stacks' | ||
|
||
mk_resource_methods | ||
commands docker: 'docker' | ||
|
||
def exists? | ||
Puppet.info("Checking for stack #{name}") | ||
stack_services = {} | ||
stack_containers = [] | ||
resource[:compose_files].each do |file| | ||
compose_file = YAML.safe_load(File.read(file), [], [], true) | ||
# rubocop:disable Style/StringLiterals | ||
containers = docker([ | ||
'ps', | ||
'--format', | ||
"{{.Label \"com.docker.swarm.service.name\"}}-{{.Image}}", | ||
'--filter', | ||
"label=com.docker.stack.namespace=#{name}", | ||
]).split("\n").each do |c| | ||
c.slice!("#{name}_") | ||
end | ||
stack_containers.push(*containers) | ||
stack_containers.uniq! | ||
# rubocop:enable Style/StringLiterals | ||
case compose_file['version'] | ||
when %r{^3(\.[0-7])?$} | ||
stack_services.merge!(compose_file['services']) | ||
else | ||
raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!") | ||
end | ||
end | ||
|
||
if stack_services.count != stack_containers.count | ||
return false | ||
end | ||
counts = Hash[*stack_services.each.map { |key, array| | ||
image = (array['image']) ? array['image'] : get_image(key, stack_services) | ||
Puppet.info("Checking for compose service #{key} #{image}") | ||
["#{key}-#{image}", stack_containers.count("#{key}-#{image}")] | ||
}.flatten] | ||
# No containers found for the project | ||
if counts.empty? || | ||
# Containers described in the compose file are not running | ||
counts.any? { |_k, v| v.zero? } | ||
false | ||
else | ||
true | ||
end | ||
end | ||
|
||
def get_image(service_name, stack_services) | ||
image = stack_services[service_name]['image'] | ||
unless image | ||
if stack_services[service_name]['extends'] | ||
image = get_image(stack_services[service_name]['extends'], stack_services) | ||
elsif stack_services[service_name]['build'] | ||
image = "#{name}_#{service_name}" | ||
end | ||
end | ||
image | ||
end | ||
|
||
def create | ||
Puppet.info("Running stack #{name}") | ||
args = ['stack', 'deploy', compose_files, name].insert(1, bundle_file).insert(4, resource[:up_args]).compact | ||
docker(args) | ||
end | ||
|
||
def destroy | ||
Puppet.info("Removing docker stack #{name}") | ||
rm_args = ['stack', 'rm', name] | ||
docker(rm_args) | ||
end | ||
|
||
def bundle_file | ||
return unless resource[:bundle_file].nil? | ||
resource[:bundle_file].map { |x| ['-c', x] }.flatten | ||
end | ||
|
||
def compose_files | ||
resource[:compose_files].map { |x| ['-c', x] }.flatten | ||
end | ||
|
||
private | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
Puppet::Type.newtype(:docker_stack) do | ||
@doc = 'A type representing a Docker Stack' | ||
|
||
ensurable | ||
|
||
newparam(:bundle_file) do | ||
desc 'Path to a Distributed Application Bundle file.' | ||
validate do |value| | ||
raise _('bundle files should be a string') unless value.is_a? String | ||
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? Array | ||
end | ||
end | ||
|
||
newparam(:up_args) do | ||
desc 'Arguments to be passed directly to docker stack deploy.' | ||
validate do |value| | ||
raise _('up_args should be a String') unless value.is_a? String | ||
end | ||
end | ||
|
||
newparam(:name) do | ||
isnamevar | ||
desc 'The name of the stack' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
|
||
stack = Puppet::Type.type(:docker_stack) | ||
|
||
describe stack do | ||
|
||
let :params do | ||
[ | ||
:name, | ||
:provider, | ||
:up_args, | ||
] | ||
end | ||
|
||
let :properties do | ||
[ | ||
:ensure, | ||
] | ||
end | ||
|
||
it 'should have expected properties' do | ||
properties.each do |property| | ||
expect(stack.properties.map(&:name)).to be_include(property) | ||
end | ||
end | ||
|
||
it 'should have expected parameters' do | ||
params.each do |param| | ||
expect(stack.parameters).to be_include(param) | ||
end | ||
end | ||
|
||
it 'should require up_args to be a string' do | ||
expect(stack).to require_string_for('up_args') | ||
end | ||
end |