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