Skip to content

Instantly share code, notes, and snippets.

@gaffneyc
gaffneyc / check.rb
Created February 15, 2024 13:57
Check Fly.io standby status
require "json"
require "terminal-table"
fly_app = ARGV[0]
if fly_app.nil?
warn "Usage: check.rb [fly app name]"
exit 1
end
Machine = Struct.new(:config) do
@gaffneyc
gaffneyc / test.rb
Created November 19, 2014 21:41
Disable logging in the test environment
Rails.application.configure do
# Don't write output to test.log
config.logger = ::ActiveSupport::Logger.new("/dev/null")
config.logger.formatter = lambda { |*_| nil }
config.logger.level = 10 # FATAL is 4
end
@gaffneyc
gaffneyc / rpc_consumer.rb
Created June 3, 2009 15:07
Basic RPC using RabbitMQ and Bunny
require 'rubygems'
gem 'gaffneyc-bunny'
require 'bunny'
# Example of simple RPC using server named queues
Bunny.open do |amqp|
# Exchange definition
amqp.exchange('reply', :type => :direct)
amqp.exchange('out', :type => :direct)
#
# Cookbook Name:: yumrepo
# Definition:: yumrepo
#
# Copyright 2010, Tippr Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
@gaffneyc
gaffneyc / remove_CLI.sh
Created October 28, 2013 13:55
Uninstall the buggered xcode 5.0 command line tools
RECEIPT_FILE=/var/db/receipts/com.apple.pkg.CLTools_Executables.bom
RECEIPT_PLIST=/var/db/receipts/com.apple.pkg.CLTools_Executables.plist
if [ ! -f "$RECEIPT_FILE" ]
then
echo "Command Line Tools not installed."
exit 1
fi
echo "Command Line Tools installed, removing ..."
@gaffneyc
gaffneyc / Vagrantfile
Created March 27, 2013 17:02
Vagrant Shell provisioning to replace default chef install with omnibus installer. Tested on the default precise64 boxes provided for Vagrant.
Vagrant::Config.run("2") do |config|
# Clean up the default image to remove some default puppet, chef, and ruby.
config.vm.provision :shell do |shell|
shell.inline = <<-EOF.gsub(/^ +/, "")
# Make sure vagrant always has sudo access
( cat << 'EOP'
Defaults exempt_group=vagrant
%vagrant ALL=NOPASSWD:ALL
EOP
) > /etc/sudoers.d/vagrant
@gaffneyc
gaffneyc / gist:4609767
Created January 23, 2013 16:52
Classes over methods?
# SomeClass.new("arg1", "arg2").call
class SomeClass < Struct.new(:arg1, :arg2)
def call
@arg1 + @arg2
end
end
# vs
# SomeClass.new.some_method(arg1, arg2)
@gaffneyc
gaffneyc / vcr_form_matcher.rb
Created November 19, 2015 16:15
VCR matcher for forms
VCR.configure do |c|
# Match the content of urlencoded forms instead of their encoded version.
# This is to address issues with different ordering of the same information.
c.register_request_matcher :form do |actual, match|
content_type = actual.headers["Content-Type"]
# Only valid for forms
if content_type.include?("application/x-www-form-urlencoded")
actual_form = Rack::Utils.parse_nested_query(actual.body)
match_form = Rack::Utils.parse_nested_query(match.body)
@gaffneyc
gaffneyc / config.ru
Created November 19, 2012 19:47
Rack app to serve the current chef repo as a tar file
require "open3"
class TarApp
def call(env)
data = nil
status = nil
if(env["REQUEST_PATH"] !~ /(tgz|tar\.gz)/)
return [ 301, { "Location" => "/current.tgz" }, "" ]
end
@gaffneyc
gaffneyc / active_record_statsd.rb
Created November 16, 2012 16:47
Instrumenting ActiveRecord with StatsD
ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
if payload[:sql] =~ /^\s*(SELECT|DELETE|INSERT|UPDATE) /
method = $1.downcase
table = nil
# Determine the table name for instrumentation. The below regexes work on
# mysql but not sqlite. Probably won't work on postgresql either.
case method
when "select", "delete"
table = $1 if payload[:sql] =~ / FROM `(\w+)`/