Puppet Class: haproxy

Inherits:
haproxy::params
Defined in:
manifests/init.pp

Overview

Class: 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.
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.
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.
package_name

The package name to install containing haproxy. Defaults to 'haproxy'

restart_command

Command to use when restarting the on config changes.

Passed directly as the <code>'restart'</code> parameter to the service resource.
Defaults to undef i.e. whatever the service default is.

Examples

class { 'haproxy':
  enable           => true,
  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'
  },
  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'
  },
}

Parameters:

  • manage_service (Any) (defaults to: true)
  • enable (Any) (defaults to: true)
  • global_options (Any) (defaults to: $haproxy::params::global_options)
  • defaults_options (Any) (defaults to: $haproxy::params::defaults_options)
  • package_name (Any) (defaults to: 'haproxy')
  • restart_command (Any) (defaults to: undef)
  • custom_source_location (Any) (defaults to: '')
  • custom_source_release (Any) (defaults to: '')
  • custom_source_repo (Any) (defaults to: '')
  • custom_source_is_backport (Any) (defaults to: false)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'manifests/init.pp', line 69

class haproxy (
  $manage_service   = true,
  $enable           = true,
  $global_options   = $haproxy::params::global_options,
  $defaults_options = $haproxy::params::defaults_options,
  $package_name     = 'haproxy',
  $restart_command  = undef,
  $custom_source_location = '',
  $custom_source_release  = '',
  $custom_source_repo     = '',
  $custom_source_is_backport = false
) inherits haproxy::params {
  include concat::setup

  if ($customer_source_location == '') {
    package { $package_name:
      ensure  => $enable ? {
        true  => present,
        false => absent,
      },
      alias   => 'haproxy',
    }
  } else {
    include apt

    apt::source { 'haproxy':
      location => $custom_source_location,
      release => $custom_source_release,
      repos => $custom_source_repo,
      include_src => false
    }

    package { $package_name:
      ensure  => $enable ? {
        true  => present,
        false => absent,
      },
      install_options => $custom_source_is_backport ? {
        true => ["-t"],
        false => []
      },
      alias   => 'haproxy'
    }
  }

  if $enable {
    concat { '/etc/haproxy/haproxy.cfg':
      owner   => '0',
      group   => '0',
      mode    => '0644',
      require => Package['haproxy'],
      notify  => $manage_service ? {
        true  => Service['haproxy'],
        false => undef,
      },
    }

    # Simple Header
    concat::fragment { '00-header':
      target  => '/etc/haproxy/haproxy.cfg',
      order   => '01',
      content => "# This file managed by Puppet\n",
    }

    # Template uses $global_options, $defaults_options
    concat::fragment { 'haproxy-base':
      target  => '/etc/haproxy/haproxy.cfg',
      order   => '10',
      content => template('haproxy/haproxy-base.cfg.erb'),
    }

    if ($::osfamily == 'Debian') {
      file { '/etc/default/haproxy':
        content => 'ENABLED=1',
        require => Package['haproxy'],
        before  => $manage_service ? {
          true  => Service['haproxy'],
          false => undef,
        },
      }
    }

    if $global_options['chroot'] {
      file { $global_options['chroot']:
        ensure => directory,
      }
    }

  }

  if $manage_service {
    if $global_options['chroot'] {
      $deps = [
        Concat['/etc/haproxy/haproxy.cfg'],
        File[$global_options['chroot']],
      ]
    } else {
      $deps = [
        Concat['/etc/haproxy/haproxy.cfg'],
      ]
    }

    service { 'haproxy':
      ensure     => $enable ? {
        true  => running,
        false => stopped,
      },
      enable     => $enable ? {
        true  => true,
        false => false,
      },
      name       => 'haproxy',
      hasrestart => true,
      hasstatus  => true,
      require    => $deps,
      restart    => $restart_command,
    }
  }
}