diff --git a/config/environments/production.rb b/config/environments/production.rb index 764948a58c4..44cb0188c02 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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 diff --git a/lib/miq_environment.rb b/lib/miq_environment.rb index 7b2af35aa10..97813acb6a1 100644 --- a/lib/miq_environment.rb +++ b/lib/miq_environment.rb @@ -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" @@ -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 diff --git a/spec/lib/miq_environment_spec.rb b/spec/lib/miq_environment_spec.rb index 8aabf0e76c6..6befc13c861 100644 --- a/spec/lib/miq_environment_spec.rb +++ b/spec/lib/miq_environment_spec.rb @@ -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