autorequire dirmngr package
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
1 # == Class: apt
2 #
3 # Manage APT (Advanced Packaging Tool)
4 #
5 class apt (
6   Hash $update_defaults         = $apt::params::update_defaults,
7   Hash $purge_defaults          = $apt::params::purge_defaults,
8   Hash $proxy_defaults          = $apt::params::proxy_defaults,
9   Hash $include_defaults        = $apt::params::include_defaults,
10   String $provider              = $apt::params::provider,
11   String $keyserver             = $apt::params::keyserver,
12   Optional[String] $ppa_options = $apt::params::ppa_options,
13   Optional[String] $ppa_package = $apt::params::ppa_package,
14   Optional[Hash] $backports     = $apt::params::backports,
15   Hash $confs                   = $apt::params::confs,
16   Hash $update                  = $apt::params::update,
17   Hash $purge                   = $apt::params::purge,
18   Hash $proxy                   = $apt::params::proxy,
19   Hash $sources                 = $apt::params::sources,
20   Hash $keys                    = $apt::params::keys,
21   Hash $ppas                    = $apt::params::ppas,
22   Hash $pins                    = $apt::params::pins,
23   Hash $settings                = $apt::params::settings,
24   String $root                  = $apt::params::root,
25   String $sources_list          = $apt::params::sources_list,
26   String $sources_list_d        = $apt::params::sources_list_d,
27   String $conf_d                = $apt::params::conf_d,
28   String $preferences           = $apt::params::preferences,
29   String $preferences_d         = $apt::params::preferences_d,
30   Hash $config_files            = $apt::params::config_files,
31   Hash $source_key_defaults     = $apt::params::source_key_defaults
32 ) inherits apt::params {
33
34   if $facts['osfamily'] != 'Debian' {
35     fail('This module only works on Debian or derivatives like Ubuntu')
36   }
37
38   $frequency_options = ['always','daily','weekly','reluctantly']
39
40   if $update['frequency'] {
41     validate_re($update['frequency'], $frequency_options)
42   }
43   if $update['timeout'] {
44     assert_type(Integer, $update['timeout'])
45   }
46   if $update['tries'] {
47     assert_type(Integer, $update['tries'])
48   }
49
50   $_update = merge($::apt::update_defaults, $update)
51   include ::apt::update
52
53   if $purge['sources.list'] {
54     assert_type(Boolean, $purge['sources.list'])
55   }
56   if $purge['sources.list.d'] {
57     assert_type(Boolean, $purge['sources.list.d'])
58   }
59   if $purge['preferences'] {
60     assert_type(Boolean, $purge['preferences'])
61   }
62   if $purge['preferences.d'] {
63     assert_type(Boolean, $purge['preferences.d'])
64   }
65
66   $_purge = merge($::apt::purge_defaults, $purge)
67
68   if $proxy['ensure'] {
69     validate_re($proxy['ensure'], ['file', 'present', 'absent'])
70   }
71   if $proxy['host'] {
72     assert_type(String, $proxy['host'])
73   }
74   if $proxy['port'] {
75     assert_type(Integer, $proxy['port'])
76   }
77   if $proxy['https']{
78     assert_type(Boolean, $proxy['https'])
79   }
80
81   $_proxy = merge($apt::proxy_defaults, $proxy)
82
83   $confheadertmp = epp('apt/_conf_header.epp')
84   $proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})
85   $updatestamptmp = epp('apt/15update-stamp.epp')
86
87   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
88     apt::setting { 'conf-proxy':
89       ensure   => $_proxy['ensure'],
90       priority => '01',
91       content  => "${confheadertmp}${proxytmp}",
92     }
93   }
94
95   $sources_list_content = $_purge['sources.list'] ? {
96     true    => "# Repos managed by puppet.\n",
97     default => undef,
98   }
99
100   $preferences_ensure = $_purge['preferences'] ? {
101     true    => absent,
102     default => file,
103   }
104
105   if $_update['frequency'] == 'always' {
106     Exec <| title=='apt_update' |> {
107       refreshonly => false,
108     }
109   }
110
111   apt::setting { 'conf-update-stamp':
112     priority => 15,
113     content  => "${confheadertmp}${updatestamptmp}",
114   }
115
116   file { 'sources.list':
117     ensure  => file,
118     path    => $::apt::sources_list,
119     owner   => root,
120     group   => root,
121     mode    => '0644',
122     content => $sources_list_content,
123     notify  => Class['apt::update'],
124   }
125
126   file { 'sources.list.d':
127     ensure  => directory,
128     path    => $::apt::sources_list_d,
129     owner   => root,
130     group   => root,
131     mode    => '0644',
132     purge   => $_purge['sources.list.d'],
133     recurse => $_purge['sources.list.d'],
134     notify  => Class['apt::update'],
135   }
136
137   file { 'preferences':
138     ensure => $preferences_ensure,
139     path   => $::apt::preferences,
140     owner  => root,
141     group  => root,
142     mode   => '0644',
143     notify => Class['apt::update'],
144   }
145
146   file { 'preferences.d':
147     ensure  => directory,
148     path    => $::apt::preferences_d,
149     owner   => root,
150     group   => root,
151     mode    => '0644',
152     purge   => $_purge['preferences.d'],
153     recurse => $_purge['preferences.d'],
154     notify  => Class['apt::update'],
155   }
156
157   if $confs {
158     create_resources('apt::conf', $confs)
159   }
160   # manage sources if present
161   if $sources {
162     create_resources('apt::source', $sources)
163   }
164   # manage keys if present
165   if $keys {
166     create_resources('apt::key', $keys)
167   }
168   # manage ppas if present
169   if $ppas {
170     create_resources('apt::ppa', $ppas)
171   }
172   # manage settings if present
173   if $settings {
174     create_resources('apt::setting', $settings)
175   }
176
177   # manage pins if present
178   if $pins {
179     create_resources('apt::pin', $pins)
180   }
181
182   # required for adding GPG keys on Debian 9 (and derivatives)
183   case $facts['os']['name'] {
184     'Debian': {
185       if versioncmp($facts['os']['release']['full'], '9.0') >= 0 {
186         ensure_packages(['dirmngr'])
187       }
188     }
189     'Ubuntu': {
190       if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
191         ensure_packages(['dirmngr'])
192       }
193     }
194     default: { }
195   }
196 }