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