Skip to content

Instantly share code, notes, and snippets.

@grantr
grantr / sax_test.rb
Created March 22, 2012 17:17
saxaphone bug?
require 'rubygems'
require 'saxaphone'
require 'stringio'
class Location < Saxaphone::Element
has_element 'categories', 'Categories' do |element|
puts "categories element #{element.categories.inspect}"
attributes['categories'] = element.categories
end
end
@grantr
grantr / status_attribute_handler.rb
Created November 18, 2011 00:02
set node attribute status:UP when a chef run succeeds
require "chef/handler"
class StatusAttributeHandler < Chef::Handler
VERSION = "0.0.1"
DEFAULTS = {
:attribute_name => 'status',
:success_status => 'UP'
}
Celluloid::ZMQ::Mailbox
behaves like a Celluloid Mailbox
receives messages
raises system events when received
prioritizes system events over other messages
selectively receives messages with a block
DCell::Node
looks up remote actors (FAILED - 1)
@grantr
grantr / timer_pool.rb
Created October 23, 2011 17:05
celluloid timers using Kernel.select
module Celluloid
class Scheduler
def initialize
read, @write = IO.pipe
@pool = TimerPool.new(read)
at_exit { stop }
end
def start
@grantr
grantr / timer.rb
Created October 23, 2011 16:43
naive timers in celluloid
module Celluloid
class OneShotTimer
include Celluloid
def initialize(timeout)
@timeout = timeout
end
def fire
puts "fired"
@grantr
grantr / xss_finder.rb
Created October 13, 2011 19:09
script to find rails 3 xss issues
# run from rails root
Dir.glob("app/views/**/*.*").each do |filename|
File.open(filename) do |f|
f.each_with_index do |line, i|
line.scan /[^"]"([^"]+)"/ do |match|
m = match.first
puts "#{f.path} :#{i+1} #{m}" if m =~ /(<[^%]|[^=%]>)/
end
end
@grantr
grantr / gist:1233599
Created September 21, 2011 23:20
elasticsearch indexing chef
curl -XPUT localhost:9200/_river/couch/_meta -d '{
"type" : "couchdb",
"couchdb" : {
"host" : "localhost",
"port" : 5984,
"db" : "chef",
"filter" : null,
"script": "ctx._type = ctx.doc.chef_type"
},
"index" : {
@grantr
grantr / gist:1105416
Created July 25, 2011 22:31
Chef mysql master/slave recipes
## mysql::master
ruby_block "store_mysql_master_status" do
block do
node.set[:mysql][:master] = true
m = Mysql.new("localhost", "root", node[:mysql][:server_root_password])
m.query("show master status") do |row|
row.each_hash do |h|
node.set[:mysql][:master_file] = h['File']
node.set[:mysql][:master_position] = h['Position']
end
# use for oneshot recipes
if node[:oneshot]
# clear oneshot and save node
oneshot, node[:oneshot] = node[:oneshot], []
node.save
oneshot.each do |recipe|
include_recipe recipe
end
end
@grantr
grantr / searchable_model.rb
Created April 25, 2011 22:08
ActiveModel module for elasticsearch indexing
# class Person
# include SearchableModel
# include SearchableModel::SearchMethods
#
# ...
#
# end
module SearchableModel