Debian Codegraph @

manifests/listen.pp

raw
1 # @summary
2 # This type will setup a listening service configuration block inside
3 # the haproxy.cfg file on an haproxy load balancer.
4 #
5 # @note
6 # Each listening service
7 # configuration needs one or more load balancer member server (that can be
8 # declared with the haproxy::balancermember defined resource type). Using
9 # storeconfigs, you can export the haproxy::balancermember resources on all
10 # load balancer member servers, and then collect them on a single haproxy
11 # load balancer server.
12 #
13 # @note
14 # Currently requires the puppetlabs/concat module on the Puppet Forge and
15 # uses storeconfigs on the Puppet Server to export/collect resources
16 # from all balancer members.
17 #
18 #
19 # @param section_name
20 # This name goes right after the 'listen' statement in haproxy.cfg
21 # Default: $name (the namevar of the resource).
22 #
23 # @param ports
24 # Ports on which the proxy will listen for connections on the ip address
25 # specified in the ipaddress parameter. Accepts either a single
26 # comma-separated string or an array of strings which may be ports or
27 # hyphenated port ranges.
28 #
29 # @param ipaddress
30 # The ip address the proxy binds to.
31 # Empty addresses, '*', and '0.0.0.0' mean that the proxy listens
32 # to all valid addresses on the system.
33 #
34 # @param bind
35 # Set of ip addresses, port and bind options
36 # $bind = { '10.0.0.1:80' => ['ssl', 'crt', '/path/to/my/crt.pem'] }
37 #
38 # @param mode
39 # The mode of operation for the listening service. Valid values are undef,
40 # 'tcp', 'http', and 'health'.
41 #
42 # @param description
43 # Allows to add a sentence to describe the related object in the HAProxy HTML
44 # stats page. The description will be printed on the right of the object name
45 # it describes. Usefull in huge environments
46 #
47 # @param options
48 # A hash of options that are inserted into the listening service
49 # configuration block.
50 #
51 # @param bind_options
52 # (Deprecated) An array of options to be specified after the bind declaration
53 # in the listening serivce's configuration block.
54 #
55 # @param collect_exported
56 # Boolean, default 'true'. True means 'collect exported @@balancermember resources'
57 # (for the case when every balancermember node exports itself), false means
58 # 'rely on the existing declared balancermember resources' (for the case when you
59 # know the full set of balancermembers in advance and use haproxy::balancermember
60 # with array arguments, which allows you to deploy everything in 1 run)
61 #
62 # @param sort_options_alphabetic
63 # Sort options either alphabetic or custom like haproxy internal sorts them.
64 # Defaults to true.
65 #
66 # @param defaults
67 # Name of the defaults section this backend will use.
68 # Defaults to undef which means the global defaults section will be used.
69 #
70 # @param config_file
71 # Optional. Path of the config file where this entry will be added.
72 # Assumes that the parent directory exists.
73 # Default: $haproxy::params::config_file
74 #
75 # @param instance
76 # Optional. Defaults to 'haproxy'
77 #
78 # @example
79 # haproxy::listen { 'puppet00':
80 # ipaddress => $::ipaddress,
81 # ports => [18140],
82 # mode => 'tcp',
83 # options => {
84 # 'option' => [
85 # 'tcplog',
86 # 'ssl-hello-chk'
87 # ],
88 # 'balance' => 'roundrobin'
89 # },
90 # }
91 #
92 # === Authors
93 #
94 # Gary Larizza <[email protected]>
95 #
96 define haproxy::listen (
97 Optional[Haproxy::Ports] $ports = undef,
98 Optional[Variant[String, Array]] $ipaddress = undef,
99 Optional[Hash] $bind = undef,
100 Optional[Enum['tcp', 'http', 'health']] $mode = undef,
101 Boolean $collect_exported = true,
102 Variant[Hash, Array[Hash]] $options = {
103 'option' => [
104 'tcplog',
105 ],
106 'balance' => 'roundrobin',
107 },
108 String $instance = 'haproxy',
109 String[1] $section_name = $name,
110 Boolean $sort_options_alphabetic = true,
111 Optional[String] $description = undef,
112 Optional[String] $defaults = undef,
113 Optional[Stdlib::Absolutepath] $config_file = undef,
114 # Deprecated
115 Optional[Array] $bind_options = undef,
116 ) {
117 if $ports and $bind {
118 fail('The use of $ports and $bind is mutually exclusive, please choose either one')
119 }
120 if $ipaddress and $bind {
121 fail('The use of $ipaddress and $bind is mutually exclusive, please choose either one')
122 }
123 if $ipaddress == undef and $bind == undef {
124 fail('Either $ipaddress or $bind is needed, please choose one')
125 }
126 if $bind_options {
127 warning('The $bind_options parameter is deprecated; please use $bind instead')
128 }
129
130 if defined(Haproxy::Backend[$section_name]) {
131 fail("An haproxy::backend resource was discovered with the same name (${section_name}) which is not supported")
132 }
133
134 include haproxy::params
135
136 if $instance == 'haproxy' {
137 $instance_name = 'haproxy'
138 $_config_file = pick($config_file, $haproxy::config_file)
139 } else {
140 $instance_name = "haproxy-${instance}"
141 $_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
142 }
143
144 include haproxy::globals
145 $_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic)
146
147 if $defaults == undef {
148 $order = "20-${section_name}-00"
149 } else {
150 $order = "25-${defaults}-${section_name}-00"
151 }
152
153 $parameters = {
154 'section_name' => $section_name,
155 'bind' => $bind,
156 'ipaddress' => $ipaddress,
157 'ports' => $ports,
158 'bind_options' => $bind_options,
159 'mode' => $mode,
160 'description' => $description,
161 'options' => $options,
162 '_sort_options_alphabetic' => $_sort_options_alphabetic,
163 }
164
165 # Template uses: $section_name, $ipaddress, $ports, $options
166 concat::fragment { "${instance_name}-${section_name}_listen_block":
167 order => $order,
168 target => $_config_file,
169 content => epp('haproxy/haproxy_listen_block.epp', $parameters),
170 }
171
172 if $collect_exported {
173 haproxy::balancermember::collect_exported { $section_name: }
174 }
175 # else: the resources have been created and they introduced their
176 # concat fragments. We don't have to do anything about them.
177 }