Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP : Add chocolatey support #237

Closed
wants to merge 20 commits into from
10 changes: 9 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I would expect this to use exec by default as one of the params. $filebeat::params::package_provider, but I can see why maybe not drop it into the params since it would not be platform agnostic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I want exec by default and chocolatey as an option. Not sure what I can do here ? I want to let the choice to the user. what do you recommend ?

Optional[String[1]] $package_name = $filebeat::params::package_name,
hdep marked this conversation as resolved.
Show resolved Hide resolved
) 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,
Expand Down
9 changes: 7 additions & 2 deletions manifests/input.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"$env:ChocolateyInstall/bin/filebeat.exe" (needs ruby code for environment) - also I'm pointing to the shim in the bin folder, but you can stick with lib if you want.

}

$validate_cmd = ($filebeat::disable_config_test or $skip_validation) ? {
true => undef,
Expand Down
2 changes: 1 addition & 1 deletion manifests/install/linux.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
fail('filebeat::install::linux shouldn\'t run on Windows')
}

package {'filebeat':
package { $filebeat::package_name:
ensure => $filebeat::package_ensure,
}
}
2 changes: 1 addition & 1 deletion manifests/install/openbsd.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# to manage filebeat installation on OpenBSD
class filebeat::install::openbsd {
package {'filebeat':
package { $filebeat::package_name:
ensure => $filebeat::package_ensure,
}
}
124 changes: 66 additions & 58 deletions manifests/install/windows.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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}"],
}
}
}
22 changes: 16 additions & 6 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'] {
Expand Down Expand Up @@ -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 ? {
Expand Down