Puppet Class: puppet_agent::osfamily::redhat

Defined in:
manifests/osfamily/redhat.pp

Summary

Determines the puppet-agent package location for RedHat OSes.

Overview



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'manifests/osfamily/redhat.pp', line 2

class puppet_agent::osfamily::redhat {
  assert_private()

  if $puppet_agent::absolute_source {
    # Absolute sources are expected to be actual packages (not repos)
    # so when absolute_source is set just download the package to the
    # system and finish with this class.
    $source = $puppet_agent::absolute_source
    class { 'puppet_agent::prepare::package':
      source => $source,
    }
    contain puppet_agent::prepare::package
  } else {
    # lint:ignore:only_variable_string
    case $facts['os']['name'] {
      'Fedora': {
        $platform_and_version = "fedora/${facts['os']['release']['major']}"
      }
      'Amazon': {
        $major_version = $facts['os']['release']['major']
        $arch = $facts['os']['architecture']
        $amz_el_version = "${major_version}-${arch}" ? {
          '2-aarch64'      => '2',
          '2-x86_64'       => '7',
          /^(2017|2018)-/  => '6',
          default          => $major_version,
        }

        $platform_and_version = "${amz_el_version}" ? {
          /^(6|7)$/  => "el/${amz_el_version}",
          default    => "amazon/${amz_el_version}",
        }
      }
      default: {
        $platform_and_version = "el/${facts['os']['release']['major']}"
      }
    }
    # lint:endignore
    if ($puppet_agent::is_pe and (!$puppet_agent::use_alternate_sources)) {
      $pe_server_version = pe_build_version()
      # Install amazon packages on AL2 (only aarch64) and 2023 and up (all arch)
      if $facts['os']['name'] == 'Amazon' {
        # lint:ignore:only_variable_string
        $pe_repo_dir = "${amz_el_version}" ? {
          /^(6|7)$/ => "el-${amz_el_version}-${facts['os']['architecture']}",
          default   => $facts['platform_tag'],
        }
        # lint:endignore
      } else {
        $pe_repo_dir = $facts['platform_tag']
      }
      if $puppet_agent::source {
        $source = "${puppet_agent::source}/packages/${pe_server_version}/${pe_repo_dir}"
      } elsif $puppet_agent::alternate_pe_source {
        $source = "${puppet_agent::alternate_pe_source}/packages/${pe_server_version}/${pe_repo_dir}"
      } else {
        $source = "https://${facts['puppet_master_server']}:8140/packages/${pe_server_version}/${pe_repo_dir}"
      }
    } else {
      if $puppet_agent::collection == 'PC1' {
        $source = "${puppet_agent::yum_source}/${platform_and_version}/${puppet_agent::collection}/${puppet_agent::arch}"
      } elsif $puppet_agent::collection =~ /core/ {
        $_collection = regsubst($puppet_agent::collection, /core/, '')
        $source = "https://yum-puppetcore.puppet.com/${_collection}/${platform_and_version}/${puppet_agent::arch}"
      } else {
        $source = "${puppet_agent::yum_source}/${puppet_agent::collection}/${platform_and_version}/${puppet_agent::arch}"
      }
    }

    if ($puppet_agent::is_pe  and (!$puppet_agent::use_alternate_sources)) {
      # In Puppet Enterprise, agent packages are served by the same server
      # as the master, which can be using either a self signed CA, or an external CA.
      # In order for yum to authenticate to the yumrepo on the PE Master, it will need
      # to be configured to pass in the agents certificates. By the time this code is called,
      # the module has already moved the certs to $ssl_dir/{certs,private_keys}, which
      # happen to be the default in PE already.

      $_ssl_dir = $puppet_agent::params::ssldir
      $_sslcacert_path = "${_ssl_dir}/certs/ca.pem"
      $_sslclientcert_path = "${_ssl_dir}/certs/${facts['clientcert']}.pem"
      $_sslclientkey_path = "${_ssl_dir}/private_keys/${facts['clientcert']}.pem"
      # Due to the file paths changing on the PE Master, the 3.8 repository is no longer valid.
      # On upgrade, remove the repo file so that a dangling reference is not left behind returning
      # a 404 on subsequent runs.
      yumrepo { 'puppetlabs-pepackages':
        ensure => absent,
      }
    }
    else {
      $_sslcacert_path = undef
      $_sslclientcert_path = undef
      $_sslclientkey_path = undef
    }

# lint:ignore:strict_indent
    $keyname = 'GPG-KEY-puppet'
    $gpg_path = "/etc/pki/rpm-gpg/RPM-${keyname}"
    $gpg_homedir = '/root/.gnupg'
    $gpg_keys = "file://${gpg_path}"

    $script = @(SCRIPT/L)
ACTION=$0
GPG_HOMEDIR=$1
GPG_KEY_PATH=$2
GPG_ARGS="--homedir $GPG_HOMEDIR --with-colons"
GPG_BIN=$(command -v gpg || command -v gpg2)
if [ -z "${GPG_BIN}" ]; then
  echo Could not find a suitable gpg command, exiting...
  exit 1
fi
GPG_PUBKEY=gpg-pubkey-$("${GPG_BIN}" ${GPG_ARGS} "${GPG_KEY_PATH}" 2>&1 | grep ^pub | cut -d: -f5 | cut --characters=9-16 | tr "[:upper:]" "[:lower:]")
if [ "${ACTION}" = "check" ]; then
  # This will return 1 if there are differences between the key imported in the
  # RPM database and the local keyfile. This means we need to purge the key and
  # reimport it.
  diff <(rpm -qi "${GPG_PUBKEY}" | "${GPG_BIN}" ${GPG_ARGS}) <("${GPG_BIN}" ${GPG_ARGS} "${GPG_KEY_PATH}")
elif [ "${ACTION}" = "import" ]; then
  (rpm -q "${GPG_PUBKEY}" && rpm -e --allmatches "${GPG_PUBKEY}") || true
  rpm --import "${GPG_KEY_PATH}"
fi
| SCRIPT
# lint:endignore

    if $puppet_agent::manage_pki_dir == true {
      file { ['/etc/pki', '/etc/pki/rpm-gpg']:
        ensure => directory,
      }
    }

    file { $gpg_path:
      ensure => file,
      owner  => 0,
      group  => 0,
      mode   => '0644',
      source => "puppet:///modules/puppet_agent/${keyname}",
    }

    exec { "import-${keyname}":
      path      => '/bin:/usr/bin:/sbin:/usr/sbin',
      command   => "/bin/bash -c '${script}' import ${gpg_homedir} ${gpg_path}",
      unless    => "/bin/bash -c '${script}' check ${gpg_homedir} ${gpg_path}",
      require   => File[$gpg_path],
      logoutput => 'on_failure',
    }

    if $puppet_agent::manage_repo == true {
      $_proxy = $puppet_agent::disable_proxy ? {
        true    => '_none_',
        default => $puppet_agent::proxy,
      }
      yumrepo { 'pc_repo':
        baseurl             => $source,
        descr               => "Puppet Labs ${puppet_agent::collection} Repository",
        enabled             => true,
        gpgcheck            => '1',
        gpgkey              => $gpg_keys,
        proxy               => $_proxy,
        sslcacert           => $_sslcacert_path,
        sslclientcert       => $_sslclientcert_path,
        sslclientkey        => $_sslclientkey_path,
        skip_if_unavailable => $puppet_agent::skip_if_unavailable,
        username            => $puppet_agent::username,
        password            => $puppet_agent::password,
      }
      file { '/etc/yum.repos.d/pc_repo.repo':
        ensure => file,
        owner  => 0,
        group  => 0,
        mode   => '0600',
      }
    }
  }
}