Skip to content

Instantly share code, notes, and snippets.

#format it
bash "format-ebs" do
code "mkfs.ext4 /dev/sdf"
not_if "e2label /dev/sdf"
end
@gilles
gilles / git-jammit-precommit
Created February 22, 2011 19:53
A pre-commit hook to check if jammit needs to be run
#!/usr/bin/env ruby
#
# A pre-commit hook to test if jammit needs to be run
# In case you don't like on the fly stuff
#
require 'rubygems'
require 'jammit'
def check_assets(config)
@gilles
gilles / prompt.sh.erb
Created February 11, 2011 19:03
Change the PS1 on your provisioned machines (chef)
# /etc/profile.d/prompt.sh
# Chef managed
extra=""
<%- @color ||= '' %>
if type rvm-prompt >/dev/null 2>&1; then
extra+="\[\e[1;36m\](\$(rvm-prompt i v g)) "
fi
export PS1="<%= @color %>[\u@\h] \[\e[1;35m\]\w\[\e[0m\] $extra\[\e[0m\]\$ "
@gilles
gilles / koala.rb
Created January 25, 2011 22:45
monkey patch to use options in koala requests: timeout and proxy
#monkey patch to get timeout, it's a bit cleaner to define a module and use .send (I think)
module PatchedNetHTTPService
def self.included(base)
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :make_request, :timeout
end
end
@gilles
gilles / rpm_extra.rb
Created January 19, 2011 17:13
Trace extra methods in rpm, add the file as a rails initializer
#trace mongo in RPM
Mongo::Collection.class_eval do
include NewRelic::Agent::MethodTracer
add_method_tracer :find
add_method_tracer :find_one
add_method_tracer :find_and_modify
add_method_tracer :update
add_method_tracer :insert
add_method_tracer :save
add_method_tracer :remove
@gilles
gilles / geo.rb
Created January 7, 2011 19:07
Some geo functions for Ruby (haversine) and a mixin for spherical search in Mongo (tested with mongoid)
# geo.rb
# Formulas from
#
# haversine formula to compute the great circle distance between two points given their latitude and longitudes
#
# Copyright (C) 2008, 360VL, Inc
# Copyright (C) 2008, Landon Cox
#
# http://www.esawdust.com (Landon Cox)
@gilles
gilles / install_egg.py
Created November 23, 2010 22:14
install eggs with chef
require 'chef/mixin/command'
class Chef
class Recipe
include Chef::Mixin::Command
def install_egg(name, options={})
version = options.delete(:version)
module_name = options.delete(:module) || name
@gilles
gilles / compact_chef_couchdb.rb
Created November 18, 2010 22:00
Compact chef's couchdb indexes
# vim:ts=2:expandtab
require 'rubygems'
require 'open-uri'
require 'json'
#http://wiki.apache.org/couchdb/Compaction
if JSON::parse(open("http://localhost:5984/chef").read)["disk_size"] > 100_000_000
res = Net::HTTP.post_form(URI.parse('http://localhost:5984/chef/_compact'), {})
puts res.body
@gilles
gilles / brace_expansion.rb
Created November 9, 2010 23:35
bash like brace expansion
# bash like brace expansion
#
# {1,2}.3.4.{5,6}.7.8.{9,10} becomes
# [1.3.4.5.7.8.9,
# 2.3.4.5.7.8.9,
# 1.3.4.6.7.8.9,
# 2.3.4.6.7.8.9,
# 1.3.4.5.7.8.10,
# 2.3.4.5.7.8.10,
# 1.3.4.6.7.8.10,
@gilles
gilles / line_logger.rb
Created July 13, 2010 16:31
rails line logger
#inspired (a lot) by http://blog.gugl.org/archives/47
# Note, to achieve this I should only have to define a formatter.
# why BufferedLogger instead of Logger with a buffered IO?
# I suspect there is a reason for BufferedLogger so I'll keep it
class LineLogger < ActiveSupport::BufferedLogger
SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}
attr_writer :formatter