(#13289) Fix some more style violations
[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           path    => '/bin:/usr/bin',
50           unless  => "/usr/bin/apt-key list | /bin/grep '${upkey}'",
51           before  => Anchor["apt::key ${upkey} present"],
52           command => $digest_command,
53         }
54       }
55
56       Anchor["apt::key $upkey present"] -> Anchor["apt::key/$title"]
57
58     }
59     absent: {
60
61       if defined(Anchor["apt::key ${upkey} present"]) {
62         fail("Cannot ensure Apt::Key[${upkey}] absent; ${upkey} already ensured present")
63       }
64
65       exec { "apt::key ${upkey} absent":
66         path    => '/bin:/usr/bin',
67         onlyif  => "apt-key list | grep '${upkey}'",
68         command => "apt-key del '${upkey}'",
69         user    => 'root',
70         group   => 'root',
71       }
72     }
73
74     default: {
75       fail "Invalid 'ensure' value '${ensure}' for aptkey"
76     }
77   }
78 }