Rails 4 cannot access environment variable but is available in shell and IRB

As per 12 factor app, I store multiple external API passwords in environment variables. This works great in development, but I got trapped in my production server. The environment variable is available in a method in mine ApplicationController

, which is available for all models.

Running Rails 4.0.2 using Puma 2.8.2 on Ubuntu 14.04. Ruby 2.1.2. Deployment with Capistrano 3.

For discussion, I'll call the environment variable PASSWORD and the value "somepassword".

In development : I am using dotenv

to load these passwords from .env

(of course not checked in source control).

  • From CLI I can run echo $PASSWORD

    and I get "somepassword" back
  • From IRB I can run ENV ['PASSWORD'] and I get "somepassword" back
  • Rails can successfully read ENV ['PASSWORD']

In production : I am using a recipe chef

to access an encrypted data packet plus a cookbook magic_shell

to securely load a password as an environment variable.

  • From CLI I can run echo $PASSWORD

    and I get "somepassword" back
  • From IRB I can run ENV ['PASSWORD'] and I get "somepassword" back
  • Rails doesn't read ENV ['PASSWORD']

Please note that in the production environment I am logged in as the same user who starts the daemon puma

and I can read the fine variable from the CLI and IRB.

Any ideas why Rails can't access a variable when I can manually access it from the CLI?

I could follow the path config/passwords.yml

(for example) that I encrypt and distribute with chef

, but setting up a working environment should work too.

+3


source to share





All Articles