63165f754666345f0a0355db42d61eaa13b1e007
[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         exec { $digest:
44           path    => '/bin:/usr/bin',
45           unless  => "/usr/bin/apt-key list | /bin/grep '${upkey}'",
46           before  => Anchor["apt::key ${upkey} present"],
47           command => $method ? {
48             'content' => "echo '${key_content}' | /usr/bin/apt-key add -",
49             'source'  => "wget -q '${key_source}' -O- | apt-key add -",
50             'server'  => "apt-key adv --keyserver '${key_server}' --recv-keys '${upkey}'",
51           },
52         }
53       }
54
55       Anchor["apt::key $upkey present"] -> Anchor["apt::key/$title"]
56
57     }
58     absent: {
59
60       if defined(Anchor["apt::key $upkey present"]) {
61         fail ("Cannot ensure Apt::Key[$upkey] absent; $upkey already ensured present")
62       }
63
64       exec { "apt::key $upkey absent":
65         path    => '/bin:/usr/bin',
66         onlyif  => "apt-key list | grep '${upkey}'",
67         command => "apt-key del '${upkey}'",
68         user    => 'root',
69         group   => 'root',
70       }
71     }
72
73     default: {
74       fail "Invalid 'ensure' value '${ensure}' for aptkey"
75     }
76   }
77 }