Release prep v9.1.0
[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 key_options
13 #   Specifies the default options for apt::key resources.
14 #
15 # @param ppa_options
16 #   Supplies options to be passed to the `add-apt-repository` command.
17 #
18 # @param ppa_package
19 #   Names the package that provides the `apt-add-repository` command.
20 #
21 # @param backports
22 #   Specifies some of the default parameters used by apt::backports. Valid options: a hash made up from the following keys:
23 #
24 # @option backports [String] :location
25 #   See apt::backports for documentation.
26 #
27 # @option backports [String] :repos
28 #   See apt::backports for documentation.
29 #
30 # @option backports [String] :key
31 #   See apt::backports for documentation.
32 #
33 # @param confs
34 #   Creates new `apt::conf` resources. Valid options: a hash to be passed to the create_resources function linked above.
35 #
36 # @param update
37 #   Configures various update settings. Valid options: a hash made up from the following keys:
38 #
39 # @option update [String] :frequency
40 #   Specifies how often to run `apt-get update`. If the exec resource `apt_update` is notified,
41 #   `apt-get update` runs regardless of this value.
42 #   Valid options:
43 #     'always' (at every Puppet run); 
44 #      daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
45 #     'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800);
46 #     'reluctantly' (only if the exec resource `apt_update` is notified).
47 #   Default: 'reluctantly'.
48 #
49 # @option update [Integer] :loglevel
50 #   Specifies the log level of logs outputted to the console. Default: undef.
51 #
52 # @option update [Integer] :timeout
53 #   Specifies how long to wait for the update to complete before canceling it. Valid options: an integer, in seconds. Default: undef.
54 #
55 # @option update [Integer] :tries
56 #    Specifies how many times to retry the update after receiving a DNS or HTTP error. Default: undef.
57 #
58 # @param update_defaults
59 #   The default update settings that are combined and merged with the passed `update` value
60 #
61 # @param purge
62 #   Specifies whether to purge any existing settings that aren't managed by Puppet. Valid options: a hash made up from the following keys:
63 #
64 # @option purge [Boolean] :sources.list
65 #   Specifies whether to purge any unmanaged entries from sources.list. Default false.
66 #
67 # @option purge [Boolean] :sources.list.d
68 #   Specifies whether to purge any unmanaged entries from sources.list.d. Default false.
69 #
70 # @option purge [Boolean] :preferences
71 #   Specifies whether to purge any unmanaged entries from preferences. Default false.
72 #
73 # @option purge [Boolean] :preferences.d.
74 #   Specifies whether to purge any unmanaged entries from preferences.d. Default false.
75 #
76 # @param purge_defaults
77 #   The default purge settings that are combined and merged with the passed `purge` value
78 #
79 # @param proxy
80 #   Configures Apt to connect to a proxy server. Valid options: a hash matching the locally defined type apt::proxy.
81 #
82 # @param proxy_defaults
83 #   The default proxy settings that are combined and merged with the passed `proxy` value
84 #
85 # @param sources
86 #   Creates new `apt::source` resources. Valid options: a hash to be passed to the create_resources function linked above.
87 #
88 # @param keys
89 #   Creates new `apt::key` resources. Valid options: a hash to be passed to the create_resources function linked above.
90 #
91 # @param ppas
92 #   Creates new `apt::ppa` resources. Valid options: a hash to be passed to the create_resources function linked above.
93 #
94 # @param pins
95 #   Creates new `apt::pin` resources. Valid options: a hash to be passed to the create_resources function linked above.
96 #
97 # @param settings
98 #   Creates new `apt::setting` resources. Valid options: a hash to be passed to the create_resources function linked above.
99 #
100 # @param manage_auth_conf
101 #   Specifies whether to manage the /etc/apt/auth.conf file. When true, the file will be overwritten with the entries specified in
102 #   the auth_conf_entries parameter. When false, the file will be ignored (note that this does not set the file to absent.
103 #
104 # @param auth_conf_entries
105 #   An optional array of login configuration settings (hashes) that are recorded in the file /etc/apt/auth.conf. This file has a netrc-like
106 #   format (similar to what curl uses) and contains the login configuration for APT sources and proxies that require authentication. See
107 #   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
108 #   password and no others. Specifying manage_auth_conf and not specifying this parameter will set /etc/apt/auth.conf to absent.
109 #
110 # @param auth_conf_owner
111 #   The owner of the file /etc/apt/auth.conf. Default: '_apt' or 'root' on old releases.
112 #
113 # @param root
114 #   Specifies root directory of Apt executable.
115 #
116 # @param sources_list
117 #   Specifies the path of the sources_list file to use.
118 #
119 # @param sources_list_d
120 #   Specifies the path of the sources_list.d file to use.
121 #
122 # @param conf_d
123 #   Specifies the path of the conf.d file to use.
124 #
125 # @param preferences
126 #   Specifies the path of the preferences file to use.
127 #
128 # @param preferences_d
129 #   Specifies the path of the preferences.d file to use.
130 #
131 # @param config_files
132 #   A hash made up of the various configuration files used by Apt.
133 #
134 # @param sources_list_force
135 #   Specifies whether to perform force purge or delete. Default false.
136 #
137 # @param include_defaults
138 #
139 # @param apt_conf_d
140 #   The path to the file `apt.conf.d`
141 #
142 # @param source_key_defaults
143 #   The fault `source_key` settings
144 #
145 class apt (
146   Hash $update_defaults                           = $apt::params::update_defaults,
147   Hash $purge_defaults                            = $apt::params::purge_defaults,
148   Hash $proxy_defaults                            = $apt::params::proxy_defaults,
149   Hash $include_defaults                          = $apt::params::include_defaults,
150   String $provider                                = $apt::params::provider,
151   String $keyserver                               = $apt::params::keyserver,
152   Optional[String] $key_options                   = $apt::params::key_options,
153   Optional[Array[String]] $ppa_options            = $apt::params::ppa_options,
154   Optional[String] $ppa_package                   = $apt::params::ppa_package,
155   Optional[Hash] $backports                       = $apt::params::backports,
156   Hash $confs                                     = $apt::params::confs,
157   Hash $update                                    = $apt::params::update,
158   Hash $purge                                     = $apt::params::purge,
159   Apt::Proxy $proxy                               = $apt::params::proxy,
160   Hash $sources                                   = $apt::params::sources,
161   Hash $keys                                      = $apt::params::keys,
162   Hash $ppas                                      = $apt::params::ppas,
163   Hash $pins                                      = $apt::params::pins,
164   Hash $settings                                  = $apt::params::settings,
165   Boolean $manage_auth_conf                       = $apt::params::manage_auth_conf,
166   Array[Apt::Auth_conf_entry] $auth_conf_entries  = $apt::params::auth_conf_entries,
167   String $auth_conf_owner                         = $apt::params::auth_conf_owner,
168   String $root                                    = $apt::params::root,
169   String $sources_list                            = $apt::params::sources_list,
170   String $sources_list_d                          = $apt::params::sources_list_d,
171   String $conf_d                                  = $apt::params::conf_d,
172   String $preferences                             = $apt::params::preferences,
173   String $preferences_d                           = $apt::params::preferences_d,
174   String $apt_conf_d                              = $apt::params::apt_conf_d,
175   Hash $config_files                              = $apt::params::config_files,
176   Boolean $sources_list_force                     = $apt::params::sources_list_force,
177
178   Hash $source_key_defaults = {
179     'server'  => $keyserver,
180     'options' => undef,
181     'content' => undef,
182     'source'  => undef,
183   }
184
185 ) inherits apt::params {
186   if $facts['os']['family'] != 'Debian' {
187     fail('This module only works on Debian or derivatives like Ubuntu')
188   }
189
190   if $update['frequency'] {
191     assert_type(
192       Enum['always','daily','weekly','reluctantly'],
193       $update['frequency'],
194     )
195   }
196   if $update['timeout'] {
197     assert_type(Integer, $update['timeout'])
198   }
199   if $update['tries'] {
200     assert_type(Integer, $update['tries'])
201   }
202
203   $_update = $apt::update_defaults + $update
204   include apt::update
205
206   if $purge['sources.list'] {
207     assert_type(Boolean, $purge['sources.list'])
208   }
209   if $purge['sources.list.d'] {
210     assert_type(Boolean, $purge['sources.list.d'])
211   }
212   if $purge['preferences'] {
213     assert_type(Boolean, $purge['preferences'])
214   }
215   if $purge['preferences.d'] {
216     assert_type(Boolean, $purge['preferences.d'])
217   }
218   if $sources_list_force {
219     assert_type(Boolean, $sources_list_force)
220   }
221   if $purge['apt.conf.d'] {
222     assert_type(Boolean, $purge['apt.conf.d'])
223   }
224
225   $_purge = $apt::purge_defaults + $purge
226
227   if $proxy['perhost'] {
228     $_perhost = $proxy['perhost'].map |$item| {
229       $_item = $apt::proxy_defaults + $item
230       $_scheme = $_item['https'] ? {
231         true    => 'https',
232         default => 'http',
233       }
234       $_port = $_item['port'] ? {
235         Integer => ":${_item['port']}",
236         default => ''
237       }
238       $_target = $_item['direct'] ? {
239         true    => 'DIRECT',
240         default => "${_scheme}://${_item['host']}${_port}/",
241       }
242       $item + { 'scheme' => $_scheme, 'target' => $_target, }
243     }
244   } else {
245     $_perhost = {}
246   }
247
248   $_proxy = $apt::proxy_defaults + $proxy + { 'perhost' => $_perhost }
249
250   $confheadertmp = epp('apt/_conf_header.epp')
251   $proxytmp = epp('apt/proxy.epp', { 'proxies' => $_proxy })
252   $updatestamptmp = epp('apt/15update-stamp.epp')
253
254   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
255     apt::setting { 'conf-proxy':
256       ensure   => $_proxy['ensure'],
257       priority => '01',
258       content  => "${confheadertmp}${proxytmp}",
259     }
260   }
261
262   if $sources_list_force {
263     $sources_list_ensure = $_purge['sources.list'] ? {
264       true    => absent,
265       default  => file,
266     }
267     $sources_list_content = $_purge['sources.list'] ? {
268       true    => nil,
269       default => undef,
270     }
271   } else {
272     $sources_list_ensure = $_purge['sources.list'] ? {
273       true    => file,
274       default => file,
275     }
276     $sources_list_content = $_purge['sources.list'] ? {
277       true    => "# Repos managed by puppet.\n",
278       default => undef,
279     }
280   }
281
282   $preferences_ensure = $_purge['preferences'] ? {
283     true    => absent,
284     default => file,
285   }
286
287   apt::setting { 'conf-update-stamp':
288     priority => 15,
289     content  => "${confheadertmp}${updatestamptmp}",
290   }
291
292   file { 'sources.list':
293     ensure  => $sources_list_ensure,
294     path    => $apt::sources_list,
295     owner   => root,
296     group   => root,
297     content => $sources_list_content,
298     notify  => Class['apt::update'],
299   }
300
301   file { 'sources.list.d':
302     ensure  => directory,
303     path    => $apt::sources_list_d,
304     owner   => root,
305     group   => root,
306     purge   => $_purge['sources.list.d'],
307     recurse => $_purge['sources.list.d'],
308     notify  => Class['apt::update'],
309   }
310
311   file { 'preferences':
312     ensure => $preferences_ensure,
313     path   => $apt::preferences,
314     owner  => root,
315     group  => root,
316     notify => Class['apt::update'],
317   }
318
319   file { 'preferences.d':
320     ensure  => directory,
321     path    => $apt::preferences_d,
322     owner   => root,
323     group   => root,
324     purge   => $_purge['preferences.d'],
325     recurse => $_purge['preferences.d'],
326     notify  => Class['apt::update'],
327   }
328
329   file { 'apt.conf.d':
330     ensure  => directory,
331     path    => $apt::apt_conf_d,
332     owner   => root,
333     group   => root,
334     purge   => $_purge['apt.conf.d'],
335     recurse => $_purge['apt.conf.d'],
336     notify  => Class['apt::update'],
337   }
338
339   if $confs {
340     create_resources('apt::conf', $confs)
341   }
342   # manage sources if present
343   if $sources {
344     create_resources('apt::source', $sources)
345   }
346   # manage keys if present
347   if $keys {
348     create_resources('apt::key', $keys)
349   }
350   # manage ppas if present
351   if $ppas {
352     create_resources('apt::ppa', $ppas)
353   }
354   # manage settings if present
355   if $settings {
356     create_resources('apt::setting', $settings)
357   }
358
359   if $manage_auth_conf {
360     $auth_conf_ensure = $auth_conf_entries ? {
361       []      => 'absent',
362       default => 'present',
363     }
364
365     $auth_conf_tmp = stdlib::deferrable_epp('apt/auth_conf.epp', { 'auth_conf_entries' => $auth_conf_entries })
366
367     file { '/etc/apt/auth.conf':
368       ensure  => $auth_conf_ensure,
369       owner   => $auth_conf_owner,
370       group   => 'root',
371       mode    => '0600',
372       content => Sensitive($auth_conf_tmp),
373       notify  => Class['apt::update'],
374     }
375   }
376
377   # manage pins if present
378   if $pins {
379     create_resources('apt::pin', $pins)
380   }
381
382   case $facts['os']['name'] {
383     'Debian': {
384       if versioncmp($facts['os']['release']['major'], '9') >= 0 {
385         stdlib::ensure_packages(['gnupg'])
386       }
387     }
388     'Ubuntu': {
389       if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
390         stdlib::ensure_packages(['gnupg'])
391       }
392     }
393     default: {
394       # Nothing in here
395     }
396   }
397 }