Skip to content

Commit

Permalink
Running pdk validate -a
Browse files Browse the repository at this point in the history
  • Loading branch information
corey-hammerton committed Apr 26, 2024
1 parent 47695c9 commit 4e2ec53
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 66 deletions.
68 changes: 33 additions & 35 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#
# @summary Manages Metricbeat's configuration file
class metricbeat::config inherits metricbeat {

# Use lookup to merge metricbeat::modules config from different levels of hiera
$modules_lookup = lookup('metricbeat::modules', undef, 'unique', undef)
# Check to see if anything has been confiugred in hiera
if $modules_lookup {
$modules_arr = $modules_lookup
# check if array is empty, no need to create a config entry then
# check if array is empty, no need to create a config entry then
} elsif $metricbeat::modules[0].length() > 0 {
$modules_arr = $metricbeat::modules
} else {
Expand All @@ -20,44 +19,44 @@

# if fields are "under root", then remove prefix
if $metricbeat::fields_under_root == true {
$fields_tmp = $metricbeat::fields.each | $key, $value | { {$key => $value} }
$fields_tmp = $metricbeat::fields.each | $key, $value | {{ $key => $value } }
} else {
$fields_tmp = $metricbeat::fields
$fields_tmp = $metricbeat::fields
}

if $metricbeat::major_version == '5' {
$metricbeat_config_base = delete_undef_values({
'cloud.id' => $metricbeat::cloud_id,
'cloud.auth' => $metricbeat::cloud_auth,
'name' => $metricbeat::beat_name,
'tags' => $metricbeat::tags,
'logging' => $metricbeat::logging,
'processors' => $metricbeat::processors,
'queue_size' => $metricbeat::queue_size,
'metricbeat' => {
'modules' => $metricbeat::modules,
},
'output' => $metricbeat::outputs,
'metricbeat.config.modules.path' => "${metricbeat::config_dir}/modules.d/*.yml",
'setup' => $metricbeat::setup,
'cloud.id' => $metricbeat::cloud_id,
'cloud.auth' => $metricbeat::cloud_auth,
'name' => $metricbeat::beat_name,
'tags' => $metricbeat::tags,
'logging' => $metricbeat::logging,
'processors' => $metricbeat::processors,
'queue_size' => $metricbeat::queue_size,
'metricbeat' => {
'modules' => $metricbeat::modules,
},
'output' => $metricbeat::outputs,
'metricbeat.config.modules.path' => "${metricbeat::config_dir}/modules.d/*.yml",
'setup' => $metricbeat::setup,
})

$metricbeat_config = deep_merge($metricbeat_config_base, $fields_tmp)
}
else {
$metricbeat_config_base = delete_undef_values({
'cloud.id' => $metricbeat::cloud_id,
'cloud.auth' => $metricbeat::cloud_auth,
'name' => $metricbeat::beat_name,
'tags' => $metricbeat::tags,
'logging' => $metricbeat::logging,
'processors' => $metricbeat::processors,
'queue' => $metricbeat::queue,
'fields_under_root' => $metricbeat::fields_under_root,
'metricbeat.modules' => $modules_arr,
'output' => $metricbeat::outputs,
'metricbeat.config.modules.path' => "${metricbeat::config_dir}/modules.d/*.yml",
'setup' => $metricbeat::setup,
'cloud.id' => $metricbeat::cloud_id,
'cloud.auth' => $metricbeat::cloud_auth,
'name' => $metricbeat::beat_name,
'tags' => $metricbeat::tags,
'logging' => $metricbeat::logging,
'processors' => $metricbeat::processors,
'queue' => $metricbeat::queue,
'fields_under_root' => $metricbeat::fields_under_root,
'metricbeat.modules' => $modules_arr,
'output' => $metricbeat::outputs,
'metricbeat.config.modules.path' => "${metricbeat::config_dir}/modules.d/*.yml",
'setup' => $metricbeat::setup,
})

if $fields_tmp {
Expand All @@ -69,12 +68,11 @@

# Add the 'xpack' section if supported (version >= 6.2.0)
if versioncmp($metricbeat::package_ensure, '6.2.0') >= 0 {
$metricbeat_config = deep_merge($metricbeat_config_temp, {'xpack' => $metricbeat::xpack})
$metricbeat_config = deep_merge($metricbeat_config_temp, { 'xpack' => $metricbeat::xpack })
}
else {
$metricbeat_config = $metricbeat_config_temp
}

}

# Create modules.d files that exist in hiera then collect any created via exported resources
Expand All @@ -84,7 +82,7 @@
}
Metricbeat::Modulesd <<||>>

case $::kernel {
case $facts['kernel'] {
'Linux': {
$validate_cmd = $metricbeat::disable_configtest ? {
true => undef,
Expand All @@ -94,7 +92,7 @@
}
}

file{'metricbeat.yml':
file { 'metricbeat.yml':
ensure => $metricbeat::ensure,
path => "${metricbeat::config_dir}/metricbeat.yml",
owner => 'root',
Expand All @@ -115,15 +113,15 @@
}
}

file{'metricbeat.yml':
file { 'metricbeat.yml':
ensure => $metricbeat::ensure,
path => "${metricbeat::config_dir}/metricbeat.yml",
content => inline_template('<%= @metricbeat_config.to_yaml() %>'),
validate_cmd => $validate_cmd,
}
}
default: {
fail("${::kernel} is not supported by metricbeat.")
fail("${facts['kernel']} is not supported by metricbeat.")
}
}
}
13 changes: 6 additions & 7 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
# * `setup`
# [Hash] Setup that will be created. Commonly used to create setup using hiera
#
class metricbeat(
class metricbeat (
Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] $apt_repo_url = $metricbeat::params::apt_repo_url,
Optional[String] $cloud_id = $metricbeat::params::cloud_id,
Optional[String] $cloud_auth = $metricbeat::params::cloud_auth,
Expand Down Expand Up @@ -200,14 +200,13 @@
Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] $yum_repo_url = $metricbeat::params::yum_repo_url,
Hash $setup = {},
) inherits metricbeat::params {

$real_download_url = $download_url ? {
undef => "https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-${package_ensure}-windows-${metricbeat::params::url_arch}.zip",
default => $download_url,
}

if $manage_repo {
class{'metricbeat::repo':}
class { 'metricbeat::repo': }

Class['metricbeat::repo']
-> Class['metricbeat::install']
Expand All @@ -228,8 +227,8 @@
-> Class['metricbeat::install']
}

anchor{'metricbeat::begin':}
class{'metricbeat::config':}
class{'metricbeat::install':}
class{'metricbeat::service':}
anchor { 'metricbeat::begin': }
class { 'metricbeat::config': }
class { 'metricbeat::install': }
class { 'metricbeat::service': }
}
20 changes: 9 additions & 11 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# @summary Manages the state of Package['metricbeat']
class metricbeat::install inherits metricbeat {
if $::kernel == 'Windows' {
if $facts['kernel'] == 'Windows' {
$filename = regsubst($metricbeat::real_download_url, '^https?.*\/([^\/]+)\.[^.].*', '\1')
$foldername = 'Metricbeat'
$zip_file = join([$metricbeat::tmp_dir, "${filename}.zip"], '/')
Expand All @@ -17,7 +17,7 @@
}

if !defined(File[$metricbeat::install_dir]) {
file{$metricbeat::install_dir:
file { $metricbeat::install_dir:
ensure => directory,
}
}
Expand All @@ -30,7 +30,7 @@
proxy_server => $metricbeat::proxy_address,
}

exec{"unzip ${filename}":
exec { "unzip ${filename}":
command => "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path '${metricbeat::install_dir}')).Copyhere(\$sh.namespace((Convert-Path '${zip_file}')).items(), 16)", # lint:ignore:140chars
creates => $version_file,
require => [
Expand All @@ -39,13 +39,12 @@
],
}
# Clean up after ourselves
file{$zip_file:
file { $zip_file:
ensure => absent,
backup => false,
require => Exec["unzip ${filename}"],
before => Exec["stop service ${filename}"],
}

} else {
archive { $zip_file:
source => $metricbeat::real_download_url,
Expand All @@ -58,24 +57,23 @@
}
}


# You can't remove the old dir while the service has files locked...
exec{"stop service ${filename}":
exec { "stop service ${filename}":
command => 'Set-Service -Name metricbeat -Status Stopped',
creates => $version_file,
onlyif => 'if(Get-WmiObject -Class Win32_Service -Filter "Name=\'metricbeat\'") {exit 0} else {exit 1}',
}
exec{"rename ${filename}":
exec { "rename ${filename}":
command => "Remove-Item '${install_folder}' -Recurse -Force -ErrorAction SilentlyContinue;Rename-Item '${metricbeat::install_dir}/${filename}' '${install_folder}'", # lint:ignore:140chars
creates => $version_file,
require => Exec["stop service ${filename}"],
}
exec{"mark ${filename}":
exec { "mark ${filename}":
command => "New-Item '${version_file}' -ItemType file",
creates => $version_file,
require => Exec["rename ${filename}"],
}
exec{"install ${filename}":
exec { "install ${filename}":
cwd => $install_folder,
command => './install-service-metricbeat.ps1',
refreshonly => true,
Expand All @@ -90,7 +88,7 @@
$package_ensure = $metricbeat::ensure
}

package{'metricbeat':
package { 'metricbeat':
ensure => $package_ensure,
}
}
Expand Down
10 changes: 5 additions & 5 deletions manifests/modulesd.pp
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Enable a modules.d file either via a custom source, custom template or default module template
# $config can be used to pass down parameters into an .erb template
define metricbeat::modulesd(
define metricbeat::modulesd (
String $template_name = $name,
Hash $config = {},
Optional[String] $source = undef,
Optional[String] $content = undef,
){
) {
# Use the default template as the source if non specified
if ! $source and ! $content {
$default_source = "puppet:///modules/metricbeat/${template_name}.yml"
} elsif $source {
$default_source = $source
}
file { "${metricbeat::config_dir}/modules.d/${template_name}.yml":
ensure => present,
ensure => file,
source => $default_source,
content => $content,
owner => 'root',
group => 'root',
mode => '0644',
require => Class['::metricbeat'],
notify => Class['::metricbeat::service'],
require => Class['metricbeat'],
notify => Class['metricbeat::service'],
}
}
4 changes: 2 additions & 2 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$apt_repo_url = undef
$yum_repo_url = undef

case $::kernel {
case $facts['kernel'] {
'Linux': {
$config_dir = '/etc/metricbeat'
$install_dir = undef
Expand Down Expand Up @@ -96,7 +96,7 @@
}
}
default: {
fail("${::kernel} is not supported by metricbeat.")
fail("${facts['kernel']} is not supported by metricbeat.")
}
}
}
10 changes: 5 additions & 5 deletions manifests/repo.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

case $facts['os']['family'] {
'Debian': {
include ::apt
include apt

if !defined(Apt::Source['beats']) {
apt::source{'beats':
apt::source { 'beats':
location => $apt_repo_url,
release => 'stable',
repos => 'main',
Expand All @@ -33,7 +33,7 @@
}
'RedHat': {
if !defined(Yumrepo['beats']) {
yumrepo{'beats':
yumrepo { 'beats':
descr => "Elastic repository for ${metricbeat::major_version}.x packages",
baseurl => $yum_repo_url,
gpgcheck => 1,
Expand All @@ -46,10 +46,10 @@
exec { 'topbeat_suse_import_gpg':
command => '/usr/bin/rpmkeys --import https://artifacts.elastic.co/GPG-KEY-elasticsearch',
unless => '/usr/bin/test $(rpm -qa gpg-pubkey | grep -i "D88E42B4" | wc -l) -eq 1 ',
notify => [ Zypprepo['beats'] ],
notify => [Zypprepo['beats']],
}
if !defined (Zypprepo['beats']) {
zypprepo{'beats':
zypprepo { 'beats':
baseurl => $yum_repo_url,
enabled => 1,
autorefresh => 1,
Expand Down
2 changes: 1 addition & 1 deletion manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$service_enable = false
}

service{'metricbeat':
service { 'metricbeat':
ensure => $service_ensure,
enable => $service_enable,
hasrestart => $metricbeat::service_has_restart,
Expand Down

0 comments on commit 4e2ec53

Please sign in to comment.