"This change pins the puppetlabs-puppet_agent module to v4.12.1. Previosuly the fixut...
[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 = merge($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 = merge($apt::purge_defaults, $purge)
226
227   if $proxy['perhost'] {
228     $_perhost = $proxy['perhost'].map |$item| {
229       $_item = merge($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       merge($item, {
243           'scheme' => $_scheme,
244           'target' => $_target,
245         }
246       )
247     }
248   } else {
249     $_perhost = {}
250   }
251
252   $_proxy = merge($apt::proxy_defaults, $proxy, { 'perhost' => $_perhost })
253
254   $confheadertmp = epp('apt/_conf_header.epp')
255   $proxytmp = epp('apt/proxy.epp', { 'proxies' => $_proxy })
256   $updatestamptmp = epp('apt/15update-stamp.epp')
257
258   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
259     apt::setting { 'conf-proxy':
260       ensure   => $_proxy['ensure'],
261       priority => '01',
262       content  => "${confheadertmp}${proxytmp}",
263     }
264   }
265
266   if $sources_list_force {
267     $sources_list_ensure = $_purge['sources.list'] ? {
268       true    => absent,
269       default  => file,
270     }
271     $sources_list_content = $_purge['sources.list'] ? {
272       true    => nil,
273       default => undef,
274     }
275   } else {
276     $sources_list_ensure = $_purge['sources.list'] ? {
277       true    => file,
278       default => file,
279     }
280     $sources_list_content = $_purge['sources.list'] ? {
281       true    => "# Repos managed by puppet.\n",
282       default => undef,
283     }
284   }
285
286   $preferences_ensure = $_purge['preferences'] ? {
287     true    => absent,
288     default => file,
289   }
290
291   apt::setting { 'conf-update-stamp':
292     priority => 15,
293     content  => "${confheadertmp}${updatestamptmp}",
294   }
295
296   file { 'sources.list':
297     ensure  => $sources_list_ensure,
298     path    => $apt::sources_list,
299     owner   => root,
300     group   => root,
301     content => $sources_list_content,
302     notify  => Class['apt::update'],
303   }
304
305   file { 'sources.list.d':
306     ensure  => directory,
307     path    => $apt::sources_list_d,
308     owner   => root,
309     group   => root,
310     purge   => $_purge['sources.list.d'],
311     recurse => $_purge['sources.list.d'],
312     notify  => Class['apt::update'],
313   }
314
315   file { 'preferences':
316     ensure => $preferences_ensure,
317     path   => $apt::preferences,
318     owner  => root,
319     group  => root,
320     notify => Class['apt::update'],
321   }
322
323   file { 'preferences.d':
324     ensure  => directory,
325     path    => $apt::preferences_d,
326     owner   => root,
327     group   => root,
328     purge   => $_purge['preferences.d'],
329     recurse => $_purge['preferences.d'],
330     notify  => Class['apt::update'],
331   }
332
333   file { 'apt.conf.d':
334     ensure  => directory,
335     path    => $apt::apt_conf_d,
336     owner   => root,
337     group   => root,
338     purge   => $_purge['apt.conf.d'],
339     recurse => $_purge['apt.conf.d'],
340     notify  => Class['apt::update'],
341   }
342
343   if $confs {
344     create_resources('apt::conf', $confs)
345   }
346   # manage sources if present
347   if $sources {
348     create_resources('apt::source', $sources)
349   }
350   # manage keys if present
351   if $keys {
352     create_resources('apt::key', $keys)
353   }
354   # manage ppas if present
355   if $ppas {
356     create_resources('apt::ppa', $ppas)
357   }
358   # manage settings if present
359   if $settings {
360     create_resources('apt::setting', $settings)
361   }
362
363   if $manage_auth_conf {
364     $auth_conf_ensure = $auth_conf_entries ? {
365       []      => 'absent',
366       default => 'present',
367     }
368
369     $auth_conf_tmp = epp('apt/auth_conf.epp')
370
371     file { '/etc/apt/auth.conf':
372       ensure  => $auth_conf_ensure,
373       owner   => $auth_conf_owner,
374       group   => 'root',
375       mode    => '0600',
376       content => Sensitive("${confheadertmp}${auth_conf_tmp}"),
377       notify  => Class['apt::update'],
378     }
379   }
380
381   # manage pins if present
382   if $pins {
383     create_resources('apt::pin', $pins)
384   }
385
386   case $facts['os']['name'] {
387     'Debian': {
388       if versioncmp($facts['os']['release']['major'], '9') >= 0 {
389         ensure_packages(['gnupg'])
390       }
391     }
392     'Ubuntu': {
393       if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
394         ensure_packages(['gnupg'])
395       }
396     }
397     default: {
398       # Nothing in here
399     }
400   }
401 }