Skip to content

Commit

Permalink
Add chocolatey support
Browse files Browse the repository at this point in the history
  • Loading branch information
hdep committed Jan 15, 2020
1 parent 9db03fe commit cd9cc86
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 62 deletions.
5 changes: 4 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,7 +102,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,

Variant[String, Enum['exec', 'chocolatey']] $package_provider = $filebeat::params::package_provider,
Optional[String[1]] $package_name = $filebeat::params::package_name,
) inherits filebeat::params {

include ::stdlib
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::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}"],
}
}
}
4 changes: 3 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
$xpack = undef
$systemd_override_dir = '/etc/systemd/system/filebeat.service.d'
$systemd_beat_log_opts_template = "${module_name}/systemd/logging.conf.erb"
$package_provider = 'exec'
$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 +43,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

0 comments on commit cd9cc86

Please sign in to comment.