Fixed circular dependency for package dirmngr
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
1 # == Define: apt::key
2 define apt::key (
3     String $id                           = $title,
4     Enum['present', 'absent'] $ensure    = present,
5     Optional[String] $content            = undef,
6     Optional[String] $source             = undef,
7     String $server                       = $::apt::keyserver,
8     Optional[String] $options            = undef,
9     ) {
10
11   validate_re($id, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z', '\A(0x)?[0-9a-fA-F]{40}\Z'])
12
13   if $source {
14     validate_re($source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
15   }
16
17   if $server {
18     validate_re($server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$'])
19   }
20
21   case $ensure {
22     present: {
23       if defined(Anchor["apt_key ${id} absent"]){
24         fail("key with id ${id} already ensured as absent")
25       }
26
27       if !defined(Anchor["apt_key ${id} present"]) {
28         apt_key { $title:
29           ensure  => $ensure,
30           id      => $id,
31           source  => $source,
32           content => $content,
33           server  => $server,
34           options => $options,
35         } -> anchor { "apt_key ${id} present": }
36
37         case $facts['os']['name'] {
38           'Debian': {
39             if versioncmp($facts['os']['release']['major'], '9') >= 0 {
40               ensure_packages(['dirmngr'])
41             }
42           }
43           'Ubuntu': {
44             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
45               ensure_packages(['dirmngr'])
46             }
47           }
48           default: { }
49         }
50       }
51     }
52
53     absent: {
54       if defined(Anchor["apt_key ${id} present"]){
55         fail("key with id ${id} already ensured as present")
56       }
57
58       if !defined(Anchor["apt_key ${id} absent"]){
59         apt_key { $title:
60           ensure  => $ensure,
61           id      => $id,
62           source  => $source,
63           content => $content,
64           server  => $server,
65           options => $options,
66         } -> anchor { "apt_key ${id} absent": }
67       }
68     }
69
70     default: {
71       fail "Invalid 'ensure' value '${ensure}' for apt::key"
72     }
73   }
74 }