File: balancermember.pp

package info (click to toggle)
puppet-module-puppetlabs-haproxy 8.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: ruby: 3,979; sh: 14; makefile: 4
file content (191 lines) | stat: -rw-r--r-- 7,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
188
189
190
191
# @summary
#  This type will setup a balancer member inside a listening service
#  configuration block in /etc/haproxy/haproxy.cfg on the load balancer.
#  
# @note
#  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.
#
# @note
#  Currently requires the puppetlabs/concat module on the Puppet Forge and
#  uses storeconfigs on the Puppet Server to export/collect resources
#  from all balancer members.
#
#
# @param listening_service
#   The haproxy service's instance name (or, the title of the
#    haproxy::listen resource). This must match up with a declared
#    haproxy::listen resource.
#
# @param ports
#   An array or commas-separated list of ports 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. If you use an array in server_names and ipaddresses, the
#    same port is used for all balancermembers.
#
# @param port
#    A port for server-template. It is an optional specification.
#
# @param server_names
#   The name of the balancer member server as known to haproxy in the
#    listening service's configuration block. This defaults to the
#    hostname. Can be an array of the same length as ipaddresses,
#    in which case a balancermember is created for each pair of
#    server_names and ipaddresses (in lockstep).
#
# @param ipaddresses
#   The ip address used to contact the balancer member server.
#    Can be an array, see documentation to server_names.
#
# @param prefix
#   A prefix for the server-template for the server names to be built.
#
# @param amount
#   If "amount" is provided, the server-template initializes <num> servers
#    with 1 up to <num> as server name suffixes. A range of numbers
#    <num_low>-<num_high> may also be used to use <num_low> up to
#    <num_high> as server name suffixes.
#
# @param fqdn
#   A FQDN for all the servers the server-template initializes.
#
# @param options
#   An array of options to be specified after the server declaration
#    in the listening service's configuration block.
#
# @param define_cookies
#   If true, then add "cookie SERVERID" stickiness options.
#    Default false.
#
# @param defaults
#   Name of the defaults section the backend or listener use.
#   Defaults to undef.
#
# @param config_file
#   Optional. Path of the config file where this entry will be added.
#   Assumes that the parent directory exists.
#   Default: $haproxy::params::config_file
#
# @param verifyhost
#   Optional. Will add the verifyhost option to the server line, using the
#   specific host from server_names as an argument.
#   Default: false
#
# @param weight
#   Optional. Will add the weight option to the server line
#   Default: undef
#
# @param instance
#   Optional. Defaults to 'haproxy'
#
# @param type
#   Optional. Defaults to 'server'
#
# @example
#  Exporting the resource for a balancer member:
#
#  @@haproxy::balancermember { 'haproxy':
#    listening_service => 'puppet00',
#    ports             => [8140],
#    server_names      => $::hostname,
#    ipaddresses       => $::ipaddress,
#    options           => 'check',
#  }
#
#
#  Collecting the resource on a load balancer
#
#  Haproxy::Balancermember <<| listening_service == 'puppet00' |>>
#
# @example
#  Creating the resource for multiple balancer members at once
#  (for single-pass installation of haproxy without requiring a first
#  pass to export the resources if you know the members in advance):
#
#  haproxy::balancermember { 'haproxy':
#    listening_service => 'puppet00',
#    ports             => 8140,
#    server_names      => ['server01', 'server02'],
#    ipaddresses       => ['192.168.56.200', '192.168.56.201'],
#    options           => 'check',
#  }
#
# @example
#  Implemented in HAPROXY 1.8:
#  Set a template to initialize servers with shared parameters.
#  The names of these servers are built from <prefix> and <amount> parameters.
#
#    Initializes 5 servers with srv1, srv2, srv3, srv4 and srv5 as names,
#    myserver.example.com as FQDN, 8140 as port, and health-check enabled.
#
#  haproxy::balancermember { 'haproxy':
#    listening_service => 'puppet00',
#    type              => 'server-template'
#    port              => '8140',
#    prefix            => 'srv',
#    amount            => '1-5',
#    fqdn              => 'myserver.example.com',
#    options           => 'check',
#  }
#
#  (this resource can be declared anywhere)
#
define haproxy::balancermember (
  String                                              $listening_service,
  Enum['server', 'default-server', 'server-template'] $type               = 'server',
  Optional[Haproxy::Ports]                            $ports              = undef,
  Optional[Variant[String, Stdlib::Port]]             $port               = undef,
  Variant[String[1], Array]                           $server_names       = $facts['networking']['hostname'],
  Variant[String, Array]                              $ipaddresses        = $facts['networking']['ip'],
  String                                              $prefix             = 'server',
  String                                              $amount             = '1',
  Optional[String]                                    $fqdn               = undef,
  Optional[Variant[String, Array]]                    $options            = undef,
  Boolean                                             $define_cookies     = false,
  String                                              $instance           = 'haproxy',
  Optional[String]                                    $defaults           = undef,
  Optional[Stdlib::Absolutepath]                      $config_file        = undef,
  Boolean                                             $verifyhost         = false,
  Optional[Variant[String, Integer]]                  $weight             = undef,
) {
  include haproxy::params

  if $instance == 'haproxy' {
    $instance_name = 'haproxy'
    $_config_file = pick($config_file, $haproxy::config_file)
  } else {
    $instance_name = "haproxy-${instance}"
    $_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
  }

  if $defaults == undef {
    $order = "20-${listening_service}-01-${name}"
  } else {
    $order = "25-${defaults}-${listening_service}-02-${name}"
  }

  $parameters = {
    'type'           => $type,
    'ipaddresses'    => $ipaddresses,
    'server_names'   => $server_names,
    'ports'          => $ports,
    'define_cookies' => $define_cookies,
    'options'        => $options,
    'verifyhost'     => $verifyhost,
    'weight'         => $weight,
    'prefix'         => $prefix,
    'amount'         => $amount,
    'fqdn'           => $fqdn,
    'port'           => $port,
  }
  # Template uses $ipaddresses, $server_name, $ports, $option
  concat::fragment { "${instance_name}-${listening_service}_balancermember_${name}":
    order   => $order,
    target  => $_config_file,
    content => epp('haproxy/haproxy_balancermember.epp', $parameters),
  }
}