Dec 29, 2006

Setting environment variables for mongrel


We were going to use Verisign's PayFlowPro merchant facilities and implemented a solution based around a ruby library called pfpro4r. A problem came up though. We needed to have a shell environment variable set in order for this to work. This was fine when I started mongrel manually but when started via Capistrano the environment variable wasn't being set by the mongrel start script.

You can't seem to set environment variables in the shell Capistrano uses so I simply added a couple of tasks to deploy.rb. These override the tasks that come with mongrel_cluster and set the required variables.

I think it's great that you can add or override tasks on a project by project basis.
I guess someone distributing their app could include a library like deprec and include it from their deploy.rb to make deployment that much easier.


desc <<-DESC Start Mongrel processes on the app server. This uses the :use_sudo variable to determine whether to use sudo or not. By default, :use_sudo is set to true. DESC task :start_mongrel_cluster , :roles => :app do
set_mongrel_conf
send(run_method, "sh -c '
LD_LIBRARY_PATH=/var/www/apps/example_app/current/vendor/libs/pfpro4r/verisign/payflowpro/linux/lib;
export LD_LIBRARY_PATH;
PFPRO_CERT_PATH=/var/www/apps/example_app/current/vendor/libs/pfpro4r/verisign/payflowpro/linux/certs;
export PFPRO_CERT_PATH;
mongrel_rails cluster::start -C #{mongrel_conf}
'
")
end

desc <<-DESC
Restart the Mongrel processes on the app server by starting and stopping the cluster. This uses the :use_sudo variable to determine whether to use sudo or not. By default, :use_sudo is set to true.
DESC
task :restart_mongrel_cluster , :roles => :app do
set_mongrel_conf

send(run_method, "sh -c '
LD_LIBRARY_PATH=/var/www/apps/example_app/current/vendor/libs/pfpro4r/verisign/payflowpro/linux/lib;
export LD_LIBRARY_PATH;
PFPRO_CERT_PATH=/var/www/apps/example_app/current/vendor/libs/pfpro4r/verisign/payflowpro/linux/certs;
export PFPRO_CERT_PATH;
mongrel_rails cluster::restart -C #{mongrel_conf}
'
")
end

1 comment:

Kramer said...

Thanks, this was helpful to me!

- Eric Kramer (Nationwide Children's Hospital)