Configuration - a recurring nightmare for many developers and devops alike. How do we provide configuration variables for a Ruby on Rails application? With occson, it’s easy.
First things first: let’s create a new Ruby on Rails app. We’ll call it
envie - since it’ll show us its environment variables. If this is nothing new to you, feel free to skip to the juicy part!
Let’s also create a controller in
And a view in
We’ll set up our routes in the usual place -
We need to start our application server,
Our Ruby on Rails app will now show us its environment variables.
Now let’s pretend that after some time spent developing it, our app turns out to need a certain configuration setting. We’ll of course provide that through an environment variable, for maximum flexibility. We’ll call this variable
If we’re just running locally, we can just provide this variable on the command line:
If we navigate to the app again, we’ll see
FOO: 1. Success!
Well… for now. But what if our app needs, say, twenty seven environment variables? We definitely don’t want to be passing all that to the application server command!
This is where occson comes in. Like a superhero to the rescue, it configures our application on demand.
Let’s set up an account on
After inputting our e-mail address we’ll get a message with a sign-in link. Occson uses a passwordless sign-in method - it’s like magic, but better.
To start working with occson, we’ll need a workspace.
To communicate with occson from code, we’ll need an access token.
Time to create a configuration file that we will provide to our application. We can create it two ways. Through a browser:
or through the command line. Since that’s where us developers live, let’s see how that’s done.
First, just install the gem:
We create the configuration file locally:
And then we push it to occson:
“That’s cool for an example,” you might say, “but there’s no way I’m just pushing my production variables somewhere, there’s super-secret stuff in there!” That’s where more occson magic comes in.
The file you upload is encrypted right on your machine (yes, even if you’re using the web interface)! The document passphrase is the secret that you have, and we don’t. You can read our docs to see how it’s done. And if you’d rather see the code, check out our gem’s reference implementation on GitHub.
With that out of the way, let’s integrate occson into our app. It’s as simple as dropping the
occson gem in the
bundle it all up,
And we’ll create an initializer in
config/initializers/001_occson.rb to fetch and set up our environment variables.
When we now run the application server,
our app shows the
FOO variable correctly!