key.pp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. define apt::key (
  2. $key = $title,
  3. $ensure = present,
  4. $key_content = false,
  5. $key_source = false,
  6. $key_server = 'keyserver.ubuntu.com'
  7. ) {
  8. include apt::params
  9. $upkey = upcase($key)
  10. if $key_content {
  11. $method = 'content'
  12. } elsif $key_source {
  13. $method = 'source'
  14. } elsif $key_server {
  15. $method = 'server'
  16. }
  17. # This is a hash of the parts of the key definition that we care about.
  18. # It is used as a unique identifier for this instance of apt::key. It gets
  19. # hashed to ensure that the resource name doesn't end up being pages and
  20. # pages (e.g. in the situation where key_content is specified).
  21. $digest = sha1("${upkey}/${key_content}/${key_source}/${key_server}/")
  22. # Allow multiple ensure => present for the same key to account for many
  23. # apt::source resources that all reference the same key.
  24. case $ensure {
  25. present: {
  26. anchor { "apt::key/${title}": }
  27. if defined(Exec["apt::key ${upkey} absent"]) {
  28. fail("Cannot ensure Apt::Key[${upkey}] present; ${upkey} already ensured absent")
  29. }
  30. if !defined(Anchor["apt::key ${upkey} present"]) {
  31. anchor { "apt::key ${upkey} present": }
  32. }
  33. if !defined(Exec[$digest]) {
  34. $digest_command = $method ? {
  35. 'content' => "echo '${key_content}' | /usr/bin/apt-key add -",
  36. 'source' => "wget -q '${key_source}' -O- | apt-key add -",
  37. 'server' => "apt-key adv --keyserver '${key_server}' --recv-keys '${upkey}'",
  38. }
  39. exec { $digest:
  40. command => $digest_command,
  41. path => '/bin:/usr/bin',
  42. unless => "/usr/bin/apt-key list | /bin/grep '${upkey}'",
  43. logoutput => 'on_failure',
  44. before => Anchor["apt::key ${upkey} present"],
  45. }
  46. }
  47. Anchor["apt::key ${upkey} present"] -> Anchor["apt::key/${title}"]
  48. }
  49. absent: {
  50. if defined(Anchor["apt::key ${upkey} present"]) {
  51. fail("Cannot ensure Apt::Key[${upkey}] absent; ${upkey} already ensured present")
  52. }
  53. exec { "apt::key ${upkey} absent":
  54. command => "apt-key del '${upkey}'",
  55. path => '/bin:/usr/bin',
  56. onlyif => "apt-key list | grep '${upkey}'",
  57. user => 'root',
  58. group => 'root',
  59. logoutput => 'on_failure',
  60. }
  61. }
  62. default: {
  63. fail "Invalid 'ensure' value '${ensure}' for aptkey"
  64. }
  65. }
  66. }