Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
mmrwoods / new_linode_centos6.markdown
Created February 7, 2012 11:39
Make me a new CentOS linode

Make me a new CentOS linode

Notes:

  • Instructions written for CentOS 6.3
  • Change, me, myhost, myip etc. to your username, hostname, ip address and so on.
  • Run all commands as root unless otherwise directed.
  • You might want to look at mounting /var and /home on separate partitions.
  • I've just allowed all members of the wheel group to operate as root. This is the height of laziness and highlights the fact that I'm just a developer that's stolen a sysadmin's
@mmrwoods
mmrwoods / show_dependencies
Last active June 3, 2016 15:40
Hacky script to show bundled dependencies of rails app
#!/usr/bin/env ruby
require 'bundler'
require 'hirb'
require 'csv'
lock_file = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
def url_for(spec)
case spec.source
#!/usr/bin/env ruby
# Dumb-ass backup - better than no backup!
#
# Run nightly and enjoy daily, weekly and monthly filesystem backups
#
# - default destination directory is /var/local/backup
# - latest, daily, weekly and monthly sub-directories are created
# - latest directory should only ever contain the latest backup
# - latest backup copied to daily, weekly and monthly directories as required
require 'active_support'
require 'hirb'
require 'debugger'
user = 'thickpaddy' # FIXME: get from argv
def send_request(url, content=nil)
curl_opts = ["--max-time 30", "--silent"]
if content
curl_opts << "--header 'Content-Type: application/json'"
@mmrwoods
mmrwoods / human_attribute_name_writers.rb
Last active January 1, 2016 05:49
Create writer methods for active model human attribute names. Useful when importing from spreadsheets.
module Importing
module HumanAttributeNameWriters
def self.included(base)
base.new # Force activerecord to initialize attributes
# only allow accessible attributes that are not foreign keys or nested attributes
foreign_keys = Agent.reflect_on_all_associations.map(&:association_foreign_key)
allowed_attributes = base.accessible_attributes.select do |name|
name.present? && !foreign_keys.include?(name) && !name.match(/_attributes$/)
@mmrwoods
mmrwoods / account.rb
Last active January 1, 2016 05:39
Dumping ground for capistrano recipes...
namespace :account do
desc "Change the deploy user password"
task :reset_password do
deploy_user = user
deploy_pass = generate_password
as_sudo_user do
find_servers.each do |server|
if read_file("/etc/passwd", :hosts => server.host).lines.grep(/#{deploy_user}:/).any?
run "echo \"#{deploy_user}:#{deploy_pass}\" | #{sudo} chpasswd", :hosts => server.host
# Rails email validator is required by the secure validatable module in devise security extension
# However, it handles internation domain names and does mx lookups by default, and there's no easy way to change this default.
class EmailValidator
def validate_mx?
options[:validate_mx] == true
end
def allow_idn?
options[:allow_idn] == true
end
end
@mmrwoods
mmrwoods / activerecord_nested_attributes_patch.rb
Created December 23, 2013 16:16
Skip validation of associated objects unless nested attributes have been provided
module ActiveRecord
module NestedAttributes
module ClassMethods
# Skips validation of associated objects unless nested attributes have been provided.
#
# This is a horrid workaround for the fact that nested_attributes_for sets the autosave
# option for each assocation to true, which causes validation to fail for associated
# objects that have been loaded, whether any nested attributes have been provided or not.
# This causes problems with the parent object is updated from forms that don't include
@mmrwoods
mmrwoods / postgres_backup_restore.rake
Last active January 1, 2016 05:39
Simple backup and restore tasks for postgres
require 'highline/import'
def highlight(str)
"\e[33m" + str + "\e[0m"
end
def run_cmd(cmd)
say highlight "Executing \"#{cmd}\""
Timeout::timeout(30) do
IO.popen(cmd).each{ |line| print line }
@mmrwoods
mmrwoods / address.rb
Last active January 1, 2016 05:39
Postal code validation example
class Address < ActiveRecord::Base
attr_accessible :line_1, :line_2, :line_3, :town, :region, :postcode
validates :line_1, :postcode, :presence => true
validates :postcode, :postcode => true, :allow_blank => true
after_validation do
self.postcode = PostalCode.new(self.postcode).to_formatted_s
end