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