Capistrano 2.0

Posted by topher
on Sunday, July 22

Capistrano 2.0 is out. Check the announcement here.

Capistrano is great for automating tasks via SSH on remote servers, like software installation, application deployment, configuration management, ad hoc server monitoring, and more.

Learn more about capistrano on it’s website.

I like to share my deployment recipe. After running “capify .” on the app directory, I got the file config/deploy.rb. Initially it looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set :application, "set your application name here" 
set :repository,  "set your repository location here" 

# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
# set :deploy_to, "/var/www/#{application}" 

# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion

role :app, "your app-server here" 
role :web, "your web-server here" 
role :db,  "your db-server here", :primary => true

After putting in the values and adding some tasks, my deploy.rb looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
set :application, "topsecret"
set :repository,  "http://svn.example.com/topsecret/trunk"

# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
set :deploy_to, "/home/topher/apps/#{application}"

role :app, "topher.88-mph.net"
role :web, "topher.88-mph.net"
role :db,  "topher.88-mph.net", :primary => true

set :mongrel_config, "/etc/mongrel_cluster/#{application}.yml" 

namespace :deploy do
  desc "Link in the *.yml files and rails."
  task :after_update_code do
    run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml" 
    run "ln -nfs #{deploy_to}/shared/config/mongrel_cluster.yml #{release_path}/config/mongrel_cluster.yml" 
    run "ln -nfs /home/topher/rails_src/rails_1_2_stable #{release_path}/vendor/rails"
  end

  desc "Restart the mongrels."
  task :restart do
    sudo "mongrel_rails cluster::restart -C #{mongrel_config}" 
  end
end
  • :application is the app name
  • :repository is the svn repository
  • :deploy_to is the path to the app in the server
  • roles :app, :web, and :db is the server. I’m using one server for the app, web, and db so they’re all the same.
  • :mongrel_config is config file to be used by the mongrel_cluster gem
  • I create symlinks to config/database.yml and config/mongrel_cluster.yml.
  • I use rails 1.2.3 for this app but the server has 1.1.6 installed. You can check in vendor/rails to svn (or svn externals) but I decided to put rails to a directory outside of the app so it can be shared by multiple apps. I just create a symlink.
  • I restart my mongrel cluster using the cluster::restart. The mongrel_cluster gem includes a recipe but so far it only works with capistrano 1.4. (Thanks to topfunky for this.)

To deploy I just run “cap deploy” on my development machine. To deploy with migrations run “cap deploy:migrations”.

Plugin: attachment_fu

Posted by topher
on Thursday, July 19
attachment_fu is a rails plugin written by Rick Olson. From the README,

attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.

Check out Mike Clark’s tutorial.

On windows, when I submit the form with the file upload, I get an error “Size not included in list”. Upon inspection, the size is zero. (The default minimum size is 1 byte.) The size of the file uploaded isn’t set properly. I found somewhere on the beast forum that you need to add “sleep 1” on your controller. I haven’t tried to use attachment_fu on my server (ubuntu) and I’m hoping this is only a problem with windows.

My friend Mon, also a rails developer, encountered another problem with attachment_fu. He added a user_id in the model which uses attachment_fu. When he saves the uploaded file, he gets an error saying that user_id is null. We’re absolutely sure there’s a user_id.

Here’s what happened: attachment_fu can create a thumbnail(s) for you when you save the uploaded image (you will need an image processor like image magick). If you don’t specify a thumbnail_class, attachment_fu will use the same model for the thumbnail. So if you have a Mugshot model, after submitting the form, 1 model will be created for the uploaded image, and another one will be created for the thumbnail. So even if you add user_id to your model, the thumbnail won’t have a user_id. You need to add a callback before_thumbnail_saved on your model.

1
2
3
before_thumbnail_saved do |record, thumbnail|
  thumbnail.user_id = record.user_id
end

Install the plugin with

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/

Editing models in migrations

Posted by topher
on Friday, July 13

In migrations, you can also edit your models. I have a Topic and a Post. A topic has many posts. The models are already created using previous migrations. Now I want to add 3 new fields to Topic—posts_count, replied_by, and replied_at.

Here is the migration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class AddCachedFieldsOnTopic < ActiveRecord::Migration
  class Topic < ActiveRecord::Base
    has_many :posts, :order => "posts.created_at"
  end
  class Post < ActiveRecord::Base; end
  
  def self.up
    add_column :topics, :posts_count, :integer, :default => 0
    add_column :topics, :replied_by, :integer
    add_column :topics, :replied_at, :datetime
    
    Topic.find(:all, :include => :posts).each do |topic|
      last_post = topic.posts.last
      posts_count = topic.posts.size
      Topic.update_all(["posts_count = ?, replied_by = ?, replied_at = ?", posts_count, last_post.user_id, last_post.created_at], ["id = ?", topic.id]) if last_post
    end
  end

  def self.down
    remove_column :topics, :posts_count
    remove_column :topics, :replied_by
    remove_column :topics, :replied_at
  end
end
Notes
  • posts_count will be used by rails for caching. In the post model, I’ll put :belongs_to :topic, :counter_cache => true. Set the default to 0.
  • You can actually get these fields through the association—topic.posts.count, topic.posts.last.user, topic.posts.last.created_at – but I’m adding it to Topic to avoid the additional queries.
  • I’m looking at (copying) the beast application.

Server Setup

Posted by topher
on Thursday, July 05

I have a VPS from Rimuhosting and I’m using nginx and a cluster of mongrel for my rails applications. I followed this tutorial which guides you on setting up everything – ruby, rails, nginx, mongrel, postgresql, subversion and capistrano. I’m sharing my vps with some friends and ruby, rails, and mysql were already installed. I’m using Dreamhost for subversion. Setting up nginx and mongrel is easy. I just followed the tutorial and didn’t encounter any problem.

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, Camping, and IOWA frameworks.

I use nginx for this reason (taken from the faq page in mongrel website):

Ruby on Rails is not thread safe so there is a synchronized block around the calls to Dispatcher.dispatch. This means that everything is threaded right before and right after Rails runs. While Rails is running there is only one controller in operation at a time. This is why people typically have to run a small set of Mongrel processes (a “Pack of Mongrels”) to get good concurrency.

For a rails application, I run 2 mongrel servers. I use the gem mongrel_cluster to manage these mongrels (this is in the tutorial). For example, mongrels are running in port 7500 and 7501. When a request is made, nginx passes that request to one of the mongrels. For bigger applications, use more mongrels.

Since I’m sharing the VPS with my friends, I can’t change the web server easily. Besides, they are running php and though it’s possible to run php in nginx I haven’t researched that yet. We are using Apache 2.0 which is not easy to set up with mongrel. Apache 2.2 is recommended. I have to run nginx on a different port, say 8088. Then on the apache conf file, I put this

1
2
3
4
5
6
7
8
9
10
<VirtualHost *:80>
        ServerName topher.88-mph.net
        ServerAdmin crigor@gmail.com
        ProxyRequests Off
        ProxyPreserveHost on
        <Location />
          ProxyPass http://topher.88-mph.net:8088/
          ProxyPassReverse http://topher.88-mph.net:8088/
        </Location>
</VirtualHost>

When you go to topher.88-mph.net, apache gets the request then passes it to ngingx which in turns passes it to one of the mongrels. Apache is not really needed on this set up.