├── test ├── cookbooks │ └── haproxy-test │ │ ├── Berksfile │ │ ├── README.md │ │ ├── recipes │ │ └── default.rb │ │ ├── attributes │ │ └── default.rb │ │ ├── .gitignore │ │ ├── metadata.rb │ │ └── .kitchen.yml └── integration │ └── lwrp │ └── bats │ └── nc.bats ├── files └── default │ └── haproxy-default ├── .gitignore ├── templates ├── default │ ├── haproxy.dynamic.cfg.erb │ ├── haproxy.cfg.erb │ └── haproxy-init.erb ├── centos │ └── haproxy-init.erb └── rhel │ └── haproxy-init.erb ├── Berksfile ├── resources ├── default.rb └── lb.rb ├── attributes ├── config.rb └── default.rb ├── providers ├── lb.rb └── default.rb ├── TESTING.md ├── recipes ├── tuning.rb ├── install_package.rb ├── app_lb.rb ├── install_source.rb └── default.rb ├── libraries └── default.rb ├── .kitchen.yml ├── CHANGELOG.md ├── metadata.rb ├── LICENSE ├── README.md └── CONTRIBUTING.md /test/cookbooks/haproxy-test/Berksfile: -------------------------------------------------------------------------------- 1 | site :opscode 2 | 3 | metadata 4 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/README.md: -------------------------------------------------------------------------------- 1 | Not much but this is required! 2 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/recipes/default.rb: -------------------------------------------------------------------------------- 1 | package node[:haproxy][:test][:netcat_package] 2 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/attributes/default.rb: -------------------------------------------------------------------------------- 1 | default[:haproxy][:test][:netcat_package] = 'nc' 2 | -------------------------------------------------------------------------------- /files/default/haproxy-default: -------------------------------------------------------------------------------- 1 | # Set ENABLED to 1 if you want the init script to start haproxy. 2 | ENABLED=1 3 | # Add extra flags here. 4 | #EXTRAOPTS="-de -m 16" 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | Berksfile.lock 3 | Gemfile.lock 4 | *~ 5 | *# 6 | .#* 7 | \#*# 8 | .*.sw[a-z] 9 | *.un~ 10 | .bundle 11 | .cache 12 | .kitchen 13 | bin 14 | .kitchen.local.yml 15 | -------------------------------------------------------------------------------- /templates/default/haproxy.dynamic.cfg.erb: -------------------------------------------------------------------------------- 1 | <% @config.each do |top_level_key, top_level_value| -%> 2 | <%= top_level_key %> 3 | <%= ChefHaproxy.config_generator(top_level_value, ' ') %> 4 | <% end %> -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | site :opscode 2 | 3 | metadata 4 | 5 | group :integration do 6 | cookbook "apt" 7 | cookbook "yum" 8 | cookbook "haproxy-test", :path => './test/cookbooks/haproxy-test' 9 | end 10 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant 2 | Berksfile.lock 3 | *~ 4 | *# 5 | .#* 6 | \#*# 7 | .*.sw[a-z] 8 | *.un~ 9 | /cookbooks 10 | 11 | # Bundler 12 | Gemfile.lock 13 | bin/* 14 | .bundle/* 15 | 16 | .kitchen/ 17 | .kitchen.local.yml 18 | -------------------------------------------------------------------------------- /resources/default.rb: -------------------------------------------------------------------------------- 1 | actions :create, :delete 2 | default_action :create 3 | 4 | attribute :config_directory, :kind_of => String 5 | 6 | def config(value=:none, &block) 7 | if(value != :none) 8 | unless(value.is_a?(Hash)) 9 | raise Exception::ValidationFailed, "Option config must be of kind Hash! You passed #{value.inspect}" 10 | end 11 | @config = value 12 | elsif(block) 13 | @config = block 14 | else 15 | @config 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/metadata.rb: -------------------------------------------------------------------------------- 1 | name "haproxy-test" 2 | maintainer "Opscode, Inc." 3 | maintainer_email "cookbooks@opscode.com" 4 | license "Apache 2.0" 5 | description "Testing cookbook for haproxy" 6 | long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) 7 | version "0.0.1" 8 | 9 | recipe "haproxy", "Install deps for proper testing" 10 | 11 | %w{ debian ubuntu centos redhat}.each do |os| 12 | supports os 13 | end 14 | -------------------------------------------------------------------------------- /attributes/config.rb: -------------------------------------------------------------------------------- 1 | # Base attributes used when generating configuration using default LWRP 2 | default[:haproxy][:config][:global][:log]['127.0.0.1'][:local0] = :warn 3 | default[:haproxy][:config][:global][:log]['127.0.0.1'][:local1] = :notice 4 | default[:haproxy][:config][:defaults][:timeout][:client] = '10s' 5 | default[:haproxy][:config][:defaults][:timeout][:client] = '10s' 6 | default[:haproxy][:config][:defaults][:timeout][:server] = '10s' 7 | default[:haproxy][:config][:defaults][:timeout][:connect] = '10s' 8 | default[:haproxy][:config][:defaults][:options] = [] 9 | -------------------------------------------------------------------------------- /providers/lb.rb: -------------------------------------------------------------------------------- 1 | use_inline_resources if defined?(use_inline_resources) 2 | 3 | action :create do 4 | #While there is no way to have an include directive for haproxy 5 | #configuration file, this provider will only modify attributes ! 6 | listener = [] 7 | listener << "bind #{new_resource.bind}" unless new_resource.bind.nil? 8 | listener << "balance #{new_resource.balance}" unless new_resource.balance.nil? 9 | listener << "mode #{new_resource.mode}" unless new_resource.mode.nil? 10 | listener += new_resource.servers.map {|server| "server #{server}" } 11 | 12 | if new_resource.params.is_a? Hash 13 | listener += new_resource.params.map { |k,v| "#{k} #{v}" } 14 | else 15 | listener += new_resource.params 16 | end 17 | 18 | node.default['haproxy']['listeners'][new_resource.type][new_resource.name] = listener 19 | end 20 | 21 | -------------------------------------------------------------------------------- /resources/lb.rb: -------------------------------------------------------------------------------- 1 | actions :create 2 | default_action :create 3 | 4 | 5 | attribute :name, :kind_of => String, :name_attribute => true 6 | attribute :type, :kind_of => String, :default => 'listen', :equal_to => ['listen', 'backend', 'frontend'] 7 | 8 | #Defining some attributes, but they can be all nil and defined in params 9 | #if convenient. 10 | attribute :servers, :kind_of => Array, :default => [] 11 | attribute :balance, :kind_of => String 12 | attribute :bind, :kind_of => String, :default => nil 13 | attribute :mode, :kind_of => String, :default => nil, 14 | :equal_to => ['http', 'tcp', 'health', nil] 15 | 16 | #I can't think of all parameters available in the future so we allow 17 | #arbitrary params. Type can be array or hash because some attributes 18 | #(like server) can set several times 19 | attribute :params, :kind_of => [Array, Hash], :default => [] 20 | -------------------------------------------------------------------------------- /TESTING.md: -------------------------------------------------------------------------------- 1 | This cookbook includes support for running tests via Test Kitchen (1.0). This has some requirements. 2 | 3 | 1. You must be using the Git repository, rather than the downloaded cookbook from the Chef Community Site. 4 | 2. You must have Vagrant 1.1 installed. 5 | 3. You must have a "sane" Ruby 1.9.3 environment. 6 | 7 | Once the above requirements are met, install the additional requirements: 8 | 9 | Install the berkshelf plugin for vagrant, and berkshelf to your local Ruby environment. 10 | 11 | vagrant plugin install vagrant-berkshelf 12 | gem install berkshelf 13 | 14 | Install Test Kitchen 1.0 (unreleased yet, use the alpha / prerelease version). 15 | 16 | gem install test-kitchen --pre 17 | 18 | Install the Vagrant driver for Test Kitchen. 19 | 20 | gem install kitchen-vagrant 21 | 22 | Once the above are installed, you should be able to run Test Kitchen: 23 | 24 | kitchen list 25 | kitchen test 26 | -------------------------------------------------------------------------------- /test/cookbooks/haproxy-test/.kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver_plugin: vagrant 3 | driver_config: 4 | require_chef_omnibus: true 5 | 6 | platforms: 7 | - name: ubuntu-12.04 8 | driver_config: 9 | box: opscode-ubuntu-12.04 10 | box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box 11 | - name: ubuntu-10.04 12 | driver_config: 13 | box: opscode-ubuntu-10.04 14 | box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box 15 | - name: centos-6.4 16 | driver_config: 17 | box: opscode-centos-6.4 18 | box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box 19 | - name: centos-5.9 20 | driver_config: 21 | box: opscode-centos-5.9 22 | box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_centos-5.9_provisionerless.box 23 | 24 | suites: 25 | - name: default 26 | run_list: ["recipe[haproxy-test]"] 27 | attributes: {} 28 | -------------------------------------------------------------------------------- /test/integration/lwrp/bats/nc.bats: -------------------------------------------------------------------------------- 1 | setup() { 2 | rm -f /tmp/{one,two} 3 | } 4 | 5 | 6 | teardown(){ 7 | pkill -9 nc || true 8 | } 9 | 10 | 11 | 12 | @test "can start netcat service" { 13 | LISTEN_PORT=5005 14 | SEND_PORT=5005 15 | FILE=/tmp/one 16 | MESSAGE=start 17 | nc -n -d -l 127.0.0.1 $LISTEN_PORT > $FILE & sleep 0.1 18 | echo $MESSAGE | nc 127.0.0.1 $SEND_PORT 19 | sleep 0.1 20 | grep $MESSAGE $FILE 21 | } 22 | 23 | @test "forwarding works" { 24 | LISTEN_PORT=5001 25 | LISTEN_PORT2=5002 26 | SEND_PORT=5000 27 | FILE=/tmp/one 28 | FILE2=/tmp/two 29 | MESSAGE=start 30 | nc -n -d -k -l 127.0.0.1 $LISTEN_PORT > $FILE & sleep 0.1 31 | nc -n -d -k -l 127.0.0.1 $LISTEN_PORT2 > $FILE2 & sleep 0.1 32 | echo $MESSAGE | nc 127.0.0.1 $SEND_PORT 33 | sleep 0.1 34 | echo $MESSAGE | nc 127.0.0.1 $SEND_PORT 35 | sleep 0.1 36 | grep $MESSAGE $FILE 37 | grep $MESSAGE $FILE2 38 | } 39 | -------------------------------------------------------------------------------- /recipes/tuning.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Author:: Guilhem Lettron 4 | # 5 | # Copyright 2012, Societe Publica. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | include_recipe "cpu::affinity" 21 | 22 | cpu_affinity "set affinity for haproxy" do 23 | pid node['haproxy']['pid_file'] 24 | cpu 0 25 | subscribes :set, resources("service[haproxy]"), :immediately 26 | end 27 | -------------------------------------------------------------------------------- /recipes/install_package.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Recipe:: install_package 4 | # 5 | # Copyright 2009, Opscode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | package "haproxy" 21 | 22 | directory node['haproxy']['conf_dir'] 23 | 24 | template "/etc/init.d/haproxy" do 25 | source "haproxy-init.erb" 26 | owner "root" 27 | group "root" 28 | mode 00755 29 | variables( 30 | :hostname => node['hostname'], 31 | :conf_dir => node['haproxy']['conf_dir'], 32 | :prefix => "/usr" 33 | ) 34 | end 35 | -------------------------------------------------------------------------------- /templates/default/haproxy.cfg.erb: -------------------------------------------------------------------------------- 1 | global 2 | log 127.0.0.1 local0 3 | log 127.0.0.1 local1 notice 4 | #log loghost local0 info 5 | maxconn <%= node['haproxy']['global_max_connections'] %> 6 | #debug 7 | #quiet 8 | user <%= node['haproxy']['user'] %> 9 | group <%= node['haproxy']['group'] %> 10 | <% if node['haproxy']['enable_stats_socket'] -%> 11 | stats socket <%= node['haproxy']['stats_socket_path'] %> user <%= node['haproxy']['stats_socket_user'] %> group <%= node['haproxy']['stats_socket_group'] %> 12 | <% end -%> 13 | 14 | defaults 15 | log global 16 | mode <%= node['haproxy']['mode'] %> 17 | retries 3 18 | <% @defaults_timeouts.sort.map do | value, time | -%> 19 | timeout <%= value %> <%= time %> 20 | <% end -%> 21 | <% @defaults_options.sort.each do | option | -%> 22 | option <%= option %> 23 | <% end -%> 24 | balance <%= node['haproxy']['balance_algorithm'] %> 25 | 26 | # Set up application listeners here. 27 | 28 | <% node['haproxy']['listeners'].each do |type, listeners | %> 29 | <% listeners.each do |name, listen| %> 30 | <%= type %> <%= name %> 31 | <% listen.each do |option| %> 32 | <%= option %> 33 | <% end %> 34 | 35 | <% end %> 36 | 37 | <% end %> 38 | -------------------------------------------------------------------------------- /libraries/default.rb: -------------------------------------------------------------------------------- 1 | # No namespace!? 2 | def haproxy_defaults_options 3 | options = node['haproxy']['defaults_options'].dup 4 | if node['haproxy']['x_forwarded_for'] 5 | options.push("forwardfor") 6 | end 7 | return options.uniq 8 | end 9 | 10 | def haproxy_defaults_timeouts 11 | node['haproxy']['defaults_timeouts'] 12 | end 13 | 14 | module ChefHaproxy 15 | class << self 16 | def config_generator(thing, prefix) 17 | result = [] 18 | case thing 19 | when Hash 20 | thing.each do |key, value| 21 | case value 22 | when Hash, Array 23 | result.push config_generator( 24 | value, [prefix, key.to_s].compact.join(' ') 25 | ) 26 | when TrueClass, FalseClass 27 | if(value) 28 | result << [prefix, key.to_s].compact.join(' ') 29 | end 30 | else 31 | result << [prefix, key.to_s, value.to_s].compact.join(' ') 32 | end 33 | end 34 | when Array 35 | thing.each do |v| 36 | result << [prefix, v.to_s].compact.join(' ') 37 | end 38 | else 39 | raise TypeError.new("Expecting Hash or Array type. Received: #{thing.class}") 40 | end 41 | result.join("\n") 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /.kitchen.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver_plugin: vagrant 3 | driver_config: 4 | require_chef_omnibus: true 5 | 6 | platforms: 7 | - name: centos-6.3 8 | driver_config: 9 | box: opscode-centos-6.3 10 | box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box 11 | - name: ubuntu-12.04 12 | driver_config: 13 | box: opscode-ubuntu-12.04 14 | box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box 15 | run_list: 16 | - recipe[apt] 17 | provisioner: 18 | attributes: 19 | haproxy: 20 | test: 21 | netcat_package: netcat 22 | - name: ubuntu-10.04 23 | driver_config: 24 | box: opscode-ubuntu-10.04 25 | box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box 26 | run_list: 27 | - recipe[apt] 28 | provisioner: 29 | attributes: 30 | haproxy: 31 | test: 32 | netcat_package: netcat 33 | 34 | suites: 35 | - name: default 36 | run_list: 37 | - recipe[haproxy] 38 | attributes: {} 39 | - name: lwrp 40 | run_list: 41 | - recipe[haproxy] 42 | - recipe[haproxy-test] 43 | attributes: 44 | haproxy: 45 | enable_default_http: false 46 | global_options: ['quiet'] 47 | listeners: 48 | listen: 49 | lb_test: 50 | - balance roundrobin 51 | - bind 0.0.0.0:5000 52 | - maxconn 10 53 | - mode tcp 54 | - server netcat1 127.0.0.1:5001 55 | - server netcat2 127.0.0.1:5002 56 | 57 | -------------------------------------------------------------------------------- /providers/default.rb: -------------------------------------------------------------------------------- 1 | use_inline_resources if respond_to?(:use_inline_resources) 2 | 3 | def load_current_resource 4 | new_resource.config_directory node['haproxy']['conf_dir'] unless new_resource.config_directory 5 | end 6 | 7 | def make_hash(attr) 8 | new_hash = {} 9 | attr.each do |k,v| 10 | if(v.is_a?(Hash)) 11 | new_hash[k] = make_hash(v) 12 | else 13 | new_hash[k] = v 14 | end 15 | end 16 | new_hash 17 | end 18 | 19 | action :create do 20 | 21 | run_context.include_recipe "haproxy::install_#{node['haproxy']['install_method']}" 22 | 23 | if(new_resource.config.is_a?(Proc)) 24 | chef_gem 'attribute_struct' 25 | require 'attribute_struct' 26 | new_resource.config AttributeStruct.new(&new_resource.config)._dump 27 | end 28 | 29 | directory new_resource.config_directory do 30 | recursive true 31 | end 32 | 33 | new_resource.config Chef::Mixin::DeepMerge.merge(make_hash(node[:haproxy][:config]), new_resource.config) 34 | 35 | cookbook_file '/etc/default/haproxy' do 36 | source 'haproxy-default' 37 | cookbook 'haproxy' 38 | owner 'root' 39 | group 'root' 40 | mode 00644 41 | notifies :restart, 'service[haproxy]' 42 | end 43 | 44 | template ::File.join(new_resource.config_directory, 'haproxy.cfg') do 45 | source 'haproxy.dynamic.cfg.erb' 46 | cookbook 'haproxy' 47 | owner 'root' 48 | group 'root' 49 | mode 00644 50 | notifies :reload, 'service[haproxy]' 51 | variables(:config => new_resource.config) 52 | end 53 | 54 | service "haproxy" do 55 | supports :restart => true, :status => true, :reload => true 56 | action [:enable, :start] 57 | end 58 | 59 | end 60 | 61 | action :delete do 62 | file ::File.join(new_resource.config_directory, 'haproxy.cfg') do 63 | action :delete 64 | end 65 | 66 | service 'haproxy' do 67 | action :stop 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /recipes/app_lb.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Recipe:: app_lb 4 | # 5 | # Copyright 2011, Opscode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | pool_members = search("node", "role:#{node['haproxy']['app_server_role']} AND chef_environment:#{node.chef_environment}") || [] 21 | 22 | # load balancer may be in the pool 23 | pool_members << node if node.run_list.roles.include?(node['haproxy']['app_server_role']) 24 | 25 | # we prefer connecting via local_ipv4 if 26 | # pool members are in the same cloud 27 | # TODO refactor this logic into library...see COOK-494 28 | pool_members.map! do |member| 29 | server_ip = begin 30 | if member.attribute?('cloud') 31 | if node.attribute?('cloud') && (member['cloud']['provider'] == node['cloud']['provider']) 32 | member['cloud']['local_ipv4'] 33 | else 34 | member['cloud']['public_ipv4'] 35 | end 36 | else 37 | member['ipaddress'] 38 | end 39 | end 40 | {:ipaddress => server_ip, :hostname => member['hostname']} 41 | end 42 | 43 | 44 | pool = ["options httpchk #{node['haproxy']['httpchk']}"] if node['haproxy']['httpchk'] 45 | servers = pool_members.uniq.map do |s| 46 | "#{s[:hostname]} #{s[:ipaddress]}:#{node['haproxy']['member_port']} weight 1 maxconn #{node['haproxy']['member_max_connections']} check" 47 | end 48 | haproxy_lb 'servers-http' do 49 | type 'backend' 50 | servers servers 51 | params pool 52 | end 53 | 54 | if node['haproxy']['enable_ssl'] 55 | pool = ["option ssl-hello-chk"] 56 | pool << ["options httpchk #{node['haproxy']['ssl_httpchk']}"] if node['haproxy']['ssl_httpchk'] 57 | servers = pool_members.uniq.map do |s| 58 | "#{s[:hostname]} #{s[:ipaddress]}:#{node['haproxy']['ssl_member_port']} weight 1 maxconn #{node['haproxy']['member_max_connections']} check" 59 | end 60 | haproxy_lb 'servers-http' do 61 | type 'backend' 62 | mode 'tcp' 63 | servers servers 64 | params pool 65 | end 66 | end 67 | 68 | include_recipe "haproxy::install_#{node['haproxy']['install_method']}" 69 | -------------------------------------------------------------------------------- /recipes/install_source.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Recipe:: default 4 | # 5 | # Copyright 2009, Opscode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | include_recipe 'build-essential' 21 | 22 | package 'libpcre3-dev' do 23 | only_if { node['haproxy']['source']['use_pcre'] } 24 | end 25 | 26 | package 'libssl-dev' do 27 | only_if { node['haproxy']['source']['use_openssl'] } 28 | end 29 | 30 | package 'zlib1g-dev' do 31 | only_if { node['haproxy']['source']['use_zlib'] } 32 | end 33 | 34 | node.default['haproxy']['conf_dir'] = "#{node['haproxy']['source']['prefix']}/#{node['haproxy']['conf_dir']}" 35 | 36 | remote_file "#{Chef::Config[:file_cache_path]}/haproxy-#{node['haproxy']['source']['version']}.tar.gz" do 37 | source node['haproxy']['source']['url'] 38 | checksum node['haproxy']['source']['checksum'] 39 | action :create_if_missing 40 | end 41 | 42 | make_cmd = "make TARGET=#{node['haproxy']['source']['target_os']}" 43 | make_cmd << " CPU=#{node['haproxy']['source']['target_cpu' ]}" unless node['haproxy']['source']['target_cpu'].empty? 44 | make_cmd << " ARCH=#{node['haproxy']['source']['target_arch']}" unless node['haproxy']['source']['target_arch'].empty? 45 | make_cmd << " USE_PCRE=1" if node['haproxy']['source']['use_pcre'] 46 | make_cmd << " USE_OPENSSL=1" if node['haproxy']['source']['use_openssl'] 47 | make_cmd << " USE_ZLIB=1" if node['haproxy']['source']['use_zlib'] 48 | 49 | bash "compile_haproxy" do 50 | cwd Chef::Config[:file_cache_path] 51 | code <<-EOH 52 | tar xzf haproxy-#{node['haproxy']['source']['version']}.tar.gz 53 | cd haproxy-#{node['haproxy']['source']['version']} 54 | #{make_cmd} && make install PREFIX=#{node['haproxy']['source']['prefix']} 55 | EOH 56 | creates "#{node['haproxy']['source']['prefix']}/sbin/haproxy" 57 | end 58 | 59 | user "haproxy" do 60 | comment "haproxy system account" 61 | system true 62 | shell "/bin/false" 63 | end 64 | 65 | directory node['haproxy']['conf_dir'] 66 | 67 | template "/etc/init.d/haproxy" do 68 | source "haproxy-init.erb" 69 | owner "root" 70 | group "root" 71 | mode 00755 72 | variables( 73 | :hostname => node['hostname'], 74 | :conf_dir => node['haproxy']['conf_dir'], 75 | :prefix => node['haproxy']['source']['prefix'] 76 | ) 77 | end 78 | -------------------------------------------------------------------------------- /templates/centos/haproxy-init.erb: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # haproxy 4 | # 5 | # chkconfig: - 85 15 6 | # description: HAProxy is a free, very fast and reliable solution \ 7 | # offering high availability, load balancing, and \ 8 | # proxying for TCP and HTTP-based applications 9 | # processname: haproxy 10 | # config: <%= @conf_dir %>/haproxy.cfg 11 | # pidfile: /var/run/haproxy.pid 12 | 13 | # Written by Chef on <%= @hostname %> 14 | 15 | # Source function library. 16 | . /etc/rc.d/init.d/functions 17 | 18 | # Source networking configuration. 19 | . /etc/sysconfig/network 20 | 21 | # Check that networking is up. 22 | [ "$NETWORKING" = "no" ] && exit 0 23 | 24 | config="<%= @conf_dir %>/haproxy.cfg" 25 | exec="<%= @prefix %>/sbin/haproxy" 26 | prog=$(basename $exec) 27 | 28 | [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog 29 | 30 | lockfile=/var/lock/subsys/haproxy 31 | 32 | check() { 33 | $exec -c -V -f $config 34 | } 35 | 36 | start() { 37 | $exec -c -q -f $config 38 | if [ $? -ne 0 ]; then 39 | echo "Errors in configuration file, check with $prog check." 40 | return 1 41 | fi 42 | 43 | echo -n $"Starting $prog: " 44 | # start it up here, usually something like "daemon $exec" 45 | daemon $exec -D -f $config -p /var/run/$prog.pid 46 | retval=$? 47 | echo 48 | [ $retval -eq 0 ] && touch $lockfile 49 | return $retval 50 | } 51 | 52 | stop() { 53 | echo -n $"Stopping $prog: " 54 | # stop it here, often "killproc $prog" 55 | killproc $prog 56 | retval=$? 57 | echo 58 | [ $retval -eq 0 ] && rm -f $lockfile 59 | return $retval 60 | } 61 | 62 | restart() { 63 | $exec -c -q -f $config 64 | if [ $? -ne 0 ]; then 65 | echo "Errors in configuration file, check with $prog check." 66 | return 1 67 | fi 68 | stop 69 | start 70 | } 71 | 72 | reload() { 73 | $exec -c -q -f $config 74 | if [ $? -ne 0 ]; then 75 | echo "Errors in configuration file, check with $prog check." 76 | return 1 77 | fi 78 | echo -n $"Reloading $prog: " 79 | $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid) 80 | retval=$? 81 | echo 82 | return $retval 83 | } 84 | 85 | force_reload() { 86 | restart 87 | } 88 | 89 | fdr_status() { 90 | status $prog 91 | } 92 | 93 | case "$1" in 94 | start|stop|restart|reload) 95 | $1 96 | ;; 97 | force-reload) 98 | force_reload 99 | ;; 100 | check) 101 | check 102 | ;; 103 | status) 104 | fdr_status 105 | ;; 106 | condrestart|try-restart) 107 | [ ! -f $lockfile ] || restart 108 | ;; 109 | *) 110 | echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" 111 | exit 2 112 | esac -------------------------------------------------------------------------------- /templates/rhel/haproxy-init.erb: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # haproxy 4 | # 5 | # chkconfig: - 85 15 6 | # description: HAProxy is a free, very fast and reliable solution \ 7 | # offering high availability, load balancing, and \ 8 | # proxying for TCP and HTTP-based applications 9 | # processname: haproxy 10 | # config: <%= @conf_dir %>/haproxy.cfg 11 | # pidfile: /var/run/haproxy.pid 12 | 13 | # Written by Chef on <%= @hostname %> 14 | 15 | # Source function library. 16 | . /etc/rc.d/init.d/functions 17 | 18 | # Source networking configuration. 19 | . /etc/sysconfig/network 20 | 21 | # Check that networking is up. 22 | [ "$NETWORKING" = "no" ] && exit 0 23 | 24 | config="<%= @conf_dir %>/haproxy.cfg" 25 | exec="<%= @prefix %>/sbin/haproxy" 26 | prog=$(basename $exec) 27 | 28 | [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog 29 | 30 | lockfile=/var/lock/subsys/haproxy 31 | 32 | check() { 33 | $exec -c -V -f $config 34 | } 35 | 36 | start() { 37 | $exec -c -q -f $config 38 | if [ $? -ne 0 ]; then 39 | echo "Errors in configuration file, check with $prog check." 40 | return 1 41 | fi 42 | 43 | echo -n $"Starting $prog: " 44 | # start it up here, usually something like "daemon $exec" 45 | daemon $exec -D -f $config -p /var/run/$prog.pid 46 | retval=$? 47 | echo 48 | [ $retval -eq 0 ] && touch $lockfile 49 | return $retval 50 | } 51 | 52 | stop() { 53 | echo -n $"Stopping $prog: " 54 | # stop it here, often "killproc $prog" 55 | killproc $prog 56 | retval=$? 57 | echo 58 | [ $retval -eq 0 ] && rm -f $lockfile 59 | return $retval 60 | } 61 | 62 | restart() { 63 | $exec -c -q -f $config 64 | if [ $? -ne 0 ]; then 65 | echo "Errors in configuration file, check with $prog check." 66 | return 1 67 | fi 68 | stop 69 | start 70 | } 71 | 72 | reload() { 73 | $exec -c -q -f $config 74 | if [ $? -ne 0 ]; then 75 | echo "Errors in configuration file, check with $prog check." 76 | return 1 77 | fi 78 | echo -n $"Reloading $prog: " 79 | $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid) 80 | retval=$? 81 | echo 82 | return $retval 83 | } 84 | 85 | force_reload() { 86 | restart 87 | } 88 | 89 | fdr_status() { 90 | status $prog 91 | } 92 | 93 | case "$1" in 94 | start|stop|restart|reload) 95 | $1 96 | ;; 97 | force-reload) 98 | force_reload 99 | ;; 100 | check) 101 | check 102 | ;; 103 | status) 104 | fdr_status 105 | ;; 106 | condrestart|try-restart) 107 | [ ! -f $lockfile ] || restart 108 | ;; 109 | *) 110 | echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" 111 | exit 2 112 | esac -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | haproxy Cookbook CHANGELOG 2 | ========================== 3 | This file is used to list changes made in each version of the haproxy cookbook. 4 | 5 | 6 | v1.6.2 7 | ------ 8 | ### Bug 9 | - **[COOK-3424](https://tickets.opscode.com/browse/COOK-3424)** - Haproxy cookbook attempts to alter an immutable attribute 10 | 11 | ### New Feature 12 | - **[COOK-3135](https://tickets.opscode.com/browse/COOK-3135)** - Allow setting of members with default recipe without changing the template 13 | 14 | 15 | v1.6.2 16 | ------ 17 | ### Bug 18 | - **[COOK-3424](https://tickets.opscode.com/browse/COOK-3424)** - Haproxy cookbook attempts to alter an immutable attribute 19 | 20 | ### New Feature 21 | - **[COOK-3135](https://tickets.opscode.com/browse/COOK-3135)** - Allow setting of members with default recipe without changing the template 22 | 23 | 24 | v1.6.0 25 | ------ 26 | ### New Feature 27 | - Allow setting of members with default recipe without changing the template 28 | 29 | 30 | v1.5.0 31 | ------ 32 | ### Improvement 33 | - **[COOK-3660](https://tickets.opscode.com/browse/COOK-3660)** - Make haproxy socket default user group configurable 34 | - **[COOK-3537](https://tickets.opscode.com/browse/COOK-3537)** - Add OpenSSL and zlib source configurations 35 | 36 | ### New Feature 37 | - **[COOK-2384](https://tickets.opscode.com/browse/COOK-2384)** - Add LWRP for multiple haproxy sites/configs 38 | 39 | v1.4.0 40 | ------ 41 | ### Improvement 42 | - **[COOK-3237](https://tickets.opscode.com/browse/COOK-3237)** - Enable cookie-based persistence in a backend 43 | - **[COOK-3216](https://tickets.opscode.com/browse/COOK-3216)** - Add metadata attributes 44 | 45 | ### New Feature 46 | - **[COOK-3211](https://tickets.opscode.com/browse/COOK-3211)** - Support RHEL 47 | - **[COOK-3133](https://tickets.opscode.com/browse/COOK-3133)** - Allow configuration of a global stats socket 48 | 49 | v1.3.2 50 | ------ 51 | ### Bug 52 | - [COOK-3046]: haproxy default recipe broken by COOK-2656 53 | 54 | ### Task 55 | - [COOK-2009]: Add test-kitchen support to haproxy 56 | 57 | v1.3.0 58 | ------ 59 | ### Improvement 60 | - [COOK-2656]: Unify the haproxy.cfg with that from app_lb 61 | 62 | ### New Feature 63 | - [COOK-1488]: Provide an option to build haproxy from source 64 | 65 | v1.2.0 66 | ------ 67 | - [COOK-1936] - use frontend / backend logic 68 | - [COOK-1937] - cleanup for configurations 69 | - [COOK-1938] - more flexibility for options 70 | - [COOK-1939] - reloading haproxy is better than restarting 71 | - [COOK-1940] - haproxy stats listen on 0.0.0.0 by default 72 | - [COOK-1944] - improve haproxy performance 73 | 74 | v1.1.4 75 | ------ 76 | - [COOK-1839] - add httpchk configuration to `app_lb` template 77 | 78 | v1.1.0 79 | ------ 80 | - [COOK-1275] - haproxy-default.erb should be a cookbook_file 81 | - [COOK-1594] - Template-Service ordering issue in app_lb recipe 82 | 83 | v1.0.6 84 | ------ 85 | - [COOK-1310] - redispatch flag has changed 86 | 87 | v1.0.4 88 | ------ 89 | - [COOK-806] - load balancer should include an SSL option 90 | - [COOK-805] - Fundamental haproxy load balancer options should be configurable 91 | 92 | v1.0.3 93 | ------ 94 | - [COOK-620] haproxy::app_lb's template should use the member cloud private IP by default 95 | 96 | v1.0.2 97 | ------ 98 | - fix regression introduced in v1.0.1 99 | 100 | v1.0.1 101 | ------ 102 | - account for the case where load balancer is in the pool 103 | 104 | v1.0.0 105 | ------ 106 | - Use `node.chef_environment` instead of `node['app_environment']` 107 | -------------------------------------------------------------------------------- /templates/default/haproxy-init.erb: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: haproxy 4 | # Required-Start: $local_fs $network $remote_fs 5 | # Required-Stop: $local_fs $remote_fs 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: fast and reliable load balancing reverse proxy 9 | # Description: This file should be used to start and stop haproxy. 10 | ### END INIT INFO 11 | 12 | # Author: Arnaud Cornet 13 | 14 | # Written by Chef on <%= @hostname %> 15 | 16 | PATH=/sbin:/usr/sbin:/bin:/usr/bin 17 | PIDFILE=/var/run/haproxy.pid 18 | CONFIG=<%= @conf_dir %>/haproxy.cfg 19 | HAPROXY=<%= @prefix %>/sbin/haproxy 20 | EXTRAOPTS= 21 | ENABLED=0 22 | 23 | test -x $HAPROXY || exit 0 24 | test -f "$CONFIG" || exit 0 25 | 26 | if [ -e /etc/default/haproxy ]; then 27 | . /etc/default/haproxy 28 | fi 29 | 30 | test "$ENABLED" != "0" || exit 0 31 | 32 | [ -f /etc/default/rcS ] && . /etc/default/rcS 33 | . /lib/lsb/init-functions 34 | 35 | 36 | haproxy_start() 37 | { 38 | start-stop-daemon --start --pidfile "$PIDFILE" \ 39 | --exec $HAPROXY -- -f "$CONFIG" -D -p "$PIDFILE" \ 40 | $EXTRAOPTS || return 2 41 | return 0 42 | } 43 | 44 | haproxy_stop() 45 | { 46 | if [ ! -f $PIDFILE ] ; then 47 | # This is a success according to LSB 48 | return 0 49 | fi 50 | for pid in $(cat $PIDFILE) ; do 51 | /bin/kill $pid || return 4 52 | done 53 | rm -f $PIDFILE 54 | return 0 55 | } 56 | 57 | haproxy_reload() 58 | { 59 | $HAPROXY -f "$CONFIG" -p $PIDFILE -D $EXTRAOPTS -sf $(cat $PIDFILE) \ 60 | || return 2 61 | return 0 62 | } 63 | 64 | haproxy_status() 65 | { 66 | if [ ! -f $PIDFILE ] ; then 67 | # program not running 68 | return 3 69 | fi 70 | 71 | for pid in $(cat $PIDFILE) ; do 72 | if ! ps --no-headers p "$pid" | grep haproxy > /dev/null ; then 73 | # program running, bogus pidfile 74 | return 1 75 | fi 76 | done 77 | 78 | return 0 79 | } 80 | 81 | 82 | case "$1" in 83 | start) 84 | log_daemon_msg "Starting haproxy" "haproxy" 85 | haproxy_start 86 | ret=$? 87 | case "$ret" in 88 | 0) 89 | log_end_msg 0 90 | ;; 91 | 1) 92 | log_end_msg 1 93 | echo "pid file '$PIDFILE' found, haproxy not started." 94 | ;; 95 | 2) 96 | log_end_msg 1 97 | ;; 98 | esac 99 | exit $ret 100 | ;; 101 | stop) 102 | log_daemon_msg "Stopping haproxy" "haproxy" 103 | haproxy_stop 104 | ret=$? 105 | case "$ret" in 106 | 0|1) 107 | log_end_msg 0 108 | ;; 109 | 2) 110 | log_end_msg 1 111 | ;; 112 | esac 113 | exit $ret 114 | ;; 115 | reload|force-reload) 116 | log_daemon_msg "Reloading haproxy" "haproxy" 117 | haproxy_reload 118 | case "$?" in 119 | 0|1) 120 | log_end_msg 0 121 | ;; 122 | 2) 123 | log_end_msg 1 124 | ;; 125 | esac 126 | ;; 127 | restart) 128 | log_daemon_msg "Restarting haproxy" "haproxy" 129 | haproxy_stop 130 | haproxy_start 131 | case "$?" in 132 | 0) 133 | log_end_msg 0 134 | ;; 135 | 1) 136 | log_end_msg 1 137 | ;; 138 | 2) 139 | log_end_msg 1 140 | ;; 141 | esac 142 | ;; 143 | status) 144 | haproxy_status 145 | ret=$? 146 | case "$ret" in 147 | 0) 148 | echo "haproxy is running." 149 | ;; 150 | 1) 151 | echo "haproxy dead, but $PIDFILE exists." 152 | ;; 153 | *) 154 | echo "haproxy not running." 155 | ;; 156 | esac 157 | exit $ret 158 | ;; 159 | *) 160 | echo "Usage: /etc/init.d/haproxy {start|stop|reload|restart|status}" 161 | exit 2 162 | ;; 163 | esac 164 | 165 | : 166 | -------------------------------------------------------------------------------- /attributes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Default:: default 4 | # 5 | # Copyright 2010, Opscode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | default['haproxy']['user'] = "haproxy" 21 | default['haproxy']['group'] = "haproxy" 22 | 23 | default['haproxy']['enable_default_http'] = true 24 | default['haproxy']['mode'] = "http" 25 | default['haproxy']['incoming_address'] = "0.0.0.0" 26 | default['haproxy']['incoming_port'] = 80 27 | default['haproxy']['members'] = [{ 28 | "hostname" => "localhost", 29 | "ipaddress" => "127.0.0.1", 30 | "port" => 4000, 31 | "ssl_port" => 4000 32 | }, { 33 | "hostname" => "localhost", 34 | "ipaddress" => "127.0.0.1", 35 | "port" => 4001, 36 | "ssl_port" => 4001 37 | }] 38 | default['haproxy']['member_port'] = 8080 39 | default['haproxy']['member_weight'] = 1 40 | default['haproxy']['app_server_role'] = "webserver" 41 | default['haproxy']['balance_algorithm'] = "roundrobin" 42 | default['haproxy']['enable_ssl'] = false 43 | default['haproxy']['ssl_incoming_address'] = "0.0.0.0" 44 | default['haproxy']['ssl_incoming_port'] = 443 45 | default['haproxy']['ssl_member_port'] = 8443 46 | default['haproxy']['httpchk'] = nil 47 | default['haproxy']['ssl_httpchk'] = nil 48 | default['haproxy']['enable_admin'] = true 49 | default['haproxy']['admin']['address_bind'] = "127.0.0.1" 50 | default['haproxy']['admin']['port'] = 22002 51 | default['haproxy']['enable_stats_socket'] = false 52 | default['haproxy']['stats_socket_path'] = "/var/run/haproxy.sock" 53 | default['haproxy']['stats_socket_user'] = node['haproxy']['user'] 54 | default['haproxy']['stats_socket_group'] = node['haproxy']['group'] 55 | default['haproxy']['pid_file'] = "/var/run/haproxy.pid" 56 | 57 | default['haproxy']['defaults_options'] = ["httplog", "dontlognull", "redispatch"] 58 | default['haproxy']['x_forwarded_for'] = false 59 | default['haproxy']['defaults_timeouts']['connect'] = "5s" 60 | default['haproxy']['defaults_timeouts']['client'] = "50s" 61 | default['haproxy']['defaults_timeouts']['server'] = "50s" 62 | default['haproxy']['cookie'] = nil 63 | 64 | default['haproxy']['global_max_connections'] = 4096 65 | default['haproxy']['member_max_connections'] = 100 66 | default['haproxy']['frontend_max_connections'] = 2000 67 | default['haproxy']['frontend_ssl_max_connections'] = 2000 68 | 69 | default['haproxy']['install_method'] = 'package' 70 | default['haproxy']['conf_dir'] = '/etc/haproxy' 71 | 72 | default['haproxy']['source']['version'] = '1.4.22' 73 | default['haproxy']['source']['url'] = 'http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz' 74 | default['haproxy']['source']['checksum'] = 'ba221b3eaa4d71233230b156c3000f5c2bd4dace94d9266235517fe42f917fc6' 75 | default['haproxy']['source']['prefix'] = '/usr/local' 76 | default['haproxy']['source']['target_os'] = 'generic' 77 | default['haproxy']['source']['target_cpu'] = '' 78 | default['haproxy']['source']['target_arch'] = '' 79 | default['haproxy']['source']['use_pcre'] = false 80 | default['haproxy']['source']['use_openssl'] = false 81 | default['haproxy']['source']['use_zlib'] = false 82 | 83 | default['haproxy']['listeners'] = { 84 | 'listen' => {}, 85 | 'frontend' => {}, 86 | 'backend' => {} 87 | } 88 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook Name:: haproxy 3 | # Recipe:: default 4 | # 5 | # Copyright 2009, Opscode, Inc. 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | include_recipe "haproxy::install_#{node['haproxy']['install_method']}" 21 | 22 | cookbook_file "/etc/default/haproxy" do 23 | source "haproxy-default" 24 | owner "root" 25 | group "root" 26 | mode 00644 27 | notifies :restart, "service[haproxy]" 28 | end 29 | 30 | 31 | if node['haproxy']['enable_admin'] 32 | admin = node['haproxy']['admin'] 33 | haproxy_lb "admin" do 34 | bind "#{admin['address_bind']}:#{admin['port']}" 35 | mode 'http' 36 | params({ 'stats' => 'uri /'}) 37 | end 38 | end 39 | 40 | conf = node['haproxy'] 41 | member_max_conn = conf['member_max_connections'] 42 | member_weight = conf['member_weight'] 43 | 44 | if conf['enable_default_http'] 45 | haproxy_lb 'http' do 46 | type 'frontend' 47 | params({ 48 | 'maxconn' => conf['frontend_max_connections'], 49 | 'bind' => "#{conf['incoming_address']}:#{conf['incoming_port']}", 50 | 'default_backend' => 'servers-http' 51 | }) 52 | end 53 | 54 | member_port = conf['member_port'] 55 | pool = [] 56 | pool << "option httpchk #{conf['httpchk']}" if conf['httpchk'] 57 | servers = node['haproxy']['members'].map do |member| 58 | "#{member['hostname']} #{member['ipaddress']}:#{member['port'] || member_port} weight #{member['weight'] || member_weight} maxconn #{member['max_connections'] || member_max_conn} check" 59 | end 60 | haproxy_lb 'servers-http' do 61 | type 'backend' 62 | servers servers 63 | params pool 64 | end 65 | end 66 | 67 | 68 | if node['haproxy']['enable_ssl'] 69 | haproxy_lb 'https' do 70 | type 'frontend' 71 | mode 'tcp' 72 | params({ 73 | 'maxconn' => node['haproxy']['frontend_ssl_max_connections'], 74 | 'bind' => "#{node['haproxy']['ssl_incoming_address']}:#{node['haproxy']['ssl_incoming_port']}", 75 | 'default_backend' => 'servers-https' 76 | }) 77 | end 78 | 79 | ssl_member_port = conf['ssl_member_port'] 80 | pool = ['option ssl-hello-chk'] 81 | pool << "option httpchk #{conf['ssl_httpchk']}" if conf['ssl_httpchk'] 82 | servers = node['haproxy']['members'].map do |member| 83 | "#{member['hostname']} #{member['ipaddress']}:#{member['ssl_port'] || ssl_member_port} weight #{member['weight'] || member_weight} maxconn #{member['max_connections'] || member_max_conn} check" 84 | end 85 | haproxy_lb 'servers-https' do 86 | type 'backend' 87 | mode 'tcp' 88 | servers servers 89 | params pool 90 | end 91 | end 92 | 93 | # Re-default user/group to account for role/recipe overrides 94 | node.default['haproxy']['stats_socket_user'] = node['haproxy']['user'] 95 | node.default['haproxy']['stats_socket_group'] = node['haproxy']['group'] 96 | 97 | template "#{node['haproxy']['conf_dir']}/haproxy.cfg" do 98 | source "haproxy.cfg.erb" 99 | owner "root" 100 | group "root" 101 | mode 00644 102 | notifies :reload, "service[haproxy]" 103 | variables( 104 | :defaults_options => haproxy_defaults_options, 105 | :defaults_timeouts => haproxy_defaults_timeouts 106 | ) 107 | end 108 | 109 | service "haproxy" do 110 | supports :restart => true, :status => true, :reload => true 111 | action [:enable, :start] 112 | end 113 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name "haproxy" 2 | maintainer "Opscode, Inc." 3 | maintainer_email "cookbooks@opscode.com" 4 | license "Apache 2.0" 5 | description "Installs and configures haproxy" 6 | long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) 7 | version "1.6.3" 8 | 9 | recipe "haproxy", "Installs and configures haproxy" 10 | recipe "haproxy::app_lb", "Installs and configures haproxy by searching for nodes of a particular role" 11 | 12 | %w{ debian ubuntu centos redhat}.each do |os| 13 | supports os 14 | end 15 | 16 | depends "cpu", ">= 0.2.0" 17 | depends "build-essential" 18 | 19 | attribute "haproxy/incoming_address", 20 | :display_name => "HAProxy incoming address", 21 | :description => "Sets the address to bind the haproxy process on, 0.0.0.0 (all addresses) by default.", 22 | :required => "optional", 23 | :default => "0.0.0.0" 24 | 25 | attribute "haproxy/incoming_port", 26 | :display_name => "HAProxy incoming port", 27 | :description => "Sets the port on which haproxy listens.", 28 | :required => "optional", 29 | :default => "80" 30 | 31 | attribute "haproxy/member_port", 32 | :display_name => "HAProxy member port", 33 | :description => "The port that member systems will be listening on, default 8080.", 34 | :required => "optional", 35 | :default => "8080" 36 | 37 | attribute "haproxy/app_server_role", 38 | :display_name => "HAProxy app server role", 39 | :description => "Used by the app_lb recipe to search for a specific role of member systems. Default webserver.", 40 | :required => "optional", 41 | :default => "webserver" 42 | 43 | attribute "haproxy/balance_algorithm", 44 | :display_name => "HAProxy balance algorithm", 45 | :description => "Sets the load balancing algorithm; defaults to roundrobin.", 46 | :required => "optional", 47 | :default => "roundrobin" 48 | 49 | attribute "haproxy/enable_ssl", 50 | :display_name => "HAProxy enable ssl", 51 | :description => "Whether or not to create listeners for ssl, default false.", 52 | :required => "optional" 53 | 54 | attribute "haproxy/ssl_incoming_address", 55 | :display_name => "HAProxy ssl incoming address", 56 | :description => "Sets the address to bind the haproxy on for SSL, 0.0.0.0 (all addresses) by default.", 57 | :required => "optional", 58 | :default => "0.0.0.0" 59 | 60 | attribute "haproxy/ssl_member_port", 61 | :display_name => "HAProxy member port", 62 | :description => "The port that member systems will be listening on for ssl, default 8443.", 63 | :required => "optional", 64 | :default => "8443" 65 | 66 | attribute "haproxy/ssl_incoming_port", 67 | :display_name => "HAProxy incoming port", 68 | :description => "Sets the port on which haproxy listens for ssl, default 443.", 69 | :required => "optional", 70 | :default => "443" 71 | 72 | attribute "haproxy/httpchk", 73 | :display_name => "HAProxy HTTP health check", 74 | :description => "Used by the haproxy::app_lb recipe. If set, will configure httpchk in haproxy.conf.", 75 | :required => "optional" 76 | 77 | attribute "haproxy/ssl_httpchk", 78 | :display_name => "HAProxy SSL HTTP health check", 79 | :description => "Used by the app_lb recipe. If set and enable_ssl is true, will configure httpchk in haproxy.conf for the ssl_application section.", 80 | :required => "optional" 81 | 82 | attribute "haproxy/enable_admin", 83 | :display_name => "HAProxy enable admin", 84 | :description => "Whether to enable the admin interface. default true. Listens on port 22002.", 85 | :required => "optional", 86 | :default => "true" 87 | 88 | attribute "haproxy/admin/address_bind", 89 | :display_name => "HAProxy admin address bind", 90 | :description => "Sets the address to bind the administrative interface on, 127.0.0.1 by default.", 91 | :required => "optional", 92 | :default => "127.0.0.1" 93 | 94 | attribute "haproxy/admin/port", 95 | :display_name => "HAProxy admin port", 96 | :description => "Sets the port for the administrative interface, 22002 by default.", 97 | :required => "optional", 98 | :default => "22002" 99 | 100 | attribute "haproxy/pid_file", 101 | :display_name => "HAProxy PID file", 102 | :description => "The PID file of the haproxy process, used in the tuning recipe.", 103 | :required => "optional", 104 | :default => "/var/run/haproxy.pid" 105 | 106 | attribute "haproxy/default options", 107 | :display_name => "HAProxy default options", 108 | :description => "An array of options to use for the config file's defaults stanza, default is [\"httplog\", \"dontlognull\", \"redispatch\"].", 109 | :required => "optional", 110 | :type => "array", 111 | :default => ["httplog", "dontlognull", "redispatch"] 112 | 113 | attribute "haproxy/defaults_timeouts/connect", 114 | :display_name => "HAProxy connect timeout", 115 | :description => "Connect timeout in defaults stanza.", 116 | :required => "optional", 117 | :default => "5s" 118 | 119 | attribute "haproxy/defaults_timeouts/client", 120 | :display_name => "HAProxy client timeout", 121 | :description => "Client timeout in defaults stanza.", 122 | :required => "optional", 123 | :default => "50s" 124 | 125 | attribute "haproxy/defaults_timeouts/server", 126 | :display_name => "HAProxy server timeout", 127 | :description => "Server timeout in defaults stanza.", 128 | :required => "optional", 129 | :default => "50s" 130 | 131 | attribute "haproxy/x_forwarded_for", 132 | :display_name => "HAProxy X-Forwarded-For", 133 | :description => "If true, creates an X-Forwarded-For header containing the original client's IP address. This option disables KeepAlive.", 134 | :required => "optional" 135 | 136 | attribute "haproxy/member_max_connections", 137 | :display_name => "HAProxy member max connections", 138 | :description => "The maxconn value to be set for each app server.", 139 | :required => "optional", 140 | :default => "100" 141 | 142 | attribute "haproxy/user", 143 | :display_name => "HAProxy user", 144 | :description => "User that haproxy runs as.", 145 | :required => "optional", 146 | :default => "haproxy" 147 | 148 | attribute "haproxy/group", 149 | :display_name => "HAProxy group", 150 | :description => "Group that haproxy runs as.", 151 | :required => "optional", 152 | :default => "haproxy" 153 | 154 | attribute "haproxy/global_max_connections", 155 | :display_name => "HAProxy global max connections", 156 | :description => "In the app_lb config, set the global maxconn.", 157 | :required => "optional", 158 | :default => "4096" 159 | 160 | attribute "haproxy/frontend_max_connections", 161 | :display_name => "HAProxy frontend max connections", 162 | :description => "In the app_lb config, set the the maxconn per frontend member.", 163 | :required => "optional", 164 | :default => "2000" 165 | 166 | attribute "haproxy/frontend_ssl_max_connections", 167 | :display_name => "HAProxy frontend SSL max connections", 168 | :description => "In the app_lb config, set the maxconn per frontend member using SSL.", 169 | :required => "optional", 170 | :default => "2000" 171 | 172 | attribute "haproxy/install_method", 173 | :display_name => "HAProxy install method", 174 | :description => "Determines which method is used to install haproxy, must be 'source' or 'package'. defaults to 'package'.", 175 | :required => "recommended", 176 | :choice => ["package", "source"], 177 | :default => "package" 178 | 179 | attribute "haproxy/conf_dir", 180 | :display_name => "HAProxy config directory", 181 | :description => "The location of the haproxy config file.", 182 | :required => "optional", 183 | :default => "/etc/haproxy" 184 | 185 | attribute "haproxy/source/version", 186 | :display_name => "HAProxy source version", 187 | :description => "The version of haproxy to install.", 188 | :required => "optional", 189 | :default => "1.4.22" 190 | 191 | attribute "haproxy/source/url", 192 | :display_name => "HAProxy source URL", 193 | :description => "The full URL to the haproxy source package.", 194 | :required => "optional", 195 | :default => "http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz" 196 | 197 | attribute "haproxy/source/checksum", 198 | :display_name => "HAProxy source checksum", 199 | :description => "The checksum of the haproxy source package.", 200 | :required => "optional", 201 | :default => "ba221b3eaa4d71233230b156c3000f5c2bd4dace94d9266235517fe42f917fc6" 202 | 203 | attribute "haproxy/source/prefix", 204 | :display_name => "HAProxy source prefix", 205 | :description => "The prefix used to make install haproxy.", 206 | :required => "optional", 207 | :default => "/usr/local" 208 | 209 | attribute "haproxy/source/target_os", 210 | :display_name => "HAProxy source target OS", 211 | :description => "The target used to make haproxy.", 212 | :required => "optional", 213 | :default => "generic" 214 | 215 | attribute "haproxy/source/target_cpu", 216 | :display_name => "HAProxy source target CPU", 217 | :description => "The target cpu used to make haproxy.", 218 | :required => "optional", 219 | :default => "" 220 | 221 | attribute "haproxy/source/target_arch", 222 | :display_name => "HAProxy source target arch", 223 | :description => "The target arch used to make haproxy.", 224 | :required => "optional", 225 | :default => "" 226 | 227 | attribute "haproxy/source/use_pcre", 228 | :display_name => "HAProxy source use PCRE", 229 | :description => "Whether to build with libpcre support.", 230 | :required => "optional" 231 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | haproxy Cookbook 2 | ================ 3 | Installs haproxy and prepares the configuration location. 4 | 5 | 6 | Requirements 7 | ------------ 8 | ### Platforms 9 | - Ubuntu (10.04+ due to config option change) 10 | - Redhat (6.0+) 11 | - Debian (6.0+) 12 | 13 | 14 | Attributes 15 | ---------- 16 | - `node['haproxy']['incoming_address']` - sets the address to bind the haproxy process on, 0.0.0.0 (all addresses) by default 17 | - `node['haproxy']['incoming_port']` - sets the port on which haproxy listens 18 | - `node['haproxy']['members']` - used by the default recipe to specify the member systems to add. Default 19 | 20 | ```ruby 21 | [{ 22 | "hostname" => "localhost", 23 | "ipaddress" => "127.0.0.1", 24 | "port" => 4000, 25 | "ssl_port" => 4000 26 | }, { 27 | "hostname" => "localhost", 28 | "ipaddress" => "127.0.0.1", 29 | "port" => 4001, 30 | "ssl_port" => 4001 31 | }] 32 | ``` 33 | 34 | - `node['haproxy']['member_port']` - the port that member systems will 35 | be listening on if not otherwise specified in the members attribute, default 8080 36 | - `node['haproxy']['member_weight']` - the weight to apply to member systems if not otherwise specified in the members attribute, default 1 37 | - `node['haproxy']['app_server_role']` - used by the `app_lb` recipe to search for a specific role of member systems. Default `webserver`. 38 | - `node['haproxy']['balance_algorithm']` - sets the load balancing algorithm; defaults to roundrobin. 39 | - `node['haproxy']['enable_ssl']` - whether or not to create listeners for ssl, default false 40 | - `node['haproxy']['ssl_incoming_address']` - sets the address to bind the haproxy on for SSL, 0.0.0.0 (all addresses) by default 41 | - `node['haproxy']['ssl_member_port']` - the port that member systems will be listening on for ssl, default 8443 42 | - `node['haproxy']['ssl_incoming_port']` - sets the port on which haproxy listens for ssl, default 443 43 | - `node['haproxy']['httpchk']` - used by the `app_lb` recipe. If set, will configure httpchk in haproxy.conf 44 | - `node['haproxy']['ssl_httpchk']` - used by the `app_lb` recipe. If set and `enable_ssl` is true, will configure httpchk in haproxy.conf for the `ssl_application` section 45 | - `node['haproxy']['enable_admin']` - whether to enable the admin interface. default true. Listens on port 22002. 46 | - `node['haproxy']['admin']['address_bind']` - sets the address to bind the administrative interface on, 127.0.0.1 by default 47 | - `node['haproxy']['admin']['port']` - sets the port for the administrative interface, 22002 by default 48 | - `node['haproxy']['pid_file']` - the PID file of the haproxy process, used in the tuning recipe. 49 | - `node['haproxy']['defaults_options']` - an array of options to use for the config file's `defaults` stanza, default is ["httplog", "dontlognull", "redispatch"] 50 | - `node['haproxy']['defaults_timeouts']['connect']` - connect timeout in defaults stanza 51 | - `node['haproxy']['defaults_timeouts']['client']` - client timeout in defaults stanza 52 | - `node['haproxy']['defaults_timeouts']['server']` - server timeout in defaults stanza 53 | - `node['haproxy']['x_forwarded_for']` - if true, creates an X-Forwarded-For header containing the original client's IP address. This option disables KeepAlive. 54 | - `node['haproxy']['member_max_connections']` - the maxconn value to be set for each app server 55 | - `node['haproxy']['cookie']` - if set, use this to pin connection to the same server with a cookie. 56 | - `node['haproxy']['user']` - user that haproxy runs as 57 | - `node['haproxy']['group']` - group that haproxy runs as 58 | - `node['haproxy']['global_max_connections']` - in the `app_lb` config, set the global maxconn 59 | - `node['haproxy']['member_max_connections']` - the maxconn value to 60 | be set for each app server if not otherwise specified in the members attribute 61 | - `node['haproxy']['frontend_max_connections']` - in the `app_lb` config, set the the maxconn per frontend member 62 | - `node['haproxy']['frontend_ssl_max_connections']` - in the `app_lb` config, set the maxconn per frontend member using SSL 63 | - `node['haproxy']['install_method']` - determines which method is used to install haproxy, must be 'source' or 'package'. defaults to 'package' 64 | - `node['haproxy']['conf_dir']` - the location of the haproxy config file 65 | - `node['haproxy']['source']['version']` - the version of haproxy to install 66 | - `node['haproxy']['source']['url']` - the full URL to the haproxy source package 67 | - `node['haproxy']['source']['checksum']` - the checksum of the haproxy source package 68 | - `node['haproxy']['source']['prefix']` - the prefix used to `make install` haproxy 69 | - `node['haproxy']['source']['target_os']` - the target used to `make` haproxy 70 | - `node['haproxy']['source']['target_cpu']` - the target cpu used to `make` haproxy 71 | - `node['haproxy']['source']['target_arch']` - the target arch used to `make` haproxy 72 | - `node['haproxy']['source']['use_pcre']` - whether to build with libpcre support 73 | 74 | Recipes 75 | ------- 76 | ### default 77 | Sets up haproxy using statically defined configuration. To override the configuration, modify the templates/default/haproxy.cfg.erb file directly, or supply your own and override the cookbook and source by reopening the `template[/etc/haproxy/haproxy.cfg]` resource. 78 | 79 | ### app_lb 80 | Sets up haproxy using dynamically defined configuration through search. See __Usage__ below. 81 | 82 | ### tuning 83 | Uses the community `cpu` cookbook's `cpu_affinity` LWRP to set affinity for the haproxy process. 84 | 85 | ### install_package 86 | Installs haproxy through the package manager. Used by the `default` and `app_lb` recipes. 87 | 88 | ### install_source 89 | Installs haproxy from source. Used by the `default` and `app_lb` recipes. 90 | 91 | 92 | Providers 93 | --------- 94 | ### haproxy_lb 95 | Configure a part of haproxy (`frontend|backend|listen`). It is used in `default` and `app_lb` recipe to configure default frontends and backends. Several common options can be set as attributes of the LWRP. Others can always be set with the `params` attribute. For instance, 96 | 97 | ```ruby 98 | haproxy_lb 'rabbitmq' do 99 | bind '0.0.0.0:5672' 100 | mode 'tcp' 101 | servers (1..4).map do |i| 102 | "rmq#{i} 10.0.0.#{i}:5672 check inter 10s rise 2 fall 3" 103 | end 104 | params({ 105 | 'maxconn' => 20000, 106 | 'balance' => 'roundrobin' 107 | }) 108 | end 109 | ``` 110 | 111 | which will be translated into: 112 | 113 | ```text 114 | listen rabbitmq' 115 | bind 0.0.0.0:5672 116 | mode tcp 117 | rmq1 10.0.0.1:5672 check inter 10s rise 2 fall 3 118 | rmq2 10.0.0.2:5672 check inter 10s rise 2 fall 3 119 | rmq3 10.0.0.3:5672 check inter 10s rise 2 fall 3 120 | rmq4 10.0.0.4:5672 check inter 10s rise 2 fall 3 121 | maxconn 20000 122 | balance roundrobin 123 | ``` 124 | 125 | All options can also be set in the params instead. In that case, you might want to provide an array to params attributes to avoid conflicts for options occuring several times. 126 | 127 | ```ruby 128 | haproxy_lb 'rabbitmq' do 129 | params([ 130 | 'bind 0.0.0.0:5672', 131 | 'mode tcp', 132 | 'rmq1 10.0.0.1:5672 check inter 10s rise 2 fall 3', 133 | 'rmq2 10.0.0.2:5672 check inter 10s rise 2 fall 3', 134 | 'rmq3 10.0.0.3:5672 check inter 10s rise 2 fall 3', 135 | 'rmq4 10.0.0.4:5672 check inter 10s rise 2 fall 3', 136 | 'maxconn' => 20000, 137 | 'balance' => 'roundrobin' 138 | ]) 139 | end 140 | ``` 141 | 142 | which will give the same result. 143 | 144 | Finally you can also configure frontends and backends by specify the type attribute of the resource. See example in the default recipe. 145 | 146 | Instead of using lwrp, you can use `node['haproxy']['listeners']` to configure all kind of listeners (`listen`, `frontend` and `backend`) 147 | 148 | ### haproxy 149 | 150 | The haproxy LWRP allows for a more freeform method of configuration. It will map a given data structure into the proper configuration 151 | format, making it easier for adjustment and expansion. 152 | 153 | ```ruby 154 | haproxy 'myhaproxy' do 155 | config Mash.new( 156 | :global => { 157 | :maxconn => node[:haproxy][:global_max_connections], 158 | :user => node[:haproxy][:user], 159 | :group => node[:haproxy][:group] 160 | }, 161 | :defaults => { 162 | :log => :global, 163 | :mode => :tcp, 164 | :retries => 3, 165 | :timeout => 5 166 | }, 167 | :frontend => { 168 | :srvs => { 169 | :maxconn => node[:haproxy][:frontend_max_connections], 170 | :bind => "#{node[:haproxy][:incoming_address]}:#{node[:haproxy][:incoming_port]}", 171 | :default_backend => :backend_servers 172 | } 173 | }, 174 | :backend => { 175 | :backend_servers => { 176 | :mode => :tcp, 177 | :server => [ 178 | "an_node 192.168.99.9:9999" => { 179 | :weight => 1, 180 | :maxconn => node[:haproxy][:member_max_connections] 181 | } 182 | ] 183 | } 184 | } 185 | ) 186 | end 187 | ``` 188 | 189 | Usage 190 | ----- 191 | Use either the default recipe or the `app_lb` recipe. 192 | 193 | When using the default recipe, the members attribute specifies the http application servers. If you wish to use the `node['haproxy']['listeners']` attribute or `haproxy_lb` lwrp instead then set `node['haproxy']['enable_default_http']` to `false`. 194 | 195 | ```ruby 196 | "haproxy" => { 197 | "members" => [{ 198 | "hostname" => "appserver1", 199 | "ipaddress" => "123.123.123.1", 200 | "port" => 8000, 201 | "ssl_port" => 8443, 202 | "weight" => 1, 203 | "max_connections" => 100 204 | }, { 205 | "hostname" => "appserver2", 206 | "ipaddress" => "123.123.123.2", 207 | "port" => 8000, 208 | "ssl_port" => 8443, 209 | "weight" => 1, 210 | "max_connections" => 100 211 | }, { 212 | "hostname" => "appserver3", 213 | "ipaddress" => "123.123.123.3", 214 | "port" => 8000, 215 | "ssl_port" => 8443, 216 | "weight" => 1, 217 | "max_connections" => 100 218 | }] 219 | } 220 | ``` 221 | 222 | Note that the following attributes are optional 223 | 224 | - `port` will default to the value of `node['haproxy']['member_port']` 225 | - `ssl_port` will default to the value of `node['haproxy']['ssl_member_port']` 226 | - `weight` will default to the value of `node['haproxy']['member_weight']` 227 | - `max_connections` will default to the value of `node['haproxy']['member_max_connections']` 228 | 229 | The `app_lb` recipe is designed to be used with the application cookbook, and provides search mechanism to find the appropriate application servers. Set this in a role that includes the haproxy::app_lb recipe. For example, 230 | 231 | ```ruby 232 | name 'load_balancer' 233 | description 'haproxy load balancer' 234 | run_list('recipe[haproxy::app_lb]') 235 | override_attributes( 236 | 'haproxy' => { 237 | 'app_server_role' => 'webserver' 238 | } 239 | ) 240 | ``` 241 | 242 | The search uses the node's `chef_environment`. For example, create `environments/production.rb`, then upload it to the server with knife 243 | 244 | 245 | License & Authors 246 | ----------------- 247 | - Author:: Joshua Timberman () 248 | 249 | ```text 250 | Copyright:: 2009-2013, Opscode, Inc 251 | 252 | Licensed under the Apache License, Version 2.0 (the "License"); 253 | you may not use this file except in compliance with the License. 254 | You may obtain a copy of the License at 255 | 256 | http://www.apache.org/licenses/LICENSE-2.0 257 | 258 | Unless required by applicable law or agreed to in writing, software 259 | distributed under the License is distributed on an "AS IS" BASIS, 260 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 261 | See the License for the specific language governing permissions and 262 | limitations under the License. 263 | ``` 264 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Opscode Cookbooks 2 | 3 | We are glad you want to contribute to Opscode Cookbooks! The first 4 | step is the desire to improve the project. 5 | 6 | You can find the answers to additional frequently asked questions 7 | [on the wiki](http://wiki.opscode.com/display/chef/How+to+Contribute). 8 | 9 | You can find additional information about 10 | [contributing to cookbooks](http://wiki.opscode.com/display/chef/How+to+Contribute+to+Opscode+Cookbooks) 11 | on the wiki as well. 12 | 13 | ## Quick-contribute 14 | 15 | * Create an account on our [bug tracker](http://tickets.opscode.com) 16 | * Sign our contributor agreement (CLA) 17 | [ online](https://secure.echosign.com/public/hostedForm?formid=PJIF5694K6L) 18 | (keep reading if you're contributing on behalf of your employer) 19 | * Create a ticket for your change on the 20 | [bug tracker](http://tickets.opscode.com) 21 | * Link to your patch as a rebased git branch or pull request from the 22 | ticket 23 | * Resolve the ticket as fixed 24 | 25 | We regularly review contributions and will get back to you if we have 26 | any suggestions or concerns. 27 | 28 | ## The Apache License and the CLA/CCLA 29 | 30 | Licensing is very important to open source projects, it helps ensure 31 | the software continues to be available under the terms that the author 32 | desired. Chef uses the Apache 2.0 license to strike a balance between 33 | open contribution and allowing you to use the software however you 34 | would like to. 35 | 36 | The license tells you what rights you have that are provided by the 37 | copyright holder. It is important that the contributor fully 38 | understands what rights they are licensing and agrees to them. 39 | Sometimes the copyright holder isn't the contributor, most often when 40 | the contributor is doing work for a company. 41 | 42 | To make a good faith effort to ensure these criteria are met, Opscode 43 | requires a Contributor License Agreement (CLA) or a Corporate 44 | Contributor License Agreement (CCLA) for all contributions. This is 45 | without exception due to some matters not being related to copyright 46 | and to avoid having to continually check with our lawyers about small 47 | patches. 48 | 49 | It only takes a few minutes to complete a CLA, and you retain the 50 | copyright to your contribution. 51 | 52 | You can complete our contributor agreement (CLA) 53 | [ online](https://secure.echosign.com/public/hostedForm?formid=PJIF5694K6L). 54 | If you're contributing on behalf of your employer, have your employer 55 | fill out our 56 | [Corporate CLA](https://secure.echosign.com/public/hostedForm?formid=PIE6C7AX856) 57 | instead. 58 | 59 | ## Ticket Tracker (JIRA) 60 | 61 | The [ticket tracker](http://tickets.opscode.com) is the most important 62 | documentation for the code base. It provides significant historical 63 | information, such as: 64 | 65 | * Which release a bug fix is included in 66 | * Discussion regarding the design and merits of features 67 | * Error output to aid in finding similar bugs 68 | 69 | Each ticket should aim to fix one bug or add one feature. 70 | 71 | ## Using git 72 | 73 | You can get a quick copy of the repository for this cookbook by 74 | running `git clone 75 | git://github.com/opscode-coobkooks/COOKBOOKNAME.git`. 76 | 77 | For collaboration purposes, it is best if you create a Github account 78 | and fork the repository to your own account. Once you do this you will 79 | be able to push your changes to your Github repository for others to 80 | see and use. 81 | 82 | If you have another repository in your GitHub account named the same 83 | as the cookbook, we suggest you suffix the repository with -cookbook. 84 | 85 | ### Branches and Commits 86 | 87 | You should submit your patch as a git branch named after the ticket, 88 | such as COOK-1337. This is called a _topic branch_ and allows users to 89 | associate a branch of code with the ticket. 90 | 91 | It is a best practice to have your commit message have a _summary 92 | line_ that includes the ticket number, followed by an empty line and 93 | then a brief description of the commit. This also helps other 94 | contributors understand the purpose of changes to the code. 95 | 96 | [COOK-1757] - platform_family and style 97 | 98 | * use platform_family for platform checking 99 | * update notifies syntax to "resource_type[resource_name]" instead of 100 | resources() lookup 101 | * COOK-692 - delete config files dropped off by packages in conf.d 102 | * dropped debian 4 support because all other platforms have the same 103 | values, and it is older than "old stable" debian release 104 | 105 | Remember that not all users use Chef in the same way or on the same 106 | operating systems as you, so it is helpful to be clear about your use 107 | case and change so they can understand it even when it doesn't apply 108 | to them. 109 | 110 | ### Github and Pull Requests 111 | 112 | All of Opscode's open source cookbook projects are available on 113 | [Github](http://www.github.com/opscode-cookbooks). 114 | 115 | We don't require you to use Github, and we will even take patch diffs 116 | attached to tickets on the tracker. However Github has a lot of 117 | convenient features, such as being able to see a diff of changes 118 | between a pull request and the main repository quickly without 119 | downloading the branch. 120 | 121 | If you do choose to use a pull request, please provide a link to the 122 | pull request from the ticket __and__ a link to the ticket from the 123 | pull request. Because pull requests only have two states, open and 124 | closed, we can't easily filter pull requests that are waiting for a 125 | reply from the author for various reasons. 126 | 127 | ### More information 128 | 129 | Additional help with git is available on the 130 | [Working with Git](http://wiki.opscode.com/display/chef/Working+with+Git) 131 | wiki page. 132 | 133 | ## Functional and Unit Tests 134 | 135 | This cookbook is set up to run tests under 136 | [Opscode's test-kitchen](https://github.com/opscode/test-kitchen). It 137 | uses minitest-chef to run integration tests after the node has been 138 | converged to verify that the state of the node. 139 | 140 | Test kitchen should run completely without exception using the default 141 | [baseboxes provided by Opscode](https://github.com/opscode/bento). 142 | Because Test Kitchen creates VirtualBox machines and runs through 143 | every configuration in the Kitchenfile, it may take some time for 144 | these tests to complete. 145 | 146 | If your changes are only for a specific recipe, run only its 147 | configuration with Test Kitchen. If you are adding a new recipe, or 148 | other functionality such as a LWRP or definition, please add 149 | appropriate tests and ensure they run with Test Kitchen. 150 | 151 | If any don't pass, investigate them before submitting your patch. 152 | 153 | Any new feature should have unit tests included with the patch with 154 | good code coverage to help protect it from future changes. Similarly, 155 | patches that fix a bug or regression should have a _regression test_. 156 | Simply put, this is a test that would fail without your patch but 157 | passes with it. The goal is to ensure this bug doesn't regress in the 158 | future. Consider a regular expression that doesn't match a certain 159 | pattern that it should, so you provide a patch and a test to ensure 160 | that the part of the code that uses this regular expression works as 161 | expected. Later another contributor may modify this regular expression 162 | in a way that breaks your use cases. The test you wrote will fail, 163 | signalling to them to research your ticket and use case and accounting 164 | for it. 165 | 166 | If you need help writing tests, please ask on the Chef Developer's 167 | mailing list, or the #chef-hacking IRC channel. 168 | 169 | ## Code Review 170 | 171 | Opscode regularly reviews code contributions and provides suggestions 172 | for improvement in the code itself or the implementation. 173 | 174 | We find contributions by searching the ticket tracker for _resolved_ 175 | tickets with a status of _fixed_. If we have feedback we will reopen 176 | the ticket and you should resolve it again when you've made the 177 | changes or have a response to our feedback. When we believe the patch 178 | is ready to be merged, we will tag the _Code Reviewed_ field with 179 | _Reviewed_. 180 | 181 | Depending on the project, these tickets are then merged within a week 182 | or two, depending on the current release cycle. 183 | 184 | ## Release Cycle 185 | 186 | The versioning for Opscode Cookbook projects is X.Y.Z. 187 | 188 | * X is a major release, which may not be fully compatible with prior 189 | major releases 190 | * Y is a minor release, which adds both new features and bug fixes 191 | * Z is a patch release, which adds just bug fixes 192 | 193 | A released version of a cookbook will end in an even number, e.g. 194 | "1.2.4" or "0.8.0". When development for the next version of the 195 | cookbook begins, the "Z" patch number is incremented to the next odd 196 | number, however the next release of the cookbook may be a major or 197 | minor incrementing version. 198 | 199 | Releases of Opscode's cookbooks are usually announced on the Chef user 200 | mailing list. Releases of several cookbooks may be batched together 201 | and announced on the [Opscode Blog](http://www.opscode.com/blog). 202 | 203 | ## Working with the community 204 | 205 | These resources will help you learn more about Chef and connect to 206 | other members of the Chef community: 207 | 208 | * [chef](http://lists.opscode.com/sympa/info/chef) and 209 | [chef-dev](http://lists.opscode.com/sympa/info/chef-dev) mailing 210 | lists 211 | * #chef and #chef-hacking IRC channels on irc.freenode.net 212 | * [Community Cookbook site](http://community.opscode.com) 213 | * [Chef wiki](http://wiki.opscode.com/display/chef) 214 | * Opscode Chef [product page](http://www.opscode.com/chef) 215 | 216 | 217 | ## Cookbook Contribution Do's and Don't's 218 | 219 | Please do include tests for your contribution. If you need help, ask 220 | on the 221 | [chef-dev mailing list](http://lists.opscode.com/sympa/info/chef-dev) 222 | or the 223 | [#chef-hacking IRC channel](http://community.opscode.com/chat/chef-hacking). 224 | Not all platforms that a cookbook supports may be supported by Test 225 | Kitchen. Please provide evidence of testing your contribution if it 226 | isn't trivial so we don't have to duplicate effort in testing. Chef 227 | 10.14+ "doc" formatted output is sufficient. 228 | 229 | Please do indicate new platform (families) or platform versions in the 230 | commit message, and update the relevant ticket. 231 | 232 | If a contribution adds new platforms or platform versions, indicate 233 | such in the body of the commit message(s), and update the relevant 234 | COOK ticket. When writing commit messages, it is helpful for others if 235 | you indicate the COOK ticket. For example: 236 | 237 | git commit -m '[COOK-1041] - Updated pool resource to correctly 238 | delete.' 239 | 240 | Please do use [foodcritic](http://acrmp.github.com/foodcritic) to 241 | lint-check the cookbook. Except FC007, it should pass all correctness 242 | rules. FC007 is okay as long as the dependent cookbooks are *required* 243 | for the default behavior of the cookbook, such as to support an 244 | uncommon platform, secondary recipe, etc. 245 | 246 | Please do ensure that your changes do not break or modify behavior for 247 | other platforms supported by the cookbook. For example if your changes 248 | are for Debian, make sure that they do not break on CentOS. 249 | 250 | Please do not modify the version number in the metadata.rb, Opscode 251 | will select the appropriate version based on the release cycle 252 | information above. 253 | 254 | Please do not update the CHANGELOG.md for a new version. Not all 255 | changes to a cookbook may be merged and released in the same versions. 256 | Opscode will update the CHANGELOG.md when releasing a new version of 257 | the cookbook. 258 | --------------------------------------------------------------------------------