Skip to content

Instantly share code, notes, and snippets.

@Yggdrasil
Created July 13, 2012 12:15
Show Gist options
  • Save Yggdrasil/3104595 to your computer and use it in GitHub Desktop.
Save Yggdrasil/3104595 to your computer and use it in GitHub Desktop.
Sinatra Hooks
require './hooks'
run Hooks
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra/base'
require 'json'
require 'pp'
class Hooks < Sinatra::Base
set :sessions, false
set :token, 'xxxxxxxxxxxxx' #A simple token
PUPPETDIR = '/srv/puppet'
MODULESDIR = "#{PUPPETDIR}/modules"
CLIENTSDIR = "#{PUPPETDIR}/clients"
def update_submodule(name)
system "cd #{MODULESDIR}/#{name} && git fetch && git pull origin master"
end
def update_client(name)
system "cd #{CLIENTSDIR}/#{name} && git fetch && git pull origin master"
end
def update_master
system "cd #{PUPPETDIR} && git fetch && git pull origin master"
end
get '/hooks*' do
'Nothing to see here! Please disperse!'
end
post '/hooks/github/puppetmaster/:token' do
token = params['token']
halt 403, 'Token is wrong' unless token == settings.token
update_master
end
post '/hooks/github/puppet/:type/:name/:token' do
type = params['type']
name = params['name']
token = params['token']
halt 403, 'Token is wrong' unless token == settings.token
case type
when 'module'
update_submodule name
when 'client'
update_client name
else
halt 404, 'Nothing to do here'
end
end
end
#push = JSON.parse(params[:payload])
#repo_name = push["repository"]["name"]
location /hooks {
alias /srv/hooks/public;
passenger_enabled on;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment