Skip to content

Instantly share code, notes, and snippets.

# Ip.valid? '1.2.3.4'
module Ip
def self.pattern
@pattern ||= {
# pattern to match a single ip address or cidr
:ip => %r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/\d{1,2})?$},
}
end
def self.valid? address
@dallasmarlow
dallasmarlow / gist:2473944
Created April 23, 2012 21:21
multipart benchmark
require 'benchmark'
require 's3_multi_upload'
config = {
:access_key_id => 'xxx',
:secret_access_key => 'xxx',
:bucket => :s3_multi_upload,
:file => 'ubuntu-12.04-beta2-dvd-amd64.iso', # 1.6 gb
}
@dallasmarlow
dallasmarlow / agent.rb
Created May 1, 2012 23:55
mysql backups
%w[sequel aws-sdk socket logger json pathname config upload].each {|l| require l}
module MysqlBackup
class Agent
include Config
attr_accessor :summary, :pool, :path, :log, :db
def initialize pool
# intent
@dallasmarlow
dallasmarlow / e.rb
Created May 17, 2012 00:31
enum math
module Enumerable
def sum
self.reduce :+
end
def mean
self.sum / self.size
end
@dallasmarlow
dallasmarlow / sequel.rb
Created May 18, 2012 18:50
quick example of sequel and time ranges
require 'sequel'
config = {
:database => {
:adapter => 'mysql2',
:database => 'mysql_backup_reports',
:host => '1.2.3.4',
:user => 'backup',
:password => 'pants'
},
@dallasmarlow
dallasmarlow / nagios_post.rb
Created May 21, 2012 20:59
nagios service commands
require 'net/https'
require 'uri'
config = {
nagios: {
url: 'https://nagios.domain.tld/nagios/cgi-bin/cmd.cgi',
auth: ['user', 'password'], # basic auth
command_types: [22, 23], # enable / disable
},
retry_limit: 3,
@dallasmarlow
dallasmarlow / gist:2936972
Created June 15, 2012 15:19
rvm for fish
set -l GITHUB https://raw.github.com/lunks/fish-nuggets/master/functions
curl --create-dirs -o ~/.config/fish/functions/rvm.fish $GITHUB/rvm.fish
curl -o ~/.config/fish/functions/cd.fish $GITHUB/cd.fish
@dallasmarlow
dallasmarlow / gist:2937920
Created June 15, 2012 18:10
rbenv for fish
set PATH $HOME/.rbenv/bin $PATH
set PATH $HOME/.rbenv/shims $PATH
rbenv rehash >/dev/null ^&1
@dallasmarlow
dallasmarlow / deploy.rb
Created June 22, 2012 22:11
unicorn cap deploy
#
# a simple cap recipe for deploying rack apps (such as sinatra or rails) using unicorn
# via scp using a dedicated deploy user with sudo access to run commands as the daemon user
#
# app options
set :application, 'app'
set :repository, '.' # deploy from the current working directory
# deploy options
@dallasmarlow
dallasmarlow / gist:3491727
Created August 27, 2012 19:50
simple thread pool
require 'thread'
class ThreadPool
attr_reader :size
def initialize size = 24
@size = size
@queue = Queue.new
@pool = size.times.collect do
Thread.new do