Skip to content

Commit

Permalink
improve ordering of options
Browse files Browse the repository at this point in the history
  • Loading branch information
rvicinus committed Feb 25, 2016
1 parent 93b218e commit 5a24f2e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 47 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ Main class, includes all other classes.

* `config_dir`: Path to the directory in which the main configuration file `haproxy.cfg` resides. Will also be used for storing any managed map files (see [`haproxy::mapfile`](#define-haproxymapfile). Default depends on platform.

* `sort_options_alphabetic`: Sort options either alphabetic or custom like haproxy internal sorts them. Defaults to true.

#### Define: `haproxy::balancermember`

Configures a service inside a listening or backend service configuration block in haproxy.cfg.
Expand Down Expand Up @@ -782,6 +784,10 @@ or /usr/local/etc/haproxy-$title/haproxy-$title.conf (FreeBSD)
The parent directory will be created automatically.
Defaults to undef.

* `sort_options_alphabetic`:
Sort options either alphabetic or custom like haproxy internal sorts them.
Defaults to true.

#### Define: `haproxy::instance_service`

Example manifest that shows one way to create the Service[] environment needed
Expand Down
1 change: 1 addition & 0 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$config_file,
$global_options,
$defaults_options,
$sort_options_alphabetic,
$config_dir = undef, # A default is required for Puppet 2.7 compatibility. When 2.7 is no longer supported, this parameter default should be removed.
$custom_fragment = undef, # A default is required for Puppet 2.7 compatibility. When 2.7 is no longer supported, this parameter default should be removed.
$merge_options = $haproxy::merge_options,
Expand Down
59 changes: 33 additions & 26 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
# resides. Will also be used for storing any managed map files (see
# `haproxy::mapfile`). Default depends on platform.
#
# [*sort_options_alphabetic*]
# Sort options either alphabetic or custom like haproxy internal sorts them.
# Defaults to true.
#
# === Examples
#
# class { 'haproxy':
Expand Down Expand Up @@ -102,22 +106,23 @@
# }
#
class haproxy (
$package_ensure = 'present',
$package_name = $haproxy::params::package_name,
$service_ensure = 'running',
$service_manage = true,
$service_options = $haproxy::params::service_options,
$global_options = $haproxy::params::global_options,
$defaults_options = $haproxy::params::defaults_options,
$merge_options = $haproxy::params::merge_options,
$restart_command = undef,
$custom_fragment = undef,
$config_dir = $haproxy::params::config_dir,
$config_file = $haproxy::params::config_file,
$package_ensure = 'present',
$package_name = $haproxy::params::package_name,
$service_ensure = 'running',
$service_manage = true,
$service_options = $haproxy::params::service_options,
$global_options = $haproxy::params::global_options,
$defaults_options = $haproxy::params::defaults_options,
$merge_options = $haproxy::params::merge_options,
$restart_command = undef,
$custom_fragment = undef,
$config_dir = $haproxy::params::config_dir,
$config_file = $haproxy::params::config_file,
$sort_options_alphabetic = $haproxy::params::sort_options_alphabetic,

# Deprecated
$manage_service = undef,
$enable = undef,
$manage_service = undef,
$enable = undef,
) inherits haproxy::params {

if $service_ensure != true and $service_ensure != false {
Expand All @@ -131,6 +136,7 @@
validate_string($service_options)
validate_hash($global_options, $defaults_options)
validate_absolute_path($config_dir)
validate_bool($sort_options_alphabetic)

# NOTE: These deprecating parameters are implemented in this class,
# not in haproxy::instance. haproxy::instance is new and therefore
Expand Down Expand Up @@ -161,18 +167,19 @@
}

haproxy::instance{ $title:
package_ensure => $_package_ensure,
package_name => $package_name,
service_ensure => $_service_ensure,
service_manage => $_service_manage,
global_options => $global_options,
defaults_options => $defaults_options,
restart_command => $restart_command,
custom_fragment => $custom_fragment,
config_dir => $config_dir,
config_file => $config_file,
merge_options => $merge_options,
service_options => $service_options,
package_ensure => $_package_ensure,
package_name => $package_name,
service_ensure => $_service_ensure,
service_manage => $_service_manage,
global_options => $global_options,
defaults_options => $defaults_options,
restart_command => $restart_command,
custom_fragment => $custom_fragment,
config_dir => $config_dir,
config_file => $config_file,
merge_options => $merge_options,
service_options => $service_options,
sort_options_alphabetic => $sort_options_alphabetic,
}

}
47 changes: 28 additions & 19 deletions manifests/instance.pp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
# The parent directory will be created automatically.
# Defaults to undef.
#
# [*sort_options_alphabetic*]
# Sort options either alphabetic or custom like haproxy internal sorts them.
# Defaults to true.
#
# === Examples
#
# A single instance of haproxy with all defaults
Expand Down Expand Up @@ -135,18 +139,19 @@
# call haproxy::instance_service.
#
define haproxy::instance (
$package_ensure = 'present',
$package_name = undef,
$service_ensure = 'running',
$service_manage = true,
$global_options = undef,
$defaults_options = undef,
$restart_command = undef,
$custom_fragment = undef,
$config_dir = undef,
$config_file = undef,
$merge_options = $haproxy::params::merge_options,
$service_options = $haproxy::params::service_options,
$package_ensure = 'present',
$package_name = undef,
$service_ensure = 'running',
$service_manage = true,
$global_options = undef,
$defaults_options = undef,
$restart_command = undef,
$custom_fragment = undef,
$config_dir = undef,
$config_file = undef,
$sort_options_alphabetic = undef,
$merge_options = $haproxy::params::merge_options,
$service_options = $haproxy::params::service_options,
) {

if $service_ensure != true and $service_ensure != false {
Expand All @@ -163,7 +168,10 @@

$_global_options = pick($global_options, $haproxy::params::global_options)
$_defaults_options = pick($defaults_options, $haproxy::params::defaults_options)
$_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::params::sort_options_alphabetic)

validate_hash($_global_options,$_defaults_options)
validate_bool($_sort_options_alphabetic)

# Determine instance_name based on:
# single-instance hosts: haproxy
Expand Down Expand Up @@ -198,13 +206,14 @@
}

haproxy::config { $title:
instance_name => $instance_name,
config_dir => $_config_dir,
config_file => $_config_file,
global_options => $_global_options,
defaults_options => $_defaults_options,
custom_fragment => $custom_fragment,
merge_options => $merge_options,
instance_name => $instance_name,
config_dir => $_config_dir,
config_file => $_config_file,
global_options => $_global_options,
defaults_options => $_defaults_options,
custom_fragment => $custom_fragment,
merge_options => $merge_options,
sort_options_alphabetic => $_sort_options_alphabetic,
}
haproxy::install { $title:
package_name => $package_name,
Expand Down
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
}
default: { fail("The ${::osfamily} operating system is not supported with the haproxy module") }
}
$sort_options_alphabetic = true
}

# TODO: test that the $config_file generated for FreeBSD instances
Expand Down
39 changes: 37 additions & 2 deletions templates/fragments/_options.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
<%-
# non mentioned option have a value of 0
# lower values come first
if @sort_options_alphabetic == true
option_order = {}
else
option_order = {
'acl' => -1,
'tcp-request' => 2,
'block' => 3,
'http-request' => 4,
'reqallow' => 5,
'reqdel' => 5,
'reqdeny' => 5,
'reqidel' => 5,
'reqideny' => 5,
'reqipass' => 5,
'reqirep' => 5,
'reqitarpit' => 5,
'reqpass' => 5,
'reqrep' => 5,
'reqtarpit' => 5,
'reqadd' => 6,
'redirect' => 7,
'use_backend' => 8,
'use-server' => 9,
'server' => 100,
}
end
-%>
<% if @options.is_a?(Hash) -%>
<% @options.sort.each do |key, val| -%>
<%# ruby sorts arrays like strings: the first elements are compared, if they -%>
<%# are equal the next elements are compared and so on. The following sort -%>
<%# provides a two element array, the first element a classification of the -%>
<%# option with a default value of 0 if the option is not found in the -%>
<%# option_order hash and the second element the option itself. -%>
<% @options.sort{|a,b| [option_order.fetch(a[0],0), a[0]] <=> [option_order.fetch(b[0],0), b[0]] }.each do |key, val| -%>
<% Array(val).each do |item| -%>
<%= key %> <%= item %>
<% end -%>
Expand All @@ -10,7 +45,7 @@
<%# sorted by key name before outputting the key name (= option name) and its -%>
<%# value (or values, one per line) -%>
<% @options.each do |option| -%>
<% option.sort.map do |key, val| -%>
<% option.sort{|a,b| [option_order.fetch(a[0],0), a[0]] <=> [option_order.fetch(b[0],0), b[0]] }.map do |key, val| -%>
<% Array(val).each do |item| -%>
<%= key %> <%= item %>
<% end -%>
Expand Down

0 comments on commit 5a24f2e

Please sign in to comment.