Debian Codegraph @

manifests/instance.pp

raw
1 # @summary
2 # Manages haproxy permitting multiple instances to run on the same machine.
3 #
4 # @note
5 # Normally users use the Class['haproxy'], which runs a single haproxy
6 # daemon on a machine.
7 #
8 # @note
9 # Currently requires the puppetlabs/concat module on the Puppet Forge and
10 # uses storeconfigs on the Puppet Server to export/collect resources
11 # from all balancer members.
12 #
13 #
14 # @param package_ensure
15 # Ensure the package is present (installed), absent or a specific version.
16 # Defaults to 'present'
17 #
18 # @param package_name
19 # The package name of haproxy. Defaults to undef, and no package is installed.
20 # NOTE: Class['haproxy'] has a different default.
21 #
22 # @param service_ensure
23 # Chooses whether the haproxy service should be running & enabled at boot, or
24 # stopped and disabled at boot. Defaults to 'running'
25 #
26 # @param service_manage
27 # Chooses whether the haproxy service state should be managed by puppet at
28 # all. Defaults to true
29 #
30 # @param chroot_dir_manage
31 # Chooses whether the haproxy chroot directory should be managed by puppet
32 # at all. Defaults to true
33 #
34 # @param service_name
35 # The service name for haproxy. Defaults to undef. If no name is given then
36 # the value computed for $instance_name will be used.
37 # NOTE: Class['haproxy'] has a different default.
38 #
39 # @param global_options
40 # A hash of all the haproxy global options. If you want to specify more
41 # than one option (i.e. multiple timeout or stats options), pass those
42 # options as an array and you will get a line for each of them in the
43 # resultant haproxy.cfg file.
44 #
45 # @param defaults_options
46 # A hash of all the haproxy defaults options. If you want to specify more
47 # than one option (i.e. multiple timeout or stats options), pass those
48 # options as an array and you will get a line for each of them in the
49 # resultant haproxy.cfg file.
50 #
51 # @param restart_command
52 # Command to use when restarting the on config changes.
53 # Passed directly as the <code>'restart'</code> parameter to the service
54 # resource. # Defaults to undef i.e. whatever the service default is.
55 #
56 # @param custom_fragment
57 # Allows arbitrary HAProxy configuration to be passed through to support
58 # additional configuration not available via parameters, or to short-circuit
59 # the defined resources such as haproxy::listen when an operater would rather
60 # just write plain configuration. Accepts a string (ie, output from the
61 # template() function). Defaults to undef
62 #
63 # @param config_file
64 # Allows arbitrary config filename to be specified. If this is used,
65 # it is assumed that the directory path to the file exists and has
66 # owner/group/permissions as desired. If set to undef, the name
67 # will be generated as follows:
68 # If $title is 'haproxy', the operating system default will be used.
69 # Otherwise, /etc/haproxy-$title/haproxy-$title.conf (Linux),
70 # or /usr/local/etc/haproxy-$title/haproxy-$title.conf (FreeBSD)
71 # The parent directory will be created automatically.
72 # Defaults to undef.
73 #
74 # @param config_validate_cmd
75 # Command used by concat validate_cmd to validate new
76 # config file concat is a valid haproxy config.
77 # Default /usr/sbin/haproxy -f % -c
78 #
79 # @param config_dir
80 # Optional. Default undef.
81 #
82 # @param merge_options
83 #
84 # @param service_options
85 #
86 # @param sysconfig_options
87 #
88 # @example
89 # A single instance of haproxy with all defaults
90 # i.e. emulate Class['haproxy']
91 # package{ 'haproxy': ensure => present }->haproxy::instance { 'haproxy': }->
92 # haproxy::listen { 'puppet00':
93 # instance => 'haproxy',
94 # collect_exported => false,
95 # ipaddress => $::ipaddress,
96 # ports => '8140',
97 # }
98 #
99 # @example
100 # Multiple instances of haproxy:
101 # haproxy::instance { 'group1': }
102 # haproxy::instance_service { 'group1':
103 # haproxy_init_source => "puppet:///modules/${module_name}/haproxy-group1.init",
104 # }
105 # haproxy::listen { 'puppet00':
106 # instance => 'group1',
107 # collect_exported => false,
108 # ipaddress => $::ipaddress,
109 # ports => '8800',
110 # requires => Package['haproxy'],
111 # }
112 # haproxy::instance { 'group2': }
113 # haproxy::instance_service { 'group2':
114 # haproxy_init_source => "puppet:///modules/${module_name}/haproxy-group1.init",
115 # }
116 # haproxy::listen { 'puppet00':
117 # instance => 'group2',
118 # collect_exported => false,
119 # ipaddress => $::ipaddress,
120 # ports => '9900',
121 # requires => Package['haproxy'],
122 # }
123 #
124 # @example
125 # Multiple instances of haproxy, one with a custom haproxy package:
126 # haproxy::instance { 'group1': }
127 # haproxy::instance_service { 'group1':
128 # haproxy_init_source => "puppet:///modules/${module_name}/haproxy-group1.init",
129 # }
130 # haproxy::listen { 'puppet00':
131 # instance => 'group1',
132 # collect_exported => false,
133 # ipaddress => $::ipaddress,
134 # ports => '8800',
135 # requires => Package['haproxy'],
136 # }
137 # haproxy::instance { 'group2': }
138 # haproxy::instance_service { 'group2':
139 # haproxy_package => 'custom_haproxy',
140 # haproxy_init_source => "puppet:///modules/${module_name}/haproxy-group2.init",
141 # }
142 # haproxy::listen { 'puppet00':
143 # instance => 'group2',
144 # collect_exported => false,
145 # ipaddress => $::ipaddress,
146 # ports => '9900',
147 # requires => Package['haproxy'],
148 # }
149 #
150 # @note
151 # When running multiple instances on one host, there must be a Service[] for
152 # each instance. One way to create the situation where Service[] works is
153 # using haproxy::instance_service.
154 # However you may want to do it some other way. For example, you may
155 # not have packages for your custom haproxy binary. Or, you may wish
156 # to use the standard haproxy package but not create links to it, or
157 # you may have different init.d scripts. In these cases, write your own
158 # puppet code that will result in Service[] working for you and do not
159 # call haproxy::instance_service.
160 #
161 define haproxy::instance (
162 Variant[Enum['present', 'absent', 'purged', 'disabled', 'installed', 'latest'], String[1]] $package_ensure = 'present',
163 Optional[String] $package_name = undef,
164 Variant[Enum['running', 'stopped'], Boolean] $service_ensure = 'running',
165 Boolean $service_manage = true,
166 Boolean $chroot_dir_manage = true,
167 Optional[String] $service_name = undef,
168 Optional[Hash] $global_options = undef,
169 Optional[Hash] $defaults_options = undef,
170 Optional[String] $restart_command = undef,
171 Optional[String] $custom_fragment = undef,
172 Optional[Stdlib::Absolutepath] $config_dir = undef,
173 Optional[Stdlib::Absolutepath] $config_file = undef,
174 Variant[Stdlib::Absolutepath, String] $config_validate_cmd = $haproxy::params::config_validate_cmd,
175 Boolean $merge_options = $haproxy::params::merge_options,
176 String $service_options = $haproxy::params::service_options,
177 String $sysconfig_options = $haproxy::params::sysconfig_options,
178 ) {
179 # Since this is a 'define', we can not use 'inherts haproxy::params'.
180 # Therefore, we "include haproxy::params" for any parameters we need.
181 contain haproxy::params
182
183 $_global_options = pick($global_options, $haproxy::params::global_options)
184 $_defaults_options = pick($defaults_options, $haproxy::params::defaults_options)
185
186 # Determine instance_name based on:
187 # single-instance hosts: haproxy
188 # multi-instance hosts: haproxy-$title
189 if $title == 'haproxy' {
190 $instance_name = 'haproxy'
191 } else {
192 $instance_name = "haproxy-${title}"
193 }
194
195 # Determine config_dir and config_file:
196 # If config_dir defined, use it. Otherwise:
197 # single-instance hosts: use defaults
198 # multi-instance hosts: use templates
199 if $instance_name == 'haproxy' {
200 $_config_file = pick($config_file, $haproxy::params::config_file)
201 } else {
202 $_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
203 }
204
205 if $instance_name == 'haproxy' {
206 $_config_dir = pick($config_dir, $haproxy::params::config_dir)
207 } else {
208 $_config_dir = pick($config_dir, inline_template($haproxy::params::config_dir_tmpl))
209 }
210
211 $instance_service_name = pick($service_name, $instance_name)
212
213 haproxy::config { $title:
214 instance_name => $instance_name,
215 config_dir => $_config_dir,
216 config_file => $_config_file,
217 global_options => $_global_options,
218 defaults_options => $_defaults_options,
219 custom_fragment => $custom_fragment,
220 merge_options => $merge_options,
221 package_ensure => $package_ensure,
222 chroot_dir_manage => $chroot_dir_manage,
223 config_validate_cmd => $config_validate_cmd,
224 }
225 haproxy::install { $title:
226 package_name => $package_name,
227 package_ensure => $package_ensure,
228 }
229 haproxy::service { $title:
230 instance_name => $instance_service_name,
231 service_ensure => $service_ensure,
232 service_manage => $service_manage,
233 restart_command => $restart_command,
234 service_options => $service_options,
235 sysconfig_options => $sysconfig_options,
236 }
237
238 if $package_ensure == 'absent' or $package_ensure == 'purged' {
239 Haproxy::Service[$title]
240 -> Haproxy::Config[$title]
241 -> Haproxy::Install[$title]
242 } else {
243 Haproxy::Install[$title]
244 -> Haproxy::Config[$title]
245 ~> Haproxy::Service[$title]
246 }
247 }