From 9514706fb7f0fc1a6b0fb5e702b890c1f08ff167 Mon Sep 17 00:00:00 2001 From: hdep Date: Wed, 8 Apr 2020 19:53:38 +0200 Subject: [PATCH] Add support for chocolatey --- manifests/config.pp | 26 +++++--- manifests/init.pp | 2 + manifests/install/windows.pp | 126 +++++++++++++++++++---------------- manifests/params.pp | 21 ++++-- 4 files changed, 103 insertions(+), 72 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index 9c400eb..4c6bb03 100755 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -191,15 +191,15 @@ } # end OpenBSD 'Windows' : { - $cmd_install_dir = regsubst($filebeat::install_dir, '/', '\\', 'G') - $filebeat_path = join([$cmd_install_dir, 'Filebeat', 'filebeat.exe'], '\\') - $validate_cmd = ($filebeat::disable_config_test or $skip_validation) ? { - true => undef, - default => $major_version ? { - '7' => "\"${filebeat_path}\" test config -c \"%\"", - default => "\"${filebeat_path}\" -N -configtest -c \"%\"", - } + if $filebeat::chocolatey_provider { + + $filebeat_path = 'C:\ProgramData\chocolatey\lib\filebeat\tools\filebeat.exe' + + } else { + + $cmd_install_dir = regsubst($filebeat::install_dir, '/', '\\', 'G') + $filebeat_path = join([$cmd_install_dir, 'Filebeat', 'filebeat.exe'], '\\') } file {'filebeat.yml': @@ -211,6 +211,16 @@ require => File['filebeat-config-dir'], } + $validate_cmd = ($filebeat::disable_config_test or $skip_validation) ? { + true => undef, + default => $major_version ? { + '7' => "\"${filebeat_path}\" test config -c \"%\"", + default => "\"${filebeat_path}\" -N -configtest -c \"%\"", + } + } + + + file {'filebeat-config-dir': ensure => $filebeat::directory_ensure, path => $filebeat::config_dir, diff --git a/manifests/init.pp b/manifests/init.pp index 82994ce..1159327 100755 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -100,6 +100,8 @@ 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, + Boolean $chocolatey_provider = false, + Optional[String[1]] $package_name = $filebeat::params::package_name, ) inherits filebeat::params { diff --git a/manifests/install/windows.pp b/manifests/install/windows.pp index 7b83145..d47e898 100644 --- a/manifests/install/windows.pp +++ b/manifests/install/windows.pp @@ -10,73 +10,83 @@ # 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::chocolatey_provider { + package { $filebeat::package_name: + ensure => $filebeat::chocolatey_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..89dd772 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -32,7 +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' + $chocolatey_basedir = "C:/ProgramData/chocolatey/lib/filebeat/tools" # These are irrelevant as long as the template is set based on the major_version parameter # if versioncmp('1.9.1', $::rubyversion) > 0 { # $conf_template = "${module_name}/filebeat.yml.ruby18.erb" @@ -123,15 +124,23 @@ } 'Windows' : { - $package_ensure = '7.1.0' + $path_exists = find_file($chocolatey_basedir) + if $path_exists { + $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' + } else { + $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' + $package_ensure = '7.1.0' $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 ? {