From c4799e59b9d9891e6c296c554a11814f14a5abfc Mon Sep 17 00:00:00 2001 From: Gary Larizza Date: Mon, 7 May 2012 14:26:21 -0700 Subject: Initial Commit --- manifests/balancermember.pp | 86 ++++++++++++++++++++++++++++++++++ manifests/config.pp | 75 ++++++++++++++++++++++++++++++ manifests/data.pp | 29 ++++++++++++ manifests/init.pp | 110 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 300 insertions(+) create mode 100644 manifests/balancermember.pp create mode 100644 manifests/config.pp create mode 100644 manifests/data.pp create mode 100644 manifests/init.pp (limited to 'manifests') diff --git a/manifests/balancermember.pp b/manifests/balancermember.pp new file mode 100644 index 0000000..50e90c0 --- /dev/null +++ b/manifests/balancermember.pp @@ -0,0 +1,86 @@ +# == Define Resource Type: haproxy::balancermember +# +# This type will setup a balancer member inside a listening service +# configuration block in /etc/haproxy/haproxy.cfg on the load balancer. +# currently it only has the ability to specify the instance name, +# ip address, port, and whether or not it is a backup. More features +# can be added as needed. The best way to implement this is to export +# this resource for all haproxy balancer member servers, and then collect +# them on the main haproxy load balancer. +# +# === Requirement/Dependencies: +# +# Currently requires the ripienaar/concat module on the Puppet Forge and +# uses storeconfigs on the Puppet Master to export/collect resources +# from all balancer members. +# +# === Parameters +# +# [*name*] +# The title of the resource is arbitrary and only utilized in the concat +# fragment name. +# +# [*listening_service*] +# The haproxy service's instance name (or, the title of the +# haproxy::config resource). This must match up with a declared +# haproxy::config resource. +# +# [*balancer_port*] +# A unique port for which the balancer member will accept connections +# from the load balancer. Note that cookie values aren't yet supported, +# but shouldn't be difficult to add to the configuration. +# +# [*order*] +# The order, or numerical weight, of the fragment created by this defined +# resource type. This is necessary to ensure the fragment is associated +# with the correct listening service instance. +# +# [*server_name*] +# The name of the balancer member server as known to haproxy in the +# listening service's configuration block. This defaults to the +# hostname +# +# [*balancer_ip*] +# The ip address used to contact the balancer member server +# +# [*balancermember_options*] +# An array of options to be specified after the server declaration +# in the listening service's configuration block. +# +# +# === Examples +# +# Exporting the resource for a balancer member: +# +# @@haproxy::balancermember { 'haproxy': +# listening_service => 'puppet00', +# balancer_port => '8140', +# order => '21', +# server_name => $::hostname, +# balancer_ip => $::ipaddress, +# balancermember_options => 'check', +# } +# +# +# Collecting the resource on a load balancer +# +# Haproxy::Balancermember <<| listening_service == 'puppet00' |>> +# +# === Authors +# +# Gary Larizza +# +define haproxy::balancermember ( + $listening_service, + $balancer_port, + $order = '20', + $server_name = $::hostname, + $balancer_ip = $::ipaddress, + $balancermember_options = '' +) { + concat::fragment { "${listening_service}_balancermember_${name}": + order => $order, + target => '/etc/haproxy/haproxy.cfg', + content => template('haproxy/haproxy_balancermember.erb'), + } +} diff --git a/manifests/config.pp b/manifests/config.pp new file mode 100644 index 0000000..55b40d7 --- /dev/null +++ b/manifests/config.pp @@ -0,0 +1,75 @@ +# == Define Resource Type: haproxy::config +# +# This type will setup a listening service configuration block inside +# the haproxy.cfg file on an haproxy load balancer. Each listening service +# configuration needs one or more load balancer member server (that can be +# declared with the haproxy::balancermember defined resource type). Using +# storeconfigs, you can export the haproxy::balancermember resources on all +# load balancer member servers, and then collect them on a single haproxy +# load balancer server. +# +# === Requirement/Dependencies: +# +# Currently requires the ripienaar/concat module on the Puppet Forge and +# uses storeconfigs on the Puppet Master to export/collect resources +# from all balancer members. +# +# === Parameters +# +# [*name*] +# The namevar of the defined resource type is the listening service's name. +# This name goes right after the 'listen' statement in haproxy.cfg +# +# [*virtual_ip_port*] +# A unique port, or a port range for which the proxy will accept connections +# for the ip address specified in the virtual_ip parameter. +# +# [*order*] +# The order, or numerical weight, of the fragment created by this defined +# resource type. This is necessary to ensure the fragment is associated +# with the correct listening service instance. +# +# [*virtual_ip*] +# The ip address the proxy binds to. Empty addresses, '*', and '0.0.0.0' +# mean that the proxy listens to all valid addresses on the system. +# +# [*mode*] +# The mode of operation for the listening service. Valid values are 'tcp', +# HTTP', and 'health'. +# +# [*haproxy_config_options*] +# A hash of options that are inserted into the listening service +# configuration block. +# +# +# === Examples +# +# Exporting the resource for a balancer member: +# +# haproxy::config { 'puppet00': +# order => '20', +# virtual_ip => $::ipaddress, +# virtual_ip_port => '18140', +# mode => 'tcp', +# haproxy_config_options => {'option' => 'tcplog', 'option' => 'ssl-hello-chk', 'balance' => 'roundrobin'}, +# } +# +# === Authors +# +# Gary Larizza +# +define haproxy::config ( + $virtual_ip_port, + $order = '20', + $virtual_ip = $::ipaddress, + $mode = 'tcp', + $haproxy_config_options = {'option' => 'tcplog', 'option' => 'ssl-hello-chk', 'balance' => 'roundrobin'}, +) { + concat::fragment { "${name}_config_block": + order => $order, + target => '/etc/haproxy/haproxy.cfg', + content => template('haproxy/haproxy_config_block.erb'), + } + + Haproxy::Balancermember <<| listening_service == $name |>> +} diff --git a/manifests/data.pp b/manifests/data.pp new file mode 100644 index 0000000..aa49285 --- /dev/null +++ b/manifests/data.pp @@ -0,0 +1,29 @@ +# == Class: haproxy::data +# +# This is a container class holding default parameters for for haproxy class. +# currently, only the Redhat family is supported, but this can be easily +# extended by changing package names and configuration file paths. +# +class haproxy::data { + case $osfamily { + Redhat: { + $haproxy_global_options = { 'log' => "${::ipaddress} local0", + 'chroot' => '/var/lib/haproxy', + 'pidfile' => '/var/run/haproxy.pid', + 'maxconn' => '4000', + 'user' => 'haproxy', + 'group' => 'haproxy', + 'daemon' => '', + 'stats' => 'socket /var/lib/haproxy/stats' + } + $haproxy_defaults_options = { 'log' => 'global', + 'stats' => 'enable', + 'option' => 'redispatch', + 'retries' => '3', + 'timeout' => ['http-request 10s', 'queue 1m', 'connect 10s', 'client 1m', 'server 1m', 'check 10s'], + 'maxconn' => '8000' + } + } + default: { fail("The $::operatingsystem operating system is not supported with the haproxy module") } + } +} diff --git a/manifests/init.pp b/manifests/init.pp new file mode 100644 index 0000000..6dde629 --- /dev/null +++ b/manifests/init.pp @@ -0,0 +1,110 @@ +# == Class: puppet-haproxy +# +# A Puppet module, using storeconfigs, to model an haproxy configuration. +# Currently VERY limited - assumes Redhat/CentOS setup. Pull requests accepted! +# +# === Requirement/Dependencies: +# +# Currently requires the ripienaar/concat module on the Puppet Forge and +# uses storeconfigs on the Puppet Master to export/collect resources +# from all balancer members. +# +# === Parameters +# +# [*enable*] +# Chooses whether haproxy should be installed or ensured absent. +# Currently ONLY accepts valid boolean true/false values. +# +# [*haproxy_global_options*] +# A hash of all the haproxy global options. If you want to specify more +# than one option (i.e. multiple timeout or stats options), pass those +# options as an array and you will get a line for each of them in the +# resultant haproxy.cfg file. +# +# [*haproxy_defaults_options*] +# A hash of all the haproxy defaults options. If you want to specify more +# than one option (i.e. multiple timeout or stats options), pass those +# options as an array and you will get a line for each of them in the +# resultant haproxy.cfg file. +# +# +# === Examples +# +# class { 'haproxy': +# enable => true, +# haproxy_global_options => { 'log' => "${::ipaddress} local0", +# 'chroot' => '/var/lib/haproxy', +# 'pidfile' => '/var/run/haproxy.pid', +# 'maxconn' => '4000', +# 'user' => 'haproxy', +# 'group' => 'haproxy', +# 'daemon' => '', +# 'stats' => 'socket /var/lib/haproxy/stats' +# }, +# haproxy_defaults_options => { 'log' => 'global', +# 'stats' => 'enable', +# 'option' => 'redispatch', +# 'retries' => '3', +# 'timeout' => ['http-request 10s', 'queue 1m', 'connect 10s', 'client 1m', 'server 1m', 'check 10s'], +# 'maxconn' => '8000' +# }, +# +# } +# +# === Authors +# +# Gary Larizza +# +class haproxy ( + $enable = true, + $haproxy_global_options = $haproxy::data::haproxy_global_options, + $haproxy_defaults_options = $haproxy::data::haproxy_defaults_options +) inherits haproxy::data { + include concat::setup + + package { 'haproxy': + ensure => $enable ? { + true => present, + false => absent, + }, + name => 'haproxy', + } + + if $enable { + concat { '/etc/haproxy/haproxy.cfg': + owner => '0', + group => '0', + mode => '0644', + require => Package['haproxy'], + notify => Service['haproxy'], + } + + # Simple Header + concat::fragment { '00-header': + target => '/etc/haproxy/haproxy.cfg', + order => '01', + content => "# This file managed by Puppet\n", + } + + # Most of the variables are used inside the haproxy-base.cfg.erb template + concat::fragment { 'haproxy-base': + target => '/etc/haproxy/haproxy.cfg', + order => '10', + content => template('haproxy/haproxy-base.cfg.erb'), + } + } + + service { 'haproxy': + ensure => $enable ? { + true => running, + false => stopped, + }, + enable => $enable ? { + true => true, + false => false, + }, + name => 'haproxy', + hasstatus => true, + require => Concat['/etc/haproxy/haproxy.cfg'], + } +} -- cgit v1.2.3