I am writing deployment script, it needs to perform based on the system variable. Is there any way to read specific environmental variable through chef recipe ?
You can use Ruby native way to manage environmental variables (ruby-doc link).
Thus you can reference a environmental variable from your recipe directly using ENV['MY_VARIABLE'].
Related
I have a rails app and am trying to use an environmental variable (API key) inside of a controller and it is failing. Debugging shows it's value to be nil. Weird thing is that other keys from that file are accessible, so I am not really understanding why. They are all in my secrets.yml file.
I have tried accessing it using both ENV["STRIPE_TEST_SECRET_KEY"] and Rails.application.secrets.stripe_test_secret_key and both come back nil. I get the error:
No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support#stripe.com if you have any questions.
using the better errors gem. How do I make these available throughout my app?
You need to set STRIPE_TEST_SECRET_KEY in the environment running the Rails app.
Check out the Choices gem, it makes overriding env vars pretty easy.
You can set the env vars on the CLI in development like this:
$ STRIPE_TEST_SECRET_KEY=abc123 rails s
If you are using Rbenv, you can create a .rbenv-vars file in the root of your project that contains one env var per line like this:
DATABASE_URL=mysql://db_user:db_pass#localhost:3306/dev_db
STRIPE_TEST_SECRET_KEY=abc123
SOME_OTHER_VAR=foo
What is the best strategy in regards to checking in sensitive information into git? For example, database connection credentials, api keys, etc. For rails app, is it best to add environment files to .gitignore?
Thanks.
Your best bet is to use environment variables.
Check out these two links. The second link will show you how to keep your sensitive information secure by using environment variables to store sensitive data.
Rails Environment Variables
Environment Variables in Ruby on Rails
Nope, you don't need to ignore your env files - just remove all the secrets and use config variables ibstead... you can then refer to them using ENV['varname']
This gist shows one way of doing that using SECRET_KEY_BASE as an example (but you should do it with every sensitive key you have):
https://gist.github.com/cjolly/6265302
Here begins the road. I need to configure a working environment manageable and accessible. I've been reading a bit about environment variables. Windows have an easy configuration, as can be easily changed through window panes. In unix is different ...
export environment_variable = argument
eg. export DISPLAY = localhost: 0.0.
The question is: should I declare the environment variables when I install rvm? need to work with ruby, and not have to be writing the source myvar each time I start the console.
Same question for node and git.
Obviously I ask because in windows if I had to do, and I have unix doubt.
The short answer to your question is No. During the installation, setup and use of rvm, you will not be setting any environment-specific variables that relate to your application, nor for the rubies/gemsets/gems that rvm will be managing for you.
Once rvm is up and running, and you have installed at least one version of ruby (managed by rvm), there are a few options available to you for conveniently managing your environment variables.
a) You can use your unix shell config files (.bashrc, .bash_profile, etc.) to set env variables, but I don't recommend it. This is the equivalent of the Windows scenario you quoted in your question, but is not the common practice in the ruby-unix community.
b) If you are using Rails, environment-specific configurations can be managed in the source code itself, in environment files. For example, production.rb, development.rb, test.rb. etc.
c) If this is a Ruby codebase (i.e. no Rails) then, you can define your environment variables in an 'initializer' file, which can be invoked at the entry point of your ruby project. I typically put my application-specific variables in a yaml file like below.
In file: env_vars.yaml
---
:env_var_a: a.b.com
:env_var_b: 1111
:env_var_c: foo
:env_var_d: bar
To load these environment variables for use in your codebase,
require 'yaml'
env_vars = YAML.load(File.read(file_path('env_vars.yml')))
If you prefer to not use YAML, you can save the configs in a text file, and use ruby's native File class to access them. I like YAML because it easily creates a hash for me.
Regardless of which option you choose, there is no way you will need to type source every time you start a session
I wanna populate a variable based on the environment it is used. For example the username must be test in the development environment while something else in the production environment. How can I achieve this?
Now I understand your problem, you should simply create a yml file.
See this Railscast.
Look at these options.
YAML Configuration. Also related video (Railscasts Video)
Configatron (Github configatron)
Also related answers
I have a url that I am using in one of the controllers. Is there a better place to put this url? The url uses an API key and I was wondering if there is a better place to add this url and/or api key such that its not added in the controller class code and ergo more editable? If i add it as an environment variable or anything else how do i access it from my controller class? thank you. ITS A RUBY AND RAILS PROJECT
Using environment variables might be a good idea if you want to keep things like API keys and passwords out of your source code. Accessing them from within your code is done with the ENV object:
my_api_key = ENV['MY_API_KEY']
To use this technique, you need to set up the variables in your environment before launching your app, and how you do this depends on your local setup, and will likely also vary between development and production.
In development, you can simply set the environment vars in your shell, e.g. with bash:
$ export MY_API_KEY=foobar123abc
$ rails s
Now rails will start and have access to this environment variable. You can also set variables for just a single command:
$ MY_API_KEY=foobar123abc rails s
Depending on what the sevice/api is, you could set some of them to default development/test values in config/environments/development.rb (or test.rb):
ENV['MY_API_KEY'] = 'non_secret_api_key_that_can_be_shared_around'
Setting up environment variables in production will depend on how you're deploying your app. Phusion have an article on using environment variables in Passenger if your using that. There's also a useful article on using environment variables with Heroku which is worth a read even if you're not using them for deployment.
You can add it to application.rb file as
config.service {
:api_key => 'api_key'
}
Or better yet, add it to development.rb and production.rb files so that you can test it better.
You can access this api_key from controller like
Rails.application.config.service[:api_key]