Skip to content

Instantly share code, notes, and snippets.

@HotFusionMan
HotFusionMan / devops_LFMM.markdown
Created July 12, 2011 16:12
My dev-ops "Learn from my mistakes" list
  • Use a version control system that allows you to deploy exactly what you want to, not "everything up to and including a particular checkin", e.g., use Git, not Perforce.

  • Have a centralized admin user account on servers, but also have individual user accounts and make people log in using their individual and then sudo. This strategy provides a better audit trail than logging in directly to the cetnral admin account.

  • Don't build a job queue system from scratch using cron. There are so many better-behaved job queue systems in various languages.

    • Cron can't schedule more than some maximum number of jobs in a single minute, so it may miss running some jobs.
    • Cron can't re-run missed jobs, so you have to come up with a way yourself of identifying any missed jobs (either because of the above issue or because cron or the system was down) and running them.
  • Databases sometimes need to be versioned in ways besides ActiveRecord-type migrations. For example, if two code branches differ in the columns th

@HotFusionMan
HotFusionMan / print_methods_of_a_class.java
Created June 17, 2011 03:26
Using Java reflection to print all the methods of a class, including their argument signatures.
import java.lang.reflect.Method ;
String className = "java.lang.Class" ; // or whatever class you're interested in
Class c = Class.forName( className ) ;
Method[] methods = c.getDeclaredMethods() ;
for ( Method m : methods )
{
@HotFusionMan
HotFusionMan / launch_OSX_Login_Items.rb
Created April 20, 2011 18:11
Requires that you've set up a ~/com.apple.loginitems.plist file defining what apps you want to launch. It would be nice to automate converting from using the system's copy of this file to this custom-located one.
#!/usr/bin/env ruby
require 'rexml/document'
def set_up_Growl
if `ps -ax | grep GrowlHelperApp\.app | grep -v grep` == '' then
system( 'open /Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app' )
end
end
@HotFusionMan
HotFusionMan / compare_times_of_day.rb
Created April 13, 2011 21:55
After searching the Web for how to tell whether it's currently later some specified time of day, I realized it's really simple to do.
now = Time.now.to_a
if ( now[2] * 60 + now[1] ) * 60 + now[0] > START_TIME_IN_SECONDS then
# start whatever you wanted to start at this time
end
@HotFusionMan
HotFusionMan / seeds.rb
Created February 28, 2011 23:39
Generic YAML fixture loader for Rails. Supply a db/seeds.yaml file containing fixtures with sections named for their ActiveRecord models, pluralized (e.g., Queries). Invoke with rake db:seed.
# Based on http://stackoverflow.com/questions/2339021/how-can-i-load-some-activerecord-models-from-a-yaml-file-and-save-them-to-the-db/
seed_filepath = File.join( RAILS_ROOT, 'db', 'seeds.yaml' )
config = YAML::load_file( seed_filepath )
config.each { |pluralized_model_name, fixtures|
model_class = pluralized_model_name.singularize.constantize
# If you like leaping without looking:
# model_class.create( fixtures )
@HotFusionMan
HotFusionMan / add_class-level_method_to_module.rb
Created January 14, 2011 15:51
How to add a "class-level" method to an existing module.
module PerfectBitsMath
def number_of_squares_in_interval( m, n )
1
end
end
module Math
extend PerfectBitsMath
end
@HotFusionMan
HotFusionMan / jEdit.rb
Created January 11, 2011 15:53
jEdit command line command for Mac OS X (strip off the .rb filename extension to make it look more like a typical Unix command line utility).
#!/usr/bin/env ruby
arguments = []
ARGV.each { |arg|
arguments << File.expand_path( arg )
}
system( "java -cp /Applications/jEdit.app/Contents/Resources/Java/jedit.jar:/System/Library/Java -Dapple.awt.textantialiasing=true -Dapple.laf.useScreenMenuBar=true -Dapple.awt.antialiasing=true org.gjt.sp.jedit.jEdit -background #{arguments.join( ' ' )} &" )
@HotFusionMan
HotFusionMan / NotImplementedError_bypasses_rescue_clause.rb
Created December 17, 2010 20:41
NotImplementedError bypasses rescue clause in Ruby 1.8.7 (REE)
# Tested under:
# $ ruby -v
# ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02
class C
def f
raise NotImplementedError
end
end
@HotFusionMan
HotFusionMan / toggle_Require_password_on_wake.sh
Created October 27, 2010 15:34
shell script for calling gist 646214 from the command line
#!/bin/bash
~/bin/toggle_Require_password_on_wake.app/Contents/MacOS/applet
@HotFusionMan
HotFusionMan / toggle_Require_password_on_wake.scpt
Created October 26, 2010 02:29
When lending your Mac to someone who doesn't have an account on it, turn off being bothered to type in your password for them every time they have to wake it from sleep.
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.security"
tell application "System Events"
tell process "System Preferences"
-- UNLOCK CHECKS
if (exists checkbox "Click the lock to make changes." of window 1) is true then
click checkbox "Click the lock to make changes." of window 1