-
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.
Merge pull request #3 from puppetlabs/CLOUD-1401
adding in docker secrets
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'shellwords' | ||
|
||
module Puppet::Parser::Functions | ||
# Transforms a hash into a string of docker swarm init flags | ||
newfunction(:docker_secrets_flags, :type => :rvalue) do |args| | ||
opts = args[0] || {} | ||
flags = [] | ||
|
||
if opts['ensure'].to_s == 'present' | ||
flags << 'create' | ||
end | ||
|
||
if opts['secret_name'].to_s != 'undef' | ||
flags << "'#{opts['secret_name']}'" | ||
end | ||
|
||
if opts['secret_path'].to_s != 'undef' | ||
flags << "'#{opts['secret_path']}'" | ||
end | ||
|
||
multi_flags = lambda { |values, format| | ||
filtered = [values].flatten.compact | ||
filtered.map { |val| sprintf(format + " \\\n", val) } | ||
} | ||
[ | ||
['-l %s', 'label'] | ||
].each do |(format, key)| | ||
values = opts[key] | ||
new_flags = multi_flags.call(values, format) | ||
flags.concat(new_flags) | ||
end | ||
|
||
flags.flatten.join(" ") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
define docker::secrets ( | ||
|
||
$ensure = 'present', | ||
$label = [], | ||
$secret_name = undef, | ||
$secret_path = undef, | ||
){ | ||
include docker::params | ||
|
||
$docker_command = "${docker::params::docker_command} secret" | ||
validate_re($ensure, '^(present|absent)$') | ||
validate_string($docker_command) | ||
validate_string($secret_name) | ||
validate_string($secret_path) | ||
validate_array($label) | ||
|
||
|
||
|
||
if $ensure == 'present'{ | ||
$docker_secrets_flags = docker_secrets_flags ({ | ||
ensure => $ensure, | ||
label => $label, | ||
secret_name => $secret_name, | ||
secret_path => $secret_path, | ||
}) | ||
|
||
$exec_secret = "${docker_command} ${docker_secrets_flags}" | ||
$unless_secret = "${docker_command} inspect ${secret_name}" | ||
|
||
exec { 'docker secret create': | ||
command => $exec_secret, | ||
unless => $unless_secret, | ||
path => ['/bin', '/usr/bin'], | ||
} | ||
} | ||
|
||
if $ensure == 'absent'{ | ||
|
||
exec { 'docker secret rm': | ||
command => "${docker_command} rm ${secret_name}", | ||
onlyif => "${docker_command} inspect ${secret_name}", | ||
path => ['/bin', '/usr/bin'], | ||
} | ||
} | ||
} |
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,30 @@ | ||
require 'spec_helper' | ||
|
||
describe 'docker::secrets', :type => :define do | ||
let(:title) { 'test_secret' } | ||
let(:facts) { { | ||
:osfamily => 'Debian', | ||
:operatingsystem => 'Debian', | ||
:lsbdistid => 'Debian', | ||
:lsbdistcodename => 'jessie', | ||
:kernelrelease => '3.2.0-4-amd64', | ||
:operatingsystemmajrelease => '8', | ||
} } | ||
|
||
context 'with secret_name => test_secret and secret_path => /root/secret.txt and label => test' do | ||
let(:params) { { | ||
'secret_name' => 'test_secret', | ||
'secret_path' => '/root/secret.txt', | ||
'label' => ['test'], | ||
} } | ||
it { should contain_exec('docker secret create').with_command(/docker secret create/) } | ||
end | ||
|
||
context 'with ensure => absent and secret_name => test_secret' do | ||
let(:params) { { | ||
'ensure' => 'absent', | ||
'secret_name' => 'test_secret'} } | ||
it { should contain_exec('docker secret rm').with_command(/docker secret rm/) } | ||
end | ||
|
||
end |