per-app environment variables with pow and rbenv

25-09-2011 @ 16:03   ·  By Paul   ·  Tagged: ruby

We've been switching our production and dev machines over from rvm to rbenv, Sam Stephenson's lightweight new ruby version manager. However with RVM we'd been taking advantage of the per-directory .rvmrc files to set per-application environment variables for configuration (e.g. DB URIs, API keys, etc.) on our dev machines. rbenv by contrast works through "shims" which are used only when you run a shim'ed ruby command

Luckily rbenv is (quickly being) built to be easily extended through hooks and plugins. Here all we need is a simple hook. Hooks are added per-command in rbenv, and in this case we want to hook into the 'exec' command, so we can export variables before e.g. the rails command is exec'd by the shim:

mkdir ~/.rbenv/rbenv.d/exec/

Any *.bash scripts added or linked into this directory will be run before an exec, so we'll just add a file ~/.rbenv/rbenv.d/exec/pow.bash:

#!/bin/bash

if [ -f $(pwd)/.powenv ]; then
  source $(pwd)/.powenv
fi

That'll load our .powenv file, if it exists. This way we can put any per-app configuration in the .powenv file and it'll be loaded by pow for web requests and rbenv for any ruby shell command:

MONGO_URL=mongodb://user:password@localhost/db_name
COOL_API_KEY=abcdefg

Obviously if this is for production/you're not using pow, modify the paths as desired

Update: Sam Stephenson's got a better solution here

blog comments powered by Disqus

© Copyright 2012 | All Rights Reserved | Pogodan Ltd | Registered in England 07429329