From 0bf26e27b610ebcfe085916a4a8962c0c716cc24 Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Mon, 28 Dec 2015 12:41:13 +0100 Subject: [PATCH 1/9] added windows support --- manifests/config.pp | 57 +++++++++++++++++++++++++++++------------ manifests/init.pp | 32 ++++++++++++++++++----- manifests/install.pp | 42 ++++++++++++++++++++++++++++++ manifests/params.pp | 19 ++++++++++++++ manifests/prospector.pp | 32 +++++++++++++++++------ manifests/repo.pp | 2 +- 6 files changed, 152 insertions(+), 32 deletions(-) create mode 100644 manifests/install.pp diff --git a/manifests/config.pp b/manifests/config.pp index 7b6f944..9c75076 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -17,23 +17,46 @@ default => 'filebeat.yml.erb', } - file {'filebeat.yml': - ensure => file, - path => '/etc/filebeat/filebeat.yml', - content => template("${module_name}/${template_file}"), - owner => 'root', - group => 'root', - mode => '0644', - notify => Service['filebeat'], - } + case $::kernel { + 'Linux' : { + file {'filebeat.yml': + ensure => file, + path => '/etc/filebeat/filebeat.yml', + content => template("${module_name}/${template_file}"), + owner => 'root', + group => 'root', + mode => '0644', + notify => Service['filebeat'], + } + + file {'filebeat-config-dir': + ensure => directory, + path => $win_filebeat::config_dir, + owner => 'root', + group => 'root', + mode => '0755', + recurse => $win_filebeat::purge_conf_dir, + purge => $win_filebeat::purge_conf_dir, + } + } # end Linux + + 'Windows' : { + file {'filebeat.yml': + ensure => file, + path => 'C:/Program Files/Filebeat/filebeat.yml', + content => template("${module_name}/${template_file}"), + notify => Service['filebeat'], + } - file {'filebeat-config-dir': - ensure => directory, - path => $filebeat::config_dir, - owner => 'root', - group => 'root', - mode => '0755', - recurse => $filebeat::purge_conf_dir, - purge => $filebeat::purge_conf_dir, + file {'filebeat-config-dir': + ensure => directory, + path => $win_filebeat::config_dir, + recurse => $win_filebeat::purge_conf_dir, + purge => $win_filebeat::purge_conf_dir, + } + } # end Windows + default : { + fail($filebeat::fail_message) + } } } diff --git a/manifests/init.pp b/manifests/init.pp index 7877b11..053fb3d 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -46,17 +46,37 @@ validate_string($idle_timeout, $registry_file, $config_dir, $package_ensure) anchor { 'filebeat::begin': } -> - class { 'filebeat::package': } -> class { 'filebeat::config': } -> class { 'filebeat::service': } -> anchor { 'filebeat::end':} - if $manage_repo { - include filebeat::repo + case $::kernel { + 'Linux' : { + include win_filebeat::package + if $manage_repo { + include win_filebeat::repo - Anchor['filebeat::begin'] -> - Class['filebeat::repo'] -> - Class['filebeat::package'] + Anchor['win_filebeat::begin'] -> + Class['win_filebeat::repo'] -> + Class['win_filebeat::package'] -> + Class['win_filebeat::config'] + } + else { + Anchor['win_filebeat::begin'] -> + Class['win_filebeat::package'] -> + Class['win_filebeat::config'] + } + } + 'Windows' : { + include win_filebeat::install + + Anchor['win_filebeat::begin'] -> + Class['win_filebeat::install'] -> + Class['win_filebeat::config'] + } + default: { + fail($filebeat::fail_message) + } } if !empty($prospectors) { diff --git a/manifests/install.pp b/manifests/install.pp new file mode 100644 index 0000000..20bae20 --- /dev/null +++ b/manifests/install.pp @@ -0,0 +1,42 @@ +class filebeat::install ( + $download_url = $filebeat::download_url, + $install_dir = $filebeat::install_dir, + $tmp_dir = $filebeat::tmp_dir, + +) { + $filename = regsubst($download_url, '^https.*\/([^\/]+)\.[^.].*', '\1') + $foldername = 'Filebeat' + + file { $tmp_dir: + ensure => directory + } + file { $install_dir: + ensure => directory + } + + exec { "download ${filename}": + command => "(New-Object System.Net.WebClient).DownloadFile('${download_url}', '${tmp_dir}/${filename}.zip')", + onlyif => "if(Test-Path -Path '${tmp_dir}/${filename}.zip') { exit 1 } else { exit 0 }", + provider => powershell, + require => File[$tmp_dir] + } + exec { "unzip ${filename}": + command => "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path '${install_dir}')).Copyhere(\$sh.namespace((Convert-Path '${tmp_dir}/${filename}.zip')).items(), 16)", + creates => "${install_dir}/Filebeat", + provider => powershell, + require => [Exec["download ${filename}"],File[$install_dir]], + } + exec { "rename folder": + command => "Rename-Item '${install_dir}/${filename}' Filebeat", + creates => "${install_dir}/Filebeat", + provider => powershell, + require => Exec["unzip ${filename}"], + } + exec { "install ${filename}": + cwd => "${install_dir}/Filebeat", + command => "./install-service-filebeat.ps1", + onlyif => "if(Get-WmiObject -Class Win32_Service -Filter \"Name='filebeat'\") { exit 1 } else {exit 0 }", + provider => powershell, + require => Exec["rename folder"], + } +} \ No newline at end of file diff --git a/manifests/params.pp b/manifests/params.pp index 94ad505..516a183 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -11,4 +11,23 @@ $outputs = {} $shipper = {} $logging = {} + + $fail_message = "${::kernel} is not yet supported by this module." + + case $::kernel { + 'Linux' : { + $config_dir = '/etc/filebeat/conf.d' + } + + 'Windows' : { + $config_dir = 'C:/Program Files/Filebeat/conf.d' + $download_url = 'https://download.elastic.co/beats/filebeat/filebeat-1.0.1-windows.zip' + $install_dir = 'C:/Program Files' + $tmp_dir = 'C:/Temp' + } + + default : { + fail($fail_message) + } + } } diff --git a/manifests/prospector.pp b/manifests/prospector.pp index 0876efa..091016a 100644 --- a/manifests/prospector.pp +++ b/manifests/prospector.pp @@ -16,13 +16,29 @@ $partial_line_waiting = '5s', $force_close_files = false, ) { - file { "filebeat-${name}": - ensure => $ensure, - path => "${filebeat::config_dir}/${name}.yml", - owner => 'root', - group => 'root', - mode => '0644', - content => template("${module_name}/prospector.yml.erb"), - notify => Service['filebeat'], + + case $::kernel { + 'Linux' : { + file { "filebeat-${name}": + ensure => $ensure, + path => "${win_filebeat::config_dir}/${name}.yml", + owner => 'root', + group => 'root', + mode => '0644', + content => template("${module_name}/prospector.yml.erb"), + notify => Service['filebeat'], + } + } + 'Windows' : { + file { "filebeat-${name}": + ensure => $ensure, + path => "${win_filebeat::config_dir}/${name}.yml", + content => template("${module_name}/prospector.yml.erb"), + notify => Service['filebeat'], + } + } + default : { + fail($fail_message) + } } } diff --git a/manifests/repo.pp b/manifests/repo.pp index 684d505..a9e43d8 100644 --- a/manifests/repo.pp +++ b/manifests/repo.pp @@ -42,7 +42,7 @@ } } default: { - fail("\"${module_name}\" provides no repository information for OSfamily \"${::osfamily}\"") + fail($fail_message) } } } From 9cc426f6154e9959d1055b000489c23332ef9b64 Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Mon, 28 Dec 2015 13:36:26 +0100 Subject: [PATCH 2/9] fixed small issue with config_dir --- manifests/init.pp | 2 +- manifests/params.pp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 053fb3d..518652e 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -48,7 +48,7 @@ anchor { 'filebeat::begin': } -> class { 'filebeat::config': } -> class { 'filebeat::service': } -> - anchor { 'filebeat::end':} + anchor { 'filebeat::end': } case $::kernel { 'Linux' : { diff --git a/manifests/params.pp b/manifests/params.pp index 516a183..2385b48 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -6,7 +6,6 @@ $spool_size = 1024 $idle_timeout = '5s' $registry_file = '.filebeat' - $config_dir = '/etc/filebeat/conf.d' $purge_conf_dir = true $outputs = {} $shipper = {} @@ -26,7 +25,7 @@ $tmp_dir = 'C:/Temp' } - default : { + default : { fail($fail_message) } } From af6c384dde28bfe19e6dd9d29be106a10bd02e95 Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Mon, 28 Dec 2015 13:51:33 +0100 Subject: [PATCH 3/9] fixed some minor issues in init.pp file --- manifests/init.pp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 518652e..21895df 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -52,27 +52,27 @@ case $::kernel { 'Linux' : { - include win_filebeat::package + include filebeat::package if $manage_repo { - include win_filebeat::repo + include filebeat::repo - Anchor['win_filebeat::begin'] -> - Class['win_filebeat::repo'] -> - Class['win_filebeat::package'] -> - Class['win_filebeat::config'] + Anchor['filebeat::begin'] -> + Class['filebeat::repo'] -> + Class['filebeat::package'] -> + Class['filebeat::config'] } else { - Anchor['win_filebeat::begin'] -> - Class['win_filebeat::package'] -> - Class['win_filebeat::config'] + Anchor['filebeat::begin'] -> + Class['filebeat::package'] -> + Class['filebeat::config'] } } 'Windows' : { - include win_filebeat::install + include filebeat::install - Anchor['win_filebeat::begin'] -> - Class['win_filebeat::install'] -> - Class['win_filebeat::config'] + Anchor['filebeat::begin'] -> + Class['filebeat::install'] -> + Class['filebeat::config'] } default: { fail($filebeat::fail_message) From 2ea66e8ad0fe80cff568dd2bb8648a2bd6afc4bb Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Mon, 28 Dec 2015 14:28:33 +0100 Subject: [PATCH 4/9] more minor issues fixed --- manifests/config.pp | 12 ++++++------ manifests/prospector.pp | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/manifests/config.pp b/manifests/config.pp index 9c75076..d897841 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -31,12 +31,12 @@ file {'filebeat-config-dir': ensure => directory, - path => $win_filebeat::config_dir, + path => $filebeat::config_dir, owner => 'root', group => 'root', mode => '0755', - recurse => $win_filebeat::purge_conf_dir, - purge => $win_filebeat::purge_conf_dir, + recurse => $filebeat::purge_conf_dir, + purge => $filebeat::purge_conf_dir, } } # end Linux @@ -50,9 +50,9 @@ file {'filebeat-config-dir': ensure => directory, - path => $win_filebeat::config_dir, - recurse => $win_filebeat::purge_conf_dir, - purge => $win_filebeat::purge_conf_dir, + path => $filebeat::config_dir, + recurse => $filebeat::purge_conf_dir, + purge => $filebeat::purge_conf_dir, } } # end Windows default : { diff --git a/manifests/prospector.pp b/manifests/prospector.pp index 091016a..3aa7fa6 100644 --- a/manifests/prospector.pp +++ b/manifests/prospector.pp @@ -21,7 +21,7 @@ 'Linux' : { file { "filebeat-${name}": ensure => $ensure, - path => "${win_filebeat::config_dir}/${name}.yml", + path => "${filebeat::config_dir}/${name}.yml", owner => 'root', group => 'root', mode => '0644', @@ -32,7 +32,7 @@ 'Windows' : { file { "filebeat-${name}": ensure => $ensure, - path => "${win_filebeat::config_dir}/${name}.yml", + path => "${filebeat::config_dir}/${name}.yml", content => template("${module_name}/prospector.yml.erb"), notify => Service['filebeat'], } From 8366ea9bb8b16ddfb2b5cfe8f1e39ac7c13f561b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 28 Dec 2015 23:53:16 +0100 Subject: [PATCH 5/9] passed the rake lint and syntax validation --- manifests/install.pp | 32 ++++++++++++++++---------------- manifests/prospector.pp | 2 +- manifests/repo.pp | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/manifests/install.pp b/manifests/install.pp index 20bae20..c87f5b0 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -1,13 +1,13 @@ class filebeat::install ( - $download_url = $filebeat::download_url, - $install_dir = $filebeat::install_dir, - $tmp_dir = $filebeat::tmp_dir, + $download_url = $filebeat::download_url, + $install_dir = $filebeat::install_dir, + $tmp_dir = $filebeat::tmp_dir, ) { - $filename = regsubst($download_url, '^https.*\/([^\/]+)\.[^.].*', '\1') + $filename = regsubst($download_url, '^https.*\/([^\/]+)\.[^.].*', '\1') $foldername = 'Filebeat' - file { $tmp_dir: + file { $tmp_dir: ensure => directory } file { $install_dir: @@ -15,28 +15,28 @@ } exec { "download ${filename}": - command => "(New-Object System.Net.WebClient).DownloadFile('${download_url}', '${tmp_dir}/${filename}.zip')", - onlyif => "if(Test-Path -Path '${tmp_dir}/${filename}.zip') { exit 1 } else { exit 0 }", - provider => powershell, - require => File[$tmp_dir] + command => "(New-Object System.Net.WebClient).DownloadFile('${download_url}', '${tmp_dir}/${filename}.zip')", + onlyif => "if(Test-Path -Path '${tmp_dir}/${filename}.zip') { exit 1 } else { exit 0 }", + provider => powershell, + require => File[$tmp_dir] } - exec { "unzip ${filename}": + exec { "unzip ${filename}": command => "\$sh=New-Object -COM Shell.Application;\$sh.namespace((Convert-Path '${install_dir}')).Copyhere(\$sh.namespace((Convert-Path '${tmp_dir}/${filename}.zip')).items(), 16)", creates => "${install_dir}/Filebeat", provider => powershell, require => [Exec["download ${filename}"],File[$install_dir]], } - exec { "rename folder": + exec { 'rename folder': command => "Rename-Item '${install_dir}/${filename}' Filebeat", creates => "${install_dir}/Filebeat", provider => powershell, require => Exec["unzip ${filename}"], } exec { "install ${filename}": - cwd => "${install_dir}/Filebeat", - command => "./install-service-filebeat.ps1", - onlyif => "if(Get-WmiObject -Class Win32_Service -Filter \"Name='filebeat'\") { exit 1 } else {exit 0 }", - provider => powershell, - require => Exec["rename folder"], + cwd => "${install_dir}/Filebeat", + command => './install-service-filebeat.ps1', + onlyif => "if(Get-WmiObject -Class Win32_Service -Filter \"Name='filebeat'\") { exit 1 } else {exit 0 }", + provider => powershell, + require => Exec['rename folder'], } } \ No newline at end of file diff --git a/manifests/prospector.pp b/manifests/prospector.pp index 3aa7fa6..3cce616 100644 --- a/manifests/prospector.pp +++ b/manifests/prospector.pp @@ -38,7 +38,7 @@ } } default : { - fail($fail_message) + fail($filebeat::fail_message) } } } diff --git a/manifests/repo.pp b/manifests/repo.pp index a9e43d8..c470df9 100644 --- a/manifests/repo.pp +++ b/manifests/repo.pp @@ -42,7 +42,7 @@ } } default: { - fail($fail_message) + fail($filebeat::fail_message) } } } From fa28e21eaa3d5962a23dde25866da19a9df0e9a6 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 29 Dec 2015 01:16:58 +0100 Subject: [PATCH 6/9] initial test --- spec/classes/filebeat_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/classes/filebeat_spec.rb b/spec/classes/filebeat_spec.rb index 87ae423..f65d2ff 100644 --- a/spec/classes/filebeat_spec.rb +++ b/spec/classes/filebeat_spec.rb @@ -3,6 +3,7 @@ describe 'filebeat', :type => :class do let :facts do { + :kernel => 'Linux', :osfamily => 'Debian', :lsbdistid => 'Ubuntu', :puppetversion => ENV['PUPPET_GEM_VERSION'], @@ -50,6 +51,25 @@ ) } end + describe 'on a Windows system' do + let :facts do + { + :kernel => 'Windows', + :puppetversion => ENV['PUPPET_GEM_VERSION'], + } + end + + it { is_expected.to contain_file('filebeat.yml').with( + :path => 'C:/Program Files/Filebeat/filebeat.yml', + )} + it { is_expected.to contain_file('filebeat-config-dir').with( + :ensure => 'directory', + :path => 'C:/Program Files/Filebeat/conf.d', + :recurse => true, + )} + end + + describe 'on a Solaris system' do let :facts do { From 14fd3b4f1535ffd0f92c4b2d944afad458afa210 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 29 Dec 2015 01:33:14 +0100 Subject: [PATCH 7/9] fixed testing issue --- spec/classes/filebeat_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/classes/filebeat_spec.rb b/spec/classes/filebeat_spec.rb index f65d2ff..befe099 100644 --- a/spec/classes/filebeat_spec.rb +++ b/spec/classes/filebeat_spec.rb @@ -40,6 +40,7 @@ describe 'on a RHEL system' do let :facts do { + :kernel => 'Linux', :osfamily => 'RedHat', :puppetversion => ENV['PUPPET_GEM_VERSION'], } From 32836fad61c3cf2e9c327d7a9dafae1a47aeaeb5 Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Tue, 29 Dec 2015 15:59:13 +0100 Subject: [PATCH 8/9] changed fixtures.yml, metadata,json and prospector_spec tests --- .fixtures.yml | 1 + metadata.json | 11 +++++ spec/defines/prospector_spec.rb | 71 +++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/.fixtures.yml b/.fixtures.yml index 3486053..aa1b858 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -2,5 +2,6 @@ fixtures: repositories: "stdlib": "git://github.com/puppetlabs/puppetlabs-stdlib.git" "apt": "git://github.com/puppetlabs/puppetlabs-apt.git" + "powershell": "git://github.com/puppetlabs/puppetlabs-powershell.git" symlinks: "filebeat": "#{source_dir}" diff --git a/metadata.json b/metadata.json index c3ad572..ea62703 100644 --- a/metadata.json +++ b/metadata.json @@ -23,6 +23,13 @@ "12.04", "14.04" ] + }, + { + "operatingsystem": "Windows", + "operatingsystemrelease": [ + "Server 2012", + "Server 2012 R2" + ] } ], "dependencies": [ @@ -33,6 +40,10 @@ { "name": "puppetlabs/apt", "version_requirement": ">=2.0.0 <3.0.0" + }, + { + "name": "puppetlabs/powershell", + "version_requirement": ">= 1.0.1 < 2.0.0" } ] } diff --git a/spec/defines/prospector_spec.rb b/spec/defines/prospector_spec.rb index 1333575..0919543 100644 --- a/spec/defines/prospector_spec.rb +++ b/spec/defines/prospector_spec.rb @@ -9,20 +9,26 @@ it { expect { should raise_error(Puppet::Error) } } end - context 'with file blobs set' do - let :params do - { - :paths => [ - '/var/log/apache2/*.log', - ], - :log_type => 'apache', - } + context 'When deploying on Linux system' do + let :facts do { + :kernel => 'Linux', + } end - it { is_expected.to contain_file('filebeat-apache-logs').with( - :path => '/apache-logs.yml', - :mode => '0644', - :content => 'filebeat: + context 'with file blobs set on Linux' do + let :params do + { + :paths => [ + '/var/log/apache2/*.log', + ], + :log_type => 'apache', + } + end + + it { is_expected.to contain_file('filebeat-apache-logs').with( + :path => '/apache-logs.yml', + :mode => '0644', + :content => 'filebeat: prospectors: - paths: - /var/log/apache2/*.log @@ -41,5 +47,46 @@ partial_line_waiting: 5s ', )} + end + end + + context 'When deploying on Windows system' do + let :facts do { + :kernel => 'Windows', + } + end + + context 'with file blobs set on Linux' do + let :params do + { + :paths => [ + 'C:/Program Files/Apache Software Foundation/Apache2.2/*.log', + ], + :log_type => 'apache', + } + end + + it { is_expected.to contain_file('filebeat-apache-logs').with( + :path => '/apache-logs.yml', + :content => 'filebeat: + prospectors: + - paths: + - C:/Program Files/Apache Software Foundation/Apache2.2/*.log + encoding: plain + fields_under_root: false + input_type: log + ignore_older: 24h + document_type: apache + scan_frequency: 10s + harvester_buffer_size: 16384 + tail_files: false + force_close_files: false + backoff: 1s + max_backoff: 10s + backoff_factor: 2 + partial_line_waiting: 5s +', + )} + end end end From 3fd12e055b6d75e36612c4b909e199f46847398a Mon Sep 17 00:00:00 2001 From: Axier Artola Date: Tue, 29 Dec 2015 16:08:14 +0100 Subject: [PATCH 9/9] added powershell module spec_helper_acceptance --- spec/spec_helper_acceptance.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index b389f22..4976660 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -19,6 +19,7 @@ hosts.each do |host| on host, puppet('module','install','puppetlabs-stdlib'), { :acceptable_exit_codes => [0,1] } on host, puppet('module','install','puppetlabs-apt'), { :acceptable_exit_codes => [0,1] } + on host, puppet('module','install','puppetlabs-powershell'), { :acceptable_exit_codes => [0,1] } end end end