30. März 2009

UIActivityIndicatorView in UINavigationBar

Here’s a short snippet I find myself using often when I have to communicate with web services: Adding an UIActivityIndicatorView to the bar of an UINavigationController, so that users are informed of the ongoing process in the background. Unfortunately you can’t plug this together in Interface Builder, so we have to do it in code.

- (void)viewDidLoad {
   [super viewDidLoad];
   // Add an activity indicator to the navigation bar
   UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityView];
   self.navigationItem.rightBarButtonItem = activityItem;
   [activityItem release];
}

This requires the activityView to be defined as a property of the view controller and setup in Interface Builder (hidden when stopped).

IBOutlet UIActivityIndicatorView *activityView;

When the communication with the web service start you can simply call

[activityView startAnimating];

22. März 2009

Rails deployment with Vlad, Git and Passenger

Now that Capistrano is discontinued, Vlad (at GitHub) seems to be the new cool deployment tool on the block. It works nicely with other usual suspects like Git and Passenger and uses Rake instead of reinventing the wheel – so let’s have a closer look.

Getting started

First of you need to install the Vlad gem: $ sudo gem install vlad

In its current version (1.3.2) Vlads default options are deploying from a SVN repository to a cluster of mongrels, which is not exactly what we are after. We need to configure it to use Git and Passenger, so switch to your Rails project and add the following lines to the end of the Rakefile:

begin
  require 'vlad'
  Vlad.load :app => :passenger, :scm => :git
rescue LoadError
  puts 'Could not load Vlad'
end

Run $ rake -T vlad to verify that Rake and Vlad know each other.

Now it’s time to set up your deployment configuration. Configuring Vlad is very similar to Capistrano, most of the variables are the same, so that you can easily migrate if you where using Capistrano before. Here is a simple configuration file including a deployment recipe which I’m currently using:

set :user, "myuser"
set :domain, "my.domain.com"
set :application, "myapp"
set :deploy_to, "/var/www/#{application}"
set :repository, "git@github.com:#{user}/#{application}.git"
 
namespace :vlad do
  desc "Symlinks the configuration files"
  remote_task :symlink_config, :roles => :web do
    %w(application.yml database.yml).each do |file|
      run "ln -s #{shared_path}/config/#{file} #{current_path}/config/#{file}"
    end
  end
 
  desc "Full deployment cycle: Update, migrate, restart, cleanup"
  remote_task :deploy, :roles => :app do
    Rake::Task['vlad:update'].invoke
    Rake::Task['vlad:symlink_config'].invoke
    Rake::Task['vlad:migrate'].invoke
    Rake::Task['vlad:start_app'].invoke
    Rake::Task['vlad:cleanup'].invoke
  end
end

Gotchas

It took me a while to get everything up and running because I encountered permission problems while trying to access the application repository from my deployment server. If you are running $ rake vlad:update and experience Git errors like fetch-pack from '...' failed, turn to Jordan Elvers blog post about it – he explains how you can solve this by setting up SSH agent forwarding.

Advancing

Need to go further and setup a multistage deployment environment? Check out this post on the Engine Yard blog, they solved this problem already.