diff --git a/lib/puppet/parser/functions/docker_plugin_enable_flags.rb b/lib/puppet/parser/functions/docker_plugin_enable_flags.rb index 409eff5..749897d 100644 --- a/lib/puppet/parser/functions/docker_plugin_enable_flags.rb +++ b/lib/puppet/parser/functions/docker_plugin_enable_flags.rb @@ -1,16 +1,20 @@ # frozen_string_literal: true require 'shellwords' # # docker_plugin_remove_flags.rb # module Puppet::Parser::Functions # Transforms a hash into a string of docker plugin remove flags newfunction(:docker_plugin_enable_flags, type: :rvalue) do |args| opts = args[0] || {} flags = [] flags << '--force' if opts['force_remove'] == true - flags << "'#{opts['plugin_name']}'" if opts['plugin_name'] && opts['plugin_name'].to_s != 'undef' + if opts['plugin_alias'] && opts['plugin_alias'].to_s != 'undef' + flags << "'#{opts['plugin_alias']}'" + elsif opts['plugin_name'] && opts['plugin_name'].to_s != 'undef' + flags << "'#{opts['plugin_name']}'" + end flags.flatten.join(' ') end end diff --git a/manifests/plugin.pp b/manifests/plugin.pp index 0eef363..08b94bd 100644 --- a/manifests/plugin.pp +++ b/manifests/plugin.pp @@ -1,135 +1,136 @@ # == Define: docker::plugin # # A define that manages a docker plugin # # == Parameters # # [*plugin_name*] # This ensures whether the plugin is installed or not. # Defaults to present # Note that the default behaviour of docker plugin # requires a plugin be disabled before it can be removed # # [*plugin_name*] # The name of the docker plugin # Defaults to the $title used in the define. # # [*enabled*] # A setting to enable or disable an installed plugin. # Defaults to true # # [*timeout*] # The number of seconds to wait when enabling a plugin # Defaults to undef # # [*plugin_alias*] # An alternative name to use for an installed plugin # Defaults to undef # # [*disable_on_install*] # Alters the default behaviour of enabling a plugin upon install # Defaults to false # # [*disable_content_trust*] # Skip image verification # Defaults to true # # [*grant_all_permissions] # Grant all permissions necessary to run the plugin # Defaults to true # # [*force_remove*] # Force the removal of an active plugin # Defaults to true # # [*settings*] # Any additional settings to pass to the plugin during install # Defaults to undef # define docker::plugin( Optional[Pattern[/^present$|^absent$/]] $ensure = 'present', String $plugin_name = $title, Optional[Boolean] $enabled = true, Optional[String] $timeout = undef, Optional[String] $plugin_alias = undef, Optional[Boolean] $disable_on_install = false, Optional[Boolean] $disable_content_trust = true, Optional[Boolean] $grant_all_permissions = true, Optional[Boolean] $force_remove = true, Optional[Array] $settings = [], ){ include docker::params $docker_command = "${docker::params::docker_command} plugin" if ($::osfamily == 'windows') { fail(translate('Feature not implemented on windows.')) } if $ensure == 'present' { $docker_plugin_install_flags = docker_plugin_install_flags({ plugin_name => $plugin_name, plugin_alias => $plugin_alias, disable_on_install => $disable_on_install, disable_content_trust => $disable_content_trust, grant_all_permissions => $grant_all_permissions, settings => $settings, }) $exec_install = "${docker_command} install ${docker_plugin_install_flags}" - $unless_install = "${docker_command} ls | grep -w ${plugin_name}" + $unless_install = "${docker_command} ls --format='{{.PluginReference}}' | grep -w ${plugin_name}" exec { "plugin install ${plugin_name}": command => $exec_install, environment => 'HOME=/root', path => ['/bin', '/usr/bin'], timeout => 0, unless => $unless_install, } } elsif $ensure == 'absent' { $docker_plugin_remove_flags = docker_plugin_remove_flags({ plugin_name => $plugin_name, force_remove => $force_remove, }) $exec_rm = "${docker_command} rm ${docker_plugin_remove_flags}" - $onlyif_rm = "${docker_command} ls | grep -w ${plugin_name}" + $onlyif_rm = "${docker_command} ls --format='{{.PluginReference}}' | grep -w ${plugin_name}" exec { "plugin remove ${plugin_name}": command => $exec_rm, environment => 'HOME=/root', path => ['/bin', '/usr/bin'], timeout => 0, onlyif => $onlyif_rm, } } if $enabled { $docker_plugin_enable_flags = docker_plugin_enable_flags({ plugin_name => $plugin_name, + plugin_alias => $plugin_alias, timeout => $timeout, }) $exec_enable = "${docker_command} enable ${docker_plugin_enable_flags}" - $onlyif_enable = "${docker_command} ls -f enabled=false | grep -w ${plugin_name}" + $onlyif_enable = "${docker_command} ls -f enabled=false --format='{{.PluginReference}}' | grep -w ${plugin_name}" exec { "plugin enable ${plugin_name}": command => $exec_enable, environment => 'HOME=/root', path => ['/bin', '/usr/bin'], timeout => 0, onlyif => $onlyif_enable, } } elsif $enabled == false { exec { "disable ${plugin_name}": command => "${docker_command} disable ${plugin_name}", environment => 'HOME=/root', path => ['/bin', '/usr/bin'], timeout => 0, - unless => "${docker_command} ls -f enabled=false | grep -w ${plugin_name}", + unless => "${docker_command} ls -f enabled=false --format='{{.PluginReference}}' | grep -w ${plugin_name}", } } } diff --git a/spec/defines/plugin_spec.rb b/spec/defines/plugin_spec.rb index a5b673b..3132716 100644 --- a/spec/defines/plugin_spec.rb +++ b/spec/defines/plugin_spec.rb @@ -1,34 +1,44 @@ require 'spec_helper' describe 'docker::plugin', type: :define do let(:title) { 'foo/plugin:latest' } let(:facts) do { osfamily: 'Debian', operatingsystem: 'Debian', lsbdistid: 'Debian', lsbdistcodename: 'jessie', kernelrelease: '3.2.0-4-amd64', operatingsystemmajrelease: '8', } end context 'with defaults for all parameters' do it { is_expected.to compile.with_all_deps } it { is_expected.to contain_exec('plugin install foo/plugin:latest').with_command(%r{docker plugin install}) } + it { is_expected.to contain_exec('plugin install foo/plugin:latest').with_unless(%r{docker ls --format='{{.PluginReference}}' | grep -w foo/plugin:latest}) } end context 'with enabled => false' do let(:params) { { 'enabled' => false } } it { is_expected.to compile.with_all_deps } it { is_expected.to contain_exec('disable foo/plugin:latest').with_command(%r{docker plugin disable}) } + it { is_expected.to contain_exec('disable foo/plugin:latest').with_unless(%r{docker ls --format='{{.PluginReference}}' | grep -w foo/plugin:latest}) } end context 'with ensure => absent' do let(:params) { { 'ensure' => 'absent' } } it { is_expected.to compile.with_all_deps } it { is_expected.to contain_exec('plugin remove foo/plugin:latest').with_command(%r{docker plugin rm}) } + it { is_expected.to contain_exec('plugin remove foo/plugin:latest').with_onlyif(%r{docker ls --format='{{.PluginReference}}' | grep -w foo/plugin:latest}) } + end + + context 'with alias => foo-plugin' do + let(:params) { { 'plugin_alias' => 'foo-plugin' } } + + it { is_expected.to contain_exec('plugin install foo/plugin:latest').with_command(%r{docker plugin install}) } + it { is_expected.to contain_exec('plugin install foo/plugin:latest').with_unless(%r{docker ls --format='{{.PluginReference}}' | grep -w foo/plugin:latest}) } end end