Showing with 62 additions and 4 deletions.
  1. +2 −0 CHANGELOG
  2. +1 −1 Modulefile
  3. +13 −0 files/prefix_command.rb
  4. +18 −2 files/webhook
  5. +4 −0 manifests/params.pp
  6. +0 −1 manifests/webhook.pp
  7. +6 −0 manifests/webhook/config.pp
  8. +18 −0 tests/webhook/config.pp
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2.2.3 - Zack Smith
* Functional prefix support for webhook
2.2.3 - Zack Smith
* Remove PID file creation from webhook
2.1.2 - Garrett Honeycutt & Zack Smith
Expand Down
2 changes: 1 addition & 1 deletion Modulefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name 'zack-r10k'
version '2.2.3'
version '2.2.4'
source 'https://github.com/acidprime/r10k'
author 'zack'
license 'Apache License, Version 2.0'
Expand Down
13 changes: 13 additions & 0 deletions files/prefix_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/opt/puppet/bin/ruby
require 'json'

if STDIN.tty?
puts 'invalid'
puts 'This command is meant be launched by webhook'
else
data = JSON.parse(STDIN.read)
description = data['repository']['description']
parsed = description.scan(/^.*\[prefix:['"]?(\S+)['"]?\].*$/)
prefix = parsed[0]
puts prefix
end
20 changes: 18 additions & 2 deletions files/webhook
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ opts = {
:Port => $config['port'],
:Logger => $logger,
:ServerType => WEBrick::Daemon,
:SSLEnable => true,
:SSLEnable => $config['enable_ssl'],
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLCertificate => OpenSSL::X509::Certificate.new( File.open(File.join("#{$config['certpath']}", "#{$config['certname']}-cert.pem")).read),
:SSLPrivateKey => OpenSSL::PKey::RSA.new( File.open(File.join("#{$config['certpath']}", "#{$config['certname']}-private.pem")).read),
Expand All @@ -65,7 +65,14 @@ class Server < Sinatra::Base
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read, :quirks_mode => true)
branch = data['ref'].split("/").last
deploy(branch)

# If prefix is enabled in our config file run the command to determine the prefix
if $config['prefix']
prefix = run_prefix_command(data.to_json)
deploy("#{prefix}_#{branch}")
else
deploy(branch)
end
end

not_found do
Expand Down Expand Up @@ -116,6 +123,15 @@ class Server < Sinatra::Base
@auth.provided? && @auth.basic? && @auth.credentials &&
@auth.credentials == [$config['user'],$config['pass']]
end #end authorized?

def run_prefix_command(payload)
IO.popen($config['prefix_command'], mode='r+') do |io|
io.write payload.to_s
io.close_write
result = io.readlines.first.chomp
end
end #end run_prefix_command

end #end helpers
end

Expand Down
4 changes: 4 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
$webhook_protected = true
$webhook_discovery_timeout = 10
$webhook_client_timeout = 120
$webhook_prefix = false
$webhook_prefix_command = '/bin/echo example'
$webhook_enable_ssl = true


if $::is_pe =~ /true/ {
# Puppet Enterprise specific settings
Expand Down
1 change: 0 additions & 1 deletion manifests/webhook.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
$group = 'peadmin',
$git_server = 'localhost',
) {
require r10k::webhook::config

File {
ensure => file,
Expand Down
6 changes: 6 additions & 0 deletions manifests/webhook/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
$protected = $r10k::params::webhook_protected,
$discovery_timeout = $r10k::params::webhook_discovery_timeout,
$client_timeout = $r10k::params::webhook_client_timeout,
$prefix = $r10k::params::webhook_prefix,
$prefix_command = $r10k::params::webhook_prefix_command,
$enable_ssl = $r10k::params::webhook_enable_ssl,
$configfile = '/etc/webhook.yaml',
) inherits r10k::params {

Expand All @@ -35,6 +38,9 @@
'use_mco_ruby' => $use_mco_ruby,
'logfile' => $logfile,
'protected' => $protected,
'prefix' => $prefix,
'prefix_command' => $prefix_command,
'enable_ssl' => $enable_ssl,
}
} else {
validate_hash($hash)
Expand Down
18 changes: 18 additions & 0 deletions tests/webhook/config.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include r10k::webhook

file {'/usr/local/bin/prefix_command.rb':
ensure => file,
mode => '0755',
owner => 'root',
group => '0',
source => "puppet:///modules/r10k/prefix_command.rb",
}

class {'r10k::webhook::config':
prefix => true,
prefix_command => '/usr/local/bin/prefix_command.rb',
enable_ssl => false,
protected => false,
notify => Service['webhook'],
require => File['/usr/local/bin/prefix_command.rb'],
}