redgreen with autotest and autospec 0

Posted by Andrew

I use both RSpec and Test/Unit and the accompanying autospec and autotest. I was struggling to get coloring for both without breakage.

Trick is to only require redgreen/autotest if the RSPEC environmental variable is not set. Here is my ~/.autotest:


require 'autotest/fsevent'
require 'autotest/growl'
require 'redgreen/autotest' unless ENV['RSPEC']

Autotest::Growl::show_modified_files = true
Autotest::Growl::remote_notification = true
Autotest::Growl::image_dir = File.join(ENV['HOME'], '.autotest-growl')

Autotest.add_hook :initialize do |at|
  at.sleep = 1
  %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}

  unless ARGV.empty?
    if File.exist? 'config/environment.rb'
      at.find_directories = ARGV.length == 1 ? ["spec/#{ARGV.first}", "app/#{ARGV.first}"] : ARGV.dup
    end
  end

  at.add_mapping(%r%^spec/(processors|mailers|middlewares)/.*rb$%) { |filename, _| filename }
end

ssh + pv + mysql

Posted by Andrew

This is a good one courtesy of Jonathan Dance.

ssh -C gateway ssh -C database-server mysqldump -u root -pPASSWORD database | pv | mysql -u root database

The -C flag does ssh compression.

pv + gzip + mysql

Posted by Andrew

I am writing this down for my own benefit:

Dumping with on-the-fly gzip and speed indicator.

$ mysqldump -u root -p database | pv | gzip -c > database.sql.gz

Loading with on-the-fly gunzip and speed indicator.

$ pv database.sql.gz | gunzip | mysql -u root -p database

Rails Development with SSL

Posted by Andrew

Big Picture

Nginx will accept connection on ports 80 (http) and https (443). Under SSL/TLS nginx will negotiate the encryption and proxy any requests that it cannot serve to our trusty mongrel running on port 3000. Mongrel doesn’t handle encrypted traffic, instead we pass a flag (X_FORWARDED_PROTO) that indicates the request came over SSL.

Prerequisites

You will need a functioning Macports installation and a functioning development environment (i.e. you can browse your project at localhost:3000).

Conventions

$ indicates a standard shell prompt.
# indicates the command needs to be run as the root user by assuming root privileges with sudo.

Install nginx with SSL Support

# port install nginx +ssl

Make Your Certs Directory

# mkdir -p /opt/local/etc/nginx/certs
$ cd /opt/local/etc/nginx/certs

Make Your Own Certificate

Follow the prompts but MAKE SURE TO USE *.example.com AS YOUR Common Name!
http://pastie.org/234929
# openssl req -new -x509 -nodes -days 365 -out server.crt -keyout server.key

Create an nginx Configuration File

Use http://pastie.org/234927 as an example.
Edit out my locations with yours. (i.e. Replace /Users/Andrew/Projects/macchiato with wherever your code lives.)
# mate /opt/local/etc/nginx/nginx.conf

Fire Up nginx and Your Mongrel

# /opt/local/sbin/nginx
$ script/server

Check Your DNS

# mate /etc/hosts to make sure app.example.com points to localhost

Accept Your New Certificate

Open https://app.example.com/ in Safari. Select Show Certificate and accept it permanently.
In Firefox you will have to manually import your certificate.
Firefox => Preferences => Advanced => Encryption => View Certificates => Authorities => Import => /opt/local/etc/nginx/certs/server.crt => Make sure Trust this CA to identify web sites is checked => Ok.

ssl_requirement

ssl_requirement is the de-facto standard Rails SSL plugin.

Convert your Rails application database to UTF-8

Posted by Andrew

Its unlikely that this will ever matter in local development, but its good practice to have your environment properly setup.

Dump your database to a file:
$ mysqldump -u root --skip-set-charset project_development > project_development.sql

Note: If you are using MacPorts for your mysql installation, mysqldump will be mysqldump5.

Remove the character sets from the table definitions: (be careful if have the phrase ’ DEFAULT CHARSET=latin1’ in your data it will be removed!)
$ sed 's/ DEFAULT CHARSET=latin1//' project_development.sql

Convert the characters in that file from latin1 to UTF-8 using iconv:
$ iconv -f latin1 -t UTF-8 project_development.sql > project_development_utf8.sql

Drop your existing database and recreate it with the appropriate defaults:
$ mysql -u root --execute="drop database project_development; create database project_development character set utf8 collate utf8_general_ci;"

Repopulate your database with your converted data:
$ mysql -u root project_development < project_development_utf8.sql

Don’t forget to add the encoding to your database.yml!
development:
  adapter: mysql
  database: project_development
  username: root
  password: 
  host: localhost
  encoding: utf8

Lighthouse Bugzilla Importer

Posted by Andrew

I have looking at some alternatives to Bugzilla for managing my projects. Basecamp was too simple, Redmine tries to do too much, but I think Lighthouse gets it juuuuuust right. In order to migrate my tickets I wrote a little script to parse Bugzilla XML and create and post Lighthouse tickets. The code is up on github.

Rails is on Git!

Posted by Andrew

Rails is moving from SVN to Git!

I have not yet patched Ruby on Rails, but I am still very excited about the move to Git. Why? I think it will spawn some cool forks that solve special cases. These branches (especially with the social nature of GitHub) will be much easier to find and develop. Its a good day in the rails community.

Rails Stack

Posted by Andrew

Linux, MySQL, nginx, Mongrel , Rails, and friends.

Big Picture:

An ideal setup uses two different servers or virtual machines to split up the load and facilitate scaling. The first machine runs nginx serving static content and acting as a reverse proxy passing dynamic requests to a pool of mongrels running the rails application. The second machine is a dedicated MySQL database server. Even if you only have one physical box, two virtual machines (Xen based) is preferred, it is easier to scale and doesn’t require as much memory context switching. Our host, Engine Yard, uses a similar setup based on Gentoo and Xen.

Git

Posted by Andrew

I’ve been experimenting with Git for source control management (SCM), I’m really starting to appreciate the benefits of the distributed features it provides. The User’s Manual is very comprehensive, and the SVN -> Git crash course was excellent in getting me up to speed. Simplistic Complexity provides an awesome SVN to Git Migration Guide that helped me split my monolithic subversion repository into a series of lean git trees. Although git is relatively new, there is some pretty good support for hooking it into existing tools like Redmine, which has support for git in its latest trunk. Hopefully I will be able to convert some of my co-workers soon, but until then I’m going to be using a hybrid workflow of git and subversion, mostly facilitated by git-svn.

Update: I’ve been playing with gitosis for managing “push” repositories that I can share with others or use to deploy with capistrano. The program requires you have SSH setup perfectly, and there are a few caveats (read: must use ssh on port 22 to deploy with capistrano), but its very simple once you get it working. I really like how access to your repositories is done with public SSH keys. Scie.nti.st has more.

Another Update: I secured a GitHub beta invite and was able to successfully push all of my repositories there. GitHub also has some great documentation on deploying with capistrano and github which enabled me to re-deploy all of my applications to my server using git. As a side-benefit I now also have all my mongrels running as an unprivileged user.

Mongrel vs Evented Mongrel

Posted by Andrew

Mongrel is just as fast as Evented Mongrel with a concurrency of 1, but under a concurrency of 10 the difference becomes clear. Evented Mongrel is able to maintain the same 270 req/sec whereas Generic Mongrel falls down to about 120 req/sec.