Setting environment variables in machines

I know from heroku that you can add environment variables by running heroku config:add MY_ENV_VAR=123 locally. How can I achieve the same with the machine?

+6
source share
5 answers

We came across the same question and asked EngineYard for help. EY's Jim Neath returned with the following response:

Unfortunately, the passenger does not receive the transferred environment variables from the system. What you need to do is create a ruby ​​wrapper that defines your environment variables and launches passengers using this, as described here:

http://blog.phusion.nl/2008/12/16/passing-environment-variables-to-ruby-from-phusion-passenger/

I have created for you a basic recipe for a cook that will do just that:

https://github.com/jimneath/ey-cloud-recipes/tree/master/cookbooks/passenger_env_vars

You will need to update the following file in your environment variables:

/ey-cloud-recipes/blob/master/cookbooks/passenger_env_vars/templates/default/env.custom.erb

+9
source

I do not think you can = /.

One solution that we use with our Rails applications is ssh (ey ssh) for EngineYard and creating a file in the file vim / data / your_app_name / shared / config / exports.rb. This file might look something like this:

 ENV["AWS_ACCESS_KEY_ID"] = "your key" ENV["AWS_SECRET_ACCESS_KEY"] = "your secret" ENV["AWS_BUCKET"] = "your bucket" 

Then in config / boot.rb you need a file:

 require File.expand_path('./exports', File.dirname(__FILE__)) 

It is not pretty and not easy. However, you can use secrets in your application that you should not check in the source code!

+4
source

It is quite simple for Unicorn using env.custom. Take a look at my answer here fooobar.com/questions/924975 / ...

+4
source

If you want to run the rake task (i.e. the cron task) that needs these environment variables, save the variables in / data / my _app / shared / config / env.custom

 source /data/my_app/shared/config/env.custom && cd /data/my_app/current/ && bundle exec rake my_rake_task 
+1
source

I also used Heroku , earlier I switched to Engineyard . This is how I get ENvironemnt variables in Heroku I added a gem figaro . This pearl mainly needs the application.yml file in the app/config directory. When the Rails application is initialized, it runs and loads the key value pair set in the YAML format into memory. Heroku figaro has the ability to set the contents of application.yml .

 $ figaro heroku:set -e production 

However, in Engineyard we need to manually copy application.yml using the SCP option of the EY package, and the rest will be done by figaro .

First include the gem figaro in the gemfile and set the gem.
Then we need to use engineyard-hooks to copy the /data/[your_app]/shared/config/application.yml file to /data/[your_app]/current/config/application.yml . we must use before_restart hook

 # inside your project repo create a 'deploy' folder and # inside deploy/before_restart.rb paste the following code with or without modifications # This file is executed everytime after deploy just before your app restarts on_app_servers_and_utilities do # Copy the yaml files from `shared/config` to `current/config` ['application.yml'].each do |file_name| run "ln -nfs #{config.shared_path}/config/#{file_name} #{config.release_path}/config/#{file_name}" end end 

Commit your changes and click on your github repository or somewhere.

Here, here the /data/[your_app]/shared/config/application.yml file does not exist. Now use the following command to copy the file from the local server to the server

 # This copies the application.yml to every instance like app_master, app_slave, utilities, database, etc $ ey scp config/application.yml HOST:/data/[your_app_name]/shared/config/ -e app_environment --all 

Now you can deploy your application and get all the environment variables.

Note. You must invoke the above command to copy the file to the server each time the environment boots. If you stop the stage (for example) and after a while load it, you need to call the command above

+1
source

Source: https://habr.com/ru/post/924971/


All Articles