Saturday, 30 April 2011

Catalyst in the Cloud

A lot of buzz was generated recently in the Perl community after DotCloud, a big PaaS player, introduced its brand new Perl stack, letting developers deploy modern Perl web applications into the "Cloud" without having to worry about the underlying system at all.

DotCloud already provides a nice Dancer example, and sri was diligent enough to provide a Mojolicious example as soon as he got his hands on an account. So when I got my own invitation to the service, I wondered how Catalyst would stand to the challenge.

After going through the whole thing in just a couple of minutes, all I can say is that:

If you were ever scared or concerned of using Catalyst due to its dependencies and how hard it would be to deploy... you're out of excuses =)


So, without further ado, here's the Catalyst DotCloud deploy process!

Install the "dotcloud" app in your local machine:

$ sudo apt-get install python-setuptools
$ sudo easy_install dotcloud

Create a DotCloud namespace (I called it "catalyst")

$ dotcloud create catalyst

Deploy a Perl stack into that namespace:

$ dotcloud deploy -t perl catalyst.www

Create a sample Catalyst app (skip if you already have one) and enter its directory:

$ catalyst.pl CatalystCloud
$ cd CatalystCloud

DotCloud uses the "static" dir for static files, so let's create a symbolic link there for Catalyst's root/static, or wherever else you placed your static files. There are probably other ways to customize this with DotCloud's standard configuration and its nginx server, but I just joined and haven't fiddled with any docs yet :)

$ ln -s root/static static

Add PSGI support for your Catalyst app. We could try the built-in FastCGI run script, but the recommended way is PSGI. To do that, simply install Catalyst::Engine::PSGI and create the new helper for your app, naming it "app.psgi" in your app's base directory:

$ cpan Catalyst::Engine::PSGI (or cpanm, or whatever you like)
$ script/catalystcloud_create.pl PSGI
$ ln -s script/catalystcloud.psgi app.psgi

The "app.psgi" file needs to know how to find your app, so add a use lib 'lib'; statement to it, right after "use warnings". The final file should look something like this (replacing "CatalystCloud" with your app's name, of course):

#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use CatalystCloud;
CatalystCloud->setup_engine('PSGI');
my $app = sub { CatalystCloud->run(@_) };

Remember to add the new dependency to your app's Makefile.PL, so DotCloud knows what's required when it deploys:

requires 'Catalyst::Engine::PSGI';

Your application is ready; let's push it to DotCloud:

$ dotcloud push catalyst.www .

Now sit back and watch as DotCloud smoothly walks through all of Catalyst's dependencies and deploys your app to the Web. Sweet!



Once you start working on your application, remember to keep your Makefile.PL up-to-date, adding any and all dependencies to it (like Catalyst::View::TT and Catalyst::Model::DBIC::Schema). But you already do that, don't you?

If you run into trouble, give "dotcloud logs catalyst.www" a try (again, replacing the catalyst.www namespace with your app's) and browse through you application's logs in real time. If you left the -Debug flag on, you can even see Catalyst's messages on the fly.

Many thanks are in order to the ever-awesome Miyagawa and the whole DotCloud team for making deploying Perl applications such a breeze =)

Have fun!!