Merge pull request #795 from eimlav/modules3307
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
1 # @summary Main class, includes all other classes.
2 #
3 # @see https://docs.puppetlabs.com/references/latest/function.html#createresources for the create resource function
4 #
5 # @param provider
6 #   Specifies the provider that should be used by apt::update.
7 #
8 # @param keyserver
9 #   Specifies a keyserver to provide the GPG key. Valid options: a string containing a domain name or a full URL (http://, https://, or 
10 #   hkp://).
11 #
12 # @param ppa_options
13 #   Supplies options to be passed to the `add-apt-repository` command. 
14 #
15 # @param ppa_package
16 #   Names the package that provides the `apt-add-repository` command.
17 #
18 # @param backports
19 #   Specifies some of the default parameters used by apt::backports. Valid options: a hash made up from the following keys:
20 #
21 # @option backports [String] :location 
22 #   See apt::backports for documentation.
23 #
24 # @option backports [String] :repos 
25 #   See apt::backports for documentation.
26 #
27 # @option backports [String] :key 
28 #   See apt::backports for documentation.
29 #
30 # @param confs
31 #   Creates new `apt::conf` resources. Valid options: a hash to be passed to the create_resources function linked above.
32 #
33 # @param update
34 #   Configures various update settings. Valid options: a hash made up from the following keys:
35 #
36 # @option update [String] :frequency
37 #   Specifies how often to run `apt-get update`. If the exec resource `apt_update` is notified, `apt-get update` runs regardless of this value. 
38 #   Valid options: 'always' (at every Puppet run); 'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400); 
39 #   'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800); and 'reluctantly' (only if the exec resource 
40 #   `apt_update` is notified). Default: 'reluctantly'.
41 #
42 # @option update [Integer] :loglevel
43 #   Specifies the log level of logs outputted to the console. Default: undef.
44 #
45 # @option update [Integer] :timeout
46 #   Specifies how long to wait for the update to complete before canceling it. Valid options: an integer, in seconds. Default: undef.
47 #
48 # @option update [Integer] :tries
49 #    Specifies how many times to retry the update after receiving a DNS or HTTP error. Default: undef.
50 #
51 # @param purge
52 #   Specifies whether to purge any existing settings that aren't managed by Puppet. Valid options: a hash made up from the following keys:
53 #
54 # @option purge [Boolean] :sources.list
55 #   Specifies whether to purge any unmanaged entries from sources.list. Default false.
56 #
57 # @option purge [Boolean] :sources.list.d
58 #   Specifies whether to purge any unmanaged entries from sources.list.d. Default false.
59 #
60 # @option purge [Boolean] :preferences
61 #   Specifies whether to purge any unmanaged entries from preferences. Default false.
62 #
63 # @option purge [Boolean] :preferences.d.
64 #   Specifies whether to purge any unmanaged entries from preferences.d. Default false.
65 #
66 # @param proxy
67 #   Configures Apt to connect to a proxy server. Valid options: a hash matching the locally defined type apt::proxy.
68 #
69 # @param sources
70 #   Creates new `apt::source` resources. Valid options: a hash to be passed to the create_resources function linked above.
71 #
72 # @param keys
73 #   Creates new `apt::key` resources. Valid options: a hash to be passed to the create_resources function linked above.
74 #
75 # @param ppas
76 #   Creates new `apt::ppa` resources. Valid options: a hash to be passed to the create_resources function linked above.
77 #
78 # @param pins
79 #   Creates new `apt::pin` resources. Valid options: a hash to be passed to the create_resources function linked above.
80 #
81 # @param settings
82 #   Creates new `apt::setting` resources. Valid options: a hash to be passed to the create_resources function linked above.
83 #
84 # @param auth_conf_entries
85 #   An optional array of login configuration settings (hashes) that are recorded in the file /etc/apt/auth.conf. This file has a netrc-like 
86 #   format (similar to what curl uses) and contains the login configuration for APT sources and proxies that require authentication. See 
87 #   https://manpages.debian.org/testing/apt/apt_auth.conf.5.en.html for details. If specified each hash must contain the keys machine, login and 
88 #   password and no others.
89 #
90 # @param root
91 #   Specifies root directory of Apt executable.
92 #
93 # @param sources_list
94 #   Specifies the path of the sources_list file to use. 
95 #
96 # @param sources_list_d
97 #   Specifies the path of the sources_list.d file to use. 
98 #
99 # @param conf_d
100 #   Specifies the path of the conf.d file to use. 
101 #
102 # @param preferences
103 #   Specifies the path of the preferences file to use. 
104 #
105 # @param preferences_d
106 #   Specifies the path of the preferences.d file to use. 
107 #
108 # @param config_files
109 #   A hash made up of the various configuration files used by Apt.
110 #
111 class apt (
112   Hash $update_defaults         = $apt::params::update_defaults,
113   Hash $purge_defaults          = $apt::params::purge_defaults,
114   Hash $proxy_defaults          = $apt::params::proxy_defaults,
115   Hash $include_defaults        = $apt::params::include_defaults,
116   String $provider              = $apt::params::provider,
117   String $keyserver             = $apt::params::keyserver,
118   Optional[String] $ppa_options = $apt::params::ppa_options,
119   Optional[String] $ppa_package = $apt::params::ppa_package,
120   Optional[Hash] $backports     = $apt::params::backports,
121   Hash $confs                   = $apt::params::confs,
122   Hash $update                  = $apt::params::update,
123   Hash $purge                   = $apt::params::purge,
124   Apt::Proxy $proxy             = $apt::params::proxy,
125   Hash $sources                 = $apt::params::sources,
126   Hash $keys                    = $apt::params::keys,
127   Hash $ppas                    = $apt::params::ppas,
128   Hash $pins                    = $apt::params::pins,
129   Hash $settings                = $apt::params::settings,
130   Array[Apt::Auth_conf_entry]
131     $auth_conf_entries          = $apt::params::auth_conf_entries,
132   String $root                  = $apt::params::root,
133   String $sources_list          = $apt::params::sources_list,
134   String $sources_list_d        = $apt::params::sources_list_d,
135   String $conf_d                = $apt::params::conf_d,
136   String $preferences           = $apt::params::preferences,
137   String $preferences_d         = $apt::params::preferences_d,
138   Hash $config_files            = $apt::params::config_files,
139   Hash $source_key_defaults     = $apt::params::source_key_defaults,
140 ) inherits apt::params {
141
142   if $facts['osfamily'] != 'Debian' {
143     fail(translate('This module only works on Debian or derivatives like Ubuntu'))
144   }
145
146   if $update['frequency'] {
147     assert_type(
148       Enum['always','daily','weekly','reluctantly'],
149       $update['frequency'],
150     )
151   }
152   if $update['timeout'] {
153     assert_type(Integer, $update['timeout'])
154   }
155   if $update['tries'] {
156     assert_type(Integer, $update['tries'])
157   }
158
159   $_update = merge($::apt::update_defaults, $update)
160   include ::apt::update
161
162   if $purge['sources.list'] {
163     assert_type(Boolean, $purge['sources.list'])
164   }
165   if $purge['sources.list.d'] {
166     assert_type(Boolean, $purge['sources.list.d'])
167   }
168   if $purge['preferences'] {
169     assert_type(Boolean, $purge['preferences'])
170   }
171   if $purge['preferences.d'] {
172     assert_type(Boolean, $purge['preferences.d'])
173   }
174
175   $_purge = merge($::apt::purge_defaults, $purge)
176   $_proxy = merge($apt::proxy_defaults, $proxy)
177
178   $confheadertmp = epp('apt/_conf_header.epp')
179   $proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})
180   $updatestamptmp = epp('apt/15update-stamp.epp')
181
182   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
183     apt::setting { 'conf-proxy':
184       ensure   => $_proxy['ensure'],
185       priority => '01',
186       content  => "${confheadertmp}${proxytmp}",
187     }
188   }
189
190   $sources_list_content = $_purge['sources.list'] ? {
191     true    => "# Repos managed by puppet.\n",
192     default => undef,
193   }
194
195   $preferences_ensure = $_purge['preferences'] ? {
196     true    => absent,
197     default => file,
198   }
199
200   if $_update['frequency'] == 'always' {
201     Exec <| title=='apt_update' |> {
202       refreshonly => false,
203     }
204   }
205
206   apt::setting { 'conf-update-stamp':
207     priority => 15,
208     content  => "${confheadertmp}${updatestamptmp}",
209   }
210
211   file { 'sources.list':
212     ensure  => file,
213     path    => $::apt::sources_list,
214     owner   => root,
215     group   => root,
216     mode    => '0644',
217     content => $sources_list_content,
218     notify  => Class['apt::update'],
219   }
220
221   file { 'sources.list.d':
222     ensure  => directory,
223     path    => $::apt::sources_list_d,
224     owner   => root,
225     group   => root,
226     mode    => '0644',
227     purge   => $_purge['sources.list.d'],
228     recurse => $_purge['sources.list.d'],
229     notify  => Class['apt::update'],
230   }
231
232   file { 'preferences':
233     ensure => $preferences_ensure,
234     path   => $::apt::preferences,
235     owner  => root,
236     group  => root,
237     mode   => '0644',
238     notify => Class['apt::update'],
239   }
240
241   file { 'preferences.d':
242     ensure  => directory,
243     path    => $::apt::preferences_d,
244     owner   => root,
245     group   => root,
246     mode    => '0644',
247     purge   => $_purge['preferences.d'],
248     recurse => $_purge['preferences.d'],
249     notify  => Class['apt::update'],
250   }
251
252   if $confs {
253     create_resources('apt::conf', $confs)
254   }
255   # manage sources if present
256   if $sources {
257     create_resources('apt::source', $sources)
258   }
259   # manage keys if present
260   if $keys {
261     create_resources('apt::key', $keys)
262   }
263   # manage ppas if present
264   if $ppas {
265     create_resources('apt::ppa', $ppas)
266   }
267   # manage settings if present
268   if $settings {
269     create_resources('apt::setting', $settings)
270   }
271
272   $auth_conf_ensure = $auth_conf_entries ? {
273     []      => 'absent',
274     default => 'present',
275   }
276
277   $auth_conf_tmp = epp('apt/auth_conf.epp')
278
279   file { '/etc/apt/auth.conf':
280     ensure  => $auth_conf_ensure,
281     owner   => 'root',
282     group   => 'root',
283     mode    => '0600',
284     content => "${confheadertmp}${auth_conf_tmp}",
285     notify  => Class['apt::update'],
286   }
287
288   # manage pins if present
289   if $pins {
290     create_resources('apt::pin', $pins)
291   }
292
293   # required for adding GPG keys on Debian 9 (and derivatives)
294   ensure_packages(['dirmngr'])
295 }