-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from jonathanio/extension-type
Added support for loading and configuring extensions.
- Loading branch information
Showing
6 changed files
with
244 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3' | ||
require 'json' if Puppet.features.json? | ||
|
||
Puppet::Type.type(:sensu_extension).provide(:json) do | ||
confine :feature => :json | ||
|
||
def conf | ||
begin | ||
@conf ||= JSON.parse(File.read(config_file)) | ||
rescue | ||
@conf ||= {} | ||
end | ||
end | ||
|
||
def flush | ||
File.open(config_file, 'w') do |f| | ||
f.puts JSON.pretty_generate(conf) | ||
end | ||
end | ||
|
||
def create | ||
conf[resource[:name]] = {} | ||
self.config = resource[:config] | ||
end | ||
|
||
def config_file | ||
"#{resource[:base_path]}/#{resource[:name]}.json" | ||
end | ||
|
||
def destroy | ||
conf.delete resource[:name] | ||
end | ||
|
||
def exists? | ||
conf.has_key? resource[:name] | ||
end | ||
|
||
def config | ||
conf[resource[:name]] | ||
end | ||
|
||
def config=(value) | ||
conf[resource[:name]] = value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Puppet::Type.newtype(:sensu_extension) do | ||
@doc = "" | ||
|
||
def initialize(*args) | ||
super | ||
|
||
self[:notify] = [ | ||
"Service[sensu-api]", | ||
"Service[sensu-server]", | ||
].select { |ref| catalog.resource(ref) } | ||
end | ||
|
||
ensurable do | ||
newvalue(:present) do | ||
provider.create | ||
end | ||
|
||
newvalue(:absent) do | ||
provider.destroy | ||
end | ||
|
||
defaultto :present | ||
end | ||
|
||
newparam(:name) do | ||
desc "This value has no effect, set it to what ever you want." | ||
end | ||
|
||
newparam(:base_path) do | ||
desc "The base path to the client config file" | ||
defaultto '/etc/sensu/conf.d/extensions/' | ||
end | ||
|
||
newproperty(:config) do | ||
desc "The configuration for this extension" | ||
defaultto {} | ||
end | ||
|
||
autorequire(:package) do | ||
['sensu'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# = Define: sensu::extension | ||
# | ||
# Defines Sensu extensions | ||
# | ||
# == Parameters | ||
# | ||
# [*ensure*] | ||
# String. Whether the check should be present or not | ||
# Default: present | ||
# Valid values: present, absent | ||
# | ||
# [*source*] | ||
# String. Source of the puppet extension | ||
# Default: undef | ||
# | ||
# [*install_path*] | ||
# String. Path to install the extension | ||
# Default: /etc/sensu/extensions | ||
# | ||
# [*config*] | ||
# Hash. Extension specific config | ||
# Default: undef | ||
# | ||
# | ||
define sensu::extension( | ||
$ensure = 'present', | ||
# Used to install the handler | ||
$source = undef, | ||
$install_path = '/etc/sensu/extensions', | ||
# Handler specific config | ||
$config = {}, | ||
) { | ||
|
||
validate_re($ensure, ['^present$', '^absent$'] ) | ||
validate_re($source, ['^puppet://'] ) | ||
|
||
if $sensu::client { | ||
$notify_services = Class['sensu::client::service'] | ||
} else { | ||
$notify_services = [] | ||
} | ||
|
||
$filename = inline_template('<%= scope.lookupvar(\'source\').split(\'/\').last %>') | ||
$handler = "${install_path}/${filename}" | ||
|
||
$file_ensure = $ensure ? { | ||
'absent' => 'absent', | ||
default => 'file' | ||
} | ||
|
||
file { $handler: | ||
ensure => $file_ensure, | ||
owner => 'sensu', | ||
group => 'sensu', | ||
mode => '0555', | ||
source => $source, | ||
} | ||
|
||
file { "/etc/sensu/conf.d/extensions/${name}.json": | ||
ensure => $ensure, | ||
owner => 'sensu', | ||
group => 'sensu', | ||
mode => '0444', | ||
before => Sensu_extension[$name], | ||
} | ||
|
||
sensu_extension { $name: | ||
ensure => $ensure, | ||
config => $config, | ||
notify => $notify_services, | ||
require => File['/etc/sensu/conf.d/extensions'], | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'spec_helper' | ||
|
||
describe 'sensu::extension', :type => :define do | ||
let(:title) { 'myextension' } | ||
|
||
context 'default (present)' do | ||
let(:params) { { | ||
:source => 'puppet:///somewhere/mycommand.rb' | ||
} } | ||
it { should contain_file('/etc/sensu/extensions/mycommand.rb').with_source('puppet:///somewhere/mycommand.rb')} | ||
it { should contain_file('/etc/sensu/conf.d/extensions/myextension.json') } | ||
it { should contain_sensu_extension('myextension').with( | ||
:ensure => 'present', | ||
:config => {} | ||
) } | ||
end | ||
|
||
context 'absent' do | ||
let(:facts) { { 'Class[sensu::service::server]' => true } } | ||
let(:params) { { | ||
:ensure => 'absent', | ||
:source => 'puppet:///somewhere/mycommand.rb' | ||
} } | ||
it { should contain_sensu_extension('myextension').with_ensure('absent') } | ||
it { should contain_file('/etc/sensu/conf.d/extensions/myextension.json').with_ensure('absent') } | ||
end | ||
|
||
context 'install path' do | ||
let(:params) { { | ||
:install_path => '/etc', | ||
:source => 'puppet:///mycommand.rb' | ||
} } | ||
it { should contain_file('/etc/mycommand.rb') } | ||
end | ||
|
||
context 'source' do | ||
let(:params) { { | ||
:source => 'puppet:///sensu/extension/script.rb' | ||
} } | ||
it { should contain_file('/etc/sensu/extensions/script.rb').with( | ||
:ensure => 'file', | ||
:source => 'puppet:///sensu/extension/script.rb' | ||
) } | ||
end | ||
|
||
context 'source and config' do | ||
let(:params) { { | ||
:source => 'puppet:///sensu/extension/script.rb', | ||
:config => {'param' => 'value'} | ||
} } | ||
it { should contain_file('/etc/sensu/extensions/script.rb').with( | ||
:ensure => 'file', | ||
:source => 'puppet:///sensu/extension/script.rb' | ||
) } | ||
it { should contain_sensu_extension('myextension').with_config( {'param' => 'value'} ) } | ||
end | ||
|
||
end |