-
Notifications
You must be signed in to change notification settings - Fork 4
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
Falkor
wants to merge
31
commits into
ncorrare:master
Choose a base branch
from
Falkor:feature/opensource_puppet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
d96e11d
rework main manifest and externalize params
59801f1
add SLACK_ICON and SLACK_USERNAME
6bc6a3a
update template accordingly
94cf27f
change order
5d6d889
syntax error correction
a395d0d
update params
ed7fc60
update dependencies to avoid the warning: The 'pe' requirement is no …
7bc6c65
untabify
5748b98
change default icon
87d5316
RVM ruby version matching puppet server
1daa52c
RVM gemset
edbfe2d
update author list
8764f20
bundle
c7f3007
restore Gemfile.lock
0c3421e
lint metadata.json
5ba729d
change gem slack-notifier to slack-notify
272da6f
update gemfile
9a77258
find appropriate ruby version
7c0f85f
update parameter list
19a5c2e
gem ensure present rather than latest
46294c3
variable call update
04b1d40
check on always notify
5d755af
debug and hotfix
2be428f
rely back on constant variables
12ba4d3
message formatting
4165ce1
code simplification
473a68d
remove extra gems
307024b
update report url
d004639
update module dependency
b03d01f
Revert "update report url"
3f1d3a9
Merge pull request #1 from clement-parisot/feature/opensource_puppet
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
puppet |
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 @@ | ||
2.0 |
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
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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' | ||
} | ||
|
||
|
@@ -30,7 +66,7 @@ | |
} | ||
|
||
ini_subsetting { 'slack_report_handler': | ||
ensure => present, | ||
ensure => $ensure, | ||
path => "${settings::confdir}/puppet.conf", | ||
section => 'master', | ||
setting => 'reports', | ||
|
@@ -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'], | ||
} | ||
} |
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,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', | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'
.