Skip to content

Commit

Permalink
Merge pull request #284 from pancentric/master
Browse files Browse the repository at this point in the history
Flapjack support for puppet
  • Loading branch information
jamtur01 committed Dec 12, 2014
2 parents 3e5e23c + 4dd74e9 commit 940a419
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 1 deletion.
63 changes: 63 additions & 0 deletions lib/puppet/provider/sensu_flapjack_config/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3'
require 'json' if Puppet.features.json?

Puppet::Type.type(:sensu_flapjack_config).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['flapjack'] = {}
self.port = resource[:port]
self.host = resource[:host]
self.db = resource[:db]
end

def config_file
"#{resource[:base_path]}/flapjack.json"
end

def destroy
conf.delete 'flapjack'
end

def exists?
conf.has_key? 'flapjack'
end

def port
conf['flapjack']['port'].to_s
end

def port=(value)
conf['flapjack']['port'] = value.to_i
end

def host
conf['flapjack']['host']
end

def host=(value)
conf['flapjack']['host'] = value
end

def db
conf['flapjack']['db']
end

def db=(value)
conf['flapjack']['db'] = value.to_i
end
end
55 changes: 55 additions & 0 deletions lib/puppet/type/sensu_flapjack_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Puppet::Type.newtype(:sensu_flapjack_config) 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/'
end

newproperty(:port) do
desc "The port that Flapjack's Redis instance is listening on"

defaultto '6380'
end

newproperty(:host) do
desc "The hostname that Flapjack's Redis instance is listening on"

defaultto 'localhost'
end

newproperty(:db) do
desc "The Flapjack Redis DB to use"

defaultto '0'
end

autorequire(:package) do
['sensu']
end
end
45 changes: 45 additions & 0 deletions manifests/flapjack/config.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# = Class: sensu::flapjack::config
#
# Sets the Sensu flapjack config
#
class sensu::flapjack::config {

if $caller_module_name != $module_name {
fail("Use of private class ${name} by ${caller_module_name}")
}

if $sensu::purge_config and !$sensu::server and !$sensu::api {
$ensure = 'absent'
} else {
$ensure = 'present'
}

file { '/etc/sensu/conf.d/flapjack.json':
ensure => $ensure,
owner => 'sensu',
group => 'sensu',
mode => '0444',
}

exec { 'flapjack.rb':
command => 'wget -O /etc/sensu/extensions/flapjack.rb https://raw.githubusercontent.com/sensu/sensu-community-plugins/master/extensions/handlers/flapjack.rb',
creates => '/etc/sensu/extensions/flapjack.rb',
path => '/usr/bin:/usr/sbin:/bin:/usr/local/bin',
before => File['/etc/sensu/extensions/flapjack.rb'],
}

file { '/etc/sensu/extensions/flapjack.rb':
ensure => $ensure,
owner => 'sensu',
group => 'sensu',
mode => '0444',
}

sensu_flapjack_config { $::fqdn:
ensure => $ensure,
host => $sensu::flapjack_redis_host,
port => $sensu::flapjack_redis_port,
db => $sensu::flapjack_redis_db,
}

}
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@
$rabbitmq_ssl_cert_chain = undef,
$redis_host = 'localhost',
$redis_port = 6379,
$flapjack_redis_host = 'localhost',
$flapjack_redis_port = 6380,
$flapjack_redis_db = 0,
$api_bind = '0.0.0.0',
$api_host = 'localhost',
$api_port = 4567,
Expand Down Expand Up @@ -277,6 +280,7 @@
class { 'sensu::rabbitmq::config': } ->
class { 'sensu::api::config': } ->
class { 'sensu::redis::config': } ->
class { 'sensu::flapjack::config': } ->
class { 'sensu::client::config': } ->
class { 'sensu::client::service': } ->
class { 'sensu::api::service': } ->
Expand Down
2 changes: 1 addition & 1 deletion manifests/server/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ensure => $ensure,
enable => $enable,
hasrestart => true,
subscribe => [ Class['sensu::package'], Class['sensu::api::config'], Class['sensu::redis::config'], Class['sensu::rabbitmq::config'] ],
subscribe => [ Class['sensu::package'], Class['sensu::api::config'], Class['sensu::redis::config'], Class['sensu::rabbitmq::config'], Class['sensu::flapjack::config'] ],
}
}
}
51 changes: 51 additions & 0 deletions spec/classes/sensu_flapjack_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe 'sensu' do
let(:facts) { { :fqdn => 'testhost.domain.com', :osfamily => 'RedHat' } }

context 'flapjack config' do

context 'default settings' do
it { should contain_sensu_flapjack_config('testhost.domain.com').with(
:host => 'localhost',
:port => 6380
)}
end # default settings

context 'be configurable' do
let(:params) { {
:flapjack_redis_host => 'flapjack.domain.com',
:flapjack_redis_port => 1234,
:flapjack_redis_db => 2
} }

it { should contain_sensu_flapjack_config('testhost.domain.com').with(
:host => 'flapjack.domain.com',
:port => 1234,
:db => 2
)}
end # be configurable

context 'with server' do
let(:params) { { :server => true } }
it { should contain_file('/etc/sensu/conf.d/flapjack.json').with_ensure('present') }
end # with server

context 'with api' do
let(:params) { { :api => true } }
it { should contain_file('/etc/sensu/conf.d/flapjack.json').with_ensure('present') }
end # with api

context 'purge configs' do
let(:params) { {
:purge_config => true,
:server => false,
:api => false,
} }

it { should contain_file('/etc/sensu/conf.d/flapjack.json').with_ensure('absent') }
end # purge configs

end #flapjack config

end

0 comments on commit 940a419

Please sign in to comment.