From 8606dac52c80415f67f65b1d28d3b39c7dc57159 Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Wed, 30 Nov 2016 13:40:43 +0100 Subject: [PATCH] mod_proxy_balancer manager --- README.md | 4 ++ manifests/mod/proxy_balancer.pp | 19 ++++- spec/classes/mod/proxy_balancer_spec.rb | 95 +++++++++++++++++++++++++ templates/mod/proxy_balancer.conf.erb | 10 +++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 spec/classes/mod/proxy_balancer_spec.rb create mode 100644 templates/mod/proxy_balancer.conf.erb diff --git a/README.md b/README.md index 9cc2625b23..529e98edbd 100644 --- a/README.md +++ b/README.md @@ -1808,9 +1808,13 @@ Installs and manages [`mod_proxy_balancer`][], which provides load balancing. **Parameters within `apache::mod::proxy_balancer`**: +- `manager`: Determines whether to enable balancer manager support. Default: `false`. +- `manager_path`: The server location of the balancer manager. Default: '/balancer-manager'. +- `allow_from`: An [array][] of IPv4 or IPv6 addresses that can access `/balancer-manager`. Default: ['127.0.0.1','::1']. - `apache_version`: Apache's version number as a string, such as '2.2' or '2.4'. Default: the value of [`$::apache::apache_version`][`apache_version`]. - On Apache >= 2.4, `mod_slotmem_shm` is loaded. + ##### Class: `apache::mod::php` Installs and configures [`mod_php`][]. diff --git a/manifests/mod/proxy_balancer.pp b/manifests/mod/proxy_balancer.pp index a225ce3396..fdb4b831ad 100644 --- a/manifests/mod/proxy_balancer.pp +++ b/manifests/mod/proxy_balancer.pp @@ -1,6 +1,12 @@ class apache::mod::proxy_balancer( + $manager = false, + $manager_path = '/balancer-manager', + $allow_from = ['127.0.0.1','::1'], $apache_version = $::apache::apache_version, ) { + validate_bool($manager) + validate_string($manager_path) + validate_array($allow_from) include ::apache::mod::proxy include ::apache::mod::proxy_http @@ -11,5 +17,16 @@ Class['::apache::mod::proxy'] -> Class['::apache::mod::proxy_balancer'] Class['::apache::mod::proxy_http'] -> Class['::apache::mod::proxy_balancer'] ::apache::mod { 'proxy_balancer': } - + if $manager { + include ::apache::mod::status + file { 'proxy_balancer.conf': + ensure => file, + path => "${::apache::mod_dir}/proxy_balancer.conf", + mode => $::apache::file_mode, + content => template('apache/mod/proxy_balancer.conf.erb'), + require => Exec["mkdir ${::apache::mod_dir}"], + before => File[$::apache::mod_dir], + notify => Class['apache::service'], + } + } } diff --git a/spec/classes/mod/proxy_balancer_spec.rb b/spec/classes/mod/proxy_balancer_spec.rb new file mode 100644 index 0000000000..d646ba7500 --- /dev/null +++ b/spec/classes/mod/proxy_balancer_spec.rb @@ -0,0 +1,95 @@ +require 'spec_helper' + +# Helper function for testing the contents of `proxy_balancer.conf` +def balancer_manager_conf_spec(allow_from, manager_path) + it do + is_expected.to contain_file("proxy_balancer.conf").with_content( + "\n"\ + " SetHandler balancer-manager\n"\ + " Require ip #{Array(allow_from).join(' ')}\n"\ + "\n" + ) + end +end + +describe 'apache::mod::proxy_balancer', :type => :class do + let :pre_condition do + [ + 'include apache::mod::proxy', + ] + end + it_behaves_like "a mod class, without including apache" + + context "default configuration with default parameters" do + context "on a Debian OS" do + let :facts do + { + :osfamily => 'Debian', + :operatingsystemrelease => '8', + :concat_basedir => '/dne', + :lsbdistcodename => 'jessie', + :operatingsystem => 'Debian', + :id => 'root', + :kernel => 'Linux', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + :is_pe => false, + } + end + + it { is_expected.to contain_apache__mod("proxy_balancer") } + + it { is_expected.to_not contain_file("proxy_balancer.conf") } + it { is_expected.to_not contain_file("proxy_balancer.conf symlink") } + + end + + context "on a RedHat OS" do + let :facts do + { + :osfamily => 'RedHat', + :operatingsystemrelease => '6', + :concat_basedir => '/dne', + :operatingsystem => 'RedHat', + :id => 'root', + :kernel => 'Linux', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + :is_pe => false, + } + end + + it { is_expected.to contain_apache__mod("proxy_balancer") } + + it { is_expected.to_not contain_file("proxy_balancer.conf") } + it { is_expected.to_not contain_file("proxy_balancer.conf symlink") } + + end + end + + context "default configuration with custom parameters $manager => true, $allow_from => ['10.10.10.10','11.11.11.11'], $status_path => '/custom-manager'" do + context "on a Debian OS" do + let :facts do + { + :osfamily => 'Debian', + :operatingsystemrelease => '8', + :concat_basedir => '/dne', + :lsbdistcodename => 'jessie', + :operatingsystem => 'Debian', + :id => 'root', + :kernel => 'Linux', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + :is_pe => false, + } + end + let :params do + { + :manager => true, + :allow_from => ['10.10.10.10','11.11.11.11'], + :manager_path => '/custom-manager', + } + end + + balancer_manager_conf_spec(["10.10.10.10", "11.11.11.11"], "/custom-manager") + + end + end +end diff --git a/templates/mod/proxy_balancer.conf.erb b/templates/mod/proxy_balancer.conf.erb new file mode 100644 index 0000000000..c1f37be8e4 --- /dev/null +++ b/templates/mod/proxy_balancer.conf.erb @@ -0,0 +1,10 @@ +> + SetHandler balancer-manager + <%- if scope.function_versioncmp([@apache_version, '2.4']) >= 0 -%> + Require ip <%= Array(@allow_from).join(" ") %> + <%- else -%> + Order deny,allow + Deny from all + Allow from <%= Array(@allow_from).join(" ") %> + <%- end -%> +