Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Feature/gzip support #158

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .kitchen.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ suites:
password: r4nd0m
database: intercity_sample_app
ruby_version: 2.1.2
gzip_enabled: true
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ suites:
password: r4nd0m
database: intercity_sample_app
ruby_version: 2.1.2
gzip_enabled: true
40 changes: 21 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion nodes/sample_host.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"username": "<db username, max 10 characters>",
"password": "<enter a random password>",
"database": "<appname>_<stage>"
}
},
"gzip_enabled": false
}
}
}
10 changes: 10 additions & 0 deletions test/integration/full-stack/serverspec/nginx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
end

end

describe file("/etc/nginx/sites-available/intercity_sample_app.conf") do
it { should be_file }

context "GZip support enabled" do
its(:content) do
should match /location ~ \^\/\(assets\)\/.*gzip_static on;/m
end
end
end
8 changes: 8 additions & 0 deletions vendor/cookbooks/rails/libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion vendor/cookbooks/rails/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@
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))
server_root_public_path: nginx_server_root(applications_root, app),
custom_configuration: nginx_custom_configuration(app_info),
gzip_enabled: app_info["gzip_enabled"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure that app_info["gzip_enabled"] has a default value in vendor/cookbooks/rails/attributed/default.rb?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not, since app_info is an application config read from rails cookbook config. There could be more then one application per node config:

35   "active_applications": {
36     "<appname>_<stage>": {
...
57       "gzip_enabled": false
58     }
59  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, good point. What about .fetch? and give false as default?

)
notifies :reload, resources(service: "nginx")
end

Expand Down
5 changes: 4 additions & 1 deletion vendor/cookbooks/rails/recipes/passenger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@
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))
server_root_public_path: nginx_server_root(applications_root, app),
custom_configuration: nginx_custom_configuration(app_info),
gzip_enabled: app_info["gzip_enabled"]
)
notifies :reload, resources(:service => "nginx")
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
location ~ ^/(assets)/ {
root <%= @server_root_public_path %>;
gzip_static on;
expires max;
add_header Cache-Control public;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, but the } is not placed correctly yet ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I've got some issues with rendering partials, the first line is always shifted 2 spaces right

4 changes: 2 additions & 2 deletions vendor/cookbooks/rails/templates/default/app_nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

<%= @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 } if @gzip_enabled %>

<%= @custom_configuration["server_main"] %>
}

Expand All @@ -23,7 +27,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 } if @gzip_enabled %>

<%= @custom_configuration["ssl_main"] %>
}

Expand Down