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

Feature/opensource puppet #18

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puppet
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source 'https://rubygems.org'

puppetversion = ENV.key?('PUPPET_VERSION') ? "#{ENV['PUPPET_VERSION']}" : ['>= 3.3']
facterversion = ENV.key?('FACTER_VERSION') ? "#{ENV['FACTER_VERSION']}" : ['>= 1.7.0']

gem 'rake', '< 11.0'
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.8.2'
#Enable linting
Expand All @@ -13,5 +15,4 @@ gem 'metadata-json-lint'
gem 'coveralls', require: false
#Tools to deploy automatically to the Puppet Forge
gem 'puppet-blacksmith'
gem 'slack-notifier'

gem 'slack-notify'

Choose a reason for hiding this comment

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

This was the line which needed a version specifier such as '~> 1.5.1'.

65 changes: 62 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,52 @@ GEM
remote: https://rubygems.org/
specs:
CFPropertyList (2.2.8)
coveralls (0.8.21)
json (>= 1.8, < 3)
simplecov (~> 0.14.1)
term-ansicolor (~> 1.3)
thor (~> 0.19.4)
tins (~> 1.6)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
facter (2.4.4)
CFPropertyList (~> 2.2.6)
faraday (0.12.1)
multipart-post (>= 1.2, < 3)
fast_gettext (1.1.0)
gettext (3.2.2)
locale (>= 2.0.5)
text (>= 1.3.0)
gettext-setup (0.24)
fast_gettext (~> 1.1.0)
gettext (>= 3.0.2)
locale
hiera (3.0.1)
json_pure
http-cookie (1.0.3)
domain_name (~> 0.5)
json (1.8.6)
json_pure (1.8.2)
locale (2.1.2)
metaclass (0.0.4)
metadata-json-lint (1.1.0)
json
semantic_puppet (>= 0.1.2, < 2.0.0)
spdx-licenses (~> 1.0)
mime-types (2.99.3)
mocha (1.1.0)
metaclass (~> 0.0.1)
multipart-post (2.0.0)
netrc (0.11.0)
puppet (4.2.2)
facter (> 2.0, < 4)
hiera (>= 2.0, < 4)
json_pure
puppet-blacksmith (3.4.0)
puppet (>= 2.7.16)
rest-client (~> 1.8.0)
puppet-lint (1.1.0)
puppet-syntax (2.0.0)
rake
Expand All @@ -25,6 +58,10 @@ GEM
rake
rspec-puppet
rake (10.4.2)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
Expand All @@ -40,17 +77,39 @@ GEM
rspec-puppet (2.2.0)
rspec
rspec-support (3.3.0)
slack-notifier (1.5.1)
semantic_puppet (1.0.0)
gettext-setup (>= 0.3)
simplecov (0.14.1)
docile (~> 1.1.0)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.1)
slack-notify (0.4.1)
faraday (~> 0.9)
json (~> 1.8)
spdx-licenses (1.1.0)
term-ansicolor (1.6.0)
tins (~> 1.0)
text (1.3.1)
thor (0.19.4)
tins (1.14.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)

PLATFORMS
ruby

DEPENDENCIES
coveralls
facter (>= 1.7.0)
metadata-json-lint
puppet (>= 3.3)
puppet-blacksmith
puppet-lint (>= 1.0.0)
puppetlabs_spec_helper (>= 0.8.2)
slack-notifier
rake (< 11.0)
slack-notify

BUNDLED WITH
1.10.6
1.14.3
67 changes: 44 additions & 23 deletions lib/puppet/reports/slack.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
require 'puppet'
require 'yaml'
require 'slack-notifier'
require 'slack-notify'

Puppet::Reports.register_report(:slack) do
if (Puppet.settings[:config]) then
configfile = File.join([File.dirname(Puppet.settings[:config]), "slack.yaml"])
else
configfile = "/etc/puppetlabs/puppet/slack.yaml"
end
Puppet.debug "Reading #{configfile}"
raise(Puppet::ParseError, "Slack report config file #{configfile} not readable") unless File.exist?(configfile)
config = YAML.load_file(configfile)
SLACK_WEBHOOK_URL = config['webhook_url']
Puppet.debug "Webhook is #{SLACK_WEBHOOK_URL}"
SLACK_CHANNEL = config['channel']
PUPPETCONSOLE = config['puppetconsole']
DISABLED_FILE = File.join([File.dirname(Puppet.settings[:config]), 'slack_disabled'])
desc "A custom reports processor to send notifications to a Slack channel"

def process
disabled = File.exists?(DISABLED_FILE)
if (Puppet.settings[:config]) then
@configfile = File.join([File.dirname(Puppet.settings[:config]), "slack.yaml"])
else
@configfile = "/etc/puppetlabs/puppet/slack.yaml"
end
Puppet.debug "Reading #{@configfile}"
raise(Puppet::ParseError, "Slack report config file #{@configfile} not readable") unless File.exist?(@configfile)
@config = YAML.load_file(@configfile)
raise(Puppet::ParseError, "Unable to parse the YAML file #{@configfile}") if @config.nil?
# Prepare Constant variables to be used within the process definition
SLACK_WEBHOOK_URL = @config['webhook_url']
SLACK_CHANNEL = @config['channel']
SLACK_USERNAME = @config['username'] || ''
SLACK_ICON_URL = @config['icon_url'] || ''
SLACK_ICON_EMOJI = @config['icon_emoji'] || ''
PUPPETCONSOLE = @config['puppetconsole'] || ''
PUPPETBOARD = @config['puppetboard'] || ''
# FOREMAN = @config['foreman'] || ''

if (!disabled && self.status != 'unchanged')
Puppet.debug "Sending notification for #{self.host} to Slack channel #{SLACK_CHANNEL}"
msg = "Puppet run for #{self.host} #{self.status} at #{Time.now.asctime} on #{self.configuration_version} in #{self.environment}. Report available <https://#{PUPPETCONSOLE}/#/node_groups/inventory/node/#{self.host}/reports|here>"
notifier = Slack::Notifier.new SLACK_WEBHOOK_URL, channel: SLACK_CHANNEL, username: 'Puppet'
notifier.ping msg, icon_url: "https://puppetlabs.com/wp-content/uploads/2010/12/PL_logo_vertical_RGB_lg.jpg"
end
end
DISABLED_FILE = "#{@configfile}.disabled"

def process
disabled = File.exists?(DISABLED_FILE)

if (!disabled && self.status != 'unchanged')
Puppet.debug "Sending notification for #{self.host} to Slack channel #{SLACK_CHANNEL}"
options = {
webhook_url: SLACK_WEBHOOK_URL,
channel: SLACK_CHANNEL
}
options[:username] = SLACK_USERNAME unless SLACK_USERNAME.empty?
options[:icon_url] = SLACK_ICON_URL unless SLACK_ICON_URL.empty?
options[:icon_emoji] = SLACK_ICON_EMOJI unless SLACK_ICON_EMOJI.empty?
# Prepare the message to display
report_url = "https://#{PUPPETCONSOLE}/#/node_groups/inventory/node/#{self.host}/reports" unless PUPPETCONSOLE.empty?
report_url = "https://#{PUPPETBOARD}/node/#{self.host}" unless PUPPETBOARD.empty?

msg = "Puppet run for `#{self.host}` *#{self.status}* at #{Time.now.asctime} on #{self.configuration_version} in environment _#{self.environment}_. Report available <#{report_url}|here>."
# TODO: adapt the link
client = SlackNotify::Client.new(options)
client.notify(msg)
end
end
end
78 changes: 61 additions & 17 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
# Authors
# -------
# -*- mode: puppet; -*-
# Time-stamp: <Sat 2017-05-27 18:27 svarrette>
# ------------------------------------------------------------------------------
# Class reportslack
#
# Nicolas Corrarello <[email protected]>
# @summary A custom report processor to send notifications to slack
#
# @param webhook [String]
# Slack Incoming WebHooks URL -- to be generated from your Slack team App Directory
# (Manage / Custom Integrations / Incoming WebHooks)
# @param ensure [String] Default: 'ensure'
# @param channel [String] Default: '#general'
# Default channel to send messages to
# @param puppetconsole [String]
# @param username [String] Default: 'Puppet'
# Username that this integration will post as.
# @param icon_url [String]
# Icon that is used for messages from this integration.
# @param icon_emoji [String] Default
# EMoji that is used for messages from this integration.
# @param puppetboard [String]
# base hostname of the puppetboard, typically "<fqdn>:<port>"
#
# @author Nicolas Corrarello <[email protected]>,
# Sebastien Varrette aka Falkor <[email protected]>
#
# Copyright
# ---------
#
# Copyright 2016 Nicolas Corrarello, unless otherwise noted.
# Copyright 2016-2017 Nicolas Corrarello, Sebastien Varrette unless otherwise noted.
#
class reportslack (
$webhook,
$channel,
$puppetconsole = $settings::ca_server,
) {
String $webhook,
Enum[
'present',
'absent'
] $ensure = $reportslack::params::ensure,
String $channel = $reportslack::params::channel,
String $puppetconsole = $settings::ca_server,
String $username = $reportslack::params::username,
String $icon_url = $reportslack::params::icon_url,
String $icon_emoji = $reportslack::params::icon_emoji,
String $puppetboard = $reportslack::params::puppetboard
)
inherits reportslack::params
{
validate_re($webhook, 'https:\/\/hooks.slack.com\/(services\/)?T.+\/B.+\/.+', 'The webhook URL is invalid')
validate_re($channel, '#.+', 'The channel should start with a hash sign')

package { 'slack-notifier':
ensure => latest,
# $gem_ensure = $ensure ? {
# 'absent' => $ensure,
# default => 'latest',
# }
package { 'slack-notify':
ensure => $ensure, # Accelerates the agent process time
name => $reportslack::params::gemname,
provider => 'puppetserver_gem'
}

Expand All @@ -30,7 +66,7 @@
}

ini_subsetting { 'slack_report_handler':
ensure => present,
ensure => $ensure,
path => "${settings::confdir}/puppet.conf",
section => 'master',
setting => 'reports',
Expand All @@ -39,12 +75,20 @@
require => Ini_setting['enable_reports'],
}

file { "${settings::confdir}/slack.yaml":
ensure => present,
owner => 'pe-puppet',
group => 'pe-puppet',
mode => '0644',
if defined(Package['pe-puppet']) {
$owner = 'pe-puppet'
$group = 'pe-puppet'
}
else {
$owner = $reportslack::params::configfile_owner
$group = $reportslack::params::configfile_group
}
file { $reportslack::params::configfile:
ensure => $ensure,
owner => $owner,
group => $group,
mode => $reportslack::params::configfile_mode,
content => template('reportslack/slack.yaml.erb'),
require => Package['slack-notifier'],
require => Package['slack-notify'],
}
}
60 changes: 60 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- mode: puppet; -*-
# Time-stamp: <Sat 2017-05-27 18:27 svarrette>
# ------------------------------------------------------------------------------
# = Class: reportslack::params
#
# In this class are defined as variables values that are used in other
# reportslack classes.
# This class should be included, where necessary, and eventually be enhanced
# with support for more OS
#
# == Warnings
#
# /!\ Always respect the style guide available
# here[http://docs.puppetlabs.com/guides/style_guide]
#
# The usage of a dedicated param classe is advised to better deal with
# parametrized classes, see
# http://docs.puppetlabs.com/guides/parameterized_classes.html
#
# [Remember: No empty lines between comments and class definition]
#
class reportslack::params {

######## DEFAULTS FOR VARIABLES USERS CAN SET ##########################
# (Here are set the defaults, provide your custom variables externally)
# (The default used is in the line with '')
###########################################

# ensure the presence (or absence) of reportslack
$ensure = 'present'
$channel = '#general'
$username = 'Puppet'
$icon_url = ''
# 'https://learn.puppet.com/static/images/logos/Puppet-Logo-Mark-Amber.png'
$icon_emoji = ''
$puppetboard = ''

#### MODULE INTERNAL VARIABLES #########
# (Modify to adapt to unsupported OSes)
#######################################
$gemname = $::operatingsystem ? {
default => 'slack-notify',
}

$configfile = $::operatingsystem ? {
default => "${settings::confdir}/slack.yaml",
}
$configfile_mode = $::operatingsystem ? {
default => '0640',
}

$configfile_owner = $::operatingsystem ? {
default => 'puppet',
}

$configfile_group = $::operatingsystem ? {
default => 'puppet',
}

}
Loading