Rails console won't load environment variables in secrets.yml - ruby-on-rails

I am trying to debug a problem with secrets.yml loading environment variables, by setting some environment variables in development and running rails c to inspect things. When I load Rails.applications.secrets this way, it is not picking up any of the environment variables I have set (namely, SECRET_KEY_BASE)
If I run the application with the same environment variables set, it picks them up fine (I'm using RubyMine to run the application, but running rails c from the terminal)
In my rails console, I can see the environment variable I've set using ENV['SECRET_KEY_BASE'], but it doesn't show up in Rails.application.secrets. Why?

TL;DR: spring stop
It turns out, as has happened so many times when things aren't making any sense, Spring is the culprit! I solved this problem (thanks to a related discussion) by running spring stop and then trying again, after which it worked perfectly!
Apparently Spring was caching the environment, or certain pieces of the Rails application, and neglecting to reload them when the environment variables changed.

Related

rbenv-vars, environment config file and Paperclip

I have an application running in Ruby on Rails. I use rbenv-vars to manage the environment variables used by the app and some of those variables are used in the environment config file to initialize AWS S3 storage setup in Paperclip's paperclip_defaults hash. However, recently I have updated the value of a S3-related variable in the .rbenv-vars file, restarted the application and Paperclip is always configured with the old (wrong) S3-related variable value. Oddly, the environment variable has the correct value (checked debugging the app and also using rails console) after Ruby environment startup. I temporarily fixed the issue by setting the variable AGAIN in ~/.bash_profile.
Has anyone ever experienced this? Any suggestions are welcome.
I suggest you to used Dot ENV GEM
By using the Gem you can define the ENV variables at system level.
If you are doing any changes related to configuration in rails application. You need to restart your application.

Rails app deployed on Centos w/ standalone Passenger and Capistrano can't access ENV variables

I set up a Centos 7 VM using this tutorial (standalone passenger) and RVM. I am deploying the rails app via Capistrano.
https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/standalone/oss/install_language_runtime.html
Everything seems to work, except no matter where I set environment variables, the ENV["myvar"] can't be read in Rails.
I've tried export myvar=test SSHed as the "deployers" as well as root. I've also tried adding it to bashrc. If I login as deployer and do the following:
#symlink to current capistrano deploy
cd ~/rails/railsapp/current/
rails r "puts ENV['myvar']"
It gives me the correct ENV output. However, if I try to output ENV['myvar'] from my actual deployed via capistrano rails app, I get nothing.
Where am I supposed to set these ENV vars? I know the ENV vars in rails are done correctly, because the app deployed to heroku, as well as on my dev machine, correctly output ENV['myvar'].
Generally, your setup should work. Since version 4, a standalone Passenger should inherit all variables defined in the shell startup scripts. There is a nice documentation about environment variables in various scenarios related to using Passenger.
I would check or two things:
That your .bashrc is loaded from .profile. If it weren't, then your variables would be loaded only in an interactive shell but not in passenger, which would explain the behavior your describe when you tried to log in as the deployers. Let me quote from the doc:
Make sure your ~/.bashrc is actually included by your ~/.profile, which might not be the case if you created the user with useradd instead of adduser for example.
Also, take a look at this section of the docs and check that you obey the conditions upon which Passenger actually passes the environment vars to the application.

Load environment variables in rails app without restarting server

How can I make my rails app aware of the new environment variables after I have edited my /etc/environment file on my remote EC2 instance?
I frequently add new (minor) things in my secrets.yml but I don't want to restart my server for it, nor do I want to use an existing secret.
In linux every process inherits envvars from its parent process and the values are passed by value, not by reference. Also, they don't behave like closures. So, child process (your rails/ruby app process) will not get any new environment variables of its parent (the shell process where you started your rails/ruby app).
That's why it is not possible. However, you can use gems like dotenv and figaro to watch some file with your environment variables and reload them when they are changed.
You should be able to add a line to your config/spring.rb:
Spring.watch "config/secrets.yml"
This will allow Spring to detect when changes have occurred in your secrets.yml file.
However, if you're actually asking about how to make your app aware that you've changed environment variables in a file, then it's not possible. Config values may be detected in files, but environment variables are detected in the shell environment. You have to load those into your shell for them to take any effect, and this would require stopping your server, sourcing the new changes into the environment, and starting the server again.
Understanding the difference between a config value in a file (.yml, .xml, .ini, etc) versus an environment variable in a shell script is important, because how it's applied and made usable is entirely different.

Why are my Rails initializers caching ENV variables in the console (Foreman, Dev env)

What started as a minor annoyance has now turned into a headache. I am building a Rails 4 app and am using Foreman for my dev setup with a Procfile and .env file for configuration. When I set an ENV variable in the .env file, it is correctly picked up by my app. In this case I am setting some ENV options for Paperclip in an initializer.
The problem surfaces when I go to change the value of the ENV variables. In the console, if I type ENV["MY_VAR"], it shows the new value. However, the value that was used in my initializer, which presumably was run when I started the console, shows the old value! Nowhere in my project is the old value listed anywhere. This leads me to believe that the environment is being cached somehow or that the env variables are being exported to my shell. I'm running out of places to look so any help would be greatly appreciated! I am developing on a Mac (10.9.4) with Ruby 1.9.3-p374 and Rails 4.1.0.
Example:
ROOT/.env
S3_BUCKET=mybucket
config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:s3_credentials] = {bucket: ENV["S3_BUCKET"]}
If I change the value of S3_BUCKET to "newbucket" and run "foreman run rails c" or "rails c" to enter the console, this is what happens:
ENV["S3_BUCKET"] # => "newbucket"
Paperclip::Attachment.default_options[:s3_credentials] # => {bucket: 'mybucket'}
I should mention that this behavior also occurs in my classes that I've put in /lib. I imagine this is all due to something silly that I've overlooked. Any ideas?
If you're using Rails 4 out of the box, it comes with a gem called Spring that's intended to make your life easier by preloading an instance of your application in the background and reloading it as your code and configuration files change.
Spring, however, only monitors Rails' default configuration files, so you'll need to configure Spring to monitor additional any other files that you wish to trigger a reload.
Spring reads ~/.spring.rb and config/spring.rb for custom settings. You can add add the following line to the file of your choosing to watch your .env file for changes:
Spring.watch '.env'
See Spring's configuration documentation in the README for more info.

Rails app not loading new environment variable added to /etc/environment

I'm trying to add an environment variable to use in my Rails App. Currently, our app uses several environment variables defined in /etc/environment. They work fine. So I've added a new variable to /etc/environment (and rebooted for the hell of it). But when I try to access the new variable, its undefined. Printing out ENV.inspect shows all the original variables, but not my newly added one. Why oh why?
Running Apache 2 / Passenger 4 / Rails 4. Recently upgraded from Passenger 2 / Rails 3 if its significant.
Edit
Turns out the ones I thought 'work fine' don't work either (they're redefined in my Rails app). So none of the variables in /etc/environment are loaded into the app. Not sure why I thought that system worked, but it doesn't.
Might not be good news to you, but you have to reboot the instance to see it happen (or check the other tricks at that question).
You can also use SetEnv at your Apache config and just restarting apache itself will make your app see the new values.
ENV
Upgrading from Rails 3 - Rails 4 should not be a major concern for the ENV variables, unless the way Rails processes them will have changed.
We use the following:
This works for us with Rails 4, Ubuntu 12.04, Passenger, Apache 2
--
The problem you may have is that your Rails application has not updated since you added the new variables.
When you run a Rails app, it essentially loads an "instance" of the application, and then keeps loading it somehow. I'm not totally sure how it works, but I do know the likes of Passenger essentially "cache" a rails app whilst in production
This means if you're adding new Environment variables, perhaps the likes of Passenger has made it so the Rails application has not been updated? If this is the case you may wish to use the touch tmp/restart.txt command:

Resources