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