Debian Codegraph @

manifests/backend.pp

raw
1 # @summary
2 # This type will setup a backend service configuration block inside the
3 # haproxy.cfg file on an haproxy load balancer.
4 # @note
5 # Each backend service needs one
6 # or more backend member servers (that can be declared with the
7 # haproxy::balancermember defined resource type). Using storeconfigs, you can
8 # export the haproxy::balancermember resources on all load balancer member
9 # servers and then collect them on a single haproxy load balancer server.
10 #
11 # @note
12 # Currently requires the puppetlabs/concat module on the Puppet Forge and
13 # uses storeconfigs on the Puppet Server to export/collect resources
14 # from all backend members.
15 #
16 # @param section_name
17 # This name goes right after the 'backend' statement in haproxy.cfg
18 # Default: $name (the namevar of the resource).
19 #
20 # @param mode
21 # The mode of operation for the backend service. Valid values are undef,
22 # 'tcp', 'http', and 'health'.
23 #
24 # @param description
25 # Allows to add a sentence to describe the related object in the HAProxy HTML
26 # stats page. The description will be printed on the right of the object name
27 # it describes. Usefull in huge environments
28 #
29 # @param options
30 # A hash of options that are inserted into the backend configuration block.
31 #
32 # @param collect_exported
33 # Boolean, default 'true'. True means 'collect exported @@balancermember
34 # resources' (for the case when every balancermember node exports itself),
35 # false means 'rely on the existing declared balancermember resources' (for
36 # the case when you know the full set of balancermember in advance and use
37 # haproxy::balancermember with array arguments, which allows you to deploy
38 # everything in 1 run)
39 #
40 # @param config_file
41 # Optional. Path of the config file where this entry will be added.
42 # Assumes that the parent directory exists.
43 # Default: $haproxy::params::config_file
44 #
45 # @param sort_options_alphabetic
46 # Sort options either alphabetic or custom like haproxy internal sorts them.
47 # Defaults to true.
48 #
49 # @param defaults
50 # Name of the defaults section this backend will use.
51 # Defaults to undef which means the global defaults section will be used.
52 #
53 # @param instance
54 # Optional. Defaults to 'haproxy'
55 #
56 # @example
57 # haproxy::backend { 'puppet00':
58 # options => {
59 # 'option' => [
60 # 'tcplog',
61 # 'ssl-hello-chk'
62 # ],
63 # 'balance' => 'roundrobin'
64 # },
65 # }
66 #
67 # === Authors
68 #
69 # Gary Larizza <[email protected]>
70 # Jeremy Kitchen <[email protected]>
71 #
72 define haproxy::backend (
73 Optional[Enum['tcp', 'http', 'health']] $mode = undef,
74 Boolean $collect_exported = true,
75 Variant[Hash, Array[Hash]] $options = {
76 'balance' => 'roundrobin',
77 },
78 String $instance = 'haproxy',
79 String[1] $section_name = $name,
80 Boolean $sort_options_alphabetic = true,
81 Optional[String] $description = undef,
82 Optional[String] $defaults = undef,
83 Optional[Stdlib::Absolutepath] $config_file = undef,
84 ) {
85 if defined(Haproxy::Listen[$section_name]) {
86 fail("An haproxy::listen resource was discovered with the same name (${section_name}) which is not supported")
87 }
88
89 include haproxy::params
90
91 if $instance == 'haproxy' {
92 $instance_name = 'haproxy'
93 $_config_file = pick($config_file, $haproxy::config_file)
94 } else {
95 $instance_name = "haproxy-${instance}"
96 $_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
97 }
98
99 include haproxy::globals
100
101 # See https://github.com/puppetlabs/puppetlabs-haproxy/pull/442 - when using the option 'httpchk', it must be positioned
102 # before the 'http-check' directive in haproxy.cfg otherwise it will be ignored
103 #
104 # logic:
105 # 1) if alphabetic sorting is explicitly disabled, accept that
106 # 2) if the options hash contains httpchk, disable alphabetic sorting *and* log a warning
107 # 3) use whats provides to this module and if that's undef, use the module default
108 $picked_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic)
109 if $picked_sort_options_alphabetic == false {
110 $_sort_options_alphabetic = $picked_sort_options_alphabetic
111 } else {
112 if $options =~ Hash and 'option' in $options {
113 if ('httpchk' in $options['option']) {
114 warning('Overriding the value of $sort_options_alphabetic to "false" due to "httpchk" option defined')
115 $_sort_options_alphabetic = false
116 } else {
117 $_sort_options_alphabetic = $picked_sort_options_alphabetic
118 }
119 } else {
120 $_sort_options_alphabetic = $picked_sort_options_alphabetic
121 }
122 }
123
124 if $defaults == undef {
125 $order = "20-${section_name}-00"
126 } else {
127 $order = "25-${defaults}-${section_name}-01"
128 }
129
130 $parameters = {
131 'section_name' => $section_name,
132 'mode' => $mode,
133 'description' => $description,
134 '_sort_options_alphabetic' => $_sort_options_alphabetic,
135 'options' => $options,
136 }
137
138 # Template uses: $section_name, $ipaddress, $ports, $options
139 concat::fragment { "${instance_name}-${section_name}_backend_block":
140 order => $order,
141 target => $_config_file,
142 content => epp('haproxy/haproxy_backend_block.epp', $parameters),
143 }
144
145 if $collect_exported {
146 haproxy::balancermember::collect_exported { $section_name: }
147 }
148 # else: the resources have been created and they introduced their
149 # concat fragments. We don't have to do anything about them.
150 }