Merge pull request #795 from eimlav/modules3307
[puppet-modules/puppetlabs-apt.git] / manifests / init.pp
index 40a40aff07650e457eabd3d56d1960f33176607a..f3472aca0097e6ce45505ca34587caf1327736ce 100644 (file)
-# == Class: apt
+# @summary Main class, includes all other classes.
 #
-# Manage APT (Advanced Packaging Tool)
+# @see https://docs.puppetlabs.com/references/latest/function.html#createresources for the create resource function
+#
+# @param provider
+#   Specifies the provider that should be used by apt::update.
+#
+# @param keyserver
+#   Specifies a keyserver to provide the GPG key. Valid options: a string containing a domain name or a full URL (http://, https://, or 
+#   hkp://).
+#
+# @param ppa_options
+#   Supplies options to be passed to the `add-apt-repository` command. 
+#
+# @param ppa_package
+#   Names the package that provides the `apt-add-repository` command.
+#
+# @param backports
+#   Specifies some of the default parameters used by apt::backports. Valid options: a hash made up from the following keys:
+#
+# @option backports [String] :location 
+#   See apt::backports for documentation.
+#
+# @option backports [String] :repos 
+#   See apt::backports for documentation.
+#
+# @option backports [String] :key 
+#   See apt::backports for documentation.
+#
+# @param confs
+#   Creates new `apt::conf` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param update
+#   Configures various update settings. Valid options: a hash made up from the following keys:
+#
+# @option update [String] :frequency
+#   Specifies how often to run `apt-get update`. If the exec resource `apt_update` is notified, `apt-get update` runs regardless of this value. 
+#   Valid options: 'always' (at every Puppet run); 'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400); 
+#   'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800); and 'reluctantly' (only if the exec resource 
+#   `apt_update` is notified). Default: 'reluctantly'.
+#
+# @option update [Integer] :loglevel
+#   Specifies the log level of logs outputted to the console. Default: undef.
+#
+# @option update [Integer] :timeout
+#   Specifies how long to wait for the update to complete before canceling it. Valid options: an integer, in seconds. Default: undef.
+#
+# @option update [Integer] :tries
+#    Specifies how many times to retry the update after receiving a DNS or HTTP error. Default: undef.
+#
+# @param purge
+#   Specifies whether to purge any existing settings that aren't managed by Puppet. Valid options: a hash made up from the following keys:
+#
+# @option purge [Boolean] :sources.list
+#   Specifies whether to purge any unmanaged entries from sources.list. Default false.
+#
+# @option purge [Boolean] :sources.list.d
+#   Specifies whether to purge any unmanaged entries from sources.list.d. Default false.
+#
+# @option purge [Boolean] :preferences
+#   Specifies whether to purge any unmanaged entries from preferences. Default false.
+#
+# @option purge [Boolean] :preferences.d.
+#   Specifies whether to purge any unmanaged entries from preferences.d. Default false.
+#
+# @param proxy
+#   Configures Apt to connect to a proxy server. Valid options: a hash matching the locally defined type apt::proxy.
+#
+# @param sources
+#   Creates new `apt::source` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param keys
+#   Creates new `apt::key` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param ppas
+#   Creates new `apt::ppa` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param pins
+#   Creates new `apt::pin` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param settings
+#   Creates new `apt::setting` resources. Valid options: a hash to be passed to the create_resources function linked above.
+#
+# @param auth_conf_entries
+#   An optional array of login configuration settings (hashes) that are recorded in the file /etc/apt/auth.conf. This file has a netrc-like 
+#   format (similar to what curl uses) and contains the login configuration for APT sources and proxies that require authentication. See 
+#   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 
+#   password and no others.
+#
+# @param root
+#   Specifies root directory of Apt executable.
+#
+# @param sources_list
+#   Specifies the path of the sources_list file to use. 
+#
+# @param sources_list_d
+#   Specifies the path of the sources_list.d file to use. 
+#
+# @param conf_d
+#   Specifies the path of the conf.d file to use. 
+#
+# @param preferences
+#   Specifies the path of the preferences file to use. 
+#
+# @param preferences_d
+#   Specifies the path of the preferences.d file to use. 
+#
+# @param config_files
+#   A hash made up of the various configuration files used by Apt.
 #
 class apt (
-  Variant[Hash, Stdlib::Compat::Hash] $update_defaults,
-  Variant[Hash, Stdlib::Compat::Hash] $purge_defaults,
-  Variant[Hash, Stdlib::Compat::Hash] $proxy_defaults,
-  Variant[Hash, Stdlib::Compat::Hash] $include_defaults,
-  Variant[String, Stdlib::Compat::String] $provider,
-  Variant[String, Stdlib::Compat::String] $keyserver,
-  Optional[Variant[String, Stdlib::Compat::String]] $ppa_options,
-  Optional[Variant[String, Stdlib::Compat::String]] $ppa_package,
-  Optional[Variant[Hash, Stdlib::Compat::Hash]] $backports,
-  Variant[Hash, Stdlib::Compat::Hash] $confs                = {},
-  Variant[Hash, Stdlib::Compat::Hash] $update               = {},
-  Variant[Hash, Stdlib::Compat::Hash] $purge                = {},
-  Variant[Hash, Stdlib::Compat::Hash] $proxy                = {},
-  Variant[Hash, Stdlib::Compat::Hash] $sources              = {},
-  Variant[Hash, Stdlib::Compat::Hash] $keys                 = {},
-  Variant[Hash, Stdlib::Compat::Hash] $ppas                 = {},
-  Variant[Hash, Stdlib::Compat::Hash] $pins                 = {},
-  Variant[Hash, Stdlib::Compat::Hash] $settings             = {},
-  Variant[String, Stdlib::Compat::String] $root             = '/etc/apt',
-  Variant[String, Stdlib::Compat::String] $sources_list     = "${root}/sources.list",
-  Variant[String, Stdlib::Compat::String] $sources_list_d   = "${root}/sources.list.d",
-  Variant[String, Stdlib::Compat::String] $conf_d           = "${root}/apt.conf.d",
-  Variant[String, Stdlib::Compat::String] $preferences      = "${root}/preferences",
-  Variant[String, Stdlib::Compat::String] $preferences_d    = "${root}/preferences.d",
-  Variant[Hash, Stdlib::Compat::Hash] $config_files         = { conf => { path => $conf_d, ext => '' }, pref => { path => $preferences_d, ext => '.pref' }, list => { path => $sources_list_d, ext => '.list' } },
-  Variant[Hash, Stdlib::Compat::Hash] $source_key_defaults  = { 'server' => $keyserver, 'options' => undef, 'content' => undef, 'source' => undef },
-) {
+  Hash $update_defaults         = $apt::params::update_defaults,
+  Hash $purge_defaults          = $apt::params::purge_defaults,
+  Hash $proxy_defaults          = $apt::params::proxy_defaults,
+  Hash $include_defaults        = $apt::params::include_defaults,
+  String $provider              = $apt::params::provider,
+  String $keyserver             = $apt::params::keyserver,
+  Optional[String] $ppa_options = $apt::params::ppa_options,
+  Optional[String] $ppa_package = $apt::params::ppa_package,
+  Optional[Hash] $backports     = $apt::params::backports,
+  Hash $confs                   = $apt::params::confs,
+  Hash $update                  = $apt::params::update,
+  Hash $purge                   = $apt::params::purge,
+  Apt::Proxy $proxy             = $apt::params::proxy,
+  Hash $sources                 = $apt::params::sources,
+  Hash $keys                    = $apt::params::keys,
+  Hash $ppas                    = $apt::params::ppas,
+  Hash $pins                    = $apt::params::pins,
+  Hash $settings                = $apt::params::settings,
+  Array[Apt::Auth_conf_entry]
+    $auth_conf_entries          = $apt::params::auth_conf_entries,
+  String $root                  = $apt::params::root,
+  String $sources_list          = $apt::params::sources_list,
+  String $sources_list_d        = $apt::params::sources_list_d,
+  String $conf_d                = $apt::params::conf_d,
+  String $preferences           = $apt::params::preferences,
+  String $preferences_d         = $apt::params::preferences_d,
+  Hash $config_files            = $apt::params::config_files,
+  Hash $source_key_defaults     = $apt::params::source_key_defaults,
+) inherits apt::params {
 
   if $facts['osfamily'] != 'Debian' {
-    fail('This module only works on Debian or derivatives like Ubuntu')
+    fail(translate('This module only works on Debian or derivatives like Ubuntu'))
   }
 
-  $frequency_options = ['always','daily','weekly','reluctantly']
-  validate_legacy(Hash, 'validate_hash', $update)
   if $update['frequency'] {
-    validate_re($update['frequency'], $frequency_options)
+    assert_type(
+      Enum['always','daily','weekly','reluctantly'],
+      $update['frequency'],
+    )
   }
   if $update['timeout'] {
-    unless is_integer($update['timeout']) {
-      fail('timeout value for update must be an integer')
-    }
+    assert_type(Integer, $update['timeout'])
   }
   if $update['tries'] {
-    unless is_integer($update['tries']) {
-      fail('tries value for update must be an integer')
-    }
+    assert_type(Integer, $update['tries'])
   }
 
   $_update = merge($::apt::update_defaults, $update)
   include ::apt::update
 
-  validate_legacy(Hash, 'validate_hash', $purge)
   if $purge['sources.list'] {
-    validate_legacy(Boolean, 'validate_bool', $purge['sources.list'])
+    assert_type(Boolean, $purge['sources.list'])
   }
   if $purge['sources.list.d'] {
-    validate_legacy(Boolean, 'validate_bool', $purge['sources.list.d'])
+    assert_type(Boolean, $purge['sources.list.d'])
   }
   if $purge['preferences'] {
-    validate_legacy(Boolean, 'validate_bool', $purge['preferences'])
+    assert_type(Boolean, $purge['preferences'])
   }
   if $purge['preferences.d'] {
-    validate_legacy(Boolean, 'validate_bool', $purge['preferences.d'])
+    assert_type(Boolean, $purge['preferences.d'])
   }
 
   $_purge = merge($::apt::purge_defaults, $purge)
-
-  validate_hash($proxy)
-  if $proxy['ensure'] {
-    validate_re($proxy['ensure'], ['file', 'present', 'absent'])
-  }
-  if $proxy['host'] {
-    validate_legacy(String, 'validate_string', $proxy['host'])
-  }
-  if $proxy['port'] {
-    unless is_integer($proxy['port']) {
-      fail('$proxy port must be an integer')
-    }
-  }
-  if $proxy['https'] {
-    validate_legacy(Boolean, 'validate_bool', $proxy['https'])
-  }
-
   $_proxy = merge($apt::proxy_defaults, $proxy)
 
-  validate_legacy(Hash, 'validate_hash', $confs)
-  validate_legacy(Hash, 'validate_hash', $sources)
-  validate_legacy(Hash, 'validate_hash', $keys)
-  validate_legacy(Hash, 'validate_hash', $settings)
-  validate_legacy(Hash, 'validate_hash', $ppas)
-  validate_legacy(Hash, 'validate_hash', $pins)
-
   $confheadertmp = epp('apt/_conf_header.epp')
   $proxytmp = epp('apt/proxy.epp', {'proxies' => $_proxy})
   $updatestamptmp = epp('apt/15update-stamp.epp')
@@ -189,8 +269,27 @@ class apt (
     create_resources('apt::setting', $settings)
   }
 
+  $auth_conf_ensure = $auth_conf_entries ? {
+    []      => 'absent',
+    default => 'present',
+  }
+
+  $auth_conf_tmp = epp('apt/auth_conf.epp')
+
+  file { '/etc/apt/auth.conf':
+    ensure  => $auth_conf_ensure,
+    owner   => 'root',
+    group   => 'root',
+    mode    => '0600',
+    content => "${confheadertmp}${auth_conf_tmp}",
+    notify  => Class['apt::update'],
+  }
+
   # manage pins if present
   if $pins {
     create_resources('apt::pin', $pins)
   }
+
+  # required for adding GPG keys on Debian 9 (and derivatives)
+  ensure_packages(['dirmngr'])
 }