Skip to content

Commit

Permalink
Merge pull request #20107 from Fryguy/production_static_assets
Browse files Browse the repository at this point in the history
Server static assets in production based on environment detection
  • Loading branch information
jrafanie authored May 15, 2020
2 parents 2aa3b79 + cbc55d8 commit daa6751
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 deletions.
4 changes: 3 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
require "miq_environment"
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? || !MiqEnvironment::Command.is_production_build?
puts "** WARN: Rails is serving static assets in production mode" if config.public_file_server.enabled

# Compress JavaScripts and CSS
config.assets.compress = true
Expand Down
6 changes: 5 additions & 1 deletion lib/miq_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def self.supports_nohup_and_backgrounding?
@supports_nohup = is_appliance? && supports_command?('nohup')
end

def self.is_production_build?
is_appliance? || is_podified? || is_container?
end

def self.is_container?
return @is_container unless @is_container.nil?
@is_container = ENV["CONTAINER"] == "true"
Expand All @@ -36,7 +40,7 @@ def self.is_podified?

def self.is_appliance?
return @is_appliance unless @is_appliance.nil?
@is_appliance = is_linux? && File.exist?('/var/www/miq/vmdb')
@is_appliance = ENV["APPLIANCE"] == "true"
end

# Return whether or not the current ManageIQ environment is a production
Expand Down
86 changes: 73 additions & 13 deletions spec/lib/miq_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,88 @@
end

context ".is_production?" do
it "should return true if Rails undefined" do
it "when Rails is not defined" do
hide_const('Rails')
expect { Rails }.to raise_error(NameError)
expect(MiqEnvironment::Command.is_production?).to be_truthy
assert_same_result_every_time(:is_production?, true)
end

it "will return true if linux and /var/www/miq/vmdb exists and cache the result" do
expect(MiqEnvironment::Command).to receive(:is_linux?).once.and_return(true)
expect(File).to receive(:exist?).once.and_return(true)
assert_same_result_every_time(:is_appliance?, true)
it "when Rails is production" do
expect(Rails.env).to receive(:production?).twice.and_return(true)
assert_same_result_every_time(:is_production?, true)
end

it "when Rails is not production" do
assert_same_result_every_time(:is_production?, false)
end
end

describe ".is_container?" do
it "returns false if the environment variable is not set" do
assert_same_result_every_time(:is_container?, false)
context "production build questions" do
def container_conditions
stub_const("ENV", ENV.to_h.merge("CONTAINER" => "true"))
end

def podified_conditions
expect(ContainerOrchestrator).to receive(:available?).and_return(true)
container_conditions
end

def appliance_conditions
stub_const("ENV", ENV.to_h.merge("APPLIANCE" => "true"))
end

describe ".is_container?" do
it "when the conditions are not met" do
assert_same_result_every_time(:is_container?, false)
end

it "when the conditions are met" do
container_conditions
assert_same_result_every_time(:is_container?, true)
end
end

it "returns true if the environment variable is set" do
ENV["CONTAINER"] = "true"
assert_same_result_every_time(:is_container?, true)
ENV.delete("CONTAINER")
describe ".is_podified?" do
it "when the conditions are not met" do
assert_same_result_every_time(:is_podified?, false)
end

it "when the conditions are met" do
podified_conditions
assert_same_result_every_time(:is_podified?, true)
end
end

describe ".is_appliance?" do
it "when the conditions are not met" do
assert_same_result_every_time(:is_appliance?, false)
end

it "when the conditions are met" do
appliance_conditions
assert_same_result_every_time(:is_appliance?, true)
end
end

describe ".is_production_build?" do
it "when the conditions are not met" do
assert_same_result_every_time(:is_production_build?, false)
end

it "when the appliance conditions are met" do
appliance_conditions
assert_same_result_every_time(:is_production_build?, true)
end

it "when the container conditions are met" do
container_conditions
assert_same_result_every_time(:is_production_build?, true)
end

it "when the podified conditions are met" do
podified_conditions
assert_same_result_every_time(:is_production_build?, true)
end
end
end
end
Expand Down

0 comments on commit daa6751

Please sign in to comment.