Skip to content
Browse files

Added support for per-application ENV variables

We check for the existence of a .env file in the application
root.  If found, we use it to set custom environment variables
for that application.  The format of the .env file is:

  VAR1=value1
  VAR2=value2

Note that blank lines are not supported.
  • Loading branch information...
1 parent 19c4797 commit bd036c08d28bea2dc29fe6c4cdeda278bb991319 Tammer Saleh & Jacob Burkhart committed Sep 14, 2011
Showing with 45 additions and 2 deletions.
  1. +14 −1 lib/phusion_passenger/utils.rb
  2. +31 −1 test/ruby/shared/spawners/spawner_spec.rb
View
15 lib/phusion_passenger/utils.rb
@@ -217,7 +217,20 @@ def prepare_app_process(startup_file, options)
end
raise(message)
end
-
+
+ dot_env_path = File.expand_path(".env", options["app_root"])
+ if File.exists?(dot_env_path)
+ begin
+ File.read(dot_env_path).each_line do |line|
+ line.strip!
+ key, value = line.split("=", 2)
+ ENV[key] = value
+ end
+ rescue => e
+ STDERR.puts "Error loading environment variables from '#{dot_env_path}': #{e.message}"
+ end
+ end
+
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options["environment"]
base_uri = options["base_uri"]
View
32 test/ruby/shared/spawners/spawner_spec.rb
@@ -43,7 +43,37 @@ def ping_app(app, connect_password)
File.read("#{app.app_root}/rails_env.txt").should == "staging"
File.read("#{app.app_root}/rack_env.txt").should == "staging"
end
-
+
+ it "sets appropriate environment variables if a .env file exists" do
+ before_start %q{
+ File.write("var1.txt", ENV['VERY_IMPORTANT_VAR']) if ENV['VERY_IMPORTANT_VAR']
+ File.write("var2.txt", ENV['SUPERFLUESNESS']) if ENV['SUPERFLUESNESS']
+ }
+ app = spawn_some_application("environment" => "production") do |stub|
+ File.open(File.join(stub.full_app_root, ".env"), "w+") do |fp|
+ fp.write("VERY_IMPORTANT_VAR=foo\nSUPERFLUESNESS=bar")
+ end
+ end
+ File.read("#{app.app_root}/var1.txt").should == "foo"
+ File.read("#{app.app_root}/var2.txt").should == "bar"
+ end
+
+ it "fails to set malformed .env entries" do
+ before_start %q{
+ File.write("var1.txt", ENV['VERY_IMPORTANT_VAR'])
+ File.write("var2.txt", ENV['SUPERFLUESNESS'])
+ File.write("var3.txt", ENV['ALSO'])
+ }
+ app = spawn_some_application("environment" => "production") do |stub|
+ File.open(File.join(stub.full_app_root, ".env"), "w+") do |fp|
+ fp.write("VERY_IMPORTANT_VAR is foo\nSUPERFLUESNESS=bar\nALSO=i=love=equals")
+ end
+ end
+ File.read("#{app.app_root}/var1.txt").should == ""
+ File.read("#{app.app_root}/var2.txt").should == "bar"
+ File.read("#{app.app_root}/var3.txt").should == "i=love=equals"
+ end
+
it "sets ENV['RAILS_RELATIVE_URL_ROOT'] and ENV['RACK_BASE_URI'] if the 'base_uri' option is set to a valid value" do
before_start %q{
File.write("rails_relative_url_root.txt", ENV['RAILS_RELATIVE_URL_ROOT'])

0 comments on commit bd036c0

Please sign in to comment.
Something went wrong with that request. Please try again.