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

Refactor defined type Splunk::Addon #224

Merged
merged 4 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn’t be necessary if it’s a requirement that one of splunk or splunk:forwarder has been included already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that should be correct. Didn't dawn me at the time.


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

Choose a reason for hiding this comment

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

Is it worth checking that Class['splunk'] has been defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, splunk::enterprise then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't hurt.

$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]') }
Copy link
Member

Choose a reason for hiding this comment

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

This is unreachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct and the reason I didn't originally add the default statement. Only added it to get it to pass lint checks. Not a bad thing to have, I suppose...?

If the manifest parses this far then something strange is happening an it'd be good to explicitly fail before damage is done.

}
}

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 {
Copy link
Member

Choose a reason for hiding this comment

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

Why? Iterating an empty hash is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct. Just didn't realize. Tested just now and works fine without testing first

$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.