The Rubyist's Guide to Environment Variables - Honeybadger

12
YOU'LL FIX BUGS BEFORE YOUR BOSS EVEN NOTICES THEM When people depend on your apps to work, you need Honeybadger. We give you complete visibility into production problems the moment they happen, and the tools you need to fix them. STARR HORNE HOW TO, RUBY The Rubyist’s Guide to Environment Variables If you want to be able to effectively manage web apps in development and in production, you have to understand environment variables. This wasn’t always the case. Just a few years ago, hardly anyone was configuring their Rails apps with environment variables. But then Heroku happened. Heroku introduced developers to the 12-factor app approach. In their 12-factor app manifesto they lay out a lot of their best practices for creating apps that are easy to deploy. The section on environment variables has been particularly influential. The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-

description

Author: Starr HorneIf you want to be able to effectively manage web apps indevelopment and in production, you have to understandenvironment variables.

Transcript of The Rubyist's Guide to Environment Variables - Honeybadger

  • YOU'LLFIXBUGS

    BEFOREYOURBOSS

    EVENNOTICESTHEM

    Whenpeople

    dependonyour

    appstowork,

    youneed

    Honeybadger.

    Wegiveyou

    complete

    visibilityinto

    production

    problemsthe

    momentthey

    happen,andthe

    toolsyouneed

    tofixthem.

    STARRHORNE HOWTO,RUBY

    The Rubyists Guide toEnvironment VariablesIf you want to be able to effectively manage web apps indevelopment and in production, you have to understandenvironment variables.

    This wasnt always the case. Just a few years ago, hardly anyonewas configuring their Rails apps with environment variables. Butthen Heroku happened.

    Heroku introduced developers to the12-factor appapproach. Intheir12-factor app manifestothey lay out a lot of their bestpractices for creating apps that are easy to deploy. The section onenvironment variables has been particularly influential.

    The twelve-factor app stores config inenvironment variables (often shortened to envvars or env). Env vars are easy to changebetween deploys without changing any code;unlike config files, there is little chance ofthem being checked into the code repoaccidentally; and unlike custom config files, orother config mechanisms such as Java SystemProperties, they are a language- and OS-

  • Takechargeofyourapps,

    FREE

    LATESTTWEETS

    The Rubyists Guide to

    Environment Variables:

    blog.honeybadger.io/ru

    by-guide-env2 DAYS AGO

    RECENTPOSTS

    The RubyistsGuide toEnvironment

    Variables

    Working withexceptionsin Pry

    Understanding theRuby ExceptionHierarchy

    More Rubyists are using environment variables than ever. Butoften its in a cargo-culty way. Were using these things withoutreally understanding how they work.

    This post will show you how environment variables really work and perhaps more importantly, how they DONT work. Well alsoexplore some of the most common ways to manage environmentvariables in your Rails apps. Lets get started!

    Every process has its own set ofenvironment variables

    Every program you run on your server has at least one process.That process gets its own set of environment variables. Once ithas them, nothing outside of that process can change them.

    An understandable mistake that beginners make is to think thatenvironment variables are somehow server-wide. Services likeHeroku sure make it seem likesetting the environment variablesis the equivalent of editing a config file on disk. But environmentvariables are nothing like config files.

    Every program you run on your server gets its own set ofenvironment variables at the moment you launch it.

    agnostic standard.

  • Every process has its own environment.

    Environment variables die with theirprocess

    Have you ever set an environment variable, rebooted and foundthat it was gone? Since environment variables belong toprocesses, that means whenever the process quits, yourenvironment variable goes away.

    You can see this by setting an environment variable in one IRBsession, closing it, and trying to access the variable in a 2nd irbsession.

    When a process shuts down, its environment variables are lost

    Plans and Pricing Sign In

    AngularJS for Rubyists

    AngularJS for Rubyists

    4 min to Spreed

  • This is the same principal that causes you to lose environmentvariables when your server reboots, or when you exit your shell. Ifyou want them to persist across sessions, you have to store themin some kind of configuration filelike .

    A process gets its environment variablesfrom its parent

    Every process has a parent. Thats because every program has tobe started by some other program.

    If you use your bash shell to launch vim, then vims parent is theshell. If your Rails app uses imagemagick to identify an image,then the parent of the program will be your Rails app.

    Child processes inherit env vars from their parent

    In the example below, Im setting the value of the $MARCOenvironment variable in my IRB process. Then I use back-ticks toshell out and echo the value of that variable.

    .bashrc

    identify

    Sign up for our Free video course on Angular.You'll get a new video every day in your email forfour days.

    Email Address

    Sign Up

    POWERED BY DRIP

  • Since IRB is the parent process of the shell I just created, it gets acopy of the $MARCO environment variable.

    Environment variables set in Ruby are inherited by child processes

    Parents can customize the environmentvariables sent to their children

    By default a child will get copies of every environment variablethat its parent has. But the parent has control over this.

    From the command line, you can use the env program. And inbash theres a special syntax to set env vars on the child withoutsetting them on the parent.

    Use the env command to set environment variables for a childwithout setting them on the parent

    If youre shelling out from inside Ruby you can also providecustom environment variables to the child process withoutlittering up your ENV hash. Just use the following syntax with the

    method:system

  • How to pass custom environment variables into Rubys systemmethod

    Children cant set their parentsenvironment variables

    Since children only get copies of their parents environmentvariables, changes made by the child have no effect on the parent.

    Environment variables are passed by value not by reference

    Here, we use the back-tick syntax to shell out and try to set anenvironment variable. While the variable will be set for the child,the new value doesnt bubble up to the parent.

  • Child processes cant change their parents env vars

    Changes to the environment dont syncbetween running processes

    In the example below Im running two copies of IRB side by side.Adding a variable to the environment of one IRB session doesnthave any effect on the other IRB session.

    Adding an environment variable to one process doesnt change it forother processes

    Your shell is justa UI for theenvironmentvariable system.

    The system itself is part of the OS kernel. That means that theshell doesnt have any magical power over environment variables.It has to follow the same rules as every other program you run.

    Environment variables are NOT the same

  • as shell variables

    One of the biggest misunderstandings happens because shells doprovide their own local shell variable systems. The syntax forusing local variables is often the same as for environmentvariables. And beginners often confuse the two.

    But local variables are not copied to the children.

    Environment variables are not the same as shell variables

    Lets take a look at an example. First I set a local shell variablenamed MARCO. Since this is a local variable, its not copied to anychild processes. Consequently, when I try to print it via Ruby, itdoesnt work.

    Next, I use the export command to convert the local variable intoan environment variable. Now its copied to every new process

  • this shell creates. Nowthe environment variable is available toRuby.

    Local variables arent available to child processes. Export converts thelocal variable to an environment variable.

    Managing Environment VariablesinPractice

    How does this all work in the real world? Lets do an example:

    Suppose you have two Rails apps running on a single computer.Youre using Honeybadger to monitor these apps for exceptions.But youve run into a problem.

    Youd like to store your Honeybadger API key in the$HONEYBADGER_API_KEY environment variable. But your twoapps have two separate API keys.

    How can one environment variable have two different values?

    By now I hope you know the answer. Since env vars are per-process, and my two rails apps are run in different processestheres no reason why they cant each have their own value for$HONEYBADGER_API_KEY.

    Now the only question is how to set it up. Fortunately there are afew gems that make this really easy.

  • Figaro

    When you install the Figaro gem in your Rails app, any values thatyou enter into config/application.yml will be loaded into the rubyENV hash on startup.

    You just install the gem:

    And start adding items to application.yml. Its very important thatyou add this file to your .gitignore, so that you dont accidentallycommit your secrets.

    Dotenv

    The dotenv gem is very similar to Figaro, except it loadsenvironment variables from .env, and it doesnt use YAML.

    Just install the gem:

    And add your configuration values to .env and make sure you gitignore the file so that you dont accidentally publish it to github.

    12# Gemfilegem "figaro"

    123

    # config/application.ymlHONEYBADGER_API_KEY: 12345

    123

    # Gemfilegem 'dotenv-rails'

    1 HONEYBADGER_API_KEY=12345

  • You can then access the values in your Ruby ENV hash

    You can also run commands in the shell with your pre-defined setof env vars like so:

    Secrets.yml?

    Sorry. Secrets.yml though cool doesnt set environmentvariables. So its not really a replacement for gems like Figaro anddotenv.

    Plain old Linux

    Its also possible to maintain unique sets of environment variablesper app using basic linux commands. One approach is to haveeach app running on your server be owned by a different user.You can then use the users .bashrc to store application-specificvalues.

    Working withexceptions in PryN

    1 ENV["HONEYBADGER_API_KEY"]

    1 dotenv ./my_script.sh

  • HONEYBADGER.IOBLOG Honeybadger.io Blog 2015

    Powered by WordPress Themify WordPress Themes

    L