Skip to content

Commit

Permalink
Merge pull request #224 from ody/refactor_addon_management
Browse files Browse the repository at this point in the history
Refactor defined type Splunk::Addon
  • Loading branch information
bastelfreak authored Apr 11, 2019
2 parents 1f0954d + 3964ea7 commit 820a15b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 66 deletions.
125 changes: 84 additions & 41 deletions manifests/addon.pp
Original file line number Diff line number Diff line change
@@ -1,72 +1,115 @@
# @summary Defined type for deploying Splunk Add-ons and Apps from either OS packages or via splunkbase compatible archives
#
# Defined type: splunk::addon
#
# This define sets up a TA (Technology Addon) for Splunk. It (optionally)
# installed a package, and configures input forwarders in
# $SPLUNK_HOME/etc/apps/<app name>/local/inputs.conf
# @example Basic usage
# splunk::addon { 'Splunk_TA_nix':
# splunkbase_source => 'puppet:///modules/splunk_qd/addons/splunk-add-on-for-unix-and-linux_602.tgz',
# inputs => {
# 'monitor:///var/log' => {
# 'whitelist' => '(\.log|log$|messages|secure|auth|mesg$|cron$|acpid$|\.out)',
# 'blacklist' => '(lastlog|anaconda\.syslog)',
# 'disabled' => 'false'
# },
# 'script://./bin/uptime.sh' => {
# 'disabled' => 'false',
# 'interval' => '86400',
# 'source' => 'Unix:Uptime',
# 'sourcetype' => 'Unix:Uptime'
# }
# }
# }
#
# Examples
# @see https://docs.splunk.com/Documentation/AddOns/released/Overview/AboutSplunkadd-ons
#
# splunk::addon { 'search':
# package_manage => false,
# }
# @param splunk_home
# Overrides the default Splunk installation target values from Class[splunk::params]
#
# splunk::addon::input { 'monitor:///var/log/messages':
# attributes => {
# 'index' => 'server_t',
# },
# }
# @param package_manage
# If a package should be installed as part of declaring a new instance of Splunk::Addon
#
# Alternatively you can feed inputs directly into splunk::addon using the
# inputs parameter (useful if you are configuring from Hiera)
# @param splunkbase_source
# When set the add-on will be installed from a splunkbase compatible archive instead of OS packages
#
# @param package_name
# The OS package to install if you are not installing via splunk compatible archive
#
# splunk::addon { 'search':
# package_manage => false,
# inputs => {
# 'monitor:///var/log/messages' => {
# 'attributes' => {
# 'index' => 'server_t',
# }
# }
# }
# }
# @param inputs
# A hash of inputs to be configured as part of add-on installation, alterntively you can also define splunk_input or splunkforwarder_input resouces seperately
#
define splunk::addon (
Optional[Stdlib::Absolutepath] $splunk_home = undef,
Boolean $package_manage = true,
Optional[String[1]] $splunkbase_source = undef,
Optional[String[1]] $package_name = undef,
Hash $inputs = {},
) {

include 'splunk::params'
include splunk::params

if defined(Class['splunk::forwarder']) {
$mode = 'forwarder'
} else {
$mode = 'enterprise'
}

if $splunk_home {
$_splunk_home = $splunk_home
}
else {
$_splunk_home = $splunk::params::forwarder_homedir
case $mode {
'forwarder': { $_splunk_home = $splunk::params::forwarder_homedir }
'enterprise': { $_splunk_home = $splunk::params::enterprise_homedir }
default: { fail('Instances of Splunk::Addon require the declaration of one of either Class[splunk::enterprise] or Class[splunk::forwarder]') }
}
}

if $package_manage {
package { $package_name:
ensure => installed,
before => File["${_splunk_home}/etc/apps/${name}/local"],
if $splunkbase_source {
$archive_name = $splunkbase_source.split('/')[-1]
archive { $name:
path => "${splunk::params::staging_dir}/${archive_name}",
source => $splunkbase_source,
extract => true,
extract_path => "${_splunk_home}/etc/apps",
creates => "${_splunk_home}/etc/apps/${name}",
cleanup => true,
before => File["${_splunk_home}/etc/apps/${name}/local"],
}
} else {
package { $package_name:
ensure => installed,
before => File["${_splunk_home}/etc/apps/${name}/local"],
}
}
}

file { "${_splunk_home}/etc/apps/${name}/local":
ensure => directory,
}
file { "${_splunk_home}/etc/apps/${name}/local": ensure => directory }

if $inputs {
concat { "splunk::addon::inputs_${name}":
path => "${_splunk_home}/etc/apps/${name}/local/inputs.conf",
require => File["${_splunk_home}/etc/apps/${name}/local"],
unless $inputs.empty {
$inputs.each |$section, $attributes| {
$attributes.each |$setting, $value| {
case $mode {
'forwarder': {
splunkforwarder_input { "${name}_${section}_${setting}":
section => $section,
setting => $setting,
value => $value,
context => "apps/${name}/local",
require => File["${_splunk_home}/etc/apps/${name}/local"],
}
}
'enterprise': {
splunk_input { "${name}_${section}_${setting}":
section => $section,
setting => $setting,
value => $value,
context => "apps/${name}/local",
require => File["${_splunk_home}/etc/apps/${name}/local"],
}
}
default: { fail('Instances of Splunk::Addon require the declaration of one of either Class[splunk::enterprise] or Class[splunk::forwarder]') }
}
}
}

create_resources('splunk::addon::input', $inputs, {'addon' => $name })
}

}

17 changes: 0 additions & 17 deletions manifests/addon/input.pp

This file was deleted.

4 changes: 2 additions & 2 deletions spec/defines/addon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
let(:facts) do
facts
end
let(:title) { 'someaddon' }
let(:params) { { 'package_name' => 'foo' } }
let(:title) { 'Splunk_TA' }
let(:params) { { 'splunkbase_source' => 'puppet:///modules/profiles/splunk-add-on.tgz' } }

it { is_expected.to compile.with_all_deps }
end
Expand Down
6 changes: 0 additions & 6 deletions templates/addon/_input.erb

This file was deleted.

0 comments on commit 820a15b

Please sign in to comment.