# == Class: openstack_extras::repo::redhat::redhat
#
# This repo sets up yum repos for use with the redhat
# osfamily and redhat operatingsystem.
#
# === Parameters:
#
# [*release*]
#   (optional) The openstack release to use if managing rdo
#   Defaults to $::openstack_extras::repo::redhat::params::release
#
# [*manage_rdo*]
#   (optional) Whether to create a predefined yumrepo resource
#   for the RDO OpenStack repository provided by RedHat
#   Defaults to true
#
# [*manage_virt*]
#   (optional) Whether to create a predefined yumrepo resource
#   for the RDO CentOS QEMU EV epository provided by RedHat.
#   This repository has been required starting from Newton.
#   Defaults to true
#
# [*manage_epel*]
#   (optional) Whether to create a predefined yumrepo resource
#   for the EPEL repository provided by RedHat
#   Note: EPEL is not required when deploying OpenStack with RDO.
#   Defaults to false
#
# [*repo_hash*]
#   (optional) A hash of yumrepo resources that will be passed to
#   create_resource. See examples folder for some useful examples.
#   Defaults to {}
#
# [*repo_source_hash*]
#   (optional) A hash of repo files
#   Defaults to {}
#
# [*repo_replace*]
#   (optional) Replace repo files when their contents are changed
#   Defaults to true
#
# [*repo_defaults*]
#   (optional) The defaults for the yumrepo resources that will be
#   created using create_resource.
#   Defaults to $::openstack_extras::repo::redhat::params::repo_defaults
#
# [*gpgkey_hash*]
#   (optional) A hash of file resources that will be passed to
#   create_resource. See examples folder for some useful examples.
#   Defaults to {}
#
# [*gpgkey_defaults*]
#   (optional) The default resource attributes to
#   create gpgkeys with.
#   Defaults to $::openstack_extras::repo::redhat::params::gpgkey_defaults
#
# [*purge_unmanaged*]
#   (optional) Purge the yum.repos.d directory of
#   all repositories not managed by Puppet
#   Defaults to false
#
# [*package_require*]
#   (optional) Set all packages to require all
#   yumrepos be set.
#   Defaults to false
#
# [*manage_priorities*]
#   (optional) Whether to install yum-plugin-priorities package so
#   'priority' value in yumrepo will be effective.
#   Defaults to true
#
# [*centos_mirror_url*]
#   (optional) URL of CentOS mirror.
#   Defaults to 'http://mirror.centos.org'
#
# [*update_packages*]
#   (optional) Whether to update all packages after yum repositories are
#   configured.
#   Defaults to false
#
class openstack_extras::repo::redhat::redhat(
  $release           = $::openstack_extras::repo::redhat::params::release,
  $manage_rdo        = true,
  $manage_virt       = true,
  $manage_epel       = false,
  $repo_hash         = {},
  $repo_source_hash  = {},
  $repo_replace      = true,
  $repo_defaults     = {},
  $gpgkey_hash       = {},
  $gpgkey_defaults   = {},
  $purge_unmanaged   = false,
  $package_require   = false,
  $manage_priorities = true,
  $centos_mirror_url = 'http://mirror.centos.org',
  $update_packages   = false,
) inherits openstack_extras::repo::redhat::params {

  validate_legacy(String, 'validate_string', $release)
  validate_legacy(Boolean, 'validate_bool', $manage_rdo)
  validate_legacy(Boolean, 'validate_bool', $manage_epel)
  validate_legacy(Hash, 'validate_hash', $repo_hash)
  validate_legacy(Hash, 'validate_hash', $repo_source_hash)
  validate_legacy(Hash, 'validate_hash', $repo_defaults)
  validate_legacy(Hash, 'validate_hash', $gpgkey_hash)
  validate_legacy(Hash, 'validate_hash', $gpgkey_defaults)
  validate_legacy(Boolean, 'validate_bool', $purge_unmanaged)
  validate_legacy(Boolean, 'validate_bool', $package_require)

  $_repo_defaults = merge($::openstack_extras::repo::redhat::params::repo_defaults, $repo_defaults)
  $_gpgkey_defaults = merge($::openstack_extras::repo::redhat::params::gpgkey_defaults, $gpgkey_defaults)

  $os_major = $::os['release']['major']

  anchor { 'openstack_extras_redhat': }

  if $manage_rdo {
    $release_cap = capitalize($release)

    $rdo_hash = {
      'rdo-release' => {
        'baseurl'  => "${centos_mirror_url}/centos/${::os['release']['major']}/cloud/\$basearch/openstack-${release}/",
        'descr'    => "OpenStack ${release_cap} Repository",
        'gpgkey'   => 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Cloud',
      }
    }

    $rdokey_hash = { '/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Cloud' => {
        'source' => 'puppet:///modules/openstack_extras/RPM-GPG-KEY-CentOS-SIG-Cloud'
      }
    }

    create_resources('file', $rdokey_hash, $_gpgkey_defaults)
    create_resources('yumrepo', $rdo_hash, $_repo_defaults)
  }

  if $manage_virt and ($::operatingsystem != 'Fedora') {

    if Integer.new($os_major) >= 8 {
      $virt_baseurl = "${centos_mirror_url}/centos/${os_major}/virt/\$basearch/advanced-virtualization/"
    } else {
      $virt_baseurl = "${centos_mirror_url}/centos/${os_major}/virt/\$basearch/kvm-common/"
    }

    $virt_hash = {
      'rdo-qemu-ev' => {
        'baseurl'  => $virt_baseurl,
        'descr'    => "RDO CentOS-${$os_major} - QEMU EV",
        'gpgkey'   => 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Virtualization',
      }
    }

    $virtkey_hash = { '/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Virtualization' => {
        'source' => 'puppet:///modules/openstack_extras/RPM-GPG-KEY-CentOS-SIG-Virtualization'
      }
    }

    create_resources('file', $virtkey_hash, $_gpgkey_defaults)
    create_resources('yumrepo', $virt_hash, $_repo_defaults)
  }

  if $manage_epel {
    if ($::osfamily == 'RedHat' and
        $::operatingsystem != 'Fedora')
    {
      $epel_hash = { 'epel' => {
          'metalink'       => "https://mirrors.fedoraproject.org/metalink?repo=epel-${::operatingsystemmajrelease}&arch=\$basearch",
          'descr'          => "Extra Packages for Enterprise Linux ${::operatingsystemmajrelease} - \$basearch",
          'gpgkey'         => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-${::operatingsystemmajrelease}",
          'failovermethod' => 'priority'
        }
      }

      $epelkey_hash = { "/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-${::operatingsystemmajrelease}" => {
          'source' => "puppet:///modules/openstack_extras/RPM-GPG-KEY-EPEL-${::operatingsystemmajrelease}"
        }
      }

      create_resources('file', $epelkey_hash, $_gpgkey_defaults)
      create_resources('yumrepo', $epel_hash, $_repo_defaults)
    }
  }

  validate_yum_hash($repo_hash)
  create_resources('yumrepo', $repo_hash, $_repo_defaults)
  create_resources('file', $gpgkey_hash, $_gpgkey_defaults)

  $repo_source_hash.each | $filename, $url | {
    file { $filename:
      path    => "/etc/yum.repos.d/${filename}",
      source  => $url,
      notify  => Exec['yum_refresh'],
      replace => $repo_replace,
    }
  }

  if ((versioncmp($::puppetversion, '3.5') > 0) and $purge_unmanaged) {
      resources { 'yumrepo': purge => true }
  }

  if $manage_priorities and ($::operatingsystem != 'Fedora') {
    if Integer.new($os_major) < 8 {
      # yum-plugin-priorities is unavailable since RHEL/CentOS8
      exec { 'installing_yum-plugin-priorities':
        command   => '/usr/bin/yum install -y yum-plugin-priorities',
        logoutput => 'on_failure',
        tries     => 3,
        try_sleep => 1,
        unless    => '/usr/bin/rpm -qa | /usr/bin/grep -q yum-plugin-priorities',
      }
      Exec['installing_yum-plugin-priorities'] -> Yumrepo<||>
    }
  }

  if $package_require {
      Yumrepo<||> -> Package<||>
  }

  if ($::operatingsystem == 'Fedora') or (Integer.new($os_major) >= 8) {
    $yum_command = 'dnf'
  } else {
    $yum_command = 'yum'
  }

  exec { 'yum_refresh':
    command     => "/usr/bin/${yum_command} clean all",
    refreshonly => true,
  }

  if $update_packages {
    exec { 'yum_update':
      command     => "/usr/bin/${yum_command} update -y",
      refreshonly => true,
    }
    Exec['yum_refresh'] ~> Exec['yum_update'] -> Package <||>
  }
  else {
    Exec['yum_refresh'] -> Package <||>
  }
}
