b23acc7890b3c9e963f861060bcbb935fd16a4f2
[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 manage_auth_conf
85 #   Specifies whether to manage the /etc/apt/auth.conf file. When true, the file will be overwritten with the entries specified in
86 #   the auth_conf_entries parameter. When false, the file will be ignored (note that this does not set the file to absent.
87 #
88 # @param auth_conf_entries
89 #   An optional array of login configuration settings (hashes) that are recorded in the file /etc/apt/auth.conf. This file has a netrc-like 
90 #   format (similar to what curl uses) and contains the login configuration for APT sources and proxies that require authentication. See 
91 #   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 
92 #   password and no others. Specifying manage_auth_conf and not specifying this parameter will set /etc/apt/auth.conf to absent.
93 #
94 # @param root
95 #   Specifies root directory of Apt executable.
96 #
97 # @param sources_list
98 #   Specifies the path of the sources_list file to use. 
99 #
100 # @param sources_list_d
101 #   Specifies the path of the sources_list.d file to use. 
102 #
103 # @param conf_d
104 #   Specifies the path of the conf.d file to use. 
105 #
106 # @param preferences
107 #   Specifies the path of the preferences file to use. 
108 #
109 # @param preferences_d
110 #   Specifies the path of the preferences.d file to use. 
111 #
112 # @param config_files
113 #   A hash made up of the various configuration files used by Apt.
114 #
115 class apt (
116   Hash $update_defaults         = $apt::params::update_defaults,
117   Hash $purge_defaults          = $apt::params::purge_defaults,
118   Hash $proxy_defaults          = $apt::params::proxy_defaults,
119   Hash $include_defaults        = $apt::params::include_defaults,
120   String $provider              = $apt::params::provider,
121   String $keyserver             = $apt::params::keyserver,
122   Optional[String] $ppa_options = $apt::params::ppa_options,
123   Optional[String] $ppa_package = $apt::params::ppa_package,
124   Optional[Hash] $backports     = $apt::params::backports,
125   Hash $confs                   = $apt::params::confs,
126   Hash $update                  = $apt::params::update,
127   Hash $purge                   = $apt::params::purge,
128   Apt::Proxy $proxy             = $apt::params::proxy,
129   Hash $sources                 = $apt::params::sources,
130   Hash $keys                    = $apt::params::keys,
131   Hash $ppas                    = $apt::params::ppas,
132   Hash $pins                    = $apt::params::pins,
133   Hash $settings                = $apt::params::settings,
134   Boolean $manage_auth_conf     = $apt::params::manage_auth_conf,
135   Array[Apt::Auth_conf_entry]
136     $auth_conf_entries          = $apt::params::auth_conf_entries,
137   String $root                  = $apt::params::root,
138   String $sources_list          = $apt::params::sources_list,
139   String $sources_list_d        = $apt::params::sources_list_d,
140   String $conf_d                = $apt::params::conf_d,
141   String $preferences           = $apt::params::preferences,
142   String $preferences_d         = $apt::params::preferences_d,
143   Hash $config_files            = $apt::params::config_files,
144   Hash $source_key_defaults     = $apt::params::source_key_defaults,
145 ) inherits apt::params {
146
147   if $facts['osfamily'] != 'Debian' {
148     fail(translate('This module only works on Debian or derivatives like Ubuntu'))
149   }
150
151   if $update['frequency'] {
152     assert_type(
153       Enum['always','daily','weekly','reluctantly'],
154       $update['frequency'],
155     )
156   }
157   if $update['timeout'] {
158     assert_type(Integer, $update['timeout'])
159   }
160   if $update['tries'] {
161     assert_type(Integer, $update['tries'])
162   }
163
164   $_update = merge($::apt::update_defaults, $update)
165   include ::apt::update
166
167   if $purge['sources.list'] {
168     assert_type(Boolean, $purge['sources.list'])
169   }
170   if $purge['sources.list.d'] {
171     assert_type(Boolean, $purge['sources.list.d'])
172   }
173   if $purge['preferences'] {
174     assert_type(Boolean, $purge['preferences'])
175   }
176   if $purge['preferences.d'] {
177     assert_type(Boolean, $purge['preferences.d'])
178   }
179
180   $_purge = merge($::apt::purge_defaults, $purge)
181   $_proxy = merge($apt::proxy_defaults, $proxy)
182
183   $confheadertmp = epp('apt/_conf_header.epp')
184   $proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})
185   $updatestamptmp = epp('apt/15update-stamp.epp')
186
187   if $_proxy['ensure'] == 'absent' or $_proxy['host'] {
188     apt::setting { 'conf-proxy':
189       ensure   => $_proxy['ensure'],
190       priority => '01',
191       content  => "${confheadertmp}${proxytmp}",
192     }
193   }
194
195   $sources_list_content = $_purge['sources.list'] ? {
196     true    => "# Repos managed by puppet.\n",
197     default => undef,
198   }
199
200   $preferences_ensure = $_purge['preferences'] ? {
201     true    => absent,
202     default => file,
203   }
204
205   if $_update['frequency'] == 'always' {
206     Exec <| title=='apt_update' |> {
207       refreshonly => false,
208     }
209   }
210
211   apt::setting { 'conf-update-stamp':
212     priority => 15,
213     content  => "${confheadertmp}${updatestamptmp}",
214   }
215
216   file { 'sources.list':
217     ensure  => file,
218     path    => $::apt::sources_list,
219     owner   => root,
220     group   => root,
221     mode    => '0644',
222     content => $sources_list_content,
223     notify  => Class['apt::update'],
224   }
225
226   file { 'sources.list.d':
227     ensure  => directory,
228     path    => $::apt::sources_list_d,
229     owner   => root,
230     group   => root,
231     mode    => '0644',
232     purge   => $_purge['sources.list.d'],
233     recurse => $_purge['sources.list.d'],
234     notify  => Class['apt::update'],
235   }
236
237   file { 'preferences':
238     ensure => $preferences_ensure,
239     path   => $::apt::preferences,
240     owner  => root,
241     group  => root,
242     mode   => '0644',
243     notify => Class['apt::update'],
244   }
245
246   file { 'preferences.d':
247     ensure  => directory,
248     path    => $::apt::preferences_d,
249     owner   => root,
250     group   => root,
251     mode    => '0644',
252     purge   => $_purge['preferences.d'],
253     recurse => $_purge['preferences.d'],
254     notify  => Class['apt::update'],
255   }
256
257   if $confs {
258     create_resources('apt::conf', $confs)
259   }
260   # manage sources if present
261   if $sources {
262     create_resources('apt::source', $sources)
263   }
264   # manage keys if present
265   if $keys {
266     create_resources('apt::key', $keys)
267   }
268   # manage ppas if present
269   if $ppas {
270     create_resources('apt::ppa', $ppas)
271   }
272   # manage settings if present
273   if $settings {
274     create_resources('apt::setting', $settings)
275   }
276
277   if $manage_auth_conf {
278     $auth_conf_ensure = $auth_conf_entries ? {
279       []      => 'absent',
280       default => 'present',
281     }
282
283     $auth_conf_tmp = epp('apt/auth_conf.epp')
284
285     file { '/etc/apt/auth.conf':
286       ensure  => $auth_conf_ensure,
287       owner   => 'root',
288       group   => 'root',
289       mode    => '0600',
290       content => "${confheadertmp}${auth_conf_tmp}",
291       notify  => Class['apt::update'],
292     }
293   }
294
295   # manage pins if present
296   if $pins {
297     create_resources('apt::pin', $pins)
298   }
299
300   # required for adding GPG keys on Debian 9 (and derivatives)
301   ensure_packages(['dirmngr'])
302 }