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

Implement custom blocks to be inserted in nginx config. #142

Closed
wants to merge 11 commits into from
3 changes: 3 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Style/LineLength:
Description: 'Limit lines to 130 characters.'
Max: 130
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project makes use of the [Sementic Versioning](http://semver.org/)

### Misc
- Sudo recipe configuration changed to match Ubuntu's default sudo behaviour more closely.
- Licensing has been moved to MIT

## 2.2.0 - 2014-10-28

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
source "https://rubygems.org"

gem "rake"
gem "chef", "~> 11.16.2"
gem "knife-solo", "~> 0.4.1"
gem "librarian-chef", "~> 0.0.4"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ GEM
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.5.2)
rake (10.3.2)
resource_kit (0.0.5)
activesupport (>= 3.0)
addressable (~> 2.3.6)
Expand Down Expand Up @@ -170,6 +171,7 @@ DEPENDENCIES
kitchen-vagrant
knife-solo (~> 0.4.1)
librarian-chef (~> 0.0.4)
rake
rspec
serverspec
test-kitchen
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2014 Firmhouse.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion config/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# License for new Cookbooks
# Can be :apachev2 or :none
NEW_COOKBOOK_LICENSE = :apachev2
NEW_COOKBOOK_LICENSE = :none

###
# Useful Extras (which you probably don't need to change)
Expand Down
3 changes: 2 additions & 1 deletion nodes/sample_host.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ssh-rsa AAA456...uvw== bar"
]
}
}
},
"ssh_deploy_keys": [
"<enter the contents of an id_rsa.pub here>"
],
Expand All @@ -37,6 +37,7 @@
"rails_env": "<stage, eg: production>",
"packages": ["nodejs"],
"domain_names": ["<domain name>", "<domain name>", "<...>"],
"redirect_domain_names": ["<domain name>", "<domain name>", "<...>"],
"ruby_version": "2.1.0",
"ssl_info": {
"key": "<ssl key>",
Expand Down
2 changes: 1 addition & 1 deletion vendor/cookbooks/backups/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name "backups"
maintainer "Firmhouse"
maintainer_email "[email protected]"
license "Apache 2.0"
license "MIT"

Choose a reason for hiding this comment

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

Put one space between the method name and the first argument.

description "Installs/Configures backups"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.1.0"
29 changes: 17 additions & 12 deletions vendor/cookbooks/backups/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#
# Cookbook Name:: backups
# Recipe:: default
#
# Copyright 2014, Firmhouse
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

backup_node = node[:backups]

Expand Down
3 changes: 2 additions & 1 deletion vendor/cookbooks/packages/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name "packages"

Choose a reason for hiding this comment

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

Put one space between the method name and the first argument.

maintainer "Michiel Sikkes"
maintainer_email "[email protected]"
license "Apache 2.0"
license "MIT"

Choose a reason for hiding this comment

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

Put one space between the method name and the first argument.

description "Installs/Configures packages"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.1.0"
31 changes: 18 additions & 13 deletions vendor/cookbooks/packages/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
#
# Copyright 2012, Michiel Sikkes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
if node[:packages]

node[:packages].each do |package|
package package
end

end
end
14 changes: 2 additions & 12 deletions vendor/cookbooks/rails/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
# CHANGELOG for rails

This file is used to list changes made in each version of rails.

## 0.1.0:

* Initial release of rails

- - -
Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown.

The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/) describes the differences between markdown on github and standard markdown.
See https://github.com/intercity/chef-repo/blob/master/CHANGELOG.md for full
changelog
20 changes: 20 additions & 0 deletions vendor/cookbooks/rails/libraries/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Rails
module Helpers
##
# Create a default set of custom config
# Assure we always send certain hash keys to the template
def nginx_custom_configuration(app_info)
empty_conf = {
"before" => "",
"server_main" => "",
"server_app" => "",
"server_ssl" => "",
"server_ssl_app" => "",
"upstream" => "",
"after" => "",
}

empty_conf.merge(app_info["nginx_custom"] || {})
end
end
end
5 changes: 3 additions & 2 deletions vendor/cookbooks/rails/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name "rails"

Choose a reason for hiding this comment

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

Put one space between the method name and the first argument.

maintainer "Michiel Sikkes"
maintainer_email "[email protected]"
license "Apache 2.0"
license "MIT"

Choose a reason for hiding this comment

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

Put one space between the method name and the first argument.

description "Installs/Configures rails"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.1.0"
depends "sudo", "> 1.2.0"
depends "database"
depends "nginx"
depends "bluepill"
depends "logrotate"
depends "logrotate"
41 changes: 27 additions & 14 deletions vendor/cookbooks/rails/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
#
# Copyright 2012, Michiel Sikkes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

include_recipe "sudo"
include_recipe "nginx"
Expand All @@ -26,6 +31,9 @@

if node[:active_applications]

# Include library helpers
::Chef::Resource.send(:include, Rails::Helpers)

node[:active_applications].each do |app, app_info|
rails_env = app_info['rails_env'] || "production"
deploy_user = app_info['deploy_user'] || "deploy"
Expand Down Expand Up @@ -105,8 +113,13 @@

template "/etc/nginx/sites-available/#{app}.conf" do
source "app_nginx.conf.erb"
variables :name => app, :domain_names => app_info['domain_names'], :enable_ssl => File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt")
notifies :reload, resources(:service => "nginx")
variables(
name: app,
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"),

Choose a reason for hiding this comment

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

Line is too long. [94/80]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixing this involves a lot more then just wrapping the line.

Proper fix would be to either introduce some helper modules and call methods such as certificate_exists_for?(app).

The next best thing, and more chef-ish, is to make these blocks into LWRPs. That allows for a lot of cleaning up.

The next best thing is to extract all these into variables, but that really does not make the code more readable.

Alll this is is a much larger, and different task than what I'm trying to achieve in this PR,

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's fine. I think I'm going to ignore the Hound line length setting and set it to 150 or something like that.

Also: agree with you on refactoring this into nice cheffy libraries and LWRPs. But let's first see what the best approach is right now.

Choose a reason for hiding this comment

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

Line is too long. [94/80]

custom_configuration: nginx_custom_configuration(app_info))
notifies :reload, resources(service: "nginx")
end

template "#{applications_root}/#{app}/shared/config/unicorn.rb" do
Expand Down
39 changes: 26 additions & 13 deletions vendor/cookbooks/rails/recipes/passenger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
#
# Copyright 2012, Michiel Sikkes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

package "apt-transport-https"

Expand Down Expand Up @@ -43,6 +48,9 @@

if node[:active_applications]

# Include library helpers
::Chef::Resource.send(:include, Rails::Helpers)

node[:active_applications].each do |app, app_info|
rails_env = app_info['rails_env'] || "production"
deploy_user = app_info['deploy_user'] || "deploy"
Expand Down Expand Up @@ -121,7 +129,12 @@

template "/etc/nginx/sites-available/#{app}.conf" do
source "app_passenger_nginx.conf.erb"
variables :name => app, :rails_env => rails_env, :domain_names => app_info['domain_names'], :enable_ssl => File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt")
variables(
name: app,
rails_env: rails_env,
domain_names: app_info["domain_names"],
enable_ssl: File.exists?("#{applications_root}/#{app}/shared/config/certificate.crt"),

Choose a reason for hiding this comment

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

Line is too long. [94/80]

Choose a reason for hiding this comment

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

Line is too long. [94/80]

custom_configuration: nginx_custom_configuration(app_info))
notifies :reload, resources(:service => "nginx")
end

Expand Down
22 changes: 20 additions & 2 deletions vendor/cookbooks/rails/templates/default/app_nginx.conf.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<%= @custom_configuration["before"] %>

<% if @redirect_domain_names && @redirect_domain_names.any? %>
server {
listen <%= node['nginx']['port'] || '80' %>;
<% if @enable_ssl %>
listen 443 ssl;
<% end %>
server_name <%= @redirect_domain_names.join(' ') %>;
return 301 $scheme://<%= @domain_names.first %>$request_uri;
}
<% end %>

server {
listen <%= node['nginx']['port'] || '80' %>;
server_name <%= @domain_names.join(' ') %>;
Expand All @@ -11,8 +24,9 @@ server {
proxy_redirect off;

proxy_pass http://<%= @name %>;
<%= @custom_configuration["server_app"] %>
}

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

<% if @enable_ssl %>
Expand All @@ -38,12 +52,16 @@ server {
proxy_redirect off;

proxy_pass http://<%= @name %>;
<%= @custom_configuration["ssl_app"] %>
}

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

<% end %>

upstream <%= @name %> {
server unix:<%= node['rails']['applications_root'] %>/<%= @name %>/shared/tmp/sockets/unicorn.sock;
<%= @custom_configuration["upstream"] %>
}

<%= @custom_configuration["after"] %>
Loading