INCLUDE_DATA

Entries Tagged 'Rails' ↓

metric-fu installation

Installing metric-fu is a really manual process. Copy the following and paste it in your shell:

sudo gem install flay

sudo gem install flog

sudo gem install rcov

sudo gem install railroad

sudo gem install reek

sudo gem install roodi

sudo gem install jscruggs-metric_fu -s http://gems.github.com/

Rails, passenger, ubuntu and EC2 - Part 1

I’ve been spending the better part of a year working on MoreDating, a dating site with a twist. It was and still is a joy to work on. I was asked to write my experience in deploying to EC2.

Let me start by saying that I am really impressed with EC2. It is super quick, and so far has been very stable. Let’s start with the technicalities.

Technical Specs:

  1. Ubuntu 8.04 Hardy
  2. Passenger 2.06
  3. Enterprise Ruby
  4. Large EC2 instance (8GB of RAM, 900 and change GB storage) @ $0.40/hour
  5. Image storage to S3
  6. MySQL database running off an EBS (Elastic Block Storage) drive

In this installment I will be talking about how I got EC2 up and running, and the preliminary configuration of the environment.

EC2 up and running with Ubuntu

I started out by getting an Amazon account, of course. You sign up by visiting: Amazon AWS. I then headed on to http://alestic.com/ to get the AMI that I need. Note that to use the 64-bit versions, you will need to setup a large EC2 instance. Now when I set it up, there wasn’t a pretty shiny interface for AWS, so I used elastic fox, the EC2 plugin for firefox (ElasticFox).

To spin up a new instance with all the necessary permissions, follow the Amazon EC2 Getting Started Guide . When it comes to the part about running an AMI, put in the ami path from the image you would like to use from alestic.

Once it’s up an running and you’re all connected, you can now start to configure it to your needs. Once we’re done with the configuration, we’ll go through taking an image of your instance so that you can spin other identical instances and to have a backup in case your instance goes haywire.

Configuring the goodies

  1. Install apache. Now, don’t go install any old apache, install the cool mpm-worker version. More information on that on the Apache Documentation Site . Install by running: sudo apt-get install apache2-mpm-worker
  2. Install ruby. Hold on, don’t go installing your vanilla ruby. Go to the good guys at phusion and download and install it. No vanilla ruby to install. Now we just need to make sure that it’s in the path server wide, so create a file in /etc/profile.d/ and call it ruby_path.sh and put this in it: export PATH=/opt/ruby/bin:$PATH. Now for this to work, make sure you have a symlink called /opt/ruby pointing to /opt/ruby/ruby-enterprise-lots-of-numbers-and-stuff-default-enterprise-installation-name. That way, whenever you install a new version of ruby enterprise edition, you just have to update the symlink.
  3. Install mysql (or postgres, or whatever). sudo apt-get install mysql-server (or whatever db server you’re going to use). Make sure you install the headers too.
  4. Little hack, I couldn’t figure out how to get it working properly. Whenever I try to install any gems using sudo, I have to provide the full path, so I made an alias for it in my .bash_profile: alias sgi=”sudo /opt/ruby/bin/gem install”. So now I just go: sgi some_gem
  5. Powered with this little hack, go ahead and run: sgi passenger. The installer is super friendly and will let you know exactly what is missing if you’re missing anything.

In my next post, I’ll be talking about capistrano setup, my deploy.rb script, and the costs of running EC2 full time + our use of S3 so far + our use of EBS.

Here’s a sample of the passenger and worker-specific of my apache2.conf and my bash_profile

ActionView::TemplateError (undefined method `adjust’ for #) on line #3 of vendor/plugins/exception_notification/views/exception_notifier/_timestamp.rhtml:

Got that one? Ha! Me too :)

Well apparently, Rails 2.1 doesn’t have TimeZone.adjust anymore, which is being called by vendor/plugins/exception_notification/views/exception_notifier/_timestamp.rhtml. All you have to do is replace those two lines:

time_zone = TimeZone.new(”Eastern Time (US & Canada)”)
timestamp = time_zone.adjust(Time.now)

and replace them with:

time_zone = ActiveSupport::TimeZone.new(”Eastern Time (US & Canada)”)
timestamp = Time.now.in_time_zone(time_zone)

A diff file is attached. Happy updating!

exception_notifier_tz_fix

vendor/rails/actionpack/lib/action_view/helpers/date_helper.rb:602:in `date_or_time_select’

Am I the only person in the world with this problem? I searched all over the web for a fix, but no one was even talking about it. The error I get is:

ArgumentError in My/profiles#show
wrong number of arguments (2 for 1)

I am running Rails 2.1, and putting a run-of-the-mill date_select crashes with the error shown in the title. I scratched my head at first, then banged it against the wall. I don’t know how this could be broken, but here is how I monkey-patched it.

Create a file in config/initializers and call it anythingyouwant.rb, and paste this in:

module ActionView
  module Helpers
    class InstanceTag #:nodoc:
      def to_date_select_tag(options = {}, html_options = {})
        date_or_time_select(options.merge(:discard_hour => true))
      end
    end
  end
end

That should fix it.

Up and running with Ubuntu, Ruby on Rails, thin, Nginx from scratch

My current most favourite hosting company is slicehost. When I start a new slice, I choose Ubuntu and get a shiny new virtual server with the default Ubuntu installation. Starting with that default installation, this step-by-step guide will give you a secure server with all the goodies mentioned above.

This example here is using Ubuntu 8.04.

1. Update your distro

sudo apt-get dist-upgrade

2. First things first, secure the server
ssh config:

sudo vi /etc/ssh/sshd_config

This is what I recommend:

  • Port: change to some port above 9000. That makes it just a bit more difficult to probe the server.
  • PermitRootLogin: change to no. You don’t ever want to login as root anyway. Login as a user and sudo instead.
  • LoginGraceTime: change to 20. That should be more than enough time for anyone to login.
  • RSAAuthentication: change to yes, so that you can use password-less logins using keys (nice when using Capistrano)
  • AllowUsers: you need to add this setting. Add account names of accounts that are allowed to login. Only users listed here will be able to login. Highly recommended.

Next, restart the ssh daemon, and test that you can still login. DO NOT LOG OFF UNLESS YOU HAVE TESTED THAT YOU CAN LOGIN FIRST :D. So, while still logged in, open another terminal and type:

ssh -p port_that_you_chose_above username@servername_or_ip_address

If all’s good, ssh configuration is complete.

Bastille Linux
This little gem is one of the most useful I know. It ensures that your server’s security is nice and tight.

sudo apt-get install bastille
sudo InteractiveBastille

Now just follow the instructions one by one, answering one question at a time. Make sure you also answer the firewall questions, and to enable it. When it’s all said and done, you’ll have a nice secure server.

3. Install build-essentials, and linux headers
We’ll need to be able to compile things, so we need this package. Note: some people believe that installing compilers on a production machine is an insecure practice, as it allows a potential hacker to compile their own attack binaries on the machine. If this is the case for you, do not install the package, and compile the binaries on another machine, then copy them to the proper locations on your production machine. We will be tightening security pretty nicely, so it is my opinion that this is not a big deal.

sudo apt-get install build-essential
sudo apt-get install linux-headers-

4. Now, let’s start by installing ruby and rubygems

sudo apt-get install ruby
sudo apt-get install rubygems

To be continued …

Upgrading to Rails 2.1 RC1 (with HAML)

Good morning one and all. Here is a step-by-step transcript of how I got my project from Rails 2.0.2 to 2.1 RC1.

1. Remove Rails 2.0.2 from the project and the svn repo:

svn rm vendor/rails
svn commit vendor/rails -m "[REMOVED] Rails 2.0.2 from repo;"

2. Checkout Rails 2.1 RC1 from the git repository, and then move to the 2.1 RC1 branch:

cd vendor
git clone git://github.com/rails/rails.git
cd rails
git checkout v2.1.0_RC1

3. Start the server:

I’m using HAML, so I got this error:

=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
Exiting
/opt/local/lib/ruby/gems/1.8/gems/haml-1.8.2/lib/haml/template/plugin.rb:42: undefined method `register_template_handler' for ActionView::Base:Class (NoMethodError)
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
	from /Users/aeldaly/Documents/clients/code/new_moredating/vendor/rails/activesupport/lib/active_support/dependencies.rb:508:in `require'
	from /Users/aeldaly/Documents/clients/code/new_moredating/vendor/rails/activesupport/lib/active_support/dependencies.rb:353:in `new_constants_in'
	from /Users/aeldaly/Documents/clients/code/new_moredating/vendor/rails/activesupport/lib/active_support/dependencies.rb:508:in `require'
	from /opt/local/lib/ruby/gems/1.8/gems/haml-1.8.2/lib/haml/template.rb:28
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
	from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
	 ... 44 levels...
	from /Users/aeldaly/Documents/clients/code/new_moredating/vendor/rails/activesupport/lib/active_support/dependencies.rb:508:in `require'
	from /Users/aeldaly/Documents/clients/code/new_moredating/vendor/rails/railties/lib/commands/server.rb:39
	from ./script/server:3:in `require'
	from ./script/server:3

A quick search on google, led me to this post: http://nex-3.com/posts/76-haml-2-0

So, I proceeded to update to HAML 2. Here goes:

sudo gem update

It updated quite a few gems, and yes, one of them was indeed HAML 2.0.

4. Start server, take 2
No errors!! You should now be running on Rails 2.1 RC1. Congratulations.

5. Commit to svn:

svn add vendor/rails
svn commit vendor/rails -m "[UPDATED] app to Rails 2.1 RC1"

ActiveRecord callbacks in specs?

While writing specs for my models, I sometimes found them failing for no apparent reason. After a bit of investigation, I found that my callbacks in the models where not being called. My solution? I call them myself, like so:

it "should set profile flag" do
  @photo.send(:before_create)
  @photo.save
  @photo.profile_pic = true
  @photo[:options][:profile_pic].should be_true
end

I find that this is a problem only with before_create and not with after_create. Not sure why this is hapenning, but this is waht worked for me.