Blog

2 2

Pool-party, Auto-scaling with EC2 1

Posted by Chris Barnes Sat, 31 May 2008 16:03:00 GMT


Photo by hrlndspnks

Having a great time at Railsconf08. Meeting some great people and learning a lot. I plan to post in more detail about a lot of this after the conference but I wanted to go ahead and mention one of the coolest presentations I've seen so far.

Ari Lerner has created a ruby gem called pool-party. It auto-scales your ec2 cluster based on criteria you set in the config file like average requests per second. Set your minimum requests per second and if the average of all the servers on your cluster goes over the maximum, pool-party will launch another instance for you. It scales down in the same way. It doesn't care what you're using your cluster for and a its going to support plugins soon so you can add in your own hook like functionality. Pool-party is using s3fuse to mount an s3 bucket on the instance at startup. Specify a bucket in pool-party and it gets mounted at /data on each instance. This solves the data persistence issue with ec2. You're not required to use it though, just don't specify a bucket in pool-party and nothing gets mounted.

Ari open-sourced Pool Party as of 2 days ago. Check it out at poolpartyrb.com. If you want to contribute, its on github at http://github.com/auser/pool-party.
2 2

Fixed Length Random numbers in Ruby 2

Posted by Chris Barnes Wed, 09 Jan 2008 16:20:00 GMT

My latest project is almost complete and we're setting up a demo site with lots of fake data already included so I used the faker gem to generate most of it.

One issue I had that I couldn't do with the faker gem out of the box is get a fixed length random number. I need this for license numbers, credit card numbers, phone numbers and others. The faker gem does offer a random phone number but the ones in my project don't support extensions or punctuation so rather than generating the number then using string functions to strip out the stuff I didn't want, I decided to find an easier way.

I started by Googling for it assuming that someone else had already figured it out and while I'm sure its been done a hundred times before, I couldn't find it anywhere. So after much thought and digging through the ruby docs, this is what I came up with.

rand(9999999999).to_s.center(10, rand(9).to_s)

rand(9999999999) will give me a number between 0 and 9999999999. Convert it to a string and use the center method to make sure its at least 10 digits long and pad the rest with a random number from 0-9. rand(9)

If you need a number of say 7 digits in length, just change the first rand to rand(9999999) and change the center method length to 7 so it looks like this.

rand(9999999).to_s.center(7, rand(9).to_s)

Hope this is helpful to someone.

2 2

Subversion script for Rails developers 3

Posted by Chris Barnes Wed, 19 Sep 2007 19:15:00 GMT

I read somewhere that good developers use version control. There's a good article on HowtoUseRailsWithSubversion.

So why should I repeat those steps for every new project I work on.

There is another article which attempted to tackle this problem. However, you still have to do the initial steps and then copy this script into your project directory and execute it.

The following script takes it a bit farther. You call it like this.

rails-svn app_dir repository username

So if I wanted to create a new rails project called toejam, I would execute:

rails-svn toejam svn://randomutterings.com/projects/toejam/trunk chris

Here's what happens under the hood.

  1. After checking to make sure the directory doesn't already exist, your rails app is created with the standard rails command. (If the directory already exists, you will be asked if you want to continue. Choosing yes will allow the script to continue but if this script has been run on the same app twice, all of the svn commits will be duplicated.)

  2. Your new app is imported to the subversion repository specified. Authentication is done with the username provided. You may be prompted for a password from your repository.

  3. The original app directory is deleted and a working copy is checked out from the repository into that directory.

  4. All the default rails log files are removed and subversion is instructed to ignore those files. (Log files can get large and we definitely don't need them in version control.)

  5. The database.yml is moved to database.example to serve as a template for anyone who checks out the code and any newly created database.yml files are ignored by subversion. (Doing this will help if you have multiple developers working on the same project in different environments. It also prevents you from overwriting the database.yml file on the production server, oops!

  6. Subversion is told to ignore everything in the tmp folder, the .htaccess files, and the dispatch.fcgi file.

Without further ado, here's the script. Just copy it and save it somewhere in your path. /usr/bin is a good choice. Don't forget to chmod 755 so you can execute it.

    #!/bin/bash

    if [ "$#" != "3" ]; then
      echo "Usage: rails-svn app_dir repository username"
      exit 1
    fi

    APPDIR=./$1
    SVN_TRUNK=$2
    SVN_USER=$3

    function check_if_exist () {
      if [[ -e $1 ]]; then
        echo ""
        echo "$1 already exists, overwrite? y or n"
        echo ""
        read OVERWRITE
        case "$OVERWRITE" in
        y)
          echo "Overwriting..."
          ;;
        *)
          echo "Action canceled"
          exit 1
          ;;
        esac
      fi
    }

    check_if_exist ${APPDIR}
    rails $APPDIR
    svn import $APPDIR $SVN_TRUNK -m "Import" --username $SVN_USER
    sudo rm -r $APPDIR
    svn checkout $SVN_TRUNK $APPDIR
    cd $APPDIR

    svn remove log/*
    svn commit -m "removing log files" 
    svn propset svn:ignore "*.log" log/
    svn update log/
    svn commit -m "Ignoring all files in /log/ ending in .log"
    svn move config/database.yml config/database.example
    svn commit -m "Moving database.yml to database.example to provide a template for anyone who checks out the code"
    svn propset svn:ignore "database.yml" config/
    svn update config/
    svn commit -m "Ignoring database.yml"
    svn remove tmp/*
    svn propset svn:ignore "*" tmp/
    svn update tmp/
    svn commit -m "ignore tmp/ content from now" 
    svn propset svn:ignore ".htaccess" public/
    svn update public/
    svn commit -m "Ignoring .htaccess"
    svn propset svn:ignore "dispatch.fcgi" public/
    svn update public/
    svn commit -m "Ignoring dispatch.fcgi"

I'm interested in converting this into a gem and possibly rewriting it in ruby. If anyone has the skills and wants to contribute, leave a comment or email me.

2 2

Rapt error during peepcodes test first development tutorial 4

Posted by Chris Barnes Thu, 13 Sep 2007 16:47:00 GMT

OK, let me start off by saying, I love the tutorials from peepcode, you guys are doing a great job so keep doing it.

Alas, I got stuck while watching the Test-first-development tutorial. They were using Rapt as an alternative to script/plugin and when I tried to run it I kept getting an error.

Error: RaPT currently does not work outside of a Rails application directory. Please change to the top level of a Rails application and try again.

This error is misleading but at least it was obviously misleading, after all I know I'm in my Rails application directory.

After some googling, I found a post describing a work around for the problem, but it basically gave instructions on installing the plugin with script/plugin. But the guys at peepcode had a reason for using rapt over script/plugin (its faster). So I Google some more. I finally find a bug on the Rapt Trac posted in 2006 that explains that the user must first add repositories by executing the following.

rapt discover

If anyone knows how to have it default to yes without prompting you for every repository, please post a comment.

Alternatively, you can add only the repositories you want to use by executing the following.

rapt source http://path-to-repository