diff --git a/manifests/init.pp b/manifests/init.pp index 2c00a6b..9b0c1a2 100755 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -47,6 +47,8 @@ # @param inputs_merge [Boolean] Whether $inputs should merge all hiera sources, or use simple automatic parameter lookup # proxy_address [String] Proxy server to use for downloading files # @param xpack [Hash] Configuration items to export internal stats to a monitoring Elasticsearch cluster +# @param package_provider [String] Accept only exec or chocolatey for windows OS. Exec will download package on internet. +# @param package_name [String] package name to install default to filebeat. class filebeat ( String $package_ensure = $filebeat::params::package_ensure, Boolean $manage_repo = $filebeat::params::manage_repo, @@ -100,11 +102,17 @@ Optional[String] $systemd_beat_log_opts_override = undef, String $systemd_beat_log_opts_template = $filebeat::params::systemd_beat_log_opts_template, String $systemd_override_dir = $filebeat::params::systemd_override_dir, - + Variant[String, Enum['exec', 'chocolatey']] $package_provider = undef, + Optional[String[1]] $package_name = $filebeat::params::package_name, ) inherits filebeat::params { include ::stdlib + $real_package_provider = $package_provider ? { + undef => 'exec', + default => $package_provider, + } + $real_download_url = $download_url ? { undef => "https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${package_ensure}-windows-${filebeat::params::url_arch}.zip", default => $download_url, diff --git a/manifests/input.pp b/manifests/input.pp index 6d7f85f..f8b3273 100755 --- a/manifests/input.pp +++ b/manifests/input.pp @@ -105,8 +105,13 @@ } 'Windows' : { - $cmd_install_dir = regsubst($filebeat::install_dir, '/', '\\', 'G') - $filebeat_path = join([$cmd_install_dir, 'Filebeat', 'filebeat.exe'], '\\') + if $filebeat::real_package_provider == 'exec' { + $cmd_install_dir = regsubst($filebeat::install_dir, '/', '\\', 'G') + $filebeat_path = join([$cmd_install_dir, 'Filebeat', 'filebeat.exe'], '\\') + } + else { + $filebeat_path = 'C:/ProgramData/chocolatey/lib/filebeat/tools/filebeat.exe' + } $validate_cmd = ($filebeat::disable_config_test or $skip_validation) ? { true => undef, diff --git a/manifests/install/linux.pp b/manifests/install/linux.pp index fd54afc..04e9bc7 100644 --- a/manifests/install/linux.pp +++ b/manifests/install/linux.pp @@ -9,7 +9,7 @@ fail('filebeat::install::linux shouldn\'t run on Windows') } - package {'filebeat': + package { $filebeat::package_name: ensure => $filebeat::package_ensure, } } diff --git a/manifests/install/openbsd.pp b/manifests/install/openbsd.pp index 07f0e1f..27f589e 100644 --- a/manifests/install/openbsd.pp +++ b/manifests/install/openbsd.pp @@ -1,6 +1,6 @@ # to manage filebeat installation on OpenBSD class filebeat::install::openbsd { - package {'filebeat': + package { $filebeat::package_name: ensure => $filebeat::package_ensure, } } diff --git a/manifests/install/windows.pp b/manifests/install/windows.pp index 7b83145..b16ae2a 100644 --- a/manifests/install/windows.pp +++ b/manifests/install/windows.pp @@ -10,73 +10,81 @@ # that in the future as it would greatly simplify this code and basically reduce it to # one package resource with type => chocolatey.... - $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 $filebeat::real_package_provider != 'exec' { + package { $filebeat::package_name: + ensure => $filebeat::package_ensure, + provider => 'chocolatey', + } } + else { + $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], '/') - if ! defined(File[$filebeat::install_dir]) { - file { $filebeat::install_dir: - ensure => directory, + Exec { + provider => powershell, } - } - # Note: We can use archive for unzip and cleanup, thus removing the following two resources. - # However, this requires 7zip, which archive can install via chocolatey: - # https://github.com/voxpupuli/puppet-archive/blob/master/manifests/init.pp#L31 - # I'm not choosing to impose those dependencies on anyone at this time... - archive { $zip_file: - source => $filebeat::real_download_url, - cleanup => false, - creates => $version_file, - proxy_server => $filebeat::proxy_address, - } + if ! defined(File[$filebeat::install_dir]) { + file { $filebeat::install_dir: + ensure => directory, + } + } - 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], - ], - } + # Note: We can use archive for unzip and cleanup, thus removing the following two resources. + # However, this requires 7zip, which archive can install via chocolatey: + # https://github.com/voxpupuli/puppet-archive/blob/master/manifests/init.pp#L31 + # I'm not choosing to impose those dependencies on anyone at this time... + archive { $zip_file: + source => $filebeat::real_download_url, + cleanup => false, + creates => $version_file, + proxy_server => $filebeat::proxy_address, + } - # Clean up after ourselves - file { $zip_file: - ensure => absent, - backup => false, - require => Exec["unzip ${filename}"], - } + 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], + ], + } - # You can't remove the old dir while the service has files locked... - exec { "stop service ${filename}": - command => 'Set-Service -Name filebeat -Status Stopped', - creates => $version_file, - onlyif => 'if(Get-WmiObject -Class Win32_Service -Filter "Name=\'filebeat\'") {exit 0} else {exit 1}', - require => Exec["unzip ${filename}"], - } + # Clean up after ourselves + file { $zip_file: + ensure => absent, + backup => false, + require => Exec["unzip ${filename}"], + } - exec { "rename ${filename}": - command => "Remove-Item '${install_folder}' -Recurse -Force -ErrorAction SilentlyContinue;Rename-Item '${filebeat::install_dir}/${filename}' '${install_folder}'", # lint:ignore:140chars - creates => $version_file, - require => Exec["stop service ${filename}"], - } + # You can't remove the old dir while the service has files locked... + exec { "stop service ${filename}": + command => 'Set-Service -Name filebeat -Status Stopped', + creates => $version_file, + onlyif => 'if(Get-WmiObject -Class Win32_Service -Filter "Name=\'filebeat\'") {exit 0} else {exit 1}', + require => Exec["unzip ${filename}"], + } - exec { "mark ${filename}": - command => "New-Item '${version_file}' -ItemType file", - creates => $version_file, - require => Exec["rename ${filename}"], - } + exec { "rename ${filename}": + command => "Remove-Item '${install_folder}' -Recurse -Force -ErrorAction SilentlyContinue;Rename-Item '${filebeat::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}"], + exec { "install ${filename}": + cwd => $install_folder, + command => './install-service-filebeat.ps1', + refreshonly => true, + subscribe => Exec["mark ${filename}"], + } } } diff --git a/manifests/params.pp b/manifests/params.pp index 66db94e..4566ff5 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -32,6 +32,8 @@ $xpack = undef $systemd_override_dir = '/etc/systemd/system/filebeat.service.d' $systemd_beat_log_opts_template = "${module_name}/systemd/logging.conf.erb" + $package_name = 'filebeat' + # These are irrelevant as long as the template is set based on the major_version parameter # if versioncmp('1.9.1', $::rubyversion) > 0 { @@ -40,7 +42,6 @@ # $conf_template = "${module_name}/filebeat.yml.erb" # } # - # Archlinux and OpenBSD have proper packages in the official repos # we shouldn't manage the repo on them case $facts['os']['family'] { @@ -123,15 +124,24 @@ } 'Windows' : { - $package_ensure = '7.1.0' + if $filebeat::real_package_provider == 'chocolatey' { + $package_ensure = '7.2.0' + $config_file = 'C:\ProgramData\chocolatey\lib\filebeat\tools\filebeat.yml' + $modules_dir = 'C:\ProgramData\chocolatey\lib\filebeat\tools\modules.d' + $config_dir = 'C:\ProgramData\chocolatey\lib\filebeat\tools\conf.d' + $install_dir = 'C:/ProgramData' + } else { + $package_ensure = '7.1.0' + $config_file = 'C:/Program Files/Filebeat/filebeat.yml' + $modules_dir = 'C:/Program Files/Filebeat/modules.d' + $config_dir = 'C:/Program Files/Filebeat/conf.d' + $install_dir = 'C:/Program Files' + } $config_file_owner = 'Administrator' $config_file_group = undef $config_dir_owner = 'Administrator' $config_dir_group = undef - $config_file = 'C:/Program Files/Filebeat/filebeat.yml' - $config_dir = 'C:/Program Files/Filebeat/conf.d' - $modules_dir = 'C:/Program Files/Filebeat/modules.d' - $install_dir = 'C:/Program Files' + $tmp_dir = 'C:/Windows/Temp' $service_provider = undef $url_arch = $::architecture ? {