Rails on Rackspace Cloud – Part 1

This is my recipe for starting up a new Rackspace Cloud Server and getting it set up for hosting Ruby on Rails applications.

Part one will take you from a new server to a working installation of RVM and Ruby. Part two will cover installing MySQL, Apache and Phusion Passenger.

If you don’t already have an account with Rackspace, I encourage you to go over to
http://www.rackspace.com/cloud/ and check them out.

I prefer Ubuntu Linux, so let’s start with that.

Starting a New Server

First, sign in to your Cloud Control Panel, then click Hosting, Cloud Servers.

Click the Add Server button, scroll down to Ubuntu 10.10 (Maverick Meerkat), then click the Select button.

Now type in a server name and select how much RAM you’ll need, then click the Create Server button.

After a short wait, your new server should be ready to go. You will receive an e-mail with your new server’s IP address and root password.

Connect to the IP Address with SSH, log in as root with the password provided, and lets get started.

Initial Login

The first thing you should do at this point is change the root password.

passwd

Type in your new root password twice. Now let’s add a new user account.

adduser testapp

Type in the information for your new user. Add the new user to the sudo group so you can execute commands as root.

adduser testapp sudo

Now that we have a new account to use, we need to deny the root user access via ssh. This will stop people trying to brute-force our root password.

vim /etc/ssh/sshd_config

Find the line that contains “PermitRootLogin” and change the value from “yes” to “no”. Restart the ssh server when you’re done.

/etc/init.d/ssh restart

We have set up a new user account with sudo access and denied root login via ssh, so let’s log out for now.

logout

New User Login

Now reconnect with your new user account and let’s make sure all of our software is up to date.

sudo apt-get update
sudo apt-get upgrade

This should be really fast. Rackspace’s internet connection puts mine to shame.

Installing RVM

Next, we’ll install git so we can install rvm.

sudo apt-get install git-core

Now copy and paste the command below to install rvm:

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Once this finishes, you will see a lot of instructions for setting up rvm as well as a nice “Thank you” from Wayne.

rvm automatically creates a file called .bash_profile in our home directory. This causes bash to skip loading
the regular .profile.

The only thing in .bash_profile is rvm configuration, so let’s just delete it…

rm .bash_profile

and add the configuration to .profile instead.

vim ~/.profile

Add this line at the very bottom:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

Finally “source” your .profile file so the change will take effect.

source ~/.profile

Installing Dependencies

Now we’ll install the rest of the packages we need to build ruby. RVM can provide us a list of needed software.

rvm notes

Look at the line starting with “For Ruby” to get a list of dependencies.

sudo apt-get install build-essential bison openssl libreadline6 \
                     libreadline6-dev curl git-core zlib1g zlib1g-dev \
                     libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev \
                     sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev \
                     ncurses-dev

Again, this shouldn’t take long.

Installing Ruby

We’re finally ready to install Ruby.

rvm install 1.9.2

We have Ruby 1.9.2 now, let’s use it.

rvm --default use 1.9.2

Installing Rails

Now that we have Ruby set up, we’re ready to install Rails. It should be as simple as this:

gem install rails

Note that when you’re using RVM, you do not have to precede this with “sudo”.

After another short wait, you should be all set.

Verify that the ruby and rails commands work as expected. In part 2 we’ll setup MySQL, Apache and Passenger.

Happy Pi Day

In honor of Pi Day, I thought I would write a little Ruby script to generate pi to a few decimal places.

A quick trip to the Wikipedia page for pi turned up many formulas for computing pi. I took one of the easy ones and set about writing some code.

Most of the formulas involve the factorial function. Unfortunately, Ruby doesn’t have a built in method for computing the factorial. After a little Googling, I found a nice, functional example at the Rosetta Code wiki.

Putting that all together, I ended up with this:

def fact(n)
  (1..n).reduce(1, :*)
end

sum = 0.0

(0..8).each do |n|
  a = fact(2 * n) ** 3.0
  b = 42.0 * n + 5.0
  c = fact(n) ** 6.0
  d = 16.0 ** (3.0 * n + 1.0)

  sum += (a * b) / (c * d)

  puts 1.0 / sum
end

Note that I’m only iterating 8 times. On my PC, that gives pi out to 15 decimal places which is all of the precision available in a floating point number.

Exploring ODBC with Ruby DBI

I recently found myself in an interesting situation.
I needed to extract data from a database. Unfortunately,
all I had to work with was an ODBC name.

If this were a MySQL database, I would have used the
command-line interface to check out the structure and
see what was available.

In this case, I didn’t have a command-line interface.
What I did have was Ruby and irb. Here’s how I got the
job done.

Required Gems

Three gems are required to make this work. First, we
need dbi and a driver (or dbd). The dbd-odbc driver also
requires the ruby-odbc gem.

dbi
dbd-odbc
ruby-odbc

In order to install ruby-odbc on Windows, you will need to
compile it from source. I used the excellent
DevKit
provided with the RubyInstaller and
had no problems.

Connect

Let’s assume that the ODBC Data Source Name is “TestData”.
First, we require the DBI library, then connect to the data
source.

require 'DBI'

dbh = DBI.connect("DBI:ODBC:TestData")

You can also pass a username and password after the connection
string if required.

List and Describe Tables

Now that we have a connection, let’s see what tables are
available.

dbh.tables

My test database only has one table. It’s called “Table1”.
Let’s list the columns in this table.

dbh.columns "Table1"

This will tell us the type of data stored in each field in
addition to the name.

Query For Data

In order to see the actual data, we can prepare and execute
an SQL query. In this case, the separate prepare and execute
is not really necessary, but it’s a good habit to get into.

sth = dbh.prepare("SELECT * FROM Table1")
sth.execute

row = sth.fetch

The fetch method will return a row of data at a time until
there is not more data. At that point it returns nil. Calling
fetch again after it returns nil will result in an exception.

A Complete Example

Here’s a simple example of connecting to the ODBC database and
displaying all of the data in a tab separated format.

require 'DBI'

dbh = DBI.connect("DBI:ODBC:TestData")

sth = dbh.prepare("SELECT * FROM Table1")
sth.execute

puts sth.column_names.join("\t")

while row = sth.fetch
  puts row.join("\t")
end

sth.finish
dbh.disconnect

You could also require the CSV class and use it to generate comma-separated
output.

To Hex and Back (With Ruby)

I am a big fan of plain text. It is easy to view and easy to edit. Unfortunately, it is sometimes necessary to work with binary files and data.

Ruby’s inspect method does a decent job of showing the contents of a binary string, but sometimes I need something a little more powerful.

Back in the day I would use a hex editor to open binary files and decipher their contents. I don’t have a need for a hex editor anymore, but I would like to occasionally view binary data in the same format.

After some intense Googling and Ruby doc reading, I came up with a few methods to convert a binary string to hex, and convert a string of hex back to the original binary.

Bin to Hex

To convert a string to it hex representation, first take each byte, convert it to hex, then join all of the hex digits back together.

def bin_to_hex(s)
  s.each_byte.map { |b| b.to_s(16) }.join
end

If you like spaces between the hex digits, change join to join(‘ ‘)

Hex to Bin

Converting the string of hex digits back to binary is just as easy. Take the hex digits two at a time (since each byte can range from 00 to FF), convert the digits to a character, and join them back together.

def hex_to_bin(s)
 s.scan(/../).map { |x| x.hex.chr }.join
end

If you find yourself using these frequently in a project, you could add the methods to the String class.

TIMTOWTDI

Of course, there is more than one way to do this. Ruby also provides the handy pack and unpack methods for Arrays and Strings respectively. These are a little more cryptic since you need to know the meaning of the format string to understand what’s going on.

def bin_to_hex(s)
  s.unpack('H*').first
end

def hex_to_bin(s)
  s.scan(/../).map { |x| x.hex }.pack('c*')
end

Check the Ruby documentation for Array and String for a complete explanation of pack and unpack.

Examples

Here’s the output of a quick IRB session to demonstrate how this works.

irb(main):001:0> s = "Hello, World!"
=> "Hello, World!"

irb(main):002:0> s = s.each_byte.map { |b| b.to\_s(16) }.join
=> "48656c6c6f2c20576f726c6421"

irb(main):003:0> s = s.scan(/../).map { |x| x.hex.chr }.join
=> "Hello, World!"

These methods are no replacement for a hex editor, but if you need to check an encryption key or some other short string of binary, they can be just the thing.

Ruby Generators

In my continuing quest to expand my knowledge of all things Computer Science, I stumbled across the Generator topic on Wikipedia. I ended up here after reading an excerpt from Python Cookbook.

The Wikipedia article describes a generator as something that looks like a function but behaves like an iterator. The interesting thing to me is that they can be used to build endless lists.

For example, the Python Cookbook gives several generators that will list all prime numbers (given enough time). The Wikipedia article gives another way of generating prime numbers.

I was surprised that Ruby wasn’t mentioned in the Wikipedia article so I set off on a search for information about Ruby generators. Unfortunately, most of what I found was dealing with code generators for Rails which is something else entirely.

The Ruby Documentation contains a description of the Generator class with a brief example. I was able to use this to convert Wikipedia’s simple prime number generator to Ruby.

require 'generator'

g = Generator.new do |g|
  n = 2
  p = []

  while true
    if p.all? { |f| n % f != 0 }
      g.yield n
      p << n
    end
    n += 1
  end
end

Using this code, each time you call g.next it will return the next prime number starting with 2. For example, this will print the first 25 prime numbers:

for i in 1..25
  puts g.next
end

If you examine the source code in both Python and Ruby you’ll find that they are remarkably similar. The only change is basically replacing Python’s any function with a call to Ruby’s Enumerable#all?.

The more I learn about Python and Ruby, the more similar I realize they are. I have yet to find anything in Python that can’t be done just as easily in Ruby or vice-versa.