From 261663de066eac50beafcdf84e26aa0b3aa4cc16 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Thu, 15 Jan 2015 16:03:12 +0200 Subject: [PATCH 1/7] upgraded serverspec --- Gemfile.lock | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 39ee0fc5..059a9349 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,10 +87,11 @@ GEM mixlib-config (2.1.0) mixlib-log (1.6.0) mixlib-shellout (1.4.0) + multi_json (1.10.1) multipart-post (2.0.0) net-scp (1.2.1) net-ssh (>= 2.6.5) - net-ssh (2.9.1) + net-ssh (2.9.2) net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) net-ssh-multi (1.2.0) @@ -120,28 +121,29 @@ GEM faraday rest-client (1.6.7) mime-types (>= 1.16) - rspec (3.0.0) - rspec-core (~> 3.0.0) - rspec-expectations (~> 3.0.0) - rspec-mocks (~> 3.0.0) - rspec-core (3.0.4) - rspec-support (~> 3.0.0) - rspec-expectations (3.0.4) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.0.0) - rspec-its (1.0.1) - rspec-core (>= 2.99.0.beta1) - rspec-expectations (>= 2.99.0.beta1) - rspec-mocks (3.0.4) - rspec-support (~> 3.0.0) - rspec-support (3.0.4) + rspec-support (~> 3.1.0) + rspec-its (1.1.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) safe_yaml (1.0.4) - serverspec (2.0.1) - rspec (~> 3.0.0) + serverspec (2.7.1) + multi_json + rspec (~> 3.0) rspec-its - specinfra (~> 2.0) + specinfra (~> 2.10) slop (3.6.0) - specinfra (2.0.2) + specinfra (2.11.3) net-scp net-ssh systemu (2.6.4) From ab45b4192fef1b84cdef060966fd4979996dd6bc Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Thu, 15 Jan 2015 16:05:18 +0200 Subject: [PATCH 2/7] Added assets location settings to nginx config with gzip enabled --- test/integration/full-stack/serverspec/nginx_spec.rb | 8 ++++++++ .../templates/default/_app_nginx_location_assets.erb | 6 ++++++ .../templates/default/app_passenger_nginx.conf.erb | 12 ++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb diff --git a/test/integration/full-stack/serverspec/nginx_spec.rb b/test/integration/full-stack/serverspec/nginx_spec.rb index c5b4826f..c5410cff 100644 --- a/test/integration/full-stack/serverspec/nginx_spec.rb +++ b/test/integration/full-stack/serverspec/nginx_spec.rb @@ -7,3 +7,11 @@ end end + +describe file('/etc/nginx/sites-available/intercity_sample_app.conf') do + it { should be_file } + + its(:content) do + should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m + end +end diff --git a/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb b/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb new file mode 100644 index 00000000..cd5de56d --- /dev/null +++ b/vendor/cookbooks/rails/templates/default/_app_nginx_location_assets.erb @@ -0,0 +1,6 @@ +location ~ ^/(assets)/ { + root <%= @server_root_public_path %>; + gzip_static on; + expires max; + add_header Cache-Control public; + } diff --git a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb index bd91afb7..6015e679 100644 --- a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb @@ -1,12 +1,17 @@ +<% server_root_public_path = "#{node['rails']['applications_root']}/#{@name}/current/public" %> + <%= @custom_configuration["before_server"] %> server { listen <%= node['nginx']['port'] || '80' %>; server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= server_root_public_path %>; passenger_enabled on; passenger_app_env <%= @rails_env %>; + + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } %> + <%= @custom_configuration["server_main"] %> } @@ -23,7 +28,10 @@ server { server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= server_root_public_path %>; + + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } %> + <%= @custom_configuration["ssl_main"] %> } From 509308feff100dac5df9994d6cdae1caabddec0e Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Fri, 16 Jan 2015 10:20:32 +0200 Subject: [PATCH 3/7] fixed houndci warnings --- test/integration/full-stack/serverspec/nginx_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/full-stack/serverspec/nginx_spec.rb b/test/integration/full-stack/serverspec/nginx_spec.rb index c5410cff..4f261203 100644 --- a/test/integration/full-stack/serverspec/nginx_spec.rb +++ b/test/integration/full-stack/serverspec/nginx_spec.rb @@ -8,10 +8,10 @@ end -describe file('/etc/nginx/sites-available/intercity_sample_app.conf') do +describe file("/etc/nginx/sites-available/intercity_sample_app.conf") do it { should be_file } - its(:content) do + its(:content) do should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m end end From 17e44b4979ec4daf7f687abc01fea71f3f7240e7 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Thu, 22 Jan 2015 16:41:28 +0200 Subject: [PATCH 4/7] New application config option: gzip_enabled --- nodes/sample_host.json | 3 ++- vendor/cookbooks/rails/recipes/default.rb | 4 +++- vendor/cookbooks/rails/recipes/passenger.rb | 4 +++- .../rails/templates/default/app_passenger_nginx.conf.erb | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/nodes/sample_host.json b/nodes/sample_host.json index 1525a285..1b52c2e1 100644 --- a/nodes/sample_host.json +++ b/nodes/sample_host.json @@ -53,7 +53,8 @@ "username": "", "password": "", "database": "_" - } + }, + "gzip_enabled": true } } } diff --git a/vendor/cookbooks/rails/recipes/default.rb b/vendor/cookbooks/rails/recipes/default.rb index 8c1022a2..8daf36ac 100644 --- a/vendor/cookbooks/rails/recipes/default.rb +++ b/vendor/cookbooks/rails/recipes/default.rb @@ -118,7 +118,9 @@ domain_names: app_info["domain_names"], redirect_domain_names: app_info["redirect_domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), - custom_configuration: nginx_custom_configuration(app_info)) + custom_configuration: nginx_custom_configuration(app_info), + gzip_enabled: app_info["gzip_enabled"] + ) notifies :reload, resources(service: "nginx") end diff --git a/vendor/cookbooks/rails/recipes/passenger.rb b/vendor/cookbooks/rails/recipes/passenger.rb index aab970b6..2455e52a 100644 --- a/vendor/cookbooks/rails/recipes/passenger.rb +++ b/vendor/cookbooks/rails/recipes/passenger.rb @@ -134,7 +134,9 @@ rails_env: rails_env, domain_names: app_info["domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), - custom_configuration: nginx_custom_configuration(app_info)) + custom_configuration: nginx_custom_configuration(app_info), + gzip_enabled: app_info["gzip_enabled"] + ) notifies :reload, resources(:service => "nginx") end diff --git a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb index 6015e679..df5ec925 100644 --- a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb @@ -10,7 +10,7 @@ server { passenger_enabled on; passenger_app_env <%= @rails_env %>; - <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } %> + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } if @gzip_enabled %> <%= @custom_configuration["server_main"] %> } @@ -30,7 +30,7 @@ server { root <%= server_root_public_path %>; - <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } %> + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } if @gzip_enabled %> <%= @custom_configuration["ssl_main"] %> } From 39a50fdae3f9ca40cc494a4ec70d7d4299b01965 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 26 Jan 2015 17:11:48 +0200 Subject: [PATCH 5/7] Set sample default gzip_enabled to false --- nodes/sample_host.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/sample_host.json b/nodes/sample_host.json index 1b52c2e1..81e5d8f4 100644 --- a/nodes/sample_host.json +++ b/nodes/sample_host.json @@ -54,7 +54,7 @@ "password": "", "database": "_" }, - "gzip_enabled": true + "gzip_enabled": false } } } From b91c7c6f166ed537be36e5a28b747c0239ae5429 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 26 Jan 2015 18:43:26 +0200 Subject: [PATCH 6/7] Added context block around nginx gzip spec for better readability --- .kitchen.local.yml | 1 + .kitchen.yml | 1 + test/integration/full-stack/serverspec/nginx_spec.rb | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.kitchen.local.yml b/.kitchen.local.yml index 5f9cc2cf..79c513ca 100644 --- a/.kitchen.local.yml +++ b/.kitchen.local.yml @@ -39,3 +39,4 @@ suites: password: r4nd0m database: intercity_sample_app ruby_version: 2.1.2 + gzip_enabled: true diff --git a/.kitchen.yml b/.kitchen.yml index bdf36bc7..efb41142 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -33,3 +33,4 @@ suites: password: r4nd0m database: intercity_sample_app ruby_version: 2.1.2 + gzip_enabled: true diff --git a/test/integration/full-stack/serverspec/nginx_spec.rb b/test/integration/full-stack/serverspec/nginx_spec.rb index 4f261203..525144d5 100644 --- a/test/integration/full-stack/serverspec/nginx_spec.rb +++ b/test/integration/full-stack/serverspec/nginx_spec.rb @@ -11,7 +11,9 @@ describe file("/etc/nginx/sites-available/intercity_sample_app.conf") do it { should be_file } - its(:content) do - should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m + context "GZip support enabled" do + its(:content) do + should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m + end end end From be800ff0bf8fbc2e439072a366e6a507667bb79c Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 26 Jan 2015 19:22:33 +0200 Subject: [PATCH 7/7] Extracted nginx server root path variable from template to recipe/helper --- vendor/cookbooks/rails/libraries/default.rb | 8 ++++++++ vendor/cookbooks/rails/recipes/default.rb | 1 + vendor/cookbooks/rails/recipes/passenger.rb | 1 + .../cookbooks/rails/templates/default/app_nginx.conf.erb | 4 ++-- .../rails/templates/default/app_passenger_nginx.conf.erb | 9 ++++----- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/vendor/cookbooks/rails/libraries/default.rb b/vendor/cookbooks/rails/libraries/default.rb index f60d2bfa..2934bb5d 100644 --- a/vendor/cookbooks/rails/libraries/default.rb +++ b/vendor/cookbooks/rails/libraries/default.rb @@ -16,5 +16,13 @@ def nginx_custom_configuration(app_info) empty_conf.merge(app_info["nginx_custom"] || {}) end + + # Builds a public root path on server + # + # nginx_server_root("/u/apps", "my_app") # => "/u/apps/my_app/current/public" + # + def nginx_server_root(applications_root, app) + "#{applications_root}/#{app}/current/public" + end end end diff --git a/vendor/cookbooks/rails/recipes/default.rb b/vendor/cookbooks/rails/recipes/default.rb index 8daf36ac..9b6bd482 100644 --- a/vendor/cookbooks/rails/recipes/default.rb +++ b/vendor/cookbooks/rails/recipes/default.rb @@ -118,6 +118,7 @@ domain_names: app_info["domain_names"], redirect_domain_names: app_info["redirect_domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), + server_root_public_path: nginx_server_root(applications_root, app), custom_configuration: nginx_custom_configuration(app_info), gzip_enabled: app_info["gzip_enabled"] ) diff --git a/vendor/cookbooks/rails/recipes/passenger.rb b/vendor/cookbooks/rails/recipes/passenger.rb index 2455e52a..6c1ee103 100644 --- a/vendor/cookbooks/rails/recipes/passenger.rb +++ b/vendor/cookbooks/rails/recipes/passenger.rb @@ -134,6 +134,7 @@ rails_env: rails_env, domain_names: app_info["domain_names"], enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"), + server_root_public_path: nginx_server_root(applications_root, app), custom_configuration: nginx_custom_configuration(app_info), gzip_enabled: app_info["gzip_enabled"] ) diff --git a/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb index de13df6a..002949f5 100644 --- a/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_nginx.conf.erb @@ -14,7 +14,7 @@ server { server { listen <%= node['nginx']['port'] || '80' %>; server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %> try_files $uri/index.html $uri.html $uri @app; @@ -39,7 +39,7 @@ server { server_name <%= @domain_names.join(' ') %>; - root <%= node['rails']['applications_root'] %>/<%= @name %>/current/public; + root <%= @server_root_public_path %> location / { try_files $uri @app; diff --git a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb index df5ec925..f0a3a884 100644 --- a/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb +++ b/vendor/cookbooks/rails/templates/default/app_passenger_nginx.conf.erb @@ -1,16 +1,15 @@ -<% server_root_public_path = "#{node['rails']['applications_root']}/#{@name}/current/public" %> <%= @custom_configuration["before_server"] %> server { listen <%= node['nginx']['port'] || '80' %>; server_name <%= @domain_names.join(' ') %>; - root <%= server_root_public_path %>; + root <%= @server_root_public_path %>; passenger_enabled on; passenger_app_env <%= @rails_env %>; - <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } if @gzip_enabled %> + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: @server_root_public_path } if @gzip_enabled %> <%= @custom_configuration["server_main"] %> } @@ -28,9 +27,9 @@ server { server_name <%= @domain_names.join(' ') %>; - root <%= server_root_public_path %>; + root <%= @server_root_public_path %>; - <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: server_root_public_path } if @gzip_enabled %> + <%= render '_app_nginx_location_assets.erb', variables: { server_root_public_path: @server_root_public_path } if @gzip_enabled %> <%= @custom_configuration["ssl_main"] %> }