diff --git a/lib/puppet/functions/yum/get_gpg_keys.rb b/lib/puppet/functions/yum/get_gpg_keys.rb new file mode 100644 index 00000000..6771469d --- /dev/null +++ b/lib/puppet/functions/yum/get_gpg_keys.rb @@ -0,0 +1,24 @@ +Puppet::Functions.create_function(:'yum::get_gpg_keys') do + dispatch :get_gpg_keys do + param 'String', :key_file + end + + def get_gpg_keys(key_file) + keys = [] + if File.exist?(key_file) + cmd = "/usr/bin/gpg #{key_file}" + outt = Puppet::Util::Execution.execute(cmd).split("\n") + # Iterate thru each output line + outt.each do |line| + # Only public keys + if line[0..2] == 'pub' + the_key = line.split(' ')[1].split('/')[1].downcase + keys.push(the_key) + end + end + else + Puppet.warning("Key file '#(key_file)' does not exist") + end + keys + end +end diff --git a/manifests/gpgkey.pp b/manifests/gpgkey.pp index 3ffefc9a..c4b7d05d 100644 --- a/manifests/gpgkey.pp +++ b/manifests/gpgkey.pp @@ -58,28 +58,25 @@ mode => $mode, } - $rpmname = "gpg-pubkey-$(gpg --with-colons ${path} | \ -head -n 1 | \ -cut -d: -f5 | \ -cut -c9-16 | \ -tr '[A-Z]' '[a-z]')" - - case $ensure { - 'present', default: { - exec { "rpm-import-${name}": - path => '/bin:/usr/bin:/sbin/:/usr/sbin', - command => "rpm --import ${path}", - unless => "rpm -q ${rpmname}", - require => File[$path], + $keys = yum::get_gpg_keys($path) + $keys.each |String $key| { + $the_rpmname = "gpg-pubkey-${key}" + case $ensure { + 'present', default: { + exec { "rpm-import-${name}": + path => '/bin:/usr/bin:/sbin/:/usr/sbin', + command => "rpm --import ${path}", + unless => "rpm -q ${the_rpmname}", + require => File[$path], + } } - } - - 'absent': { - exec { "rpm-delete-${name}": - path => '/bin:/usr/bin:/sbin/:/usr/sbin', - command => "rpm -e ${rpmname}", - onlyif => ["test -f ${path}", "rpm -q ${rpmname}"], - before => File[$path], + 'absent': { + exec { "rpm-delete-${name}": + path => '/bin:/usr/bin:/sbin/:/usr/sbin', + command => "rpm -e ${the_rpmname}", + onlyif => ["test -f ${path}", "rpm -q ${the_rpmname}"], + before => File[$path], + } } } }