Apr 10, 2012

Perl on dotCloud


Yes, it is !!! Soooo easy to deploy a demo about my Perl webapp on dotCloud.

Here it is: pnidemo-fibo.dotcloud.com



Powered by CPAN and thanks to this Mojolicious trick, all stuff I need is packed in my PNI distro, included JavaScript libs like jQuery and Kinetic, .css and other assets.


I red this article Mojolicious in the cloud, but it is outdated, since dotcloud "run" command seems to be deprecated.

My dotCloud app is called "pnidemo" and it is just a thin wrapper around my pni Mojolicious app.

I putted all inside a misc/dotcloud dir in my github repo.

Before you start, to make it work, stand up and click the next link

1. Install dotcloud CLI, refer to dotCloud docs.

2. Create dotcloud app.
$ dotcloud create pnidemo

3. Configure it.
$ mkdir -p misc/dotcloud
$ cd misc/dotcloud
$ vim dotcloud.yml

www:
    type: perl
    approot: pnidemo

4. Add your deps, included your CPAN distro, for instance PNI.
Following dotCloud docs, Plack, Test::More and YAML should be always included.
$ mkdir pnidemo
$ cd pnidemo
$ vim Makefile.PL

#!/usr/bin/env perl
use ExtUtils::MakeMaker;
WriteMakefile(
    PREREQ_PM => {
         'Plack'         => 0,
         'Test::More'  => 0,
         'YAML'       => 0,
         'PNI'           => 0,
   },
);

5. Copy original Mojolicious app and create an app.psgi that requires it.
$ cp ../../../bin/pni .
$ vim app.psgi

$ENV{MOJO_MODE}='production';
require 'pni';

6. Now just deploy it with dotcloud CLI.
$ cd ..
$ dotcloud push pnidemo

It will install all deps with cpanm, and that's all !


Ah, one last cool feature is that when you are ready to upgrade your app, just release it on CPAN and update the Makefile.PL with last version,

for instance
         'PNI'           => 0.30,

then launch
$ dotcloud push pnidemo

and you're done! Soooo easy (:

Feb 18, 2012

Install your own Perl with your own CPAN


Yes I know, it exists a featured tool called perlbrew but can happen that
  • your home dir has not enough space
  • you don't have an internet connection
  • you want to tweak your perl distro
  • you just want to DIY

In this article I talk about how to build your own Perl, and use your own CPAN mirror. 

Suppose your login is "user" on host "darkstar" that is a Linux box with Internet connection.
You can start there to build your Perl and mirror your own CPAN.
After that you can hop to hosts in your Intranet, I mean with no connection outside your Firewall.

You can draw your topology

DEV --> TEST --> PROD 

which are three hosts with custom Perl installation (latest). This is just an example topology that matches my job place policy. Adapt it to your needs, mutatis mutandis.
  • DEV is the first host, connected to Internet, with a freezed CPAN mirror serving to the TEST host.
  • The TEST host, mirrors CPAN from DEV which is test safe, specially for make installdeps.
  • The PROD host, runs Perl software tested in the TEST host.

Let's start now!


You can copy and paste to get your latest Perl installed, except for bold command lines where you have to choose you Perl root dir and your Perl version.

Create your Perl root folder, for instance /opt/Perl and set permissions properly,
probably you will need to use sudo.
[user@darkstar]$ export PERL_ROOT=/opt/Perl
[user@darkstar]$ sudo mkdir $PERL_ROOT
[user@darkstar]$ sudo chown user:group $PERL_ROOT

Create folder structure.
[user@darkstar]$ mkdir $PERL_ROOT/src
[user@darkstar]$ mkdir $PERL_ROOT/CPAN

Download latest Perl sources, for instance version 5.14.2,
and put them under $PERL_ROOT/src folder.
[user@darkstar]$ export PERL_VERSION=5.14.2
[user@darkstar]$ cd $PERL_ROOT/src
[user@darkstar]$ tar xzvf perl-$PERL_VERSION.tar.gz
[user@darkstar]$ cd perl-$PERL_VERSION

Now build your Perl !

Just use defaults and point to your folders.
[user@darkstar]$ sh Configure -des -Dprefix=$PERL_ROOT/$PERL_VERSION -Dscriptdir=$PERL_ROOT/$PERL_VERSION/bin

And pray the mantra, make ... make test ( DON'T MISS IT ! ) ... make install.
[user@darkstar]$ make
[user@darkstar]$ make test
[user@darkstar]$ make install

Clean up.
[user@darkstar]$ cd ..
[user@darkstar]$ rm -rf perl-$PERL_VERSION

Now add this symbolic link, it will be useful in the future to switch to latest Perl version.
[user@darkstar]$ ln -s $PERL_ROOT/$PERL_VERSION $PERL_ROOT/current

Set up your env, for instance add these lines to your bash profile ...
export PERL_ROOT=/opt/Perl
export CPAN_ROOT=$PERL_ROOT/CPAN
export CPAN_MINI_CONFIG=$PERL_ROOT/.minicpanrc
export PATH=$PERL_ROOT/current/bin:$PATH

and reload it.
[user@darkstar]$ source $HOME/.bash_profile

You can check if you are pointing to the right Perl.
[user@darkstar]$ perl -v

You can upgrade Perl simply repeating installation and using the sympolic link to point to latest version.

Habemus Perl, now it's time to update CPAN !

Since this is the first time you use the cpan shell, it will prompt you for configuration: just use defaults, i.e. type <ENTER> as in an '80s shoot em all :)
[user@darkstar]$ cpan CPAN

Install CPAN::Mini
[user@darkstar]$ cpan CPAN::Mini

Now you can set up your local mirror using your favourite editor (of course vim :).
[user@darkstar]$ vim $CPAN_MINI_CONFIG

local: $ENV{CPAN_ROOT}
remote: REMOTE_CPAN_MIRROR
exact_mirror: 1

Put exactly $ENV{CPAN_ROOT}, it works !

REMOTE_CPAN_MIRROR  is a working CPAN mirror,
type the following to know your current list ...
[user@darkstar]$ cpan
cpan> o conf urllist

the last entry should work (type q to exit the cpan shell).

It's time to update your mirror, just type ...
[user@darkstar]$ minicpan

since it takes a lot of time to mirror all the stuff :), so go out and have a beer.

Ok, ok ... maybe you should type instead
[user@darkstar]$ nohup minicpan > /dev/null &

so you can go holiday :)

After a while evaluate this pseudo code

if( pickedUpGirl || pickedUpBoy ) 
    raise Exception;
} else { 
    return toHaveFun; 
}

I assume pickedUpGirl is false :-).

Now you can configure cpan shell to point to your local CPAN mirror
[user@darkstar]$ cpan
cpan> o conf urllist
cpan> o conf urllist file:///path/to/local/cpan
cpan> o conf commit

where path/to/local/cpan is the absolute path to your local CPAN mirror, i.e. what you set for CPAN_ROOT in your .bash_profile.

Your DEV host is ready, now repeat Perl installation on TEST and PROD hosts.

Develop your software on DEV host.

Pack your software as a compliant CPAN distribution.
This helps organize your code, add documentation, write tests, include scripts.

Then use CPAN::Mini::Inject to add your distro, to CPAN mirror on DEV and make it installable via cpan client.
[user@darkstar]$ cpan CPAN::Mini::Inject
[user@darkstar]$ mcpani -H

And read the mcpani man ... I'm sorry but right now I don't remember the options :-P


On DEV and TEST host install CPAN::Mirror::Server::HTTP, it works great!

[user@darkstar]$ cpan CPAN::Mirror::Server::HTTP
[user@ darkstar]$ cpanmirrorhttpd --root $CPAN_ROOT

Yep, there is a running CPAN listening to 8080 port.
Install and configure minicpan on TEST.

Mirror CPAN on TEST host from DEV host if your distro tests on DEV are ok.
Launch cpanmirrorhttpd on TEST host
Configure cpan client on PROD host to point to TEST host on 8080 port.

Summary

On DEV, TEST and PROD you have latest Perl release.

On DEV:
  • you develop your software packaged as a CPAN distro
  • there is a CPAN mirror, where you inject your distro
  • cpan client points to local CPAN
  • there is CPAN server on port 8080
On TEST:
  • there is a CPAN mirror, that points to DEV
  • cpan client points to local CPAN
  • install your distro using cpan client and run your software
On PROD:
  • cpan client points to CPAN mirror on TEST
  • run your software installed via cpan client

Come on guys, Perl is awesome !!!