Showing with 20 additions and 10 deletions.
  1. +20 −10 templates/webhook.erb
30 changes: 20 additions & 10 deletions templates/webhook.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
# Ben Ford
# Adam Crews
# Zack Smith
# Jeff Malnick

require 'rubygems'

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'mcollective'


require 'resolv'
require 'json'

include MCollective::RPC


LOGFILE = '/var/lib/peadmin/webhook.log'
DEPLOYCMD = "/opt/puppet/bin/mco r10k deploy_all --np >> %{LOGFILE} 2>&1"
USER = 'puppet'
Expand Down Expand Up @@ -51,10 +54,11 @@ 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
post '/payload' do
protected!
request.body.rewind # in case someone already read it
data = JSON.parse request.body.read
data = JSON.parse(request.body.read, :quirks_mode => true)
branch = data['ref'].split("/").last
deploy(branch)
end
Expand All @@ -64,26 +68,32 @@ class Server < Sinatra::Base
end

helpers do
def deploy()
def deploy(branch)
begin
if USE_MCO
mco()
result = mco(branch).first
if result.results[:statuscode] == 0
message = result.results[:statusmsg]
else
raise result.results[:statusmsg]
end
else
message = 'forked'
Process.detach(fork{ exec "#{DEPLOYCMD} &"})
end
{:status => :success, :message => "Deploying environments."}.to_json
rescue
{:status => :fail, :message => "Deploy failed.", :trace => e.message}.to_json
{:status => :success, :message => message.to_s }.to_json
rescue => e
{:status => :fail, :message => e.message, :trace => e.backtrace}.to_json
end
end #end deploy()

def mco()
def mco(branch)
options = MCollective::Util.default_options
options[:config] = CLIENT_CFG
client = rpcclient('r10k', :exit_on_failure => false,:options => options)
client.discovery_timeout = DISCOVER_TIMEOUT
client.timeout = CLIENT_TIMEOUT
result = client.send('deploy',branch)
result = client.send('deploy',{:environment => branch})
end # end deploy()


Expand Down