Showing with 34 additions and 12 deletions.
  1. +12 −0 README.md
  2. +1 −1 files/prefix_command.rb
  3. +13 −6 files/webhook
  4. +3 −2 manifests/params.pp
  5. +4 −2 manifests/webhook/config.pp
  6. +1 −1 tests/webhook/config.pp
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Using mco you can then trigger mcollective to call r10k using
mco r10k synchronize
```

You can sync an individual environment using:

```shell
mco r10k deploy <environment>
```

An example post-recieve hook is included in the files directory.
This hook can automatically cause code to synchronize on your
servers at time of push in git.
Expand Down Expand Up @@ -84,4 +90,10 @@ and copy the certs to somewhere that is readable by the respective user.
_Note: PE2 only requires the .mcollective file as the default auth was psk_
##Support

# Webhook Support

For version control systems that use web driven post-receive processes you can use the example webhook included in this module.
This webhook currently only runs on Puppet Enterprise and uses mcollective to automatically synchronize your environment across multiple masters.
The webhook must be configured on the respective "control" repository a master that has mco installed and can contact the other masters in your fleet.

Please log tickets and issues at our [Projects site](https://github.com/acidprime/r10k/issues)
2 changes: 1 addition & 1 deletion files/prefix_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
else
data = JSON.parse(STDIN.read)
description = data['repository']['description']
parsed = description.scan(/^.*\[prefix:['"]?(\S+)['"]?\].*$/)
parsed = description.scan(/^.*\[prefix:['"]?(\w+)['"]?\].*$/)
prefix = parsed[0]
puts prefix
end
19 changes: 13 additions & 6 deletions files/webhook
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ end
ENV['HOME'] = '/var/lib/peadmin'
ENV['PATH'] = '/sbin:/usr/sbin:/bin:/usr/bin:/opt/puppet/bin'

$logger = WEBrick::Log::new($config['logfile'], WEBrick::Log::DEBUG)

$logger = WEBrick::Log::new($config['access_logfile'], WEBrick::Log::DEBUG)
$mco_logfile = $config['mco_logfile']

opts = {
:Port => $config['port'],
Expand All @@ -58,13 +58,20 @@ class Server < Sinatra::Base
raise Sinatra::NotFound
end

# curl -d '{ "ref": "refs/heads/production" }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
# Simulate a github post:
# curl -d '{ "ref": "refs/heads/production" }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
#
# Somulate a stash post:
# curl -d '{ "refChanges":[ { "refId":"refs/heads/production" } ] }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
#

post '/payload' do
protected! if $config['protected']
$logger.info("authenticated: #{$config['user']}")
request.body.rewind # in case someone already read it
data = JSON.parse(request.body.read, :quirks_mode => true)
branch = data['ref'].split("/").last
# github sends a 'ref', stash sends an array in 'refChanges'
branch = ( data['ref'] || data['refChanges'][0]['refId'] ).split("/").last

# If prefix is enabled in our config file run the command to determine the prefix
if $config['prefix']
Expand All @@ -90,8 +97,8 @@ class Server < Sinatra::Base
raise result.results[:statusmsg]
end
else
message = "triggered: mco r10k deploy #{branch} -p"
Process.detach(fork{ exec "/opt/puppet/bin/mco r10k deploy #{branch} -p >> #{LOGFILE} 2>&1 &"})
message = "triggered: mco r10k deploy #{branch}"
Process.detach(fork{ exec "/opt/puppet/bin/mco r10k deploy #{branch} >> #{mco_logfile} 2>&1 &"})
end
$logger.info("message: #{message} branch: #{branch}")
{:status => :success, :message => message.to_s }.to_json
Expand Down
5 changes: 3 additions & 2 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
$webhook_user = 'puppet'
$webhook_pass = 'puppet'
$webhook_port = '8088'
$webhook_logfile = '/var/log/webhook/access.log'
$webhook_access_logfile = '/var/log/webhook/access.log'
$webhook_mco_logfile = '/var/log/webhook/mco_output.log'
$webhook_certname = 'peadmin'
$webhook_certpath = '/var/lib/peadmin/.mcollective.d'
$webhook_client_cfg = '/var/lib/peadmin/.mcollective'
Expand All @@ -43,7 +44,7 @@
$webhook_client_timeout = 120
$webhook_prefix = false
$webhook_prefix_command = '/bin/echo example'
$webhook_enable_ssl = true
$webhook_enable_ssl = true


if $::is_pe =~ /true/ {
Expand Down
6 changes: 4 additions & 2 deletions manifests/webhook/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
$user = $r10k::params::webhook_user,
$pass = $r10k::params::webhook_pass,
$port = $r10k::params::webhook_port,
$logfile = $r10k::params::webhook_logfile,
$access_logfile = $r10k::params::webhook_access_logfile,
$mco_logfile = $r10k::params::webhook_mco_logfile,
$client_cfg = $r10k::params::webhook_client_cfg,
$use_mco_ruby = $r10k::params::webhook_use_mco_ruby,
$protected = $r10k::params::webhook_protected,
Expand All @@ -36,7 +37,8 @@
'client_cfg' => $client_cfg,
'certpath' => $certpath,
'use_mco_ruby' => $use_mco_ruby,
'logfile' => $logfile,
'access_logfile' => $access_logfile,
'mco_logfile' => $mco_logfile,
'protected' => $protected,
'prefix' => $prefix,
'prefix_command' => $prefix_command,
Expand Down
2 changes: 1 addition & 1 deletion tests/webhook/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
mode => '0755',
owner => 'root',
group => '0',
source => "puppet:///modules/r10k/prefix_command.rb",
source => 'puppet:///modules/r10k/prefix_command.rb',
}

class {'r10k::webhook::config':
Expand Down