Skip to content
Browse files

Application plugin to provide a load balancer via nginx.

  • Loading branch information...
0 parents commit 10e2a2540e5e475a9879020791cf657668ff20e1 @coderanger coderanger committed Sep 5, 2011
0 README.md
No changes.
9 metadata.rb
@@ -0,0 +1,9 @@
+maintainer "Opscode, Inc."
+maintainer_email "cookbooks@opscode.com"
+license "Apache 2.0"
+description "Deploys and configures Nginx as an application server"
+long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
+version "0.99.12"
+
+depends "application"
+depends "nginx"
62 providers/nginx_load_balancer.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Noah Kantrowitz <noah@opscode.com>
+# Cookbook Name:: application_nginx
+# Provider:: nginx_load_balancer
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# 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.
+#
+
+include Chef::Mixin::LanguageIncludeRecipe
+
+action :before_compile do
+
+ include_recipe 'nginx'
+
+ new_resource.application_server_role "#{new_resource.application.name}_application_server" unless new_resource.application_server_role
+
+end
+
+action :before_deploy do
+
+ template "#{node[:nginx][:dir]}/sites-available/#{new_resource.application.name}.conf" do
+ source new_resource.template ? new_resource.template : "load_balancer.conf.erb"
+ cookbook new_resource.template ? new_resource.cookbook_name : "application_nginx"
+ owner "root"
+ group "root"
+ mode "644"
+ variables :resource => new_resource, :hosts => new_resource.find_matching_role(new_resource.application_server_role, false)
+ notifies :reload, resources(:service => 'nginx')
+ end
+
+ nginx_site "#{new_resource.application.name}.conf"
+
+ nginx_site "default" do
+ enable false
+ end
+
+end
+
+action :before_migrate do
+end
+
+action :before_symlink do
+end
+
+action :before_restart do
+end
+
+action :after_restart do
+end
+
28 resources/nginx_load_balancer.rb
@@ -0,0 +1,28 @@
+#
+# Author:: Noah Kantrowitz <noah@opscode.com>
+# Cookbook Name:: application_nginx
+# Resource:: nginx_load_balancer
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# 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.
+#
+
+include Chef::Resource::ApplicationBase
+
+attribute :application_server_role, :kind_of => [String, Symbol, NilClass], :default => nil
+attribute :template, :kind_of => [String, NilClass], :default => nil
+attribute :server_name, :kind_of => [String, Array], :default => node['fqdn']
+attribute :port, :kind_of => Integer, :default => 80
+attribute :application_port, :kind_of => Integer, :default => 8000
+attribute :static_files, :kind_of => Hash, :default => {}
18 templates/default/load_balancer.conf.erb
@@ -0,0 +1,18 @@
+upstream <%= @resource.application.name %> {
+ <% @hosts.each do |node| %>
+ server <%= node.attribute?('cloud') ? node['cloud']['local_ipv4'] : node['ipaddress'] %>:<%= @resource.application_port %>;
+ <% end %>
+}
+
+server {
+ listen <%= @resource.port %>;
+ server_name <%= @resource.server_name.is_a?(Array) ? @resource.server_name.join(' ') : @resource.server_name %>;
+ <% @resource.static_files.each do |url, path| %>
+ location <%= url %> {
+ alias <%= path %>;
+ }
+ <% end %>
+ location / {
+ proxy_pass http://<%= @resource.application.name %>;
+ }
+}

0 comments on commit 10e2a25

Please sign in to comment.
Something went wrong with that request. Please try again.