From c00efd1f4786df664b31fdd42aac90c66cda189c Mon Sep 17 00:00:00 2001 From: corey-hammerton Date: Sun, 27 Jan 2019 17:09:14 -0500 Subject: [PATCH 1/7] manifests: Adding a params.pp class containing all default parameter values This makes long term management of default param values easier since we can now declare default values based on differing criteria. --- manifests/init.pp | 63 ++++++++++++++------------------------------- manifests/params.pp | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 manifests/params.pp diff --git a/manifests/init.pp b/manifests/init.pp index 4f89fbb..6dac1d3 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -108,49 +108,26 @@ # Optional[Hash] Configuration items to export internal stats to a # monitoring Elasticsearch cluster class metricbeat( - Array[Hash] $modules = [{}], - Hash $outputs = {}, - String $beat_name = $::hostname, - Pattern[/^0[0-7]{3}$/] $config_mode = '0600', - Boolean $disable_configtest = false, - Enum['present', 'absent'] $ensure = 'present', - Optional[Hash] $fields = undef, - Boolean $fields_under_root = false, - Hash $logging = { - 'level' => 'info', - 'files' => { - 'keepfiles' => 7, - 'name' => 'metricbeat', - 'path' => '/var/log/metricbeat', - 'rotateeverybytes' => '10485760', - }, - 'metrics' => { - 'enabled' => false, - 'period' => '30s', - }, - 'selectors' => undef, - 'to_files' => true, - 'to_syslog' => false, - }, - Enum['5', '6'] $major_version = '5', - Boolean $manage_repo = true, - String $package_ensure = 'present', - Optional[Array[Hash]] $processors = undef, - Hash $queue = { - 'mem' => { - 'events' => 4096, - 'flush' => { - 'min_events' => 0, - 'timeout' => '0s', - }, - }, - }, - Integer $queue_size = 1000, - Enum['enabled', 'disabled', 'running', 'unmanaged'] $service_ensure = 'enabled', - Boolean $service_has_restart = true, - Optional[Array[String]] $tags = undef, - Optional[Hash] $xpack = undef, -) { + Array[Hash] $modules = $metricbeat::params::modules, + Hash $outputs = $metricbeat::params::outputs, + String $beat_name = $metricbeat::params::beat_name, + Pattern[/^0[0-7]{3}$/] $config_mode = $metricbeat::params::config_mode, + Boolean $disable_configtest = $metricbeat::params::disable_configtest, + Enum['present', 'absent'] $ensure = $metricbeat::params::ensure, + Optional[Hash] $fields = $metricbeat::params::fields, + Boolean $fields_under_root = $metricbeat::params::fields_under_root, + Hash $logging = $metricbeat::params::logging, + Enum['5', '6'] $major_version = $metricbeat::params::major_version, + Boolean $manage_repo = $metricbeat::params::manage_repo, + String $package_ensure = $metricbeat::params::package_ensure, + Optional[Array[Hash]] $processors = $metricbeat::params::processors, + Hash $queue = $metricbeat::params::queue, + Integer $queue_size = $metricbeat::params::queue_size, + Enum['enabled', 'disabled', 'running', 'unmanaged'] $service_ensure = $metricbeat::params::service_ensure, + Boolean $service_has_restart = $metricbeat::params::service_has_restart, + Optional[Array[String]] $tags = $metricbeat::params::tags, + Optional[Hash] $xpack = $metricbeat::params::xpack, +) inherits metricbeat::params { if $manage_repo { class{'metricbeat::repo':} diff --git a/manifests/params.pp b/manifests/params.pp new file mode 100644 index 0000000..f19b142 --- /dev/null +++ b/manifests/params.pp @@ -0,0 +1,58 @@ +# A description of what this class does +# +# @summary A short summary of the purpose of this class +# +# @example +# include metricbeat::params +class metricbeat::params { + $ensure = 'present' + $beat_name = $::hostname + $config_mode = '0600' + $disable_configtest = false + $fields = undef + $fields_under_root = false + $manage_repo = true + $major_version = '5' + $modules = [{}] + $outputs = {} + $package_ensure = 'present' + $processors = undef + $queue = { + 'mem' => { + 'events' => 4096, + 'flush' => { + 'min_events' => 0, + 'timeout' => '0s', + }, + }, + } + $queue_size = 1000 + $service_ensure = 'enabled' + $service_has_restart = true + $tags = undef + $xpack = undef + + case $::kernel { + 'Linux': { + $logging = { + 'level' => 'info', + 'files' => { + 'keepfiles' => 7, + 'name' => 'metricbeat', + 'path' => '/var/log/metricbeat', + 'rotateeverybytes' => '10485760', + }, + 'metrics' => { + 'enabled' => false, + 'period' => '30s', + }, + 'selectors' => undef, + 'to_files' => true, + 'to_syslog' => false, + } + } + default: { + fail("${::kernel} is not supported by metricbeat.") + } + } +} From 8658ac96ba82b7a080110b070ef621e59cf381e6 Mon Sep 17 00:00:00 2001 From: corey-hammerton Date: Sun, 27 Jan 2019 17:16:06 -0500 Subject: [PATCH 2/7] Adding a test case for params --- spec/classes/params_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/classes/params_spec.rb diff --git a/spec/classes/params_spec.rb b/spec/classes/params_spec.rb new file mode 100644 index 0000000..97794e6 --- /dev/null +++ b/spec/classes/params_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'metricbeat::params' do + on_supported_os(facterversion: '2.4').each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + + it { is_expected.to compile } + end + end +end From 8a7c888c4acc966bc262d3814d90bbc7c69e5006 Mon Sep 17 00:00:00 2001 From: "corey.hammerton@gmail.com" Date: Sun, 10 Feb 2019 13:42:36 -0500 Subject: [PATCH 3/7] Initial work on Windows support --- manifests/config.pp | 56 +++++++++++++++++++++----------- manifests/init.pp | 46 +++++++++++++++++++++++++- manifests/install.pp | 77 ++++++++++++++++++++++++++++++++++++++++---- manifests/params.pp | 26 +++++++++++++-- metadata.json | 8 +++++ 5 files changed, 186 insertions(+), 27 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index 87d430a..2b229c9 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -19,11 +19,6 @@ }, 'output' => $metricbeat::outputs, }) - - $validate_cmd = $metricbeat::disable_configtest ? { - true => undef, - default => '/usr/share/metricbeat/bin/metricbeat -configtest -c %', - } } elsif $metricbeat::major_version == '6' { $metricbeat_config_temp = delete_undef_values({ @@ -40,11 +35,6 @@ 'output' => $metricbeat::outputs, }) - $validate_cmd = $metricbeat::disable_configtest ? { - true => undef, - default => '/usr/share/metricbeat/bin/metricbeat test config', - } - # 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}) @@ -55,13 +45,43 @@ } - file{'metricbeat.yml': - ensure => $metricbeat::ensure, - path => '/etc/metricbeat/metricbeat.yml', - owner => 'root', - group => 'root', - mode => $metricbeat::config_mode, - content => inline_template('<%= @metricbeat_config.to_yaml() %>'), - validate_cmd => $validate_cmd, + case $::kernel { + 'Linux': { + $validate_cmd = $metricbeat::disable_configtest ? { + true => undef, + default => $metricbeat::major_version ? { + '5' => '/usr/share/metricbeat/bin/metricbeat -configtest -c %', + default => '/usr/share/metricbeat/bin/metricbeat test config', + } + } + + file{'metricbeat.yml': + ensure => $metricbeat::ensure, + path => $metricbeat::config_file, + owner => 'root', + group => 'root', + mode => $metricbeat::config_mode, + content => inline_template('<%= @metricbeat_config.to_yaml() %>'), + validate_cmd => $validate_cmd, + } + } + 'Windows': { + $cmd_install_dir = regsubst($metricbeat::install_dir, '/', '\\', 'G') + $metricbeat_path = join([$cmd_install_dir, 'Metricbeat', 'metricbeat.exe'], '\\') + $validate_cmd = $metricbeat::disable_configtest ? { + true => undef, + default => "\"${metricbeat_path}\" -N configtest -c \"%\"" + } + + file{'metricbeat.yml': + ensure => $metricbeat::ensure, + path => $metricbeat::config_file, + content => inline_template('<%= @metricbeat_config.to_yaml() %>'), + validate_cmd => $validate_cmd, + } + } + default: { + fail("${::kernel} is not supported by metricbeat.") + } } } diff --git a/manifests/init.pp b/manifests/init.pp index 6dac1d3..c71d29e 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -36,6 +36,11 @@ # [String] The name of the beat which is published as the `beat.name` # field of each transaction. (default: $::hostname) # +# * `config_file` +# [String] The absolute path to the configuration file location. (default: +# /etc/metricbeat/metricbeat.yaml on Linux, C:/Program Files/Metricbeat/metricbeat.yml +# on Windows) +# # * `config_mode` # [String] The file permission mode of the config file. Must be in Linux # octal format. Default: '0600' @@ -44,6 +49,10 @@ # [Boolean] If true disable configuration file testing. It is generally # recommended to leave this parameter at its default value. (default: false) # +# * `download_url` +# Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] The URL of the ZIP +# file to download. Only valid on Windows nodes. (default: undef) +# # * `ensure` # [String] Ensures that all required resources are managed or removed # from the target node. This is good for bulk uninstallation across a @@ -58,6 +67,10 @@ # sub-dictionary. When this is true custom fields are added to the top # level dictionary of each transaction. (default: false) # +# * `install_dir` +# Optional[String] The absolute path to the location where metricbeat will +# be installed. Only applicable on Windows. (default: C:/Program Files) +# # * `logging` # [Hash] The configuration section of File['metricbeat.yml'] for the # logging output. @@ -72,13 +85,18 @@ # # * `package_ensure` # [String] The desired state of Package['metricbeat']. Only valid when -# $ensure is present. (default: 'present') +# $ensure is present. On Windows this is the version number of the package. +# (default: 'present') # # * `processors` # Optional[Array[Hash]] An optional list of dictionaries to configure # processors, provided by libbeat, to process events before they are # sent to the output. (default: undef) # +# * `proxy_address* +# Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] The Proxy server used +# for downloading files. (default: undef) +# # * `queue` # [Hash] Configure the internal queue before being consumed by the output(s) # in bulk transactions. As of 6.0 only a memory queue is available, all @@ -99,11 +117,24 @@ # When false the init script's stop and start functions will be used. # (default: true) # +# * `service_provider` +# Optional[String] The optional service provider of the node. (default: +# 'redhat' on RedHat nodes, undef otherwise) +# # * `tags` # Optional[Array[String]] An optional list of values to include in the # `tag` field of each published transaction. This is useful for # identifying groups of servers by logical property. (default: undef) # +# * `tmp_dir` +# String The absolute path to the temporary directory. On Windows, this +# is the target directory for the ZIP file download. (default: /tmp on +# Linux, C:\Windows\Temp on Windows) +# +# * `url_arch +# Optional[String] An optional string describing the architecture of +# the target node. Only applicable on Windows nodes. (default: x86 or x64) +# # * `xpack` # Optional[Hash] Configuration items to export internal stats to a # monitoring Elasticsearch cluster @@ -111,23 +142,36 @@ Array[Hash] $modules = $metricbeat::params::modules, Hash $outputs = $metricbeat::params::outputs, String $beat_name = $metricbeat::params::beat_name, + String $config_file = $metricbeat::params::config_file, Pattern[/^0[0-7]{3}$/] $config_mode = $metricbeat::params::config_mode, Boolean $disable_configtest = $metricbeat::params::disable_configtest, + Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] $download_url = $metricbeat::params::download_url, Enum['present', 'absent'] $ensure = $metricbeat::params::ensure, Optional[Hash] $fields = $metricbeat::params::fields, Boolean $fields_under_root = $metricbeat::params::fields_under_root, + Optional[String] $install_dir = $metricbeat::params::install_dir, Hash $logging = $metricbeat::params::logging, Enum['5', '6'] $major_version = $metricbeat::params::major_version, Boolean $manage_repo = $metricbeat::params::manage_repo, String $package_ensure = $metricbeat::params::package_ensure, Optional[Array[Hash]] $processors = $metricbeat::params::processors, + Optional[Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]] $proxy_address = $metricbeat::params::proxy_address, Hash $queue = $metricbeat::params::queue, Integer $queue_size = $metricbeat::params::queue_size, Enum['enabled', 'disabled', 'running', 'unmanaged'] $service_ensure = $metricbeat::params::service_ensure, Boolean $service_has_restart = $metricbeat::params::service_has_restart, + Optional[String] $service_provider = $metricbeat::params::service_provider, Optional[Array[String]] $tags = $metricbeat::params::tags, + String $tmp_dir = $metricbeat::params::tmp_dir, + Optional[String] $url_arch = $metricbeat::params::url_arch, Optional[Hash] $xpack = $metricbeat::params::xpack, ) 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':} diff --git a/manifests/install.pp b/manifests/install.pp index 61dc1fe..67112a9 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -5,14 +5,79 @@ # # @summary Manages the state of Package['metricbeat'] class metricbeat::install inherits metricbeat { - if $metricbeat::ensure == 'present' { - $package_ensure = $metricbeat::package_ensure + if $::kernel == 'Windows' { + $filename = regsubst($filebeat::real_download_url, '^https?.*\/([^\/]+)\.[^.].*', '\1') + $foldername = 'Filebeat' + $zip_file = join([$filebeat::tmp_dir, "${filename}.zip"], '/') + $install_folder = join([$filebeat::install_dir, $foldername], '/') + $version_file = join([$install_folder, $filename], '/') + + Exec { + provider => powershell, + } + + if !defined(File[$metricbeat::install_dir]) { + file{$metricbeat::install_dir: + ensure => directory, + } + } + + archive{ $zip_file: + source => $metricbeat::real_download_url, + cleanup => false, + creates => $version_file, + proxy_server => $metricbeat::proxy_address, + } + exec{"unzip ${filename}": + command => "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path '${filebeat::install_dir}')).Copyhere(\$sh.namespace((Convert-Path '${zip_file}')).items(), 16)", # lint:ignore:140chars + creates => $version_file, + require => [ + File[$filebeat::install_dir], + Archive[$zip_file], + ], + } + + # Clean up after ourselves + file{$zip_file: + ensure => absent, + backup => false, + require => Exec["unzip ${filename}"], + } + + # You can't remove the old dir while the service has files locked... + 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}', + require => Exec["unzip ${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}": + command => "New-Item '${version_file}' -ItemType file", + creates => $version_file, + require => Exec["rename ${filename}"], + } + exec{"install ${filename}": + cwd => $install_folder, + command => './install-service-filebeat.ps1', + refreshonly => true, + subscribe => Exec["mark ${filename}"], + } } else { - $package_ensure = $metricbeat::ensure - } + if $metricbeat::ensure == 'present' { + $package_ensure = $metricbeat::package_ensure + } + else { + $package_ensure = $metricbeat::ensure + } - package{'metricbeat': - ensure => $package_ensure, + package{'metricbeat': + ensure => $package_ensure, + } } } diff --git a/manifests/params.pp b/manifests/params.pp index f19b142..1b73038 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -9,14 +9,15 @@ $beat_name = $::hostname $config_mode = '0600' $disable_configtest = false + $download_url = undef $fields = undef $fields_under_root = false $manage_repo = true $major_version = '5' $modules = [{}] $outputs = {} - $package_ensure = 'present' $processors = undef + $proxy_address = undef $queue = { 'mem' => { 'events' => 4096, @@ -34,7 +35,9 @@ case $::kernel { 'Linux': { - $logging = { + $config_file = '/etc/metricbeat/metricbeat.yml' + $install_dir = undef + $logging = { 'level' => 'info', 'files' => { 'keepfiles' => 7, @@ -50,6 +53,25 @@ 'to_files' => true, 'to_syslog' => false, } + $package_ensure = 'present' + $service_provider = $::osfamily ? { + 'RedHat' => 'redhat', + default => undef, + } + $tmp_dir = '/tmp' + $url_arch = undef + } + 'Windows': { + $config_file = 'C:/Program Files/Metricbeat/metricbeat.yml' + $install_dir = 'C:/Program Files' + $package_ensure = '5.6.2' + $service_provider = undef + $tmp_dir = 'C:/Windows/Temp' + $url_arch = $::architecture ? { + 'x86' => 'x86', + 'x64' => 'x86_64', + default => fail("${::architecture} is not supported by metricbeat."), + } } default: { fail("${::kernel} is not supported by metricbeat.") diff --git a/metadata.json b/metadata.json index 31ff94d..2bd51e1 100644 --- a/metadata.json +++ b/metadata.json @@ -16,6 +16,14 @@ "name": "puppetlabs-apt", "version_requirement": ">= 4.0.0 < 7.0.0" }, + { + "name": "puppet/archive", + "version_requirement": ">= 0.5.0 < 4.0.0" + }, + { + "name": "puppetlabs-powershell", + "version_requirement": ">= 1.0.1 < 3.0.0" + }, { "name": "darin-zypprepo", "version_requirement": ">= 1.0.0 < 2.0.0" From 3fff9b545a5c011a21c9422b9a512741da0a1481 Mon Sep 17 00:00:00 2001 From: "corey.hammerton@gmail.com" Date: Sun, 10 Feb 2019 15:39:21 -0500 Subject: [PATCH 4/7] Officially adding windows support and stablizing tests. TODO: Add Windows specific test cases. --- .fixtures.yml | 2 + manifests/install.pp | 12 ++-- manifests/params.pp | 22 ++++++- metadata.json | 7 ++ spec/classes/metricbeat_spec.rb | 110 +++++++++++++++++++------------- 5 files changed, 98 insertions(+), 55 deletions(-) diff --git a/.fixtures.yml b/.fixtures.yml index 189b03e..2aaa556 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -4,6 +4,8 @@ fixtures: forge_modules: apt: "puppetlabs/apt" + archive: "puppet/archive" + powershell: "puppetlabs/powershell" stdlib: "puppetlabs/stdlib" zypprepo: "darin/zypprepo" symlinks: diff --git a/manifests/install.pp b/manifests/install.pp index 67112a9..3c477ba 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -6,10 +6,10 @@ # @summary Manages the state of Package['metricbeat'] class metricbeat::install inherits metricbeat { if $::kernel == 'Windows' { - $filename = regsubst($filebeat::real_download_url, '^https?.*\/([^\/]+)\.[^.].*', '\1') + $filename = regsubst($metricbeat::real_download_url, '^https?.*\/([^\/]+)\.[^.].*', '\1') $foldername = 'Filebeat' - $zip_file = join([$filebeat::tmp_dir, "${filename}.zip"], '/') - $install_folder = join([$filebeat::install_dir, $foldername], '/') + $zip_file = join([$metricbeat::tmp_dir, "${filename}.zip"], '/') + $install_folder = join([$metricbeat::install_dir, $foldername], '/') $version_file = join([$install_folder, $filename], '/') Exec { @@ -29,10 +29,10 @@ proxy_server => $metricbeat::proxy_address, } exec{"unzip ${filename}": - command => "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path '${filebeat::install_dir}')).Copyhere(\$sh.namespace((Convert-Path '${zip_file}')).items(), 16)", # lint:ignore:140chars + 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 => [ - File[$filebeat::install_dir], + File[$metricbeat::install_dir], Archive[$zip_file], ], } @@ -63,7 +63,7 @@ } exec{"install ${filename}": cwd => $install_folder, - command => './install-service-filebeat.ps1', + command => './install-service-metricbeat.ps1', refreshonly => true, subscribe => Exec["mark ${filename}"], } diff --git a/manifests/params.pp b/manifests/params.pp index 1b73038..cf8f8ad 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -38,14 +38,14 @@ $config_file = '/etc/metricbeat/metricbeat.yml' $install_dir = undef $logging = { - 'level' => 'info', - 'files' => { + 'level' => 'info', + 'files' => { 'keepfiles' => 7, 'name' => 'metricbeat', 'path' => '/var/log/metricbeat', 'rotateeverybytes' => '10485760', }, - 'metrics' => { + 'metrics' => { 'enabled' => false, 'period' => '30s', }, @@ -64,6 +64,22 @@ 'Windows': { $config_file = 'C:/Program Files/Metricbeat/metricbeat.yml' $install_dir = 'C:/Program Files' + $logging = { + 'level' => 'info', + 'files' => { + 'keepfiles' => 7, + 'name' => 'metricbeat', + 'path' => 'C:/Program Files/Metricbeat/logs', + 'rotateeverybytes' => '10485760', + }, + 'metrics' => { + 'enabled' => false, + 'period' => '30s', + }, + 'selectors' => undef, + 'to_eventlog' => false, + 'to_files' => true, + } $package_ensure = '5.6.2' $service_provider = undef $tmp_dir = 'C:/Windows/Temp' diff --git a/metadata.json b/metadata.json index c87c097..abace11 100644 --- a/metadata.json +++ b/metadata.json @@ -77,6 +77,13 @@ "operatingsystemrelease": [ "12" ] + }, + { + "operatingsystem": "windows", + "operatingsystemrelease": [ + "2012", + "2012 R2" + ] } ], "requirements": [ diff --git a/spec/classes/metricbeat_spec.rb b/spec/classes/metricbeat_spec.rb index 4407ff2..d1be23c 100644 --- a/spec/classes/metricbeat_spec.rb +++ b/spec/classes/metricbeat_spec.rb @@ -8,56 +8,64 @@ it { is_expected.to compile } describe 'metricbeat::config' do - it do - is_expected.to contain_file('metricbeat.yml').with( - ensure: 'present', - owner: 'root', - group: 'root', - mode: '0600', - path: '/etc/metricbeat/metricbeat.yml', - validate_cmd: '/usr/share/metricbeat/bin/metricbeat -configtest -c %', - ) - end - - describe 'with ensure = absent' do - let(:params) { { 'ensure' => 'absent' } } - + if os_facts[:kernel] != 'windows' it do is_expected.to contain_file('metricbeat.yml').with( - ensure: 'absent', + ensure: 'present', + owner: 'root', + group: 'root', + mode: '0600', path: '/etc/metricbeat/metricbeat.yml', validate_cmd: '/usr/share/metricbeat/bin/metricbeat -configtest -c %', ) end end + describe 'with ensure = absent' do + let(:params) { { 'ensure' => 'absent' } } + + if os_facts[:kernel] != 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'absent', + path: '/etc/metricbeat/metricbeat.yml', + validate_cmd: '/usr/share/metricbeat/bin/metricbeat -configtest -c %', + ) + end + end + end + describe 'with disable_configtest = true' do let(:params) { { 'disable_configtest' => true } } - it do - is_expected.to contain_file('metricbeat.yml').with( - ensure: 'present', - owner: 'root', - group: 'root', - mode: '0600', - path: '/etc/metricbeat/metricbeat.yml', - validate_cmd: nil, - ) + if os_facts[:kernel] != 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + owner: 'root', + group: 'root', + mode: '0600', + path: '/etc/metricbeat/metricbeat.yml', + validate_cmd: nil, + ) + end end end describe 'with config_mode = 0644' do let(:params) { { 'config_mode' => '0644' } } - it do - is_expected.to contain_file('metricbeat.yml').with( - ensure: 'present', - owner: 'root', - group: 'root', - mode: '0644', - path: '/etc/metricbeat/metricbeat.yml', - validate_cmd: '/usr/share/metricbeat/bin/metricbeat -configtest -c %', - ) + if os_facts[:kernel] != 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + owner: 'root', + group: 'root', + mode: '0644', + path: '/etc/metricbeat/metricbeat.yml', + validate_cmd: '/usr/share/metricbeat/bin/metricbeat -configtest -c %', + ) + end end end @@ -70,38 +78,48 @@ describe 'with major_version = 6 for new config test flag' do let(:params) { { 'major_version' => '6' } } - it do - is_expected.to contain_file('metricbeat.yml').with( - ensure: 'present', - owner: 'root', - group: 'root', - mode: '0600', - path: '/etc/metricbeat/metricbeat.yml', - validate_cmd: '/usr/share/metricbeat/bin/metricbeat test config', - ) + if os_facts[:kernel] != 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + owner: 'root', + group: 'root', + mode: '0600', + path: '/etc/metricbeat/metricbeat.yml', + validate_cmd: '/usr/share/metricbeat/bin/metricbeat test config', + ) + end end end end describe 'metricbeat::install' do - it { is_expected.to contain_package('metricbeat').with(ensure: 'present') } + if os_facts[:kernel] != 'windows' + it { is_expected.to contain_package('metricbeat').with(ensure: 'present') } + end describe 'with ensure = absent' do let(:params) { { 'ensure' => 'absent' } } - it { is_expected.to contain_package('metricbeat').with(ensure: 'absent') } + if os_facts[:kernel] != 'windows' + it { is_expected.to contain_package('metricbeat').with(ensure: 'absent') } + end end describe 'with package_ensure to a specific version' do let(:params) { { 'package_ensure' => '5.6.2-1' } } - it { is_expected.to contain_package('metricbeat').with(ensure: '5.6.2-1') } + if os_facts[:kernel] != 'windows' + it { is_expected.to contain_package('metricbeat').with(ensure: '5.6.2-1') } + end end describe 'with package_ensure = latest' do let(:params) { { 'package_ensure' => 'latest' } } - it { is_expected.to contain_package('metricbeat').with(ensure: 'latest') } + if os_facts[:kernel] != 'windows' + it { is_expected.to contain_package('metricbeat').with(ensure: 'latest') } + end end end From 6b3b94a10b075e1d53ddd298d7e22a42afc43620 Mon Sep 17 00:00:00 2001 From: "corey.hammerton@gmail.com" Date: Sun, 10 Feb 2019 17:28:29 -0500 Subject: [PATCH 5/7] Adding test cases specific to Windows. --- manifests/config.pp | 5 ++- manifests/install.pp | 2 +- spec/classes/metricbeat_spec.rb | 72 ++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index 2b229c9..59f4540 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -70,7 +70,10 @@ $metricbeat_path = join([$cmd_install_dir, 'Metricbeat', 'metricbeat.exe'], '\\') $validate_cmd = $metricbeat::disable_configtest ? { true => undef, - default => "\"${metricbeat_path}\" -N configtest -c \"%\"" + default => $metricbeat::major_version ? { + '5' => "\"${metricbeat_path}\" -N configtest -c \"%\"", + default => "\"${metricbeat_path}\" test config", + } } file{'metricbeat.yml': diff --git a/manifests/install.pp b/manifests/install.pp index 3c477ba..59de535 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -7,7 +7,7 @@ class metricbeat::install inherits metricbeat { if $::kernel == 'Windows' { $filename = regsubst($metricbeat::real_download_url, '^https?.*\/([^\/]+)\.[^.].*', '\1') - $foldername = 'Filebeat' + $foldername = 'Metricbeat' $zip_file = join([$metricbeat::tmp_dir, "${filename}.zip"], '/') $install_folder = join([$metricbeat::install_dir, $foldername], '/') $version_file = join([$install_folder, $filename], '/') diff --git a/spec/classes/metricbeat_spec.rb b/spec/classes/metricbeat_spec.rb index d1be23c..7d92a54 100644 --- a/spec/classes/metricbeat_spec.rb +++ b/spec/classes/metricbeat_spec.rb @@ -8,7 +8,15 @@ it { is_expected.to compile } describe 'metricbeat::config' do - if os_facts[:kernel] != 'windows' + if os_facts[:kernel] == 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + path: 'C:/Program Files/Metricbeat/metricbeat.yml', + validate_cmd: "\"C:\\Program Files\\Metricbeat\\metricbeat.exe\" -N configtest -c \"%\"", # rubocop:disable StringLiterals + ) + end + else it do is_expected.to contain_file('metricbeat.yml').with( ensure: 'present', @@ -24,7 +32,15 @@ describe 'with ensure = absent' do let(:params) { { 'ensure' => 'absent' } } - if os_facts[:kernel] != 'windows' + if os_facts[:kernel] == 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'absent', + path: 'C:/Program Files/Metricbeat/metricbeat.yml', + validate_cmd: "\"C:\\Program Files\\Metricbeat\\metricbeat.exe\" -N configtest -c \"%\"", # rubocop:disable StringLiterals + ) + end + else it do is_expected.to contain_file('metricbeat.yml').with( ensure: 'absent', @@ -38,7 +54,15 @@ describe 'with disable_configtest = true' do let(:params) { { 'disable_configtest' => true } } - if os_facts[:kernel] != 'windows' + if os_facts[:kernel] == 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + path: 'C:/Program Files/Metricbeat/metricbeat.yml', + validate_cmd: nil, + ) + end + else it do is_expected.to contain_file('metricbeat.yml').with( ensure: 'present', @@ -78,7 +102,15 @@ describe 'with major_version = 6 for new config test flag' do let(:params) { { 'major_version' => '6' } } - if os_facts[:kernel] != 'windows' + if os_facts[:kernel] == 'windows' + it do + is_expected.to contain_file('metricbeat.yml').with( + ensure: 'present', + path: 'C:/Program Files/Metricbeat/metricbeat.yml', + validate_cmd: "\"C:\\Program Files\\Metricbeat\\metricbeat.exe\" test config", # rubocop:disable StringLiterals + ) + end + else it do is_expected.to contain_file('metricbeat.yml').with( ensure: 'present', @@ -94,7 +126,37 @@ end describe 'metricbeat::install' do - if os_facts[:kernel] != 'windows' + if os_facts[:kernel] == 'windows' + it do + is_expected.to contain_file('C:/Program Files').with(ensure: 'directory') + is_expected.to contain_archive('C:/Windows/Temp/metricbeat-5.6.2-windows-x86_64.zip').with( + creates: 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64', + source: 'https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-5.6.2-windows-x86_64.zip', + ) + is_expected.to contain_exec('unzip metricbeat-5.6.2-windows-x86_64').with( + command: "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path 'C:/Program Files')).Copyhere(\$sh.namespace((Convert-Path 'C:/Windows/Temp/metricbeat-5.6.2-windows-x86_64.zip')).items(), 16)", # rubocop:disable LineLength + creates: 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64', + ) + is_expected.to contain_exec('stop service metricbeat-5.6.2-windows-x86_64').with( + creates: 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64', + command: 'Set-Service -Name metricbeat -Status Stopped', + onlyif: 'if(Get-WmiObject -Class Win32_Service -Filter "Name=\'metricbeat\'") {exit 0} else {exit 1}', + ) + is_expected.to contain_exec('rename metricbeat-5.6.2-windows-x86_64').with( + creates: 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64', + command: "Remove-Item 'C:/Program Files/Metricbeat' -Recurse -Force -ErrorAction SilentlyContinue;Rename-Item 'C:/Program Files/metricbeat-5.6.2-windows-x86_64' 'C:/Program Files/Metricbeat'", # rubocop:disable LineLength + ) + is_expected.to contain_exec('mark metricbeat-5.6.2-windows-x86_64').with( + creates: 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64', + command: "New-Item 'C:/Program Files/Metricbeat/metricbeat-5.6.2-windows-x86_64' -ItemType file", + ) + is_expected.to contain_exec('install metricbeat-5.6.2-windows-x86_64').with( + command: './install-service-metricbeat.ps1', + cwd: 'C:/Program Files/Metricbeat', + refreshonly: true, + ) + end + else it { is_expected.to contain_package('metricbeat').with(ensure: 'present') } end From 6bd3d5a2b22d66180c7fe3d041c895ba682642f8 Mon Sep 17 00:00:00 2001 From: "corey.hammerton@gmail.com" Date: Sun, 10 Feb 2019 20:51:41 -0500 Subject: [PATCH 6/7] Re-adding required archive dependency --- .fixtures.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.fixtures.yml b/.fixtures.yml index 8bb1d3c..1f00023 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -7,6 +7,7 @@ fixtures: repo: "https://github.com/puppetlabs/puppetlabs-yumrepo_core.git" puppet_version: ">= 6.0.0" apt: "https://github.com/puppetlabs/puppetlabs-apt.git" + archive: "https://github.com/voxpupuli/puppet-archive.git" stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" zypprepo: "https://github.com/voxpupuli/puppet-zypprepo.git" symlinks: From 317d5ca17471a95267436aa905c9fa5bd774a597 Mon Sep 17 00:00:00 2001 From: "corey.hammerton@gmail.com" Date: Sun, 10 Feb 2019 20:56:19 -0500 Subject: [PATCH 7/7] Another missing dependency --- .fixtures.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.fixtures.yml b/.fixtures.yml index 1f00023..d54324f 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -8,6 +8,7 @@ fixtures: puppet_version: ">= 6.0.0" apt: "https://github.com/puppetlabs/puppetlabs-apt.git" archive: "https://github.com/voxpupuli/puppet-archive.git" + powershell: "https://github.com/puppetlabs/puppetlabs-powershell.git" stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" zypprepo: "https://github.com/voxpupuli/puppet-zypprepo.git" symlinks: