Skip to content

Commit a32adb5

Browse files
committed
(CONT-1144) - Conversion of ERB to EPP templates
1 parent 69d41eb commit a32adb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+411
-234
lines changed

REFERENCE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ Data type: `String`
12471247

12481248
The template that will be used to create an unit file.
12491249

1250-
Default value: `'haproxy/instance_service_unit.erb'`
1250+
Default value: `'haproxy/instance_service_unit.epp'`
12511251

12521252
### <a name="haproxy--listen"></a>`haproxy::listen`
12531253

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
# Function created to generate error message. Any string as error message can be passed and the function can
4+
# be called in epp templates.
5+
6+
Puppet::Functions.create_function(:'haproxy::generate_error_message') do
7+
dispatch :generate_error_message do
8+
param 'String', :error_message
9+
end
10+
11+
def generate_error_message(error_message)
12+
raise(error_message)
13+
end
14+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
Puppet::Functions.create_function(:'haproxy::sort_bind') do
4+
dispatch :sort_bind do
5+
param 'Hash', :bind
6+
return_type 'Array'
7+
end
8+
9+
def sort_bind(bind)
10+
bind.sort_by do |address_port|
11+
md = %r{^((\d+)\.(\d+)\.(\d+)\.(\d+))?(.*)}.match(address_port[0])
12+
[(md[1] ? md[2..5].inject(0) { |addr, octet| (addr << 8) + octet.to_i } : -1), md[6]]
13+
end
14+
end
15+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require 'ipaddr'
4+
5+
Puppet::Functions.create_function(:'haproxy::validate_ip_addr') do
6+
dispatch :validate_ip_addr do
7+
param 'String', :virtual_ip
8+
return_type 'Boolean'
9+
end
10+
11+
def validate_ip_addr(virtual_ip)
12+
IPAddr.new(virtual_ip)
13+
true
14+
rescue StandardError
15+
false
16+
end
17+
end

manifests/backend.pp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@
125125
$order = "25-${defaults}-${section_name}-01"
126126
}
127127

128+
$parameters = {
129+
'section_name' => $section_name,
130+
'mode' => $mode,
131+
'description' => $description,
132+
'_sort_options_alphabetic' => $_sort_options_alphabetic,
133+
'options' => $options,
134+
}
135+
128136
# Template uses: $section_name, $ipaddress, $ports, $options
129137
concat::fragment { "${instance_name}-${section_name}_backend_block":
130138
order => $order,
131139
target => $_config_file,
132-
content => template('haproxy/haproxy_backend_block.erb'),
140+
content => epp('haproxy/haproxy_backend_block.epp', $parameters),
133141
}
134142

135143
if $collect_exported {

manifests/balancermember.pp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,25 @@
167167
} else {
168168
$order = "25-${defaults}-${listening_service}-02-${name}"
169169
}
170+
171+
$parameters = {
172+
'type' => $type,
173+
'ipaddresses' => $ipaddresses,
174+
'server_names' => $server_names,
175+
'ports' => $ports,
176+
'define_cookies' => $define_cookies,
177+
'options' => $options,
178+
'verifyhost' => $verifyhost,
179+
'weight' => $weight,
180+
'prefix' => $prefix,
181+
'amount' => $amount,
182+
'fqdn' => $fqdn,
183+
'port' => $port,
184+
}
170185
# Template uses $ipaddresses, $server_name, $ports, $option
171186
concat::fragment { "${instance_name}-${listening_service}_balancermember_${name}":
172187
order => $order,
173188
target => $_config_file,
174-
content => template('haproxy/haproxy_balancermember.erb'),
189+
content => epp('haproxy/haproxy_balancermember.epp', $parameters),
175190
}
176191
}

manifests/config.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,17 @@
7070
content => "# This file is managed by Puppet\n",
7171
}
7272

73+
$parameters = {
74+
'_global_options' => $_global_options,
75+
'_defaults_options' => $_defaults_options,
76+
'custom_fragment' => $custom_fragment,
77+
}
78+
7379
# Template uses $_global_options, $_defaults_options, $custom_fragment
7480
concat::fragment { "${instance_name}-haproxy-base":
7581
target => $_config_file,
7682
order => '10',
77-
content => template("${module_name}/haproxy-base.cfg.erb"),
83+
content => epp("${module_name}/haproxy-base.cfg.epp", $parameters),
7884
}
7985
}
8086

manifests/defaults.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@
3636
include haproxy::globals
3737
$_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic)
3838

39+
$parameters = {
40+
'_sort_options_alphabetic' => $_sort_options_alphabetic,
41+
'options' => $options,
42+
'name' => $name,
43+
}
44+
3945
concat::fragment { "${instance_name}-${name}_defaults_block":
4046
order => "25-${name}",
4147
target => $config_file,
42-
content => template('haproxy/haproxy_defaults_block.erb'),
48+
content => epp('haproxy/haproxy_defaults_block.epp', $parameters),
4349
}
4450
}

manifests/frontend.pp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,23 @@
143143
$order = "25-${defaults}-${section_name}-00"
144144
}
145145
}
146+
147+
$parameters = {
148+
'section_name' => $section_name,
149+
'bind' => $bind,
150+
'ipaddress' => $ipaddress,
151+
'ports' => $ports,
152+
'bind_options' => $bind_options,
153+
'mode' => $mode,
154+
'description' => $description,
155+
'options' => $options,
156+
'_sort_options_alphabetic' => $_sort_options_alphabetic,
157+
}
158+
146159
# Template uses: $section_name, $ipaddress, $ports, $options
147160
concat::fragment { "${instance_name}-${section_name}_frontend_block":
148161
order => $order,
149162
target => $_config_file,
150-
content => template('haproxy/haproxy_frontend_block.erb'),
163+
content => epp('haproxy/haproxy_frontend_block.epp', $parameters),
151164
}
152165
}

manifests/instance_service.pp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#
4343
define haproxy::instance_service (
4444
Optional[String] $haproxy_init_source = undef,
45-
String $haproxy_unit_template = 'haproxy/instance_service_unit.erb',
45+
String $haproxy_unit_template = 'haproxy/instance_service_unit.epp',
4646
String $haproxy_package = 'haproxy',
4747
Stdlib::Absolutepath $bindir = '/opt/haproxy/bin',
4848
) {
@@ -103,12 +103,16 @@
103103
$unitfile = "/lib/systemd/system/haproxy-${title}.service"
104104
}
105105

106+
$parameters = {
107+
'title' => $title,
108+
'wrapper' => $wrapper,
109+
}
106110
file { $unitfile:
107111
ensure => file,
108112
mode => '0644',
109113
owner => 'root',
110114
group => 'root',
111-
content => template($haproxy_unit_template),
115+
content => epp($haproxy_unit_template, $parameters),
112116
notify => Exec['systemctl daemon-reload'],
113117
}
114118
if (!defined(Exec['systemctl daemon-reload'])) {

manifests/listen.pp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,23 @@
150150
$order = "25-${defaults}-${section_name}-00"
151151
}
152152

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+
153165
# Template uses: $section_name, $ipaddress, $ports, $options
154166
concat::fragment { "${instance_name}-${section_name}_listen_block":
155167
order => $order,
156168
target => $_config_file,
157-
content => template('haproxy/haproxy_listen_block.erb'),
169+
content => epp('haproxy/haproxy_listen_block.epp', $parameters),
158170
}
159171

160172
if $collect_exported {

manifests/mailer.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@
4848
$config_file = inline_template($haproxy::params::config_file_tmpl)
4949
}
5050

51+
$parameters = {
52+
'ipaddresses' => $ipaddresses,
53+
'server_names' => $server_names,
54+
'port' => $port,
55+
}
56+
5157
# Templates uses $ipaddresses, $server_name, $ports, $option
5258
concat::fragment { "${instance_name}-mailers-${mailers_name}-${name}":
5359
order => "40-mailers-01-${mailers_name}-${name}",
5460
target => $config_file,
55-
content => template('haproxy/haproxy_mailer.erb'),
61+
content => epp('haproxy/haproxy_mailer.epp', $parameters),
5662
}
5763
}

manifests/mailers.pp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
$config_file = inline_template($haproxy::params::config_file_tmpl)
2525
}
2626

27+
$parameters = {
28+
'name' => $name,
29+
}
30+
2731
# Template uses: $name
2832
concat::fragment { "${instance_name}-${name}_mailers_block":
2933
order => "40-mailers-00-${name}",
3034
target => $config_file,
31-
content => template('haproxy/haproxy_mailers_block.erb'),
35+
content => epp('haproxy/haproxy_mailers_block.epp', $parameters),
3236
}
3337

3438
if $collect_exported {

manifests/mapfile.pp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@
5757
notify => Haproxy::Service[$_instances],
5858
}
5959

60+
$parameters = {
61+
'mappings' => $mappings,
62+
'mapfile_name' => $mapfile_name,
63+
}
64+
6065
concat::fragment { "haproxy_mapfile_${mapfile_name}-top":
6166
target => $_mapfile_name,
62-
content => template('haproxy/haproxy_mapfile.erb'),
67+
content => epp('haproxy/haproxy_mapfile.epp', $parameters),
6368
order => '00',
6469
}
6570
}

manifests/mapfile/entry.pp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
) {
3030
$_mapfile_name = "${haproxy::config_dir}/${mapfile}.map"
3131

32+
$parameters = {
33+
'mappings' => $mappings,
34+
'mapfile_name' => $title,
35+
}
36+
3237
concat::fragment { "haproxy_mapfile_${mapfile}-${title}":
3338
target => $_mapfile_name,
34-
content => template('haproxy/haproxy_mapfile.erb'),
39+
content => epp('haproxy/haproxy_mapfile.epp', $parameters),
3540
order => $order,
3641
}
3742
}

manifests/peer.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@
5656
$_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
5757
}
5858

59+
$parameters = {
60+
'ipaddresses' => $ipaddresses,
61+
'server_names' => $server_names,
62+
'port' => $port,
63+
}
64+
5965
# Templates uses $ipaddresses, $server_name, $ports, $option
6066
concat::fragment { "${instance_name}-peers-${peers_name}-${name}":
6167
order => "30-peers-01-${peers_name}-${name}",
6268
target => $_config_file,
63-
content => template('haproxy/haproxy_peer.erb'),
69+
content => epp('haproxy/haproxy_peer.epp', $parameters),
6470
}
6571
}

manifests/peers.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
concat::fragment { "${instance_name}-${name}_peers_block":
4242
order => "30-peers-00-${name}",
4343
target => $_config_file,
44-
content => template('haproxy/haproxy_peers_block.erb'),
44+
content => epp('haproxy/haproxy_peers_block.epp', { 'name' => $name }),
4545
}
4646

4747
if $collect_exported {

manifests/resolver.pp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,20 @@
131131
$order = "25-${defaults}-${section_name}-02"
132132
}
133133

134+
$parameters = {
135+
'section_name' => $section_name,
136+
'nameservers' => $nameservers,
137+
'parse_resolv_conf' => $parse_resolv_conf,
138+
'resolve_retries' => $resolve_retries,
139+
'timeout' => $timeout,
140+
'hold' => $hold,
141+
'accepted_payload_size' => $accepted_payload_size,
142+
}
134143
# Template uses: $section_name
135144
concat::fragment { "${instance_name}-${section_name}_resolver_block":
136145
order => $order,
137146
target => $_config_file,
138-
content => template('haproxy/haproxy_resolver_block.erb'),
147+
content => epp('haproxy/haproxy_resolver_block.epp', $parameters),
139148
}
140149

141150
if $collect_exported {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'haproxy::generate_error_message' do
6+
it { is_expected.to run.with_params('Invalid IP address or hostname 2323.23.23').and_raise_error('Invalid IP address or hostname 2323.23.23') }
7+
it { is_expected.to run.with_params('Port 181400 is outside of range 1-65535').and_raise_error('Port 181400 is outside of range 1-65535') }
8+
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
9+
end

spec/functions/sort_bind_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'haproxy::sort_bind' do
6+
it { is_expected.to run.with_params({ '0.0.0.0:48001-48003' => [] }).and_return([['0.0.0.0:48001-48003', []]]) }
7+
it { is_expected.to run.with_params({ '192.168.0.1:80' => ['ssl'] }).and_return([['192.168.0.1:80', ['ssl']]]) }
8+
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'haproxy::validate_ip_addr' do
6+
it { is_expected.to run.with_params('10.0.0.10').and_return(true) }
7+
it { is_expected.to run.with_params('256.168.0.1').and_return(false) }
8+
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
9+
end

templates/empty.erb

Whitespace-only changes.

templates/fragments/_bind.epp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<% if $bind { -%>
2+
<%- haproxy::sort_bind($bind).map |$address_port_bind_params| { -%>
3+
bind <%= $address_port_bind_params[0] %> <%= Array($address_port_bind_params[1].flatten).join(" ") %>
4+
<%- } -%>
5+
<% } else { -%>
6+
<%- Array($ipaddress.flatten).unique.each |$virtual_ip| { -%>
7+
<%- if String(type($ports, 'generalized')).index('Array') == 0 { -%>
8+
<%- $ports_as_array = $ports -%>
9+
<%- } elsif String(type($ports, 'generalized')).index('String') == 0 { -%>
10+
<%- $ports_as_array = Array($ports.split(",")) -%>
11+
<%- } else { -%>
12+
<%- $ports_as_array = [] -%>
13+
<%- } -%>
14+
<%- $ports_as_array.each |$port| { -%>
15+
<%- $valid_ip = haproxy::validate_ip_addr($virtual_ip) -%>
16+
<%- if !$valid_ip and !String($virtual_ip).match(/^[A-Za-z][A-Za-z0-9\.-]+$/) and $virtual_ip != '*' and $virtual_ip != "::" { -%>
17+
<%- haproxy::generate_error_message("Invalid IP address or hostname [${virtual_ip}]") -%>
18+
<%- } -%>
19+
<%- if $port.convert_to(Integer) < 1 or $port.convert_to(Integer) > 65535 { -%>
20+
<%- haproxy::generate_error_message("Port [${port}] is outside of range 1-65535") -%>
21+
<%- } -%>
22+
bind <%= $virtual_ip -%>:<%= $port -%> <% if $bind_options { %><%= " ${Array($bind_options.flatten).join(' ')}" %><%} else { %><%= " " %><% } %>
23+
<%- } -%>
24+
<%- } -%>
25+
<%- } -%>

0 commit comments

Comments
 (0)