Skip to content

Instantly share code, notes, and snippets.

@harshals
Forked from jerowe/gist:f71fae928cef9a8b6c2c
Created January 8, 2016 10:58
Show Gist options
  • Save harshals/5c7645cc525299817b4c to your computer and use it in GitHub Desktop.
Save harshals/5c7645cc525299817b4c to your computer and use it in GitHub Desktop.
Catalyst with Perlbrew Setup

Overview

Perlbrew is a way of managing perl versions and application specific libraries.

In general it doesn't require root, unless you want it installed in a location only writable by root. ;)

Its actually not such a great idea to have this in a home directory if you want it shared by multiple users, such as nginx/apache, so it may be a better idea to have your $PERLBREW_ROOT as /opt/perlbrew or something similar

CPAN documentation

Perlbrew install script

PERLV="perl-5.16.3"

#The default for this is $HOME
# export PERLBREW_ROOT=/opt/perlbrew

export PERLBREW_HOME=${HOME}/.perlbrew
export PERLBREW_ROOT=${HOME}/perl5/perlbrew

source $HOME/.bashrc
if [ ! -d $PERLBREW_ROOT ]; then
    #mkdir -p $PERLBREW_ROOT
    #cd $PERLBREW_ROOT
    curl -kL http://install.perlbrew.pl | bash

#If you want this in your home envir
    echo 'source ${PERLBREW_ROOT}/etc/bashrc' >> $HOME/.bashrc
    source ${PERLBREW_ROOT}/etc/bashrc
    source ~/.bashrc
    #perlbrew mirror
    #Perlbrew available will show the different versions available
    #perlbrew available
    perlbrew install --notest --thread --multi --debug $PERLV
    perlbrew switch $PERLV
    perlbrew install-cpanm
    cpanm App::cpanoutdated
else
#Uncomment this if you want updates of all perl modules
    cpanm App::cpanoutdated
    cpan-outdated | cpanm
    perlbrew switch $PERLV
fi

perlbrew switch $PERLV
#If you want application specific libraries...
#From the commandline you can do 
#perlbrew lib create perl-5.16.3@myapp
#perlbrew switch perl-5.16.3@myapp

export PERLL=myapp
export PERL5LIB=${PERLBREW_HOME}/libs/$PERLV@$PERLL/lib/perl5
export LD_LIBRARY_PATH=$PERL5LIB:$LD_LIBRARY_PATH

#This does not work directly
#export PERLBREW_LIB=myapp

#When in doubt on where things are installing do a 
# env |grep -i perl
# Your variables should look something like this
# PERLBREW_LIB=myapp
# PERLBREW_ROOT=/opt/perlbrew
# PERLBREW_HOME=${PERLBREW_ROOT}/.perlbrew
# MANPATH=${PERLBREW_ROOT}/perls/perl-5.16.3/man:/usr/local/share/man:/usr/share/man/en:/usr/share/man:/usr/local/man
# PERLBREW_VERSION=0.66
# PERLBREW_PERL=perl-5.16.3
# PERLBREW_LIB=myapp
# PERL5LIB=${PERLBREW_ROOT}/.perlbrew/libs/perl-5.16.3@myapp/lib/perl5
# PERL_MB_OPT=--install_base ${PERLBREW_ROOT}/.perlbrew/libs/perl-5.16.3@myapp
# PERL_MM_OPT=INSTALL_BASE=${PERLBREW_ROOT}/.perlbrew/libs/perl-5.16.3@myapp
# PATH=${PERLBREW_ROOT}/bin:${PERLBREW_ROOT}/perls/perl-5.16.3/bin:$PATH
# PERL_LOCAL_LIB_ROOT=${PERLBREW_ROOT}/.perlbrew/libs/perl-5.16.3@myapp

cpanm --notest Module::Install
#requires use of Test::TCP which needs to bind to a random port on localhost - not allowed with openshift
cpanm --notest Catalyst::Devel
cpanm --notest Catalyst::ScriptRunner
cpanm --notest Term::ReadKey Log::Log4perl::Catalyst Catalyst::Restarter Catalyst::View::TT
cpanm --notest HTML::FormHandler
cpanm --notest HTML::FormHandler::Model::DBIC
cpanm --notest File::Find::Rule
cpanm --notest MongoDB
cpanm --notest Carp::Always Devel::SimpleTrace
cpanm --notest File::Find::Rule

#Only need this for fastcgi
cpanm --notest Moose
cpanm --notest FCGI FCGI::ProcManager

cpanm --notest CatalystX::RoleApplicator
cpanm --notest Catalyst::TraitFor::Request::ProxyBase
cpanm --notest Catalyst::Plugin::Session::State::Cookie
cpanm --notest Catalyst::Plugin::Session::Store::File
cpanm --notest Catalyst::Authentication::Store::LDAP
cpanm --notest Catalyst::Plugin::StackTrace
cpanm --notest Catalyst::Plugin::Params::Nested
cpanm --notest Catalyst::Helper::Model::DBIC::Schema
cpanm --notest DBIx::Class::Schema::Loader
cpanm --notest MooseX::MarkAsMethods
cpanm --notest MooseX::NonMoose

cpanm --notest YAML
cpanm --notest Path::Class
cpanm --notest DBD::mysql
cpanm --notest Catalyst::Plugin::Redirect
cpanm --notest List::Uniq

Now at any time you can run

perlbrew switch perl-5.16.3 #or perl-5.16.3@myapp
And this will set your paths fine

Run from a bash script

Or if you would like to source everything from a bash script

#!/bin/bash

## /var/www/MyApp/script/myapp_server.sh

export PERLBREW_HOME=${HOME}/.perlbrew
export PERLBREW_ROOT=${HOME}/perl5/perlbrew
#export PERLBREW_HOME=/data/apps/software/perl/.perlbrew
#export PERLBREW_ROOT=/data/apps/software/perl
source ${PERLBREW_ROOT}/etc/bashrc
PERLV="perl-5.16.3"
V="5.16.3"
PERLL="myapp"

export PERL5LIB=$PERLBREW_HOME/libs/$PERLV@$PERLL/lib/perl5
export LD_LIBRARY_PATH=$PERL5LIB:$LD_LIBRARY_PATH

#Any perl scripts must have the line #!/usr/bin/env perl
/var/www/MyApp/script/myapp_server.pl -r -p 8080

Setup as a service

On centos this goes into /etc/init.d/web_myapp

We are using this setup with nginx for production.

#!/bin/bash

# web_myapp Startup script for MyApp Web Server
#
# description: The MyApp Web Server
# processname: myapp_server.sh

PROG=myapp
SCRIPT="/var/www/MyApp/script/myapp_server.pl"
PORT_NUM=5000
PIDFILE=/var/run/${PROG}.pid
USER=apache

MyApp_prod=1
export MyApp_prod

PERLV="perl-5.16.3"
V="5.16.3"
PERLL="myapp"

export PERLBREW_HOME=/home/jillian/.perlbrew
export PERLBREW_ROOT=/home/jillian/perl5/perlbrew

#Does not work within a service
#source ${PERLBREW_ROOT}/etc/bashrc

export PERLBREW_PERL=$PERLV
export PATH=${PERLBREW_ROOT}/bin:${PERLBREW_ROOT}/perls/$PERLV/bin:$PATH
export PERLBREW_PATH=${PERLBREW_ROOT}/bin:${PERLBREW_ROOT}/perls/$PERLV/bin

export PERL5LIB=${PERLBREW_HOME}/libs/$PERLV@$PERLL/lib/perl5
export LD_LIBRARY_PATH=$PERL5LIB:$LD_LIBRARY_PATH

PID=`lsof -i:${PORT_NUM} | grep LISTEN | awk '{print $2}'`

#This one checks if its already running
start () {
    #echo PROG is on $PID
    if [ -z "$PID" ] ;  then
        runweb
    else
        echo "$PROG already running on $PID"
    fi  
}

#This is if we want to just start it anyways
runweb () {
    echo "Starting $PROG Web Services"
    nohup $SCRIPT -r -p $PORT_NUM > /tmp/${PROG}_server.log 2>&1 &
    # Wait for services to startup
    sleep 3
}
stop () {
    if [ ! -z "$PID" ] ;  then
        echo "Stopping $PROG Web Service"
        ps -ef | grep "$SCRIPT" | grep -v grep | awk '{print $2}' | xargs kill
        sleep 3
    else
        echo $PROG not running
    fi  
}

status () {
    if [ ! -z "$PID" ] ;  then
        echo "$PROG running on PORT $PORT_NUM pid=$PID"
    else
        echo $PROG not running
    fi  
}

case "$1" in
    restart)
        echo "Restarting $PROG Service"
        stop
        runweb
        ;;
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: `basename $0` {start|stop|restart|status}"
        exit 1
esac

exit 0

# end

Manage your sessions with Tmux and Tmuxifier

Just add the bits above with exporting the correct variables to the last script shown.

Catalyst and Tmux

This is what I use in my development environment.

Acknowledgements

This module was originally developed at and for Weill Cornell Medical College in Qatar within ITS Advanced Computing Team. With approval from WCMC-Q, this information was generalized and put on github, for which the authors would like to express their gratitude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment